This repository has been archived by the owner on Jan 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
205 lines (171 loc) · 5.85 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const _ = require('lodash');
const path = require('path');
const Bookshelf = require('bookshelf');
const paranoia = require('bookshelf-paranoia');
const eloquent = require('bookshelf-eloquent');
const Knex = require('knex');
const Service = require('bi-service');
const semver = require('semver');
module.exports = bookshelfBuilder;
const debug = getDebugStrategy();
/**
* @param {Object} options - knex options object
* @param {String} options.connection.db - a non-standard alias for `options.connection.database`
* @param {String} options.connection.database
* @param {String} options.connection.username - a non-standard alias for `options.connection.user`
* @param {String} options.connection.user
* @param {String} options.connection.password
* @param {String} options.connection.host
* @param {Integer} options.connection.port
* @param {Integer} options.pool.max
* @param {Integer} options.pool.min
* @param {Integer} options.pool.idle
* @param {Integer} options.dialect - a non-standard alias for `options.client`
* @param {String} options.client - mysql|pg|postgres|postgresql|sqlite3|sqlite|mariadb|mariasql|maria ^
* @param {String} [options.version] - version string in semver format
* @param {Boolean} [options.debug=false]
*
* @return {Bookshelf}
*/
function bookshelfBuilder(options) {
var defaults = {
pool: {
max: 10,
min: 0,
idle: 10000
},
debug: debug
};
options = _.merge(defaults, options);
//resolve database name
let database = _.compact([
_.get(options, ['connection', 'database']),
_.get(options, ['connection', 'db'])
]);
if (database[0] & database[1]) {
throw new Error(`Both 'connection.database' option and 'connection.db' alias can NOT be set.`);
} else {
database = database.shift();
}
//resolve user name
let user = _.compact([
_.get(options, ['connection', 'user']),
_.get(options, ['connection', 'username'])
]);
if (user[0] & user[1]) {
throw new Error(`Both 'connection.user' option and 'connection.username' alias can NOT be set.`);
} else {
user = user.shift();
}
//resolve sql dialect
let dialect = _.compact([
_.get(options, ['client']),
_.get(options, ['dialect'])
]);
if (dialect[0] & dialect[1]) {
throw new Error(`Both 'client' option and 'dialect' alias can NOT be set.`);
} else {
dialect = dialect.shift();
}
//
_.set(options, ['connection', 'user'], user);
_.set(options, ['connection', 'database'], database);
_.set(options, ['client'], dialect);
//remove non-standard aliases
if (options.connection) {
delete options.connection.username;
delete options.connection.db;
}
delete options.dialect;
const knex = Knex(options);
const bookshelf = Bookshelf(knex);
if (options.version) {
bookshelf.minServerVersion = options.version;
}
bookshelf.plugin('registry');
bookshelf.plugin('virtuals');
bookshelf.plugin('visibility');
bookshelf.plugin('pagination');
bookshelf.plugin(paranoia);
bookshelf.plugin(eloquent);
bookshelf.inspectIntegrity = inspectIntegrity;
bookshelf.loadModels = loadModels;
return bookshelf;
}
bookshelfBuilder.Bookshelf = Bookshelf;
bookshelfBuilder.Knex = Knex;
/**
* @return {Boolean|Function}
*/
function getDebugStrategy() {
var env = process.env;
var possibleValues = [1,0,true,false];
if (env.SQL_DEBUG === undefined || env.SQL_DEBUG === '') {
return false;
}
try {
var parsedDebugEnv = JSON.parse(env.SQL_DEBUG);
var valueIndex = possibleValues.indexOf(parsedDebugEnv);
if (valueIndex === -1) {
throw new SyntaxError;
} else if (possibleValues[valueIndex]) {
return true;
} else {
return false;
}
} catch(e) {
console.warn('Failed to parse SQL_DEBUG environment variable. Thus the option is disabled. Expects boolean value which can also be represented by 0/1 integer values.');
}
}
/**
* @return {Promise<Boolean>}
*/
function inspectIntegrity() {
const dialect = this.knex.client.dialect;
let q = void 0;
switch (dialect) {
case 'postgres':
case 'postgresql':
q='SHOW server_version;';
break;
case 'mysql':
case 'mariadb':
q='SELECT VERSION() as server_version;';
break;
default:
throw new Error('Integrity check - unsupported dialect');
}
return this.knex.raw(q).bind(this).then(function (result) {
var requiredVer = this.minServerVersion;
var currentVer = result.rows[0]['server_version'];
if ( requiredVer
&& semver.valid(requiredVer)
&& semver.lt(currentVer, requiredVer)
) {
throw new Error(`Requires ${dialect.toUpperCase()} version >= ${requiredVer}`);
}
return true;
});
};
/**
* @public
* @param {Array|String} paths - collection of files/directories, or single string path
* @param {Object} [options]
* @param {Array} [options.except] - collection of files/directories that should be excluded
*
* @return {Object}
*/
function loadModels(paths, options) {
const dict = {}
, bookshelf = this;
Service.moduleLoader.fileIterator(paths, options, function(file, dir) {
const pth = path.join(dir, file);
const _model = module.require(pth)(bookshelf);
if (!_model.prototype.modelName) {
_model.prototype.modelName = _.upperFirst(_.camelCase(_model.prototype.tableName));
}
const model = bookshelf.model(_model.prototype.modelName, _model);
dict[_model.prototype.modelName] = model;
});
return dict;
}