-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinserts.js
48 lines (37 loc) · 1.01 KB
/
inserts.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
'use strict';
var pgp = require('pg-promise');
function Inserts (record, values, concat) {
if (!(this instanceof Inserts)) {
return new Inserts(record, values, concat);
}
var self = this;
if (!record || !record.table || !record.values) {
throw 'Inserts: Invalid record argument';
}
if (!values) {
throw 'Inserts: Invalid values argument';
}
this.namedTemplate = function () {
return record.fields.map(function (field, index) {
return '${' + field + '}';
}).join(',');
};
this._template = this.namedTemplate();
this.template = function () {
var values;
var fields = record.fields.map(pgp.as.name).join(',');
if (concat) {
values = '$1';
} else {
values = '(' + this.namedTemplate() + ')';
}
return pgp.as.format('INSERT INTO $1~($2^) VALUES $3^', [record.table, fields, values]);
};
this._rawDBType = true;
this.formatDBType = function () {
return values.map(function (v) {
return '(' + pgp.as.format(self._template, v) + ')';
}).join(',');
};
}
module.exports = Inserts;