1 |
const { Database: DB } = require('sqlite3'); |
/** |
2 |
|
* This file is part of SudoBot. |
3 |
class Database { |
* |
4 |
constructor(dbpath) { |
* Copyright (C) 2021-2022 OSN Inc. |
5 |
this.dbpath = dbpath; |
* |
6 |
this.db = new DB(dbpath, (err) => { |
* SudoBot is free software; you can redistribute it and/or modify it |
7 |
if (err) { |
* under the terms of the GNU Affero General Public License as published by |
8 |
console.log(err); |
* the Free Software Foundation, either version 3 of the License, or |
9 |
} |
* (at your option) any later version. |
10 |
}); |
* |
11 |
} |
* SudoBot is distributed in the hope that it will be useful, but |
12 |
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
get(sql, callback1, callback2) { |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
return this.db.get(sql, callback1, callback2); |
* GNU Affero General Public License for more details. |
15 |
} |
* |
16 |
|
* You should have received a copy of the GNU Affero General Public License |
17 |
all(sql, callback1, callback2) { |
* along with SudoBot. If not, see <https://www.gnu.org/licenses/>. |
18 |
return this.db.all(sql, callback1, callback2); |
*/ |
19 |
} |
|
20 |
|
import DiscordClient from './Client'; |
21 |
runAsync(sql, paramsOrCallback = []) { |
import mongoose from "mongoose"; |
22 |
return new Promise((resolve, reject) => { |
|
23 |
this.db.run(sql, paramsOrCallback, err => { |
export default class Database { |
24 |
if (err) { |
client: DiscordClient; |
25 |
reject(err); |
|
26 |
return; |
constructor(client: DiscordClient) { |
27 |
} |
this.client = client; |
28 |
|
|
29 |
resolve(); |
mongoose.connect(process.env.MONGO_URI!) |
30 |
}); |
.then(() => console.log("Connected to MongoDB")) |
31 |
}); |
.catch(console.error); |
32 |
} |
} |
|
|
|
|
getAsync(sql, paramsOrCallback = []) { |
|
|
return new Promise((resolve, reject) => { |
|
|
this.db.get(sql, paramsOrCallback, (err, data) => { |
|
|
if (err) { |
|
|
reject(err); |
|
|
return; |
|
|
} |
|
|
|
|
|
resolve(data); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
|
|
|
allAsync(sql, paramsOrCallback = []) { |
|
|
return new Promise((resolve, reject) => { |
|
|
this.db.all(sql, paramsOrCallback, (err, data) => { |
|
|
if (err) { |
|
|
reject(err); |
|
|
return; |
|
|
} |
|
|
|
|
|
resolve(data); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
module.exports = Database; |
|
33 |
|
} |