* ES6 & misc cleanup

* Add index to message for quicker retrieval
This commit is contained in:
Bryan Ashby 2016-07-04 12:56:15 -06:00
parent f6d363b53d
commit ce1d3e6a7e
1 changed files with 126 additions and 117 deletions

View File

@ -1,17 +1,18 @@
/* jslint node: true */ /* jslint node: true */
'use strict'; 'use strict';
var conf = require('./config.js'); // ENiGMA½
const conf = require('./config.js');
var sqlite3 = require('sqlite3');
var paths = require('path');
var async = require('async');
// deps
const sqlite3 = require('sqlite3');
const paths = require('path');
const async = require('async');
const _ = require('lodash'); const _ = require('lodash');
const assert = require('assert'); const assert = require('assert');
// database handles // database handles
var dbs = {}; let dbs = {};
exports.getModDatabasePath = getModDatabasePath; exports.getModDatabasePath = getModDatabasePath;
exports.initializeDatabases = initializeDatabases; exports.initializeDatabases = initializeDatabases;
@ -46,51 +47,52 @@ function getModDatabasePath(moduleInfo, suffix) {
} }
function initializeDatabases(cb) { function initializeDatabases(cb) {
// :TODO: this will need to change if more DB's are added ... why?
async.series( async.series(
[ [
function systemDb(callback) { function systemDb(callback) {
dbs.system = new sqlite3.Database(getDatabasePath('system'), function dbCreated(err) { dbs.system = new sqlite3.Database(getDatabasePath('system'), err => {
if(err) { if(err) {
callback(err); return callback(err);
} else { }
dbs.system.serialize(function serialized() {
dbs.system.serialize( () => {
createSystemTables(); createSystemTables();
}); });
callback(null);
} return callback(null);
}); });
}, },
function userDb(callback) { function userDb(callback) {
dbs.user = new sqlite3.Database(getDatabasePath('user'), function dbCreated(err) { dbs.user = new sqlite3.Database(getDatabasePath('user'), err => {
if(err) { if(err) {
callback(err); return callback(err);
} else { }
dbs.user.serialize(function serialized() {
dbs.user.serialize( () => {
createUserTables(); createUserTables();
createInitialUserValues(); createInitialUserValues();
}); });
callback(null);
} return callback(null);
}); });
}, },
function messageDb(callback) { function messageDb(callback) {
dbs.message = new sqlite3.Database(getDatabasePath('message'), function dbCreated(err) { dbs.message = new sqlite3.Database(getDatabasePath('message'), err => {
if(err) { if(err) {
callback(err); return callback(err);
} else { }
dbs.message.serialize(function serialized() { dbs.message.serialize(function serialized() {
createMessageBaseTables(); createMessageBaseTables();
createInitialMessageValues(); createInitialMessageValues();
}); });
callback(null);
} return callback(null);
}); });
} }
], ],
function complete(err) { cb
cb(err);
}
); );
} }
@ -119,119 +121,126 @@ function createSystemTables() {
function createUserTables() { function createUserTables() {
dbs.user.run( dbs.user.run(
'CREATE TABLE IF NOT EXISTS user (' + `CREATE TABLE IF NOT EXISTS user (
' id INTEGER PRIMARY KEY,' + id INTEGER PRIMARY KEY,
' user_name VARCHAR NOT NULL,' + user_name VARCHAR NOT NULL,
' UNIQUE(user_name)' + UNIQUE(user_name)
');' );`
); );
// :TODO: create FK on delete/etc. // :TODO: create FK on delete/etc.
dbs.user.run( dbs.user.run(
'CREATE TABLE IF NOT EXISTS user_property (' + `CREATE TABLE IF NOT EXISTS user_property (
' user_id INTEGER NOT NULL,' + user_id INTEGER NOT NULL,
' prop_name VARCHAR NOT NULL,' + prop_name VARCHAR NOT NULL,
' prop_value VARCHAR,' + prop_value VARCHAR,
' UNIQUE(user_id, prop_name),' + UNIQUE(user_id, prop_name),
' FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE' + FOREIGN KEY(user_id) REFERENCES user(id) ON DELETE CASCADE
');' );`
); );
dbs.user.run( dbs.user.run(
'CREATE TABLE IF NOT EXISTS user_group_member (' + `CREATE TABLE IF NOT EXISTS user_group_member (
' group_name VARCHAR NOT NULL,' + group_name VARCHAR NOT NULL,
' user_id INTEGER NOT NULL,' + user_id INTEGER NOT NULL,
' UNIQUE(group_name, user_id)' + UNIQUE(group_name, user_id)
');' );`
); );
dbs.user.run( dbs.user.run(
'CREATE TABLE IF NOT EXISTS user_login_history (' + `CREATE TABLE IF NOT EXISTS user_login_history (
' user_id INTEGER NOT NULL,' + user_id INTEGER NOT NULL,
' user_name VARCHAR NOT NULL,' + user_name VARCHAR NOT NULL,
' timestamp DATETIME NOT NULL' + timestamp DATETIME NOT NULL
');' );`
); );
} }
function createMessageBaseTables() { function createMessageBaseTables() {
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS message (' + `CREATE TABLE IF NOT EXISTS message (
' message_id INTEGER PRIMARY KEY,' + message_id INTEGER PRIMARY KEY,
' area_tag VARCHAR NOT NULL,' + area_tag VARCHAR NOT NULL,
' message_uuid VARCHAR(36) NOT NULL,' + message_uuid VARCHAR(36) NOT NULL,
' reply_to_message_id INTEGER,' + reply_to_message_id INTEGER,
' to_user_name VARCHAR NOT NULL,' + to_user_name VARCHAR NOT NULL,
' from_user_name VARCHAR NOT NULL,' + from_user_name VARCHAR NOT NULL,
' subject,' + // FTS @ message_fts subject, /* FTS @ message_fts */
' message,' + // FTS @ message_fts message, /* FTS @ message_fts */
' modified_timestamp DATETIME NOT NULL,' + modified_timestamp DATETIME NOT NULL,
' view_count INTEGER NOT NULL DEFAULT 0,' + view_count INTEGER NOT NULL DEFAULT 0,
' UNIQUE(message_uuid)' + UNIQUE(message_uuid)
');' );`
); );
dbs.message.run( dbs.message.run(
'CREATE VIRTUAL TABLE IF NOT EXISTS message_fts USING fts4 (' + `CREATE INDEX IF NOT EXISTS message_by_area_tag_index
' content="message",' + ON message (area_tag);`
' subject,' +
' message' +
');'
); );
dbs.message.run( dbs.message.run(
'CREATE TRIGGER IF NOT EXISTS message_before_update BEFORE UPDATE ON message BEGIN' + `CREATE VIRTUAL TABLE IF NOT EXISTS message_fts USING fts4 (
' DELETE FROM message_fts WHERE docid=old.rowid;' + content="message",
'END;' + subject,
'CREATE TRIGGER IF NOT EXISTS message_before_delete BEFORE DELETE ON message BEGIN' + message
' DELETE FROM message_fts WHERE docid=old.rowid;' + );`
'END;' + );
'' +
'CREATE TRIGGER IF NOT EXISTS message_after_update AFTER UPDATE ON message BEGIN' + dbs.message.run(
' INSERT INTO message_fts(docid, subject, message) VALUES(new.rowid, new.subject, new.message);' + `CREATE TRIGGER IF NOT EXISTS message_before_update BEFORE UPDATE ON message BEGIN
'END;' + DELETE FROM message_fts WHERE docid=old.rowid;
'CREATE TRIGGER IF NOT EXISTS message_after_insert AFTER INSERT ON message BEGIN' + END;
' INSERT INTO message_fts(docid, subject, message) VALUES(new.rowid, new.subject, new.message);' +
'END;' CREATE TRIGGER IF NOT EXISTS message_before_delete BEFORE DELETE ON message BEGIN
DELETE FROM message_fts WHERE docid=old.rowid;
END;
CREATE TRIGGER IF NOT EXISTS message_after_update AFTER UPDATE ON message BEGIN
INSERT INTO message_fts(docid, subject, message) VALUES(new.rowid, new.subject, new.message);
END;
CREATE TRIGGER IF NOT EXISTS message_after_insert AFTER INSERT ON message BEGIN
INSERT INTO message_fts(docid, subject, message) VALUES(new.rowid, new.subject, new.message);
END;`
); );
// :TODO: need SQL to ensure cleaned up if delete from message? // :TODO: need SQL to ensure cleaned up if delete from message?
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS message_meta (' + `CREATE TABLE IF NOT EXISTS message_meta (
' message_id INTEGER NOT NULL,' + message_id INTEGER NOT NULL,
' meta_category INTEGER NOT NULL,' + meta_category INTEGER NOT NULL,
' meta_name VARCHAR NOT NULL,' + meta_name VARCHAR NOT NULL,
' meta_value VARCHAR NOT NULL,' + meta_value VARCHAR NOT NULL,
' UNIQUE(message_id, meta_category, meta_name, meta_value),' + // :TODO:why unique here? UNIQUE(message_id, meta_category, meta_name, meta_value),
' FOREIGN KEY(message_id) REFERENCES message(message_id)' + FOREIGN KEY(message_id) REFERENCES message(message_id)
');' );`
); );
// :TODO: need SQL to ensure cleaned up if delete from message? // :TODO: need SQL to ensure cleaned up if delete from message?
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS hash_tag (' + `CREATE TABLE IF NOT EXISTS hash_tag (
' hash_tag_id INTEGER PRIMARY KEY,' + hash_tag_id INTEGER PRIMARY KEY,
' hash_tag_name VARCHAR NOT NULL,' + hash_tag_name VARCHAR NOT NULL,
' UNIQUE(hash_tag_name)' + UNIQUE(hash_tag_name)
');' );`
); );
// :TODO: need SQL to ensure cleaned up if delete from message? // :TODO: need SQL to ensure cleaned up if delete from message?
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS message_hash_tag (' + `CREATE TABLE IF NOT EXISTS message_hash_tag (
' hash_tag_id INTEGER NOT NULL,' + hash_tag_id INTEGER NOT NULL,
' message_id INTEGER NOT NULL' + message_id INTEGER NOT NULL
');' );`
); );
dbs.message.run( dbs.message.run(
'CREATE TABLE IF NOT EXISTS user_message_area_last_read (' + `CREATE TABLE IF NOT EXISTS user_message_area_last_read (
' user_id INTEGER NOT NULL,' + user_id INTEGER NOT NULL,
' area_tag VARCHAR NOT NULL,' + area_tag VARCHAR NOT NULL,
' message_id INTEGER NOT NULL,' + message_id INTEGER NOT NULL,
' UNIQUE(user_id, area_tag)' + UNIQUE(user_id, area_tag)
');' );`
); );
dbs.message.run( dbs.message.run(