-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdatabase.model.js
More file actions
49 lines (42 loc) · 919 Bytes
/
database.model.js
File metadata and controls
49 lines (42 loc) · 919 Bytes
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
'use-strict'
// Database
const sqlite3 = require('sqlite3').verbose()
const db = new sqlite3.Database('housing.db')
db.serialize(() => {
let cmd = "SELECT name FROM sqlite_master WHERE type='table' AND name='housingTable' ";
db.get(cmd, (err, val) => {
if (err) throw err;
if (val == undefined) {
console.log("Creating Database ...");
createHousingDB();
} else {
console.log("Database file found", val);
}
});
})
function createHousingDB() {
const sql = (
`CREATE TABLE housingTable (
id INTEGER PRIMARY KEY UNIQUE,
email TEXT,
type TEXT,
bed INTEGER,
bath INTEGER,
price INTEGER,
covidTested TEXT,
moveIn TEXT,
location TEXT,
desc TEXT,
date TEXT,
image TEXT
)`
);
db.run(sql, function(err, val) {
if (err) {
console.log("Database creation failure", err.message);
} else {
console.log("Created database");
}
});
}
module.exports = db;