1 |
rakin |
59 |
import { Sequelize } from 'sequelize'; |
2 |
rakin |
51 |
import { Database as DB } from 'sqlite3'; |
3 |
|
|
import DiscordClient from './Client'; |
4 |
rakin |
327 |
import mongoose from "mongoose"; |
5 |
rakin |
5 |
|
6 |
rakin |
51 |
export default class Database { |
7 |
|
|
client: DiscordClient; |
8 |
|
|
dbpath: string; |
9 |
|
|
db: DB; |
10 |
rakin |
59 |
sequelize: Sequelize; |
11 |
rakin |
51 |
|
12 |
|
|
constructor(dbpath: string, client: DiscordClient) { |
13 |
|
|
this.client = client; |
14 |
rakin |
5 |
this.dbpath = dbpath; |
15 |
rakin |
59 |
|
16 |
|
|
this.sequelize = new Sequelize({ |
17 |
|
|
dialect: 'sqlite', |
18 |
rakin |
86 |
storage: dbpath, |
19 |
rakin |
59 |
}); |
20 |
|
|
|
21 |
rakin |
5 |
this.db = new DB(dbpath, (err) => { |
22 |
|
|
if (err) { |
23 |
|
|
console.log(err); |
24 |
|
|
} |
25 |
|
|
}); |
26 |
rakin |
327 |
|
27 |
|
|
mongoose.connect(process.env.MONGO_URI!) |
28 |
|
|
.then(() => console.log("Connected to MongoDB")) |
29 |
|
|
.catch(console.error); |
30 |
rakin |
5 |
} |
31 |
|
|
|
32 |
rakin |
51 |
get(sql: string, params: any[] | Function, callback?: Function) { |
33 |
|
|
return this.db.get(sql, params, callback); |
34 |
rakin |
5 |
} |
35 |
|
|
|
36 |
rakin |
51 |
all(sql: string, params: any[] | Function, callback?: Function) { |
37 |
|
|
return this.db.all(sql, params, callback); |
38 |
rakin |
5 |
} |
39 |
rakin |
45 |
|
40 |
rakin |
51 |
runAsync(sql: string, paramsOrCallback: any[] | Function = []) { |
41 |
|
|
return new Promise<void>((resolve, reject) => { |
42 |
rakin |
45 |
this.db.run(sql, paramsOrCallback, err => { |
43 |
|
|
if (err) { |
44 |
|
|
reject(err); |
45 |
|
|
return; |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
resolve(); |
49 |
|
|
}); |
50 |
|
|
}); |
51 |
|
|
} |
52 |
|
|
|
53 |
rakin |
51 |
getAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> { |
54 |
rakin |
45 |
return new Promise((resolve, reject) => { |
55 |
|
|
this.db.get(sql, paramsOrCallback, (err, data) => { |
56 |
|
|
if (err) { |
57 |
|
|
reject(err); |
58 |
|
|
return; |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
resolve(data); |
62 |
|
|
}); |
63 |
|
|
}); |
64 |
|
|
} |
65 |
|
|
|
66 |
rakin |
51 |
allAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> { |
67 |
rakin |
45 |
return new Promise((resolve, reject) => { |
68 |
|
|
this.db.all(sql, paramsOrCallback, (err, data) => { |
69 |
|
|
if (err) { |
70 |
|
|
reject(err); |
71 |
|
|
return; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
resolve(data); |
75 |
|
|
}); |
76 |
|
|
}); |
77 |
|
|
} |
78 |
rakin |
59 |
|
79 |
|
|
get s(): Sequelize { |
80 |
|
|
return this.sequelize; |
81 |
|
|
} |
82 |
rakin |
51 |
}; |