-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
228 lines (178 loc) · 5.45 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
'use strict';
const Wadofgum = require('Wadofgum');
const WadofgumValidation = require('Wadofgum-validation');
const WadofgumProcess = require('Wadofgum-process');
const WadofgumKeyMap = require('Wadofgum-keymap');
const EventEmitter = require('events').EventEmitter;
const pg = require('pg-promise')();
const qrm = pg.queryResult;
const pkg = require('root-require')('./package.json');
const versuf = String.prototype.split.call(pkg.version, '.').join('x');
module.exports = (config) => {
const db = pg(config.db);
const createQuery = new pg.QueryFile('./sql/createCollection.sql');
const whereFunc = `GOALPOST_WHERECLAUSEv${versuf}`;
const findFunc = `GOALPOST_FINDv${versuf}`;
const putFunc = `GOALPOST_PUTv${versuf}`;
const postFunc = `GOALPOST_POSTv${versuf}`;
const sizeofFunc = `GOALPOST_SIZEOFv${versuf}`;
const updateFunc = `GOALPOST_UPDATEv${versuf}`;
const destroyFunc = `GOALPOST_DESTROYv${versuf}`;
const getFunc = `GOALPOST_GETv${versuf}`;
const deleteFunc = `GOALPOST_DELETEv${versuf}`;
const deleteWhereFunc = `GOALPOST_DELETEWHEREv${versuf}`;
class Collection extends Wadofgum.mixin(WadofgumValidation, WadofgumProcess, WadofgumKeyMap) {
constructor(opts) {
super(opts);
EventEmitter.call(this);
this.listenClient = null;
this.listener = null;
this.name = opts.name;
/* $lab:coverage:off$ */
this.exists = opts.exists || false;
this.primary = opts.primary || 'id';
/* $lab:coverage:on$ */
}
_ready() {
if (this.exists) {
return Promise.resolve(db);
}
return db.query(createQuery, [this.name, `idx_${this.name}_gin`, `pk_${this.name}_seq`, versuf])
.then(() => {
return new Promise((resolve, reject) => {
this.listenClient = new pg.pg.Client(config.db);
this.listenClient.connect((err, client) => {
this.listener = client;
const hint = `goalpost_hint_${this.name}`;
const full = `goalpost_full_${this.name}`;
/* $lab:coverage:off$ */
if (err) {
return reject(err);
}
/* $lab:coverage:on$ */
this.listener = client;
client.on('notification', (msg) => {
/* $lab:coverage:off$ */
if (msg.name === 'notification') {
/* $lab:coverage:on$ */
const payload = JSON.parse(msg.payload);
if (msg.channel === hint) {
this.emit('changeHint', payload);
}
else if (msg.channel === full) {
this.emit('changeFull', payload);
}
}
});
client.query(`LISTEN goalpost_full_${this.name}`, (err) => {
/* $lab:coverage:off$ */
if (err) {
return reject(err);
}
/* $lab:coverage:on$ */
client.query(`LISTEN goalpost_hint_${this.name}`, (err) => {
/* $lab:coverage:off$ */
if (err) {
return reject(err);
}
/* $lab:coverage:on$ */
return resolve();
});
});
});
});
})
.then(() => {
this.exists = true;
return db;
});
}
size(obj) {
return this._ready()
.then(() => {
return db.func(sizeofFunc, [this.name], qrm.one)
.then((result) => {
return parseInt(result[sizeofFunc.toLowerCase()], 10);
});
});
}
destroy() {
return db.func(destroyFunc, [this.name, `pk_${this.name}_seq`]);
}
put(obj) {
return this._ready()
.then(() => {
if (obj[this.primary]) {
return db.func(putFunc, [this.name, obj[this.primary], obj], qrm.one);
}
return db.func(postFunc, [this.name, obj], qrm.one);
})
.then((results) => {
const result = results.doc;
result[this.primary] = results.id;
return result;
});
}
delete(input) {
return this._ready()
.then(() => {
if (typeof input === 'object') {
return db.func(deleteWhereFunc, [this.name, input])
.then((res) => {
return res;
});
}
return db.func(deleteFunc, [this.name, input])
.then((res) => {
return;
});
});
}
update(id, obj) {
return this._ready()
.then(() => {
return db.func(updateFunc, [this.name, id, obj], qrm.one);
})
.then((results) => {
const obj2 = results.doc;
obj2[this.primary] = results.id;
return obj2;
});
}
get(id) {
return this._ready()
.then(() => {
return db.func(getFunc, [this.name, id], qrm.one);
})
.then((results) => {
const obj = results.doc;
obj[this.primary] = results.id;
return obj;
});
}
find(opts) {
return this._ready()
.then(() => {
return db.func(findFunc, [this.name, opts]);
})
.then((results) => {
return results.map((result) => {
const obj = result.doc;
obj[this.primary] = result.id;
return obj;
});
});
}
list(opts) {
return this.find(opts);
}
}
Object.assign(Collection.prototype, EventEmitter.prototype);
return {
Collection,
funcs: {
where: whereFunc,
find: findFunc
}
};
};