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,237 +1,235 @@
1
- "use strict";
2
- /**
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS-IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
- var __importDefault = (this && this.__importDefault) || function (mod) {
16
- return (mod && mod.__esModule) ? mod : { "default": mod };
17
- };
18
- Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.Database = void 0;
20
- const AbstractDatabase_1 = __importDefault(require("../lib/AbstractDatabase"));
21
- const cassandra_driver_1 = __importDefault(require("cassandra-driver"));
22
- const Database = class Cassandra_db extends AbstractDatabase_1.default {
23
- client;
24
- pool;
25
- /**
26
- * @param {Object} settings The required settings object to initiate the Cassandra database
27
- * @param {String[]} settings.clientOptions See
28
- * http://www.datastax.com/drivers/nodejs/2.0/global.html#ClientOptions for a full set of
29
- * options that can be used
30
- * @param {String} settings.columnFamily The column family that should be used to store data. The
31
- * column family will be created if it doesn't exist
32
- * @param {Function} [settings.logger] Function that will be used to pass on log events emitted by
33
- * the Cassandra driver. See https://github.com/datastax/nodejs-driver#logging for more
34
- * information
35
- */
36
- constructor(settings) {
37
- super();
38
- if (!settings.clientOptions) {
39
- throw new Error('The Cassandra client options should be defined');
40
- }
41
- if (!settings.columnFamily) {
42
- throw new Error('The Cassandra column family should be defined');
43
- }
44
- this.settings = { database: settings.database };
45
- this.settings.clientOptions = settings.clientOptions;
46
- this.settings.columnFamily = settings.columnFamily;
47
- this.settings.logger = settings.logger;
48
- }
49
- /**
50
- * Initializes the Cassandra client, connects to Cassandra and creates the CF if it didn't exist
51
- * already
52
- *
53
- * @param {Function} callback Standard callback method.
54
- * @param {Error} callback.err An error object (if any.)
55
- */
56
- init(callback) {
57
- // Create a client
58
- this.client = new cassandra_driver_1.default.Client(this.settings.clientOptions);
59
- // Pass on log messages if a logger has been configured
60
- if (this.settings.logger) {
61
- this.client.on('log', this.settings.logger);
62
- }
63
- // Check whether our column family already exists and create it if necessary
64
- this.client.execute('SELECT columnfamily_name FROM system.schema_columnfamilies WHERE keyspace_name = ?', [this.settings.clientOptions.keyspace], (err, result) => {
65
- if (err) {
66
- return callback(err);
67
- }
68
- let isDefined = false;
69
- const length = result.rows.length;
70
- for (let i = 0; i < length; i++) {
71
- if (result.rows[i].columnfamily_name === this.settings.columnFamily) {
72
- isDefined = true;
73
- break;
74
- }
75
- }
76
- if (isDefined) {
77
- return callback(null);
78
- }
79
- else {
80
- const cql = `CREATE COLUMNFAMILY "${this.settings.columnFamily}" ` +
81
- '(key text PRIMARY KEY, data text)';
82
- this.client && this.client.execute(cql, callback);
83
- }
84
- });
85
- }
86
- /**
87
- * Gets a value from Cassandra
88
- *
89
- * @param {String} key The key for which the value should be retrieved
90
- * @param {Function} callback Standard callback method
91
- * @param {Error} callback.err An error object, if any
92
- * @param {String} callback.value The value for the given key (if any)
93
- */
94
- get(key, callback) {
95
- const cql = `SELECT data FROM "${this.settings.columnFamily}" WHERE key = ?`;
96
- this.client && this.client.execute(cql, [key], (err, result) => {
97
- if (err) {
98
- return callback(err);
99
- }
100
- if (!result.rows || result.rows.length === 0) {
101
- return callback(null, null);
102
- }
103
- return callback(null, result.rows[0].data);
104
- });
105
- }
106
- /**
107
- * Cassandra has no native `findKeys` method. This function implements a naive filter by
108
- * retrieving *all* the keys and filtering those. This should obviously be used with the utmost
109
- * care and is probably not something you want to run in production.
110
- *
111
- * @param {String} key The filter for keys that should match
112
- * @param {String} [notKey] The filter for keys that shouldn't match
113
- * @param {Function} callback Standard callback method
114
- * @param {Error} callback.err An error object, if any
115
- * @param {String[]} callback.keys An array of keys that match the specified filters
116
- */
117
- findKeys(key, notKey, callback) {
118
- let cql = null;
119
- if (!notKey) {
120
- // Get all the keys
121
- cql = `SELECT key FROM "${this.settings.columnFamily}"`;
122
- this.client && this.client.execute(cql, (err, result) => {
123
- if (err) {
124
- return callback(err);
125
- }
126
- // Construct a regular expression based on the given key
127
- const regex = new RegExp(`^${key.replace(/\*/g, '.*')}$`);
128
- const keys = [];
129
- result.rows.forEach((row) => {
130
- if (regex.test(row.key)) {
131
- keys.push(row.key);
132
- }
133
- });
134
- return callback(null, keys);
135
- });
136
- }
137
- else if (notKey === '*:*:*') {
138
- // restrict key to format 'text:*'
139
- const matches = /^([^:]+):\*$/.exec(key);
140
- if (matches) {
141
- // Get the 'text' bit out of the key and get all those keys from a special column.
142
- // We can retrieve them from this column as we're duplicating them on .set/.remove
143
- cql = `SELECT * from "${this.settings.columnFamily}" WHERE key = ?`;
144
- this.client &&
145
- this.client
146
- .execute(cql, [`ueberdb:keys:${matches[1]}`], (err, result) => {
147
- if (err) {
148
- return callback(err);
149
- }
150
- if (!result.rows || result.rows.length === 0) {
151
- return callback(null, []);
152
- }
153
- const keys = result.rows.map((row) => row.data);
154
- return callback(null, keys);
155
- });
156
- }
157
- else {
158
- const msg = 'Cassandra db only supports key patterns like pad:* when notKey is set to *:*:*';
159
- return callback(new Error(msg), null);
160
- }
161
- }
162
- else {
163
- return callback(new Error('Cassandra db currently only supports *:*:* as notKey'), null);
164
- }
165
- }
166
- /**
167
- * Sets a value for a key
168
- *
169
- * @param {String} key The key to set
170
- * @param {String} value The value associated to this key
171
- * @param {Function} callback Standard callback method
172
- * @param {Error} callback.err An error object, if any
173
- */
174
- set(key, value, callback) {
175
- this.doBulk([{ type: 'set', key, value }], callback);
176
- }
177
- /**
178
- * Removes a key and it's value from the column family
179
- *
180
- * @param {String} key The key to remove
181
- * @param {Function} callback Standard callback method
182
- * @param {Error} callback.err An error object, if any
183
- */
184
- remove(key, callback) {
185
- this.doBulk([{ type: 'remove', key }], callback);
186
- }
187
- /**
188
- * Performs multiple operations in one action
189
- *
190
- * @param {Object[]} bulk The set of operations that should be performed
191
- * @param {Function} callback Standard callback method
192
- * @param {Error} callback.err An error object, if any
193
- */
194
- doBulk(bulk, callback) {
195
- const queries = [];
196
- bulk.forEach((operation) => {
197
- // We support finding keys of the form `test:*`. If anything matches, we will try and save
198
- // this
199
- const matches = /^([^:]+):([^:]+)$/.exec(operation.key);
200
- if (operation.type === 'set') {
201
- queries.push({
202
- query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
203
- params: [operation.value, operation.key],
204
- });
205
- if (matches) {
206
- queries.push({
207
- query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
208
- params: ['1', `ueberdb:keys:${matches[1]}`],
209
- });
210
- }
211
- }
212
- else if (operation.type === 'remove') {
213
- queries.push({
214
- query: `DELETE FROM "${this.settings.columnFamily}" WHERE key=?`,
215
- params: [operation.key],
216
- });
217
- if (matches) {
218
- queries.push({
219
- query: `DELETE FROM "${this.settings.columnFamily}" WHERE key = ?`,
220
- params: [`ueberdb:keys:${matches[1]}`],
221
- });
222
- }
223
- }
224
- });
225
- this.client && this.client.batch(queries, { prepare: true }, callback);
226
- }
227
- /**
228
- * Closes the Cassandra connection
229
- *
230
- * @param {Function} callback Standard callback method
231
- * @param {Error} callback.err Error object in case something goes wrong
232
- */
233
- close(callback) {
234
- this.pool.shutdown(callback);
235
- }
1
+ 'use strict';
2
+
3
+ var AbstractDatabase = require('../lib/AbstractDatabase.js');
4
+ var cassandraDriver = require('cassandra-driver');
5
+
6
+ /**
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS-IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+ const Database = class Cassandra_db extends AbstractDatabase {
20
+ client;
21
+ pool;
22
+ /**
23
+ * @param {Object} settings The required settings object to initiate the Cassandra database
24
+ * @param {String[]} settings.clientOptions See
25
+ * http://www.datastax.com/drivers/nodejs/2.0/global.html#ClientOptions for a full set of
26
+ * options that can be used
27
+ * @param {String} settings.columnFamily The column family that should be used to store data. The
28
+ * column family will be created if it doesn't exist
29
+ * @param {Function} [settings.logger] Function that will be used to pass on log events emitted by
30
+ * the Cassandra driver. See https://github.com/datastax/nodejs-driver#logging for more
31
+ * information
32
+ */
33
+ constructor(settings) {
34
+ super();
35
+ if (!settings.clientOptions) {
36
+ throw new Error('The Cassandra client options should be defined');
37
+ }
38
+ if (!settings.columnFamily) {
39
+ throw new Error('The Cassandra column family should be defined');
40
+ }
41
+ this.settings = { database: settings.database };
42
+ this.settings.clientOptions = settings.clientOptions;
43
+ this.settings.columnFamily = settings.columnFamily;
44
+ this.settings.logger = settings.logger;
45
+ }
46
+ /**
47
+ * Initializes the Cassandra client, connects to Cassandra and creates the CF if it didn't exist
48
+ * already
49
+ *
50
+ * @param {Function} callback Standard callback method.
51
+ * @param {Error} callback.err An error object (if any.)
52
+ */
53
+ init(callback) {
54
+ // Create a client
55
+ this.client = new cassandraDriver.Client(this.settings.clientOptions);
56
+ // Pass on log messages if a logger has been configured
57
+ if (this.settings.logger) {
58
+ this.client.on('log', this.settings.logger);
59
+ }
60
+ // Check whether our column family already exists and create it if necessary
61
+ this.client.execute('SELECT columnfamily_name FROM system.schema_columnfamilies WHERE keyspace_name = ?', [this.settings.clientOptions.keyspace], (err, result) => {
62
+ if (err) {
63
+ return callback(err);
64
+ }
65
+ let isDefined = false;
66
+ const length = result.rows.length;
67
+ for (let i = 0; i < length; i++) {
68
+ if (result.rows[i].columnfamily_name === this.settings.columnFamily) {
69
+ isDefined = true;
70
+ break;
71
+ }
72
+ }
73
+ if (isDefined) {
74
+ return callback(null);
75
+ }
76
+ else {
77
+ const cql = `CREATE COLUMNFAMILY "${this.settings.columnFamily}" ` +
78
+ '(key text PRIMARY KEY, data text)';
79
+ this.client && this.client.execute(cql, callback);
80
+ }
81
+ });
82
+ }
83
+ /**
84
+ * Gets a value from Cassandra
85
+ *
86
+ * @param {String} key The key for which the value should be retrieved
87
+ * @param {Function} callback Standard callback method
88
+ * @param {Error} callback.err An error object, if any
89
+ * @param {String} callback.value The value for the given key (if any)
90
+ */
91
+ get(key, callback) {
92
+ const cql = `SELECT data FROM "${this.settings.columnFamily}" WHERE key = ?`;
93
+ this.client && this.client.execute(cql, [key], (err, result) => {
94
+ if (err) {
95
+ return callback(err);
96
+ }
97
+ if (!result.rows || result.rows.length === 0) {
98
+ return callback(null, null);
99
+ }
100
+ return callback(null, result.rows[0].data);
101
+ });
102
+ }
103
+ /**
104
+ * Cassandra has no native `findKeys` method. This function implements a naive filter by
105
+ * retrieving *all* the keys and filtering those. This should obviously be used with the utmost
106
+ * care and is probably not something you want to run in production.
107
+ *
108
+ * @param {String} key The filter for keys that should match
109
+ * @param {String} [notKey] The filter for keys that shouldn't match
110
+ * @param {Function} callback Standard callback method
111
+ * @param {Error} callback.err An error object, if any
112
+ * @param {String[]} callback.keys An array of keys that match the specified filters
113
+ */
114
+ findKeys(key, notKey, callback) {
115
+ let cql = null;
116
+ if (!notKey) {
117
+ // Get all the keys
118
+ cql = `SELECT key FROM "${this.settings.columnFamily}"`;
119
+ this.client && this.client.execute(cql, (err, result) => {
120
+ if (err) {
121
+ return callback(err);
122
+ }
123
+ // Construct a regular expression based on the given key
124
+ const regex = new RegExp(`^${key.replace(/\*/g, '.*')}$`);
125
+ const keys = [];
126
+ result.rows.forEach((row) => {
127
+ if (regex.test(row.key)) {
128
+ keys.push(row.key);
129
+ }
130
+ });
131
+ return callback(null, keys);
132
+ });
133
+ }
134
+ else if (notKey === '*:*:*') {
135
+ // restrict key to format 'text:*'
136
+ const matches = /^([^:]+):\*$/.exec(key);
137
+ if (matches) {
138
+ // Get the 'text' bit out of the key and get all those keys from a special column.
139
+ // We can retrieve them from this column as we're duplicating them on .set/.remove
140
+ cql = `SELECT * from "${this.settings.columnFamily}" WHERE key = ?`;
141
+ this.client &&
142
+ this.client
143
+ .execute(cql, [`ueberdb:keys:${matches[1]}`], (err, result) => {
144
+ if (err) {
145
+ return callback(err);
146
+ }
147
+ if (!result.rows || result.rows.length === 0) {
148
+ return callback(null, []);
149
+ }
150
+ const keys = result.rows.map((row) => row.data);
151
+ return callback(null, keys);
152
+ });
153
+ }
154
+ else {
155
+ const msg = 'Cassandra db only supports key patterns like pad:* when notKey is set to *:*:*';
156
+ return callback(new Error(msg), null);
157
+ }
158
+ }
159
+ else {
160
+ return callback(new Error('Cassandra db currently only supports *:*:* as notKey'), null);
161
+ }
162
+ }
163
+ /**
164
+ * Sets a value for a key
165
+ *
166
+ * @param {String} key The key to set
167
+ * @param {String} value The value associated to this key
168
+ * @param {Function} callback Standard callback method
169
+ * @param {Error} callback.err An error object, if any
170
+ */
171
+ set(key, value, callback) {
172
+ this.doBulk([{ type: 'set', key, value }], callback);
173
+ }
174
+ /**
175
+ * Removes a key and it's value from the column family
176
+ *
177
+ * @param {String} key The key to remove
178
+ * @param {Function} callback Standard callback method
179
+ * @param {Error} callback.err An error object, if any
180
+ */
181
+ remove(key, callback) {
182
+ this.doBulk([{ type: 'remove', key }], callback);
183
+ }
184
+ /**
185
+ * Performs multiple operations in one action
186
+ *
187
+ * @param {Object[]} bulk The set of operations that should be performed
188
+ * @param {Function} callback Standard callback method
189
+ * @param {Error} callback.err An error object, if any
190
+ */
191
+ doBulk(bulk, callback) {
192
+ const queries = [];
193
+ bulk.forEach((operation) => {
194
+ // We support finding keys of the form `test:*`. If anything matches, we will try and save
195
+ // this
196
+ const matches = /^([^:]+):([^:]+)$/.exec(operation.key);
197
+ if (operation.type === 'set') {
198
+ queries.push({
199
+ query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
200
+ params: [operation.value, operation.key],
201
+ });
202
+ if (matches) {
203
+ queries.push({
204
+ query: `UPDATE "${this.settings.columnFamily}" SET data = ? WHERE key = ?`,
205
+ params: ['1', `ueberdb:keys:${matches[1]}`],
206
+ });
207
+ }
208
+ }
209
+ else if (operation.type === 'remove') {
210
+ queries.push({
211
+ query: `DELETE FROM "${this.settings.columnFamily}" WHERE key=?`,
212
+ params: [operation.key],
213
+ });
214
+ if (matches) {
215
+ queries.push({
216
+ query: `DELETE FROM "${this.settings.columnFamily}" WHERE key = ?`,
217
+ params: [`ueberdb:keys:${matches[1]}`],
218
+ });
219
+ }
220
+ }
221
+ });
222
+ this.client && this.client.batch(queries, { prepare: true }, callback);
223
+ }
224
+ /**
225
+ * Closes the Cassandra connection
226
+ *
227
+ * @param {Function} callback Standard callback method
228
+ * @param {Error} callback.err Error object in case something goes wrong
229
+ */
230
+ close(callback) {
231
+ this.pool.shutdown(callback);
232
+ }
236
233
  };
234
+
237
235
  exports.Database = Database;