app.js
/**
 * sqlite_init
 * 
 * Creates database if needed by defining tables and iniitial settings.
 */


Creates database if needed by defining tables and iniitial settings.
Creates database if needed by defining tables and iniitial settings .
function sqlite_init() {
  return new Promise((resolve, reject) => {
    DB.serialize(() => {
      DB.get(`SELECT name FROM sqlite_master 
              WHERE type='table' AND name='settings'`, (err, row) => {
        if (err) throw err;
        if (row === undefined) {
          var create = `CREATE TABLE settings (
                            id INTEGER PRIMARY KEY AUTOINCREMENT,
                            key TEXT UNIQUE,
                            value TEXT
                        );`;
          var version = `INSERT INTO settings (key, value) VALUES ('version', ?)`;
          var build = `INSERT INTO settings (key, value) VALUES ('build', ?)`;
          var subreddit_table = `CREATE TABLE subs (
                                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                                    name TEXT UNIQUE,
                                    is_active INTEGER DEFAULT 1
                                  );`
          var subreddit_info = `CREATE TABLE sub_info (
                                    id INTEGER PRIMARY KEY AUTOINCREMENT,
                                    sub_id INTEGER,
                                    timestamp INTEGER,
                                    subscribers INTEGER,
                                    accounts_active INTEGER,
                                    public_traffic INTEGER,
                                    created_utc INTEGER,
                                    quarantine INTEGER,
                                    accounts_active_is_fuzzed INTEGER
                                  );`
          var subreddit_index = `CREATE INDEX subs_index ON subs(name);`
            db_run_func(create, [])()
              .then(db_run_func(version, VERSION))
              .then(db_run_func(build, 1))
              .then(db_run_func(subreddit_table))
              .then(db_run_func(subreddit_index))
              .then(db_run_func(subreddit_info))
              .then(load_database)
              .then(update_database)
              .then(load_subreddits)
              .then(resolve);
        } else {
          load_database()
              .then(update_database)
              .then(load_subreddits)
              .then(resolve);
        }
      });
    });
  });
}
function sqlite_init ( ) { return new Promise ( ( resolve , reject ) => { DB . serialize ( ( ) => { DB . get ( ` ` , ( err , row ) => { if ( err ) throw err ; if ( row === undefined ) { var create = ` ` ; var version = ` ` ; var build = ` ` ; var subreddit_table = ` ` var subreddit_info = ` ` var subreddit_index = ` ` db_run_func ( create , [ ] ) ( ) . then ( db_run_func ( version , VERSION ) ) . then ( db_run_func ( build , 1 ) ) . then ( db_run_func ( subreddit_table ) ) . then ( db_run_func ( subreddit_index ) ) . then ( db_run_func ( subreddit_info ) ) . then ( load_database ) . then ( update_database ) . then ( load_subreddits ) . then ( resolve ) ; } else { load_database ( ) . then ( update_database ) . then ( load_subreddits ) . then ( resolve ) ; } } ) ; } ) ; } ) ; }
sqlite_init
sqlite_init
// blank database - setup.