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