enigma-bbs/core/database.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-10-20 05:30:44 +00:00
/* jslint node: true */
'use strict';
var conf = require('./config.js');
var sqlite3 = require('sqlite3');
var paths = require('path');
2014-10-21 04:47:13 +00:00
// database handles
var dbs = {};
2014-10-20 05:30:44 +00:00
exports.initializeDatabases = initializeDatabases;
2014-10-21 04:47:13 +00:00
exports.dbs = dbs;
2014-10-20 05:30:44 +00:00
function getDatabasePath(name) {
return paths.join(conf.config.paths.db, name + '.sqlite3');
}
function initializeDatabases() {
// :TODO: this will need to change if more DB's are added
2014-10-21 04:47:13 +00:00
dbs.user = new sqlite3.Database(getDatabasePath('user'));
2014-10-20 05:30:44 +00:00
2014-10-21 04:47:13 +00:00
dbs.user.serialize(function onSerialized() {
2014-10-20 05:30:44 +00:00
createUserTables();
});
}
function createUserTables() {
2014-10-21 04:47:13 +00:00
dbs.user.run(
2014-10-20 05:30:44 +00:00
'CREATE TABLE IF NOT EXISTS user (' +
' id INTEGER PRIMARY KEY,' +
' user_name VARCHAR NOT NULL,' +
' UNIQUE(user_name)' +
');'
);
2014-10-21 04:47:13 +00:00
// :TODO: create FK on delete/etc.
dbs.user.run(
2014-10-20 05:30:44 +00:00
'CREATE TABLE IF NOT EXISTS user_property (' +
' user_id INTEGER NOT NULL,' +
' prop_name VARCHAR NOT NULL,' +
' prop_value VARCHAR,' +
' UNIQUE(user_id, prop_name),' +
' FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE' +
2014-10-20 05:30:44 +00:00
');'
);
}