-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.ts
132 lines (108 loc) · 3.2 KB
/
core.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
'use strict';
/**
* Module Dependencies
* @private
*/
import uuid from 'uuid';
import sqlite from 'sqlite3';
/**
* DB Setup
* @param { String } dbPath
* @returns { protonDB }
* @public
*/
export class protonDB {
private db: sqlite.Database;
constructor(dbPath: string) {
this.db = new sqlite.Database(dbPath);
}
/**
* Open a database
*/
public open(): Promise<void> {
return new Promise((resolve) => {
this.db.serialize(() => {
resolve()
})
})
}
/**
* Close a database
*/
public close(): Promise<void> {
return new Promise((resolve, reject) =>
this.db.close((err) => {
err ? reject(err) : resolve();
}))
}
/**
* SQLite3 Command Procesor
* @param { string } command
* @param { any[] } params
*/
public run(command: string, params: any[] = []): Promise<void> {
return new Promise((resolve, reject) => {
this.db.run(command, params, (err) => {
err ? reject(err) : resolve();
})
})
}
/**
* Search a database
* @param { string } query What to search for
* @param { any[] } params
* @returns { T } Matches | undefined
*/
public get<T>(query: string, params: any[] = []): Promise<T | undefined> {
return new Promise((resolve, reject) => {
this.db.get(query, params, (err, row: T) => {
err ? reject(err) : resolve(row);
})
})
}
/**
* Search for all in a database
* @param { string } query What to search for
* @param { any[] } params
* @returns { T[] } Matches | undefined
*/
public all<T>(query: string, params: any[] = []): Promise<T[] | undefined> {
return new Promise((resolve, reject) => {
this.db.all(query, params, (err, rows: T[]) => {
err ? reject(err) : resolve(rows);
})
})
}
}
/**
* UUID Generation and DB Interface
* @public
*/
export class protonUUID {
generateClientId(): string {
return uuid.v4();
}
/**
* Check if user is in the database -- (database must already be open and will not be closed)
* @param { protonDB } db - Database to search
* @param { string } clientId - Client id to lookup
* @returns { Boolean }
*/
isClientInProtonDB(db: protonDB, clientId: string, ): Promise<boolean> {
return new Promise(async (resolve, reject) => {
await db.get('SELECT * FROM users WHERE uuid = ?', [clientId]) ? resolve(true) : reject()
})
}
/**
* Store new user id in the database -- (database must already be open and will not be closed)
* @param { protonDB } db - Database to search
* @param { string } clientId - Client id to save
* @returns
*/
storeClientIdInProtonDB(db: protonDB, clientId: string): Promise<void> {
return new Promise(async (resolve, reject) => {
await db.run('INSERT INTO users (uuid) VALUES (?)', [clientId])
await this.isClientInProtonDB(db, clientId) == true ? resolve() : reject()
})
}
}