-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (84 loc) · 2.63 KB
/
index.js
File metadata and controls
98 lines (84 loc) · 2.63 KB
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
'use strict';
const _pull = require('object-pull');
const _pullv = require('object-pullvalues');
const _format = require('object-format');
const _array = require('array-to');
const _object = require('object-to');
const _pullmap = require('objectarray-pullmap');
const $ = function MapPg(db, tab, typ, key, val) {
this._db = db;
this._tab = tab||'map';
this._key = key||'key';
this._val = val||'value';
this._typ = typ||{'key': 'TEXT', 'value': 'TEXT'};
this._keys = _format(_array(this._key), '"%v"');
this._where = _format(_array(this._key), '"%v"=$%i', ' AND ', 1);
};
module.exports = $;
const _ = $.prototype;
_.setup = function() {
return this._db.query(
`CREATE TABLE IF NOT EXISTS "${this._tab}" (`+
`${_format(this._typ, '"%k" %v')},`+
`PRIMARY KEY (${this._keys})`+
`);`
);
};
Object.defineProperty(_, 'size', {'get': function() {
return this._db.query(
`SELECT COUNT(*) AS n FROM ${this._tab};`
).then((ans) => parseInt(ans.rows[0].n));
}});
_.set = function(k, v) {
if(v===undefined) return this.delete(k);
var k = _object(k, this._key), v = _object(v, this._val);
var par = [], kv = Object.assign({}, k, v);
return this._db.query(
`INSERT INTO "${this._tab}" (${_format(kv, '"%k"', ',', 1, par)}) `+
`VALUES (${_format(kv, '$%i', ',', 1)}) ON CONFLICT (${this._keys}) `+
`DO UPDATE SET ${_format(v, '"%k"=$%i', ',', par.length+1, par)};`, par
).then((ans) => ans.rowCount);
};
_.get = function(k) {
return this._db.query(
`SELECT * FROM "${this._tab}" `+
`WHERE ${this._where};`, _array(_pullv(k, this._key))
).then((ans) => ans.rowCount? _pull(ans.rows[0], this._val) : undefined);
};
_.delete = function(k) {
return this._db.query(
`DELETE FROM "${this._tab}" `+
`WHERE ${this._where};`, _array(_pullv(k, this._key))
).then((ans) => ans.rowCount);
};
_.has = function(k) {
return this._db.query(
`SELECT * FROM "${this._tab}" `+
`WHERE ${this._where};`, _array(_pullv(k, this._key))
).then((ans) => ans.rowCount===1);
};
_.clear = function() {
return this._db.query(
`DELETE FROM "${this._tab}";`
).then((ans) => ans.rowCount);
};
_.valueOf = function() {
return this._db.query(
`SELECT * FROM "${this._tab}";`
).then((ans) => _pullmap(ans.rows||[], this._key, this._val));
};
_.forEach = function(fn, thisArg) {
return this.valueOf().then((ans) => {
ans.forEach(fn, thisArg);
return ans.size;
});
};
_.entries = function() {
return this.valueOf().then((ans) => ans.entries());
};
_.keys = function() {
return this.valueOf().then((ans) => ans.keys());
};
_.values = function() {
return this.valueOf().then((ans) => ans.values());
};