/[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 341 by rakin, Mon Jul 29 17:29:33 2024 UTC revision 342 by rakin, Mon Jul 29 17:29:39 2024 UTC
# Line 6  import mongoose from "mongoose"; Line 6  import mongoose from "mongoose";
6  export default class Database {  export default class Database {
7      client: DiscordClient;      client: DiscordClient;
8      dbpath: string;      dbpath: string;
     db: DB;  
     sequelize: Sequelize;  
9    
10      constructor(dbpath: string, client: DiscordClient) {      constructor(dbpath: string, client: DiscordClient) {
11          this.client = client;          this.client = client;
12          this.dbpath = dbpath;          this.dbpath = dbpath;
13    
         this.sequelize = new Sequelize({  
             dialect: 'sqlite',  
             storage: dbpath,  
         });  
   
         this.db = new DB(dbpath, (err) => {  
             if (err) {  
                 console.log(err);  
             }  
         });  
   
14          mongoose.connect(process.env.MONGO_URI!)          mongoose.connect(process.env.MONGO_URI!)
15              .then(() => console.log("Connected to MongoDB"))              .then(() => console.log("Connected to MongoDB"))
16              .catch(console.error);              .catch(console.error);
17      }      }
   
     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;  
     }  
 };  
18    }

Legend:
Removed from v.341  
changed lines
  Added in v.342

team@onesoftnet.eu.org
ViewVC Help
Powered by ViewVC 1.1.26