-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
55 lines (43 loc) · 1.27 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
'use strict';
const verymodel = require('verymodel');
const pgp = require('pg-promise')({ pgFormatting: true });
const lodashAssign = require('lodash.assign');
const modelPrototype = require('./lib/modelmethods');
const EmptyResult = require('./lib/utils').EmptyResult;
module.exports = function (default_connection) {
const model_cache = {};
function Model() {
verymodel.VeryModel.apply(this, arguments);
model_cache[this.options.name] = this;
this.getModelByName = function (name) {
return model_cache[name];
};
/* $lab:coverage:off$ */
if (!this.options.connection) {
this.options.connection = default_connection;
}
/* $lab:coverage:on$ */
this.getDB = function () {
return pgp(this.options.connection);
};
this.extendModel({
getDB: function (callback) {
return pgp(this.__verymeta.connection);
}
});
}
Model.prototype = Object.create(verymodel.VeryModel.prototype);
lodashAssign(Model.prototype, modelPrototype);
return {
Model: Model,
/* $lab:coverage:off$ */
setConnection: function (connection) {
default_connection = connection;
},
getClient: function () {
return pgp(default_connection);
},
/* $lab:coverage:on$ */
EmptyResult
};
};