-
Notifications
You must be signed in to change notification settings - Fork 55
/
postgres.js
167 lines (143 loc) · 3.9 KB
/
postgres.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
var utils = require('utilities')
, pg
, generator = require('../../../lib/generators/sql')
, model = require('../../index')
, Query = require('../../query/query').Query
, BaseAdapter = require('./base').Adapter
, Adapter
, _baseConfig;
// DB lib should be locally installed in the consuming app
pg = utils.file.requireLocal('pg', model.localRequireError);
_baseConfig = {
user: process.env.USER
, database: process.env.USER
, password: null
, host: null
, port: 5432
};
Adapter = function (options) {
var opts = options || {}
, config;
this.name = 'postgres';
this.type = 'sql';
this.config = this.loadConfig(_baseConfig, opts);
this.client = null;
this.generator = generator.getGeneratorForAdapter(this);
this.init.apply(this, arguments);
};
Adapter.prototype = new BaseAdapter();
Adapter.prototype.constructor = Adapter;
utils.mixin(Adapter.prototype, new (function () {
this._getCount = function (data) {
return (data.rows.length) ? data.rows[0].count : 0;
};
this.init = function () {
this.client = new pg.Client(this.config);
};
this.connect = function (callback) {
var self = this
, cb = callback || function () {};
this.client.connect(function (err, data) {
if (err) {
self.emit('error', err);
cb(err);
}
else {
self.emit('connect');
cb();
}
});
};
this.disconnect = function (callback) {
var self = this
, cb = callback || function () {};
this.client.once('end', function () {
self.emit('disconnect');
cb();
});
this.client.end();
};
this.exec = function (query, callback) {
if (model.log) {
model.log(query);
}
return this.client.query(query, callback);
};
this.bySQL = function (query, model, callback) {
var name = this._tableizeModelName(model.modelName)
, sql = 'SELECT ' + name + '.* FROM ' + name;
sql += ' ' + query;
this._itemsWithSQL.apply(sql, [name], name, query, callback);
};
this.update = function (data, query, callback) {
var sql = this._createUpdateStatementWithConditions(data, query);
this.exec(sql, function (err, res) {
if (err) {
callback(err, null);
}
else {
if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
}
});
};
this.remove = function (query, callback) {
var sql = this._createDeleteStatementWithConditions(query);
this.exec(sql, function (err, data) {
if (err) {
callback(err, null);
}
else {
callback(null, true);
}
});
};
this.insert = function (data, opts, callback) {
var self = this
, items = Array.isArray(data) ? data : [data]
, modelName = items[0].type
, ctor = model[modelName]
, reg = model.descriptionRegistry
, props = reg[modelName].properties
, sql = '';
items.forEach(function (item) {
var statement = self._createInsertStatement(item, props);
statement = statement.replace(/;$/, ' RETURNING id;');
sql += statement;
});
this.exec(sql, function (err, res) {
var item
, rows;
if (err) {
callback(err, null);
}
else {
rows = res.rows;
for (var i = 0, ii = items.length; i < ii; i++) {
item = items[i];
item.id = rows[i].id; // Set the id from the row
item._saved = true;
}
if (data instanceof model.ModelBase) {
callback(null, data);
}
else {
callback(null, true);
}
}
});
};
this.createTable = function (names, callback) {
var sql = this.generator.createTable(names);
this.exec(sql, callback);
};
this.dropTable = function (names, callback) {
var sql = this.generator.dropTable(names);
this.exec(sql, callback);
};
})());
module.exports.Adapter = Adapter;