1 |
|
import { Sequelize } from 'sequelize'; |
2 |
import { Database as DB } from 'sqlite3'; |
import { Database as DB } from 'sqlite3'; |
3 |
import DiscordClient from './Client'; |
import DiscordClient from './Client'; |
4 |
|
import mongoose from "mongoose"; |
5 |
|
|
6 |
export default class Database { |
export default class Database { |
7 |
client: DiscordClient; |
client: DiscordClient; |
8 |
dbpath: string; |
dbpath: string; |
|
db: DB; |
|
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; |
|
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; |
|
|
} |
|
13 |
|
|
14 |
resolve(data); |
mongoose.connect(process.env.MONGO_URI!) |
15 |
}); |
.then(() => console.log("Connected to MongoDB")) |
16 |
}); |
.catch(console.error); |
17 |
} |
} |
|
}; |
|
18 |
|
} |