-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
57 lines (46 loc) · 1.69 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
'use strict';
const MySQL = require('mysql2');
const EmptyModel = require('./model');
const { DatabaseLoader } = require('@booljs/api');
const { readFileSync } = require('fs');
const { join } = require('path');
module.exports = class BoolJSMySQL extends DatabaseLoader {
constructor ({ connectionSettingsStoreName = 'mysql' } = {}) {
super('booljs.mysql', connectionSettingsStoreName);
}
async openDatabase (configuration) {
const settings = configuration[process.env.NODE_ENV || 'development'];
const { promises, pool, ...connectionOptions } = settings;
let connection;
if (pool !== undefined && pool !== null) {
const connectionLimit = 1000;
const waitForConnections = true;
connection = await MySQL.createPool({
connectionLimit, waitForConnections, ...connectionOptions
});
} else {
connection = await MySQL.createConnection(connectionOptions);
}
this.connection = connection;
return connection;
};
async fetchModels (instance, models, Component, connection) {
return class extends EmptyModel {
constructor (...params) {
return new (Function.prototype.bind.apply(Component, [
null, instance.getComponents(), connection
].concat(params)))();
}
};
}
modelClass () {
return EmptyModel;
}
modelTemplate () {
return readFileSync(join(require.resolve('.'), 'templates/model.js'));
}
modelConfiguration () {
return readFileSync(
join(require.resolve('.'), 'templates/configuration.json'));
}
};