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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 51 - (show annotations)
Mon Jul 29 17:28:23 2024 UTC (8 months ago) by rakin
File MIME type: application/typescript
File size: 1773 byte(s)
Release version 2.0
1 import { Database as DB } from 'sqlite3';
2 import DiscordClient from './Client';
3
4 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 this.dbpath = dbpath;
12 this.db = new DB(dbpath, (err) => {
13 if (err) {
14 console.log(err);
15 }
16 });
17 }
18
19 get(sql: string, params: any[] | Function, callback?: Function) {
20 return this.db.get(sql, params, callback);
21 }
22
23 all(sql: string, params: any[] | Function, callback?: Function) {
24 return this.db.all(sql, params, callback);
25 }
26
27 runAsync(sql: string, paramsOrCallback: any[] | Function = []) {
28 return new Promise<void>((resolve, reject) => {
29 this.db.run(sql, paramsOrCallback, err => {
30 if (err) {
31 reject(err);
32 return;
33 }
34
35 resolve();
36 });
37 });
38 }
39
40 getAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {
41 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 allAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {
54 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 };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26