This repository has been archived by the owner on Jul 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdatabase.js
47 lines (39 loc) · 1.66 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
'use strict';
// Dependencies
const sqlite = require('sqlite3');
const connectSQLite = require('connect-sqlite3');
const chalk = require('chalk');
// Initialise database connection
const database = new sqlite.Database('./database.db', (err) => {
if (err) throw err;
console.log(chalk.bold.red('Database connected...'));
});
// Execute in serialised mode
database.serialize(() => {
// Create user table query then run the query
let sql_user = `CREATE TABLE IF NOT EXISTS user (
id INTEGER PRIMARY KEY NOT NULL UNIQUE,
username TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL,
wins INTEGER DEFAULT 0,
losses INTEGER DEFAULT 0
);`;
database.run(sql_user, (err) => { if (err) throw err; });
// Create game table query then run the query
let sql_game = `CREATE TABLE IF NOT EXISTS game (
id INTEGER PRIMARY KEY NOT NULL UNIQUE,
player1Id INTEGER NOT NULL,
player2Id INTEGER NOT NULL,
player1Score INTEGER DEFAULT 0,
player2Score INTEGER DEFAULT 0,
time TEXT DEFAULT CURRENT_TIMESTAMP
);`;
database.run(sql_game, (err) => { if (err) throw err; });
});
// Session store
database.sessionStore = (session) => new (connectSQLite(session))({ db: 'database.db' });
// Export the database module
module.exports = database;