1 |
rakin |
51 |
import { Database as DB } from 'sqlite3'; |
2 |
|
|
import DiscordClient from './Client'; |
3 |
rakin |
5 |
|
4 |
rakin |
51 |
export default class Database { |
5 |
|
|
client: DiscordClient; |
6 |
|
|
dbpath: string; |
7 |
|
|
db: DB; |
8 |
|
|
|
9 |
|
|
constructor(dbpath: string, client: DiscordClient) { |
10 |
|
|
this.client = client; |
11 |
rakin |
5 |
this.dbpath = dbpath; |
12 |
|
|
this.db = new DB(dbpath, (err) => { |
13 |
|
|
if (err) { |
14 |
|
|
console.log(err); |
15 |
|
|
} |
16 |
|
|
}); |
17 |
|
|
} |
18 |
|
|
|
19 |
rakin |
51 |
get(sql: string, params: any[] | Function, callback?: Function) { |
20 |
|
|
return this.db.get(sql, params, callback); |
21 |
rakin |
5 |
} |
22 |
|
|
|
23 |
rakin |
51 |
all(sql: string, params: any[] | Function, callback?: Function) { |
24 |
|
|
return this.db.all(sql, params, callback); |
25 |
rakin |
5 |
} |
26 |
rakin |
45 |
|
27 |
rakin |
51 |
runAsync(sql: string, paramsOrCallback: any[] | Function = []) { |
28 |
|
|
return new Promise<void>((resolve, reject) => { |
29 |
rakin |
45 |
this.db.run(sql, paramsOrCallback, err => { |
30 |
|
|
if (err) { |
31 |
|
|
reject(err); |
32 |
|
|
return; |
33 |
|
|
} |
34 |
|
|
|
35 |
|
|
resolve(); |
36 |
|
|
}); |
37 |
|
|
}); |
38 |
|
|
} |
39 |
|
|
|
40 |
rakin |
51 |
getAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> { |
41 |
rakin |
45 |
return new Promise((resolve, reject) => { |
42 |
|
|
this.db.get(sql, paramsOrCallback, (err, data) => { |
43 |
|
|
if (err) { |
44 |
|
|
reject(err); |
45 |
|
|
return; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
resolve(data); |
49 |
|
|
}); |
50 |
|
|
}); |
51 |
|
|
} |
52 |
|
|
|
53 |
rakin |
51 |
allAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> { |
54 |
rakin |
45 |
return new Promise((resolve, reject) => { |
55 |
|
|
this.db.all(sql, paramsOrCallback, (err, data) => { |
56 |
|
|
if (err) { |
57 |
|
|
reject(err); |
58 |
|
|
return; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
resolve(data); |
62 |
|
|
}); |
63 |
|
|
}); |
64 |
|
|
} |
65 |
rakin |
51 |
}; |