Skip to content

Commit

Permalink
feat(migrations): use sqlite instead of sqlite3 to use migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
g-div committed Oct 12, 2017
1 parent 01e738e commit 411545a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 27 deletions.
25 changes: 25 additions & 0 deletions migrations/001-initial-schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- Up
CREATE TABLE comment (
id INTEGER PRIMARY KEY NOT NULL,
user_id NOT NULL,
slug CHAR(128) NOT NULL,
created_at TEXT NOT NULL,
comment CHAR(4000) NOT NULL,
rejected BOOLEAN,
approved BOOLEAN
);

CREATE TABLE user (
id INTEGER PRIMARY KEY NOT NULL,
name CHAR(128),
display_name CHAR(128),
provider CHAR(128),
provider_id CHAR(128),
created_at TEXT NOT NULL,
blocked BOOLEAN,
trusted BOOLEAN
);

-- Down
DROP TABLE comment;
DROP TABLE user;
17 changes: 17 additions & 0 deletions migrations/002-settings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Up
CREATE TABLE setting (
name CHAR(128) PRIMARY KEY NOT NULL,
active BOOLEAN NOT NULL
);

INSERT INTO setting (name, active) VALUES ('notification', 1);

CREATE TABLE subscription (
endpoint CHAR(600) PRIMARY KEY NOT NULL,
publicKey CHAR(4096) NOT NULL,
auth CHAR(600) NOT NULL
);

-- Down
DROP TABLE setting;
DROP TABLE subscription;
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
"passport-twitter": "^1.0.4",
"pushover-notifications": "^0.2.4",
"rss": "^1.2.2",
"sqlite3": "^3.1.13",
"sqlite": "^2.8.0",
"unfetch": "^3.0.0"
},
"scripts": {
"build": "rollup -cw",
"test": "http-server test -o 'http://localhost:8080/'",
"dev": "nodemon index.js"
"test": "http-server test -o 'http://localhost:8080/' -P 'http://localhost:3000/'",
"dev": "NODE_ENV=development nodemon index.js"
},
"description": "a simple node app for disqus-like drop-in commenting on static websites",
"main": "index.js",
Expand Down
31 changes: 7 additions & 24 deletions src/db/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
const fs = require('fs');
const path = require('path');
const sqlite3 = require('sqlite3').verbose();
const db = require('sqlite');
const dbname = 'comments.db';
const dbpath = path.resolve(__dirname, `../../${dbname}`);
const schemapath = path.resolve(__dirname, `./schema.sql`);

function connect() {
return new Promise((resolve, reject) => {
const db = new sqlite3.Database(dbpath, (err) => {
if (err) return reject(err);
console.log('connected to db.');
resolve(db);
});
});
}

function createSchema(db) {
return new Promise((resolve, reject) => {
db.all("SELECT name FROM sqlite_master WHERE type = 'table'", (err, rows) => {
if (err) return resolve(err);
if (!rows.length) return db.exec(fs.readFileSync(schemapath, 'utf-8'), () => resolve(db));

resolve(db);
});
});
}

// returns promise that passes db obj
function init() {
return connect().then(db => createSchema(db));
return Promise.resolve(db.open(dbpath, { Promise }))
.then(db => db.migrate({
force: process.env.NODE_ENV === 'development' ? 'last' : false
}))
.then(db => db.driver) // @FIXME
.catch(err => console.error(err));
}

module.exports = {
Expand Down

0 comments on commit 411545a

Please sign in to comment.