-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.js
46 lines (31 loc) · 1.08 KB
/
Database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use strict';
const Table = require('./Table');
const Bone = require('@botsocket/bone');
module.exports = class Database {
constructor(options) {
const { provider: Provider, providerOptions, tablePrefix } = options;
this.providerOptions = providerOptions ?? {};
this.provider = new Provider(this.providerOptions);
this.tablePrefix = tablePrefix ?? '';
this._ready = false;
this.tables = {};
}
async _initialize() {
if (this._ready) {
return;
}
if (typeof this.provider.initialize === 'function') {
await this.provider.initialize();
}
this._ready = true;
}
async table(name, options) {
Bone.assert(typeof name === 'string', 'Table name must be a string');
if (this.tables[name]) {
return this.tables[name];
}
const table = new Table(this, { ...options, name: `${this.tablePrefix}${name}` });
await this.provider.ensureTable(`${this.tablePrefix}${name}`);
return (this.tables[name] = table);
}
};