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 |
|
} |