/[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

revision 59 by rakin, Mon Jul 29 17:28:25 2024 UTC revision 346 by rakin, Mon Jul 29 17:29:42 2024 UTC
# Line 1  Line 1 
 import { Sequelize } from 'sequelize';  
 import { Database as DB } from 'sqlite3';  
1  import DiscordClient from './Client';  import DiscordClient from './Client';
2    import mongoose from "mongoose";
3    
4  export default class Database {  export default class Database {
5      client: DiscordClient;      client: DiscordClient;
     dbpath: string;  
     db: DB;  
     sequelize: Sequelize;  
6    
7      constructor(dbpath: string, client: DiscordClient) {      constructor(client: DiscordClient) {
8          this.client = client;          this.client = client;
         this.dbpath = dbpath;  
9    
10          this.sequelize = new Sequelize({          mongoose.connect(process.env.MONGO_URI!)
11              dialect: 'sqlite',              .then(() => console.log("Connected to MongoDB"))
12              storage: dbpath              .catch(console.error);
         });  
   
         this.db = new DB(dbpath, (err) => {  
             if (err) {  
                 console.log(err);  
             }  
         });  
     }  
   
     get(sql: string, params: any[] | Function, callback?: Function) {  
         return this.db.get(sql, params, callback);  
     }  
   
     all(sql: string, params: any[] | Function, callback?: Function) {  
         return this.db.all(sql, params, callback);  
     }  
   
     runAsync(sql: string, paramsOrCallback: any[] | Function = []) {  
         return new Promise<void>((resolve, reject) => {  
             this.db.run(sql, paramsOrCallback, err => {  
                 if (err) {  
                     reject(err);  
                     return;  
                 }  
   
                 resolve();  
             });  
         });  
     }  
   
     getAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {  
         return new Promise((resolve, reject) => {  
             this.db.get(sql, paramsOrCallback, (err, data) => {  
                 if (err) {  
                     reject(err);  
                     return;  
                 }  
   
                 resolve(data);  
             });  
         });  
     }  
   
     allAsync(sql: string, paramsOrCallback: any[] | Function = []): Promise <any> {  
         return new Promise((resolve, reject) => {  
             this.db.all(sql, paramsOrCallback, (err, data) => {  
                 if (err) {  
                     reject(err);  
                     return;  
                 }  
   
                 resolve(data);  
             });  
         });  
     }  
   
     get s(): Sequelize {  
         return this.sequelize;  
13      }      }
 };  
14    }

Legend:
Removed from v.59  
changed lines
  Added in v.346

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26