This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbconnection.js
57 lines (48 loc) · 1.87 KB
/
dbconnection.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
50
51
52
53
54
55
56
57
// For connecting to the database (sqlite3)
const sqlite3 = require('sqlite3')
exports.dbconnect = (dbname, callback) => {
const db = new sqlite3.Database(dbname) // Connect to the database or create one if necessary
checkTables(db, () => { // Check if the database is ready to be used.
callback(db) // All set. Keep going.
})
}
checkTables = (db, callback) => { // Is the database OK?
db.all("SELECT 1 FROM sqlite_master WHERE type='table' AND name='REPOS'", (err, allRows) => { // Does 'REPOS' table exist?
if(err) {
console.log('Error reading database.')
return
}
if(allRows.length == 0) createTables(db, callback) // It doesn't exist. Create all tables.
else callback()
})
}
createTables = (db, callback) => { // Create the necessary tables for the program to work.
db.serialize(() => { // Wait until all the tables are created.
/*
HOW THE DATABASE IS STRUCTURED
- REPOS
-- NAME (text, primary key) - Name of the repository.
-- ANONREAD (int, default: 0) - Can the repository be cloned by a non-logged in user?
--- 0: No. Private.
--- 1: Yes. Public.
- USERS
-- USERNAME (text, primary key)
-- PASSWORD (text) - in plain text. I know.
- PERMISSIONS
-- ID (int, primary key, autoincrement)
-- USER (text) - The user who the permission is assigned to.
-- REPO (text) - The repository (context of the permission).
-- LEVEL (int, default: 1) - The permission level.
--- 0: No access at all.
--- 1: Read access (clone)
--- 2: Write access (push)
*/
Q = [
"CREATE TABLE `PERMISSIONS` (`ID` INTEGER PRIMARY KEY AUTOINCREMENT, `USER` TEXT, `REPO` TEXT, `LEVEL` INT DEFAULT '1')",
"CREATE TABLE `REPOS` (`NAME` TEXT PRIMARY KEY, `ANONREAD` INT DEFAULT '0')",
"CREATE TABLE `USERS` (`USERNAME` TEXT PRIMARY KEY, `PASSWORD` TEXT)"
]
for(var i=0;i<Q.length;++i) db.run(Q[i]) // Run every statement
})
callback() // Go back.
}