/[sudobot]/trunk/src/client/Database.ts
ViewVC logotype

Annotation of /trunk/src/client/Database.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 59 - (hide annotations)
Mon Jul 29 17:28:25 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2015 byte(s)
Improved spam filter
1 rakin 59 import { Sequelize } from 'sequelize';
2 rakin 51 import { Database as DB } from 'sqlite3';
3     import DiscordClient from './Client';
4 rakin 5
5 rakin 51 export default class Database {
6     client: DiscordClient;
7     dbpath: string;
8     db: DB;
9 rakin 59 sequelize: Sequelize;
10 rakin 51
11     constructor(dbpath: string, client: DiscordClient) {
12     this.client = client;
13 rakin 5 this.dbpath = dbpath;
14 rakin 59
15     this.sequelize = new Sequelize({
16     dialect: 'sqlite',
17     storage: dbpath
18     });
19    
20 rakin 5 this.db = new DB(dbpath, (err) => {
21     if (err) {
22     console.log(err);
23     }
24     });
25     }
26    
27 rakin 51 get(sql: string, params: any[] | Function, callback?: Function) {
28     return this.db.get(sql, params, callback);
29 rakin 5 }
30    
31 rakin 51 all(sql: string, params: any[] | Function, callback?: Function) {
32     return this.db.all(sql, params, callback);
33 rakin 5 }
34 rakin 45
35 rakin 51 runAsync(sql: string, paramsOrCallback: any[] | Function = []) {
36     return new Promise<void>((resolve, reject) => {
37 rakin 45 this.db.run(sql, paramsOrCallback, err => {
38     if (err) {
39     reject(err);
40     return;
41     }
42    
43     resolve();
44     });
45     });
46     }
47    
48 rakin 51 getAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {
49 rakin 45 return new Promise((resolve, reject) => {
50     this.db.get(sql, paramsOrCallback, (err, data) => {
51     if (err) {
52     reject(err);
53     return;
54     }
55    
56     resolve(data);
57     });
58     });
59     }
60    
61 rakin 51 allAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {
62 rakin 45 return new Promise((resolve, reject) => {
63     this.db.all(sql, paramsOrCallback, (err, data) => {
64     if (err) {
65     reject(err);
66     return;
67     }
68    
69     resolve(data);
70     });
71     });
72     }
73 rakin 59
74     get s(): Sequelize {
75     return this.sequelize;
76     }
77 rakin 51 };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26