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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/src/Database.js revision 5 by rakin, Mon Jul 29 17:28:11 2024 UTC trunk/src/client/Database.ts revision 327 by rakin, Mon Jul 29 17:29:33 2024 UTC
# Line 1  Line 1 
1  const { Database: DB } = require('sqlite3');  import { Sequelize } from 'sequelize';
2    import { Database as DB } from 'sqlite3';
3    import DiscordClient from './Client';
4    import mongoose from "mongoose";
5    
6  class Database {  export default class Database {
7      constructor(dbpath) {      client: DiscordClient;
8        dbpath: string;
9        db: DB;
10        sequelize: Sequelize;
11    
12        constructor(dbpath: string, client: DiscordClient) {
13            this.client = client;
14          this.dbpath = dbpath;          this.dbpath = dbpath;
15    
16            this.sequelize = new Sequelize({
17                dialect: 'sqlite',
18                storage: dbpath,
19            });
20    
21          this.db = new DB(dbpath, (err) => {          this.db = new DB(dbpath, (err) => {
22              if (err) {              if (err) {
23                  console.log(err);                  console.log(err);
24              }              }
25          });          });
26    
27            mongoose.connect(process.env.MONGO_URI!)
28                .then(() => console.log("Connected to MongoDB"))
29                .catch(console.error);
30        }
31    
32        get(sql: string, params: any[] | Function, callback?: Function) {
33            return this.db.get(sql, params, callback);
34        }
35    
36        all(sql: string, params: any[] | Function, callback?: Function) {
37            return this.db.all(sql, params, callback);
38        }
39    
40        runAsync(sql: string, paramsOrCallback: any[] | Function = []) {
41            return new Promise<void>((resolve, reject) => {
42                this.db.run(sql, paramsOrCallback, err => {
43                    if (err) {
44                        reject(err);
45                        return;
46                    }
47    
48                    resolve();
49                });
50            });
51      }      }
52    
53      get(sql, callback1, callback2) {      getAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {
54          return this.db.get(sql, callback1, callback2);          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      all(sql, callback1, callback2) {      allAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {
67          return this.db.all(sql, callback1, callback2);          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    
 module.exports = Database;  
79        get s(): Sequelize {
80            return this.sequelize;
81        }
82    };

Legend:
Removed from v.5  
changed lines
  Added in v.327

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26