-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
38 lines (32 loc) · 819 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 POSTS_SCHEMA = `
CREATE TABLE IF NOT EXISTS posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
titulo VARCHAR(50) NOT NULL,
conteudo VARCHAR(140)
)
`;
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
)
`;
db.serialize(() => {
db.run("PRAGMA foreign_keys=ON");
db.run(POSTS_SCHEMA);
db.run(USUARIOS_SCHEMA);
db.each("SELECT * FROM usuarios", (err, usuario) => {
console.log("Usuarios: ");
console.log(usuario);
});
});
process.on("SIGINT", () =>
db.close(() => {
process.exit(0);
})
);
module.exports = db;