-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.js
38 lines (32 loc) · 876 Bytes
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database('db.sqlite');
const USUARIOS_SCHEMA = `
CREATE TABLE IF NOT EXISTS usuarios (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nome VARCHAR(40) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
senhaHash VARCHAR(255) NOT NULL,
emailVerificado INTEGER,
cargo VARCHAR(15) CHECK (cargo in ('admin', 'editor', 'assinante')) NOT NULL
)
`;
const POSTS_SCHEMA = `
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
titulo VARCHAR(50) NOT NULL,
conteudo VARCHAR(140),
autor INTEGER NOT NULL,
FOREIGN KEY (autor) REFERENCES usuarios(id)
)
`;
db.serialize(() => {
db.run('PRAGMA foreign_keys=ON');
db.run(POSTS_SCHEMA);
db.run(USUARIOS_SCHEMA);
});
process.on('SIGINT', () =>
db.close(() => {
process.exit(0);
})
);
module.exports = db;