ueberdb2 4.1.8 → 4.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/databases/cassandra_db.js +233 -235
- package/dist/databases/couch_db.js +171 -173
- package/dist/databases/dirty_db.js +73 -76
- package/dist/databases/dirty_git_db.js +57 -75
- package/dist/databases/elasticsearch_db.js +241 -267
- package/dist/databases/memory_db.js +35 -37
- package/dist/databases/mock_db.js +36 -38
- package/dist/databases/mongodb_db.js +131 -133
- package/dist/databases/mssql_db.js +183 -185
- package/dist/databases/mysql_db.js +166 -168
- package/dist/databases/postgres_db.js +188 -190
- package/dist/databases/postgrespool_db.js +10 -10
- package/dist/databases/redis_db.js +118 -120
- package/dist/databases/rethink_db.js +119 -121
- package/dist/databases/sqlite_db.js +135 -137
- package/dist/index.js +195 -213
- package/lib/AbstractDatabase.ts +79 -0
- package/lib/CacheAndBufferLayer.ts +665 -0
- package/lib/logging.ts +32 -0
- package/package.json +16 -12
- package/dist/lib/AbstractDatabase.js +0 -38
- package/dist/lib/CacheAndBufferLayer.js +0 -657
- package/dist/lib/logging.js +0 -34
- package/dist/test/lib/databases.js +0 -74
- package/dist/test/test.js +0 -327
- package/dist/test/test_bulk.js +0 -74
- package/dist/test/test_elasticsearch.js +0 -157
- package/dist/test/test_findKeys.js +0 -69
- package/dist/test/test_flush.js +0 -83
- package/dist/test/test_getSub.js +0 -57
- package/dist/test/test_lru.js +0 -155
- package/dist/test/test_memory.js +0 -59
- package/dist/test/test_metrics.js +0 -772
- package/dist/test/test_mysql.js +0 -90
- package/dist/test/test_postgres.js +0 -40
- package/dist/test/test_setSub.js +0 -48
- package/dist/test/test_tojson.js +0 -62
|
@@ -1,192 +1,190 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
this.settings =
|
|
33
|
-
|
|
34
|
-
this.settings.
|
|
35
|
-
this.settings.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
this.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
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
|
-
if (bulk[i].type === '
|
|
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
|
-
close(callback) {
|
|
189
|
-
this.db.end(callback);
|
|
190
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var AbstractDatabase = require('../lib/AbstractDatabase.js');
|
|
4
|
+
var async = require('async');
|
|
5
|
+
var pg = require('pg');
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 2011 Peter 'Pita' Martischka
|
|
9
|
+
*
|
|
10
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
11
|
+
* you may not use this file except in compliance with the License.
|
|
12
|
+
* You may obtain a copy of the License at
|
|
13
|
+
*
|
|
14
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
17
|
+
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
18
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
19
|
+
* See the License for the specific language governing permissions and
|
|
20
|
+
* limitations under the License.
|
|
21
|
+
*/
|
|
22
|
+
const Database = class extends AbstractDatabase {
|
|
23
|
+
db;
|
|
24
|
+
upsertStatement;
|
|
25
|
+
constructor(settings) {
|
|
26
|
+
super();
|
|
27
|
+
if (typeof settings === 'string')
|
|
28
|
+
settings = { connectionString: settings };
|
|
29
|
+
this.settings = settings;
|
|
30
|
+
this.settings.cache = settings.cache || 1000;
|
|
31
|
+
this.settings.writeInterval = 100;
|
|
32
|
+
this.settings.json = true;
|
|
33
|
+
// Pool specific defaults
|
|
34
|
+
this.settings.max = this.settings.max || 20;
|
|
35
|
+
this.settings.min = this.settings.min || 4;
|
|
36
|
+
this.settings.idleTimeoutMillis = this.settings.idleTimeoutMillis || 1000;
|
|
37
|
+
// @ts-ignore
|
|
38
|
+
this.db = new pg.Pool(this.settings);
|
|
39
|
+
}
|
|
40
|
+
init(callback) {
|
|
41
|
+
const testTableExists = "SELECT 1 as exists FROM pg_tables WHERE tablename = 'store'";
|
|
42
|
+
const createTable = 'CREATE TABLE IF NOT EXISTS store (' +
|
|
43
|
+
'"key" character varying(100) NOT NULL, ' +
|
|
44
|
+
'"value" text NOT NULL, ' +
|
|
45
|
+
'CONSTRAINT store_pkey PRIMARY KEY (key))';
|
|
46
|
+
// this variable will be given a value depending on the result of the
|
|
47
|
+
// feature detection
|
|
48
|
+
this.upsertStatement = null;
|
|
49
|
+
/*
|
|
50
|
+
* - Detects if this Postgres version supports INSERT .. ON CONFLICT
|
|
51
|
+
* UPDATE (PostgreSQL >= 9.5 and CockroachDB)
|
|
52
|
+
* - If upsert is not supported natively, creates in the DB a pl/pgsql
|
|
53
|
+
* function that emulates it
|
|
54
|
+
* - Performs a side effect, setting this.upsertStatement to the sql
|
|
55
|
+
* statement that needs to be used, based on the detection result
|
|
56
|
+
* - calls the callback
|
|
57
|
+
*/
|
|
58
|
+
const detectUpsertMethod = (callback) => {
|
|
59
|
+
const upsertViaFunction = 'SELECT ueberdb_insert_or_update($1,$2)';
|
|
60
|
+
const upsertNatively = 'INSERT INTO store(key, value) VALUES ($1, $2) ' +
|
|
61
|
+
'ON CONFLICT (key) DO UPDATE SET value = excluded.value';
|
|
62
|
+
const createFunc = 'CREATE OR REPLACE FUNCTION ueberdb_insert_or_update(character varying, text) ' +
|
|
63
|
+
'RETURNS void AS $$ ' +
|
|
64
|
+
'BEGIN ' +
|
|
65
|
+
' IF EXISTS( SELECT * FROM store WHERE key = $1 ) THEN ' +
|
|
66
|
+
' UPDATE store SET value = $2 WHERE key = $1; ' +
|
|
67
|
+
' ELSE ' +
|
|
68
|
+
' INSERT INTO store(key,value) VALUES( $1, $2 ); ' +
|
|
69
|
+
' END IF; ' +
|
|
70
|
+
' RETURN; ' +
|
|
71
|
+
'END; ' +
|
|
72
|
+
'$$ LANGUAGE plpgsql;';
|
|
73
|
+
const testNativeUpsert = `EXPLAIN ${upsertNatively}`;
|
|
74
|
+
this.db.query(testNativeUpsert, ['test-key', 'test-value'], (err) => {
|
|
75
|
+
if (err) {
|
|
76
|
+
// the UPSERT statement failed: we will have to emulate it via
|
|
77
|
+
// an sql function
|
|
78
|
+
this.upsertStatement = upsertViaFunction;
|
|
79
|
+
// actually create the emulation function
|
|
80
|
+
this.db.query(createFunc, [], callback);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
// if we get here, the EXPLAIN UPSERT succeeded, and we can use a
|
|
84
|
+
// native UPSERT
|
|
85
|
+
this.upsertStatement = upsertNatively;
|
|
86
|
+
callback();
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
this.db.query(testTableExists, (err, result) => {
|
|
90
|
+
if (err != null)
|
|
91
|
+
return callback(err);
|
|
92
|
+
if (result.rows.length === 0) {
|
|
93
|
+
this.db.query(createTable, (err) => {
|
|
94
|
+
if (err != null)
|
|
95
|
+
return callback(err);
|
|
96
|
+
// @ts-ignore
|
|
97
|
+
detectUpsertMethod(callback);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
detectUpsertMethod(callback);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
get(key, callback) {
|
|
107
|
+
this.db.query('SELECT value FROM store WHERE key=$1', [key], (err, results) => {
|
|
108
|
+
let value = null;
|
|
109
|
+
if (!err && results.rows.length === 1) {
|
|
110
|
+
value = results.rows[0].value;
|
|
111
|
+
}
|
|
112
|
+
callback(err, value);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
findKeys(key, notKey, callback) {
|
|
116
|
+
let query = 'SELECT key FROM store WHERE key LIKE $1';
|
|
117
|
+
const params = [];
|
|
118
|
+
// desired keys are %key:%, e.g. pad:%
|
|
119
|
+
key = key.replace(/\*/g, '%');
|
|
120
|
+
params.push(key);
|
|
121
|
+
if (notKey != null) {
|
|
122
|
+
// not desired keys are notKey:%, e.g. %:%:%
|
|
123
|
+
notKey = notKey.replace(/\*/g, '%');
|
|
124
|
+
query += ' AND key NOT LIKE $2';
|
|
125
|
+
params.push(notKey);
|
|
126
|
+
}
|
|
127
|
+
this.db.query(query, params, (err, results) => {
|
|
128
|
+
const value = [];
|
|
129
|
+
if (!err && results.rows.length > 0) {
|
|
130
|
+
results.rows.forEach((val) => {
|
|
131
|
+
value.push(val.key);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
callback(err, value);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
set(key, value, callback) {
|
|
138
|
+
if (key.length > 100) {
|
|
139
|
+
const val = '';
|
|
140
|
+
callback(Error('Your Key can only be 100 chars'), val);
|
|
141
|
+
}
|
|
142
|
+
else if (this.upsertStatement != null) {
|
|
143
|
+
this.db.query(this.upsertStatement, [key, value], callback);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
remove(key, callback) {
|
|
147
|
+
this.db.query('DELETE FROM store WHERE key=$1', [key], callback);
|
|
148
|
+
}
|
|
149
|
+
doBulk(bulk, callback) {
|
|
150
|
+
const replaceVALs = [];
|
|
151
|
+
let removeSQL = 'DELETE FROM store WHERE key IN (';
|
|
152
|
+
const removeVALs = [];
|
|
153
|
+
let removeCount = 0;
|
|
154
|
+
for (const i in bulk) {
|
|
155
|
+
if (bulk[i].type === 'set') {
|
|
156
|
+
replaceVALs.push([bulk[i].key, bulk[i].value]);
|
|
157
|
+
}
|
|
158
|
+
else if (bulk[i].type === 'remove') {
|
|
159
|
+
if (removeCount !== 0)
|
|
160
|
+
removeSQL += ',';
|
|
161
|
+
removeCount += 1;
|
|
162
|
+
removeSQL += `$${removeCount}`;
|
|
163
|
+
removeVALs.push(bulk[i].key);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
removeSQL += ');';
|
|
167
|
+
if (!this.upsertStatement) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
// @ts-ignore
|
|
171
|
+
const functions = replaceVALs.map((v) => (cb) => this.db.query(this.upsertStatement, v, cb));
|
|
172
|
+
const removeFunction = (callback) => {
|
|
173
|
+
// @ts-ignore
|
|
174
|
+
if (!(removeVALs.length) > 1) {
|
|
175
|
+
this.db.query(removeSQL, removeVALs, callback);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
callback();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
functions.push(removeFunction);
|
|
182
|
+
// @ts-ignore
|
|
183
|
+
async.parallel(functions, callback);
|
|
184
|
+
}
|
|
185
|
+
close(callback) {
|
|
186
|
+
this.db.end(callback);
|
|
187
|
+
}
|
|
191
188
|
};
|
|
189
|
+
|
|
192
190
|
exports.Database = Database;
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const postgres = require('./postgres_db');
|
|
4
|
+
const Database = class PostgresDB extends postgres.Database {
|
|
5
|
+
constructor(settings) {
|
|
6
|
+
console.warn('ueberdb: The postgrespool database driver is deprecated ' +
|
|
7
|
+
'and will be removed in a future version. Use postgres instead.');
|
|
8
|
+
super(settings);
|
|
9
|
+
}
|
|
11
10
|
};
|
|
11
|
+
|
|
12
12
|
exports.Database = Database;
|