-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
49 lines (30 loc) · 1.05 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
var sqlite3 = require('sqlite3').verbose()
var md5 = require('md5')
const DATABASE = "database.db"
let db = new sqlite3.Database(DATABASE, (err) => {
if ( err ) {
// Cannot open database
console.error(err.message)
throw err
} else {
console.log('Connected to the SQLite database.')
db.run(`CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name text NOT NULL,
email text NOT NULL UNIQUE,
password text NOT NULL,
CONSTRAINT email_unique UNIQUE (email)
)`,
(err) => {
if (err) {
// Table already created
} else {
// Table just created, creating some rows
var insert = 'INSERT INTO users (name, email, password) VALUES (?,?,?)';
db.run( insert, ["admin", "[email protected]", md5("adm123")] );
db.run( insert, ["user", "[email protected]", md5("user123")] );
}
});
}
});
module.exports = db