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,269 +1,243 @@
1
- "use strict";
2
- /**
3
- * 2015 Visionist, Inc.
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- var desc = Object.getOwnPropertyDescriptor(m, k);
20
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
- desc = { enumerable: true, get: function() { return m[k]; } };
22
- }
23
- Object.defineProperty(o, k2, desc);
24
- }) : (function(o, m, k, k2) {
25
- if (k2 === undefined) k2 = k;
26
- o[k2] = m[k];
27
- }));
28
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
- Object.defineProperty(o, "default", { enumerable: true, value: v });
30
- }) : function(o, v) {
31
- o["default"] = v;
32
- });
33
- var __importStar = (this && this.__importStar) || function (mod) {
34
- if (mod && mod.__esModule) return mod;
35
- var result = {};
36
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37
- __setModuleDefault(result, mod);
38
- return result;
39
- };
40
- var __importDefault = (this && this.__importDefault) || function (mod) {
41
- return (mod && mod.__esModule) ? mod : { "default": mod };
42
- };
43
- Object.defineProperty(exports, "__esModule", { value: true });
44
- exports.Database = void 0;
45
- const AbstractDatabase_1 = __importDefault(require("../lib/AbstractDatabase"));
46
- const assert_1 = __importStar(require("assert"));
47
- const buffer_1 = require("buffer");
48
- const crypto_1 = require("crypto");
49
- const elasticsearch8_1 = require("elasticsearch8");
50
- const schema = '2';
51
- const keyToId = (key) => {
52
- const keyBuf = buffer_1.Buffer.from(key);
53
- return keyBuf.length > 512 ? (0, crypto_1.createHash)('sha512').update(keyBuf).digest('hex') : key;
54
- };
55
- const mappings = {
56
- // _id is expected to equal key, unless the UTF-8 encoded key is > 512 bytes, in which case it is
57
- // the hex-encoded sha512 hash of the UTF-8 encoded key.
58
- properties: {
59
- key: { type: 'wildcard' },
60
- value: { type: 'object', enabled: false }, // Values should be opaque to Elasticsearch.
61
- },
62
- };
63
- const migrateToSchema2 = async (client, v1BaseIndex, v2Index, logger) => {
64
- let recordsMigratedLastLogged = 0;
65
- let recordsMigrated = 0;
66
- const totals = new Map();
67
- logger.info('Attempting elasticsearch record migration from schema v1 at base index ' +
68
- `${v1BaseIndex} to schema v2 at index ${v2Index}...`);
69
- const indices = await client.indices.get({ index: [v1BaseIndex, `${v1BaseIndex}-*-*`] });
70
- const scrollIds = new Map();
71
- const q = [];
72
- try {
73
- for (const index of Object.keys(indices)) {
74
- const res = await client.search({ index, scroll: '10m' });
75
- scrollIds.set(index, res._scroll_id);
76
- q.push({ index, res });
77
- }
78
- while (q.length) {
79
- const { index, res: { hits: { hits, total: { value: total } } } } = q.shift();
80
- if (hits.length === 0)
81
- continue;
82
- totals.set(index, total);
83
- const body = [];
84
- for (const { _id, _type, _source: { val } } of hits) {
85
- let key = `${_type}:${_id}`;
86
- if (v1BaseIndex && index !== v1BaseIndex) {
87
- const parts = index.slice(v1BaseIndex.length + 1).split('-');
88
- if (parts.length !== 2) {
89
- throw new Error(`unable to migrate records from index ${index} due to data ambiguity`);
90
- }
91
- key = `${parts[0]}:${decodeURIComponent(_type)}:${parts[1]}:${_id}`;
92
- }
93
- body.push({ index: { _id: keyToId(key) } }, { key, value: JSON.parse(val) });
94
- }
95
- await client.bulk({ index: v2Index, body });
96
- recordsMigrated += hits.length;
97
- if (Math.floor(recordsMigrated / 100) > Math.floor(recordsMigratedLastLogged / 100)) {
98
- const total = [...totals.values()].reduce((a, b) => a + b, 0);
99
- logger.info(`Migrated ${recordsMigrated} records out of ${total}`);
100
- recordsMigratedLastLogged = recordsMigrated;
101
- }
102
- q.push({ index, res: (await client.scroll({ scroll: '5m', scroll_id: scrollIds.get(index) })) });
103
- }
104
- logger.info(`Finished migrating ${recordsMigrated} records`);
105
- }
106
- finally {
107
- await Promise.all([...scrollIds.values()].map((scrollId) => client.clearScroll({ scroll_id: scrollId })));
108
- }
109
- };
110
- const Database = class extends AbstractDatabase_1.default {
111
- _client;
112
- _index;
113
- _indexClean;
114
- _q;
115
- constructor(settings) {
116
- super();
117
- this._client = null;
118
- this.settings = {
119
- host: '127.0.0.1',
120
- port: '9200',
121
- base_index: 'ueberes',
122
- migrate_to_newer_schema: false,
123
- // for a list of valid API values see:
124
- // https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html#config-options
125
- api: '7.6',
126
- ...settings || {},
127
- json: false, // Elasticsearch will do the JSON conversion as necessary.
128
- };
129
- this._index = `${this.settings.base_index}_s${schema}`;
130
- this._q = { index: this._index };
131
- this._indexClean = true;
132
- }
133
- get isAsync() { return true; }
134
- async _refreshIndex() {
135
- if (this._indexClean)
136
- return;
137
- this._indexClean = true;
138
- await this._client.indices.refresh(this._q);
139
- }
140
- /**
141
- * Initialize the elasticsearch client, then ping the server to ensure that a
142
- * connection was made.
143
- */
144
- async init() {
145
- // create elasticsearch client
146
- const client = new elasticsearch8_1.Client({
147
- node: `http://${this.settings.host}:${this.settings.port}`,
148
- });
149
- await client.ping();
150
- if (!(await client.indices.exists({ index: this._index }))) {
151
- let tmpIndex;
152
- const exists = await client.indices.exists({ index: this.settings.base_index });
153
- if (exists && !this.settings.migrate_to_newer_schema) {
154
- throw new Error(`Data exists under the legacy index (schema) named ${this.settings.base_index}. ` +
155
- 'Set migrate_to_newer_schema to true to copy the existing data to a new index ' +
156
- `named ${this._index}.`);
157
- }
158
- let attempt = 0;
159
- while (true) {
160
- tmpIndex = `${this._index}_${exists ? 'migrate_attempt_' : 'i'}${attempt++}`;
161
- if (!(await client.indices.exists({ index: tmpIndex })))
162
- break;
163
- }
164
- await client.indices.create({ index: tmpIndex, mappings: mappings });
165
- if (exists)
166
- await migrateToSchema2(client, this.settings.base_index, tmpIndex, this.logger);
167
- await client.indices.putAlias({ index: tmpIndex, name: this._index });
168
- }
169
- const indices = Object.values((await client.indices.get({ index: this._index })));
170
- (0, assert_1.equal)(indices.length, 1);
171
- try {
172
- assert_1.default.deepEqual(indices[0].mappings, mappings);
173
- }
174
- catch (err) {
175
- this.logger.warn(`Index ${this._index} mappings does not match expected; ` +
176
- `attempting to use index anyway. Details: ${err}`);
177
- }
178
- this._client = client;
179
- }
180
- /**
181
- * This function provides read functionality to the database.
182
- *
183
- * @param {String} key Key
184
- */
185
- async get(key) {
186
- const res = await this._client.get({ ...this._q, id: keyToId(key) }, { ignore: [404] });
187
- if (!res.found)
188
- return null;
189
- return res._source.value;
190
- }
191
- /**
192
- * @param key Search key, which uses an asterisk (*) as the wild card.
193
- * @param notKey Used to filter the result set
194
- */
195
- async findKeys(key, notKey) {
196
- await this._refreshIndex();
197
- const q = {
198
- ...this._q,
199
- body: {
200
- query: {
201
- bool: {
202
- filter: { wildcard: { key: { value: key } } },
203
- ...notKey == null ? {} : {
204
- must_not: { wildcard: { key: { value: notKey } } },
205
- },
206
- },
207
- },
208
- },
209
- };
210
- const { hits: hits } = await this._client.search(q);
211
- return hits.hits.map((h) => h._source.key);
212
- }
213
- /**
214
- * This function provides write functionality to the database.
215
- *
216
- * @param {String} key Record identifier.
217
- * @param {JSON|String} value The value to store in the database.
218
- */
219
- async set(key, value) {
220
- this._indexClean = false;
221
- await this._client.index({ ...this._q, id: keyToId(key), body: { key, value } });
222
- }
223
- /**
224
- * This function provides delete functionality to the database.
225
- *
226
- * The index, type, and ID will be parsed from the key, and this document will
227
- * be deleted from the database.
228
- *
229
- * @param {String} key Record identifier.
230
- */
231
- async remove(key) {
232
- this._indexClean = false;
233
- await this._client.delete({ ...this._q, id: keyToId(key) }, { ignore: [404] });
234
- }
235
- /**
236
- * This uses the bulk upload functionality of elasticsearch (url:port/_bulk).
237
- *
238
- * The CacheAndBufferLayer will periodically (every this.settings.writeInterval)
239
- * flush writes that have already been done in the local cache out to the database.
240
- *
241
- * @param {Array} bulk An array of JSON data in the format:
242
- * {"type":type, "key":key, "value":value}
243
- */
244
- async doBulk(bulk) {
245
- // bulk is an array of JSON:
246
- // example: [{"type":"set", "key":"sessionstorage:{id}", "value":{"cookie":{...}}]
247
- const operations = [];
248
- for (const { type, key, value } of bulk) {
249
- this._indexClean = false;
250
- switch (type) {
251
- case 'set':
252
- operations.push({ index: { _id: keyToId(key) } });
253
- operations.push({ key, value });
254
- break;
255
- case 'remove':
256
- operations.push({ delete: { _id: keyToId(key) } });
257
- break;
258
- default:
259
- }
260
- }
261
- await this._client.bulk({ ...this._q, body: operations });
262
- }
263
- async close() {
264
- if (this._client != null)
265
- this._client.close();
266
- this._client = null;
267
- }
1
+ 'use strict';
2
+
3
+ var AbstractDatabase = require('../lib/AbstractDatabase.js');
4
+ var assert$0 = require('assert');
5
+ var buffer = require('buffer');
6
+ var crypto = require('crypto');
7
+ var es = require('elasticsearch8');
8
+
9
+ /**
10
+ * 2015 Visionist, Inc.
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS-IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+ const schema = '2';
25
+ const keyToId = (key) => {
26
+ const keyBuf = buffer.Buffer.from(key);
27
+ return keyBuf.length > 512 ? crypto.createHash('sha512').update(keyBuf).digest('hex') : key;
28
+ };
29
+ const mappings = {
30
+ // _id is expected to equal key, unless the UTF-8 encoded key is > 512 bytes, in which case it is
31
+ // the hex-encoded sha512 hash of the UTF-8 encoded key.
32
+ properties: {
33
+ key: { type: 'wildcard' },
34
+ value: { type: 'object', enabled: false }, // Values should be opaque to Elasticsearch.
35
+ },
36
+ };
37
+ const migrateToSchema2 = async (client, v1BaseIndex, v2Index, logger) => {
38
+ let recordsMigratedLastLogged = 0;
39
+ let recordsMigrated = 0;
40
+ const totals = new Map();
41
+ logger.info('Attempting elasticsearch record migration from schema v1 at base index ' +
42
+ `${v1BaseIndex} to schema v2 at index ${v2Index}...`);
43
+ const indices = await client.indices.get({ index: [v1BaseIndex, `${v1BaseIndex}-*-*`] });
44
+ const scrollIds = new Map();
45
+ const q = [];
46
+ try {
47
+ for (const index of Object.keys(indices)) {
48
+ const res = await client.search({ index, scroll: '10m' });
49
+ scrollIds.set(index, res._scroll_id);
50
+ q.push({ index, res });
51
+ }
52
+ while (q.length) {
53
+ const { index, res: { hits: { hits, total: { value: total } } } } = q.shift();
54
+ if (hits.length === 0)
55
+ continue;
56
+ totals.set(index, total);
57
+ const body = [];
58
+ for (const { _id, _type, _source: { val } } of hits) {
59
+ let key = `${_type}:${_id}`;
60
+ if (v1BaseIndex && index !== v1BaseIndex) {
61
+ const parts = index.slice(v1BaseIndex.length + 1).split('-');
62
+ if (parts.length !== 2) {
63
+ throw new Error(`unable to migrate records from index ${index} due to data ambiguity`);
64
+ }
65
+ key = `${parts[0]}:${decodeURIComponent(_type)}:${parts[1]}:${_id}`;
66
+ }
67
+ body.push({ index: { _id: keyToId(key) } }, { key, value: JSON.parse(val) });
68
+ }
69
+ await client.bulk({ index: v2Index, body });
70
+ recordsMigrated += hits.length;
71
+ if (Math.floor(recordsMigrated / 100) > Math.floor(recordsMigratedLastLogged / 100)) {
72
+ const total = [...totals.values()].reduce((a, b) => a + b, 0);
73
+ logger.info(`Migrated ${recordsMigrated} records out of ${total}`);
74
+ recordsMigratedLastLogged = recordsMigrated;
75
+ }
76
+ q.push({ index, res: (await client.scroll({ scroll: '5m', scroll_id: scrollIds.get(index) })) });
77
+ }
78
+ logger.info(`Finished migrating ${recordsMigrated} records`);
79
+ }
80
+ finally {
81
+ await Promise.all([...scrollIds.values()].map((scrollId) => client.clearScroll({ scroll_id: scrollId })));
82
+ }
83
+ };
84
+ const Database = class extends AbstractDatabase {
85
+ _client;
86
+ _index;
87
+ _indexClean;
88
+ _q;
89
+ constructor(settings) {
90
+ super();
91
+ this._client = null;
92
+ this.settings = {
93
+ host: '127.0.0.1',
94
+ port: '9200',
95
+ base_index: 'ueberes',
96
+ migrate_to_newer_schema: false,
97
+ // for a list of valid API values see:
98
+ // https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html#config-options
99
+ api: '7.6',
100
+ ...settings || {},
101
+ json: false, // Elasticsearch will do the JSON conversion as necessary.
102
+ };
103
+ this._index = `${this.settings.base_index}_s${schema}`;
104
+ this._q = { index: this._index };
105
+ this._indexClean = true;
106
+ }
107
+ get isAsync() { return true; }
108
+ async _refreshIndex() {
109
+ if (this._indexClean)
110
+ return;
111
+ this._indexClean = true;
112
+ await this._client.indices.refresh(this._q);
113
+ }
114
+ /**
115
+ * Initialize the elasticsearch client, then ping the server to ensure that a
116
+ * connection was made.
117
+ */
118
+ async init() {
119
+ // create elasticsearch client
120
+ const client = new es.Client({
121
+ node: `http://${this.settings.host}:${this.settings.port}`,
122
+ });
123
+ await client.ping();
124
+ if (!(await client.indices.exists({ index: this._index }))) {
125
+ let tmpIndex;
126
+ const exists = await client.indices.exists({ index: this.settings.base_index });
127
+ if (exists && !this.settings.migrate_to_newer_schema) {
128
+ throw new Error(`Data exists under the legacy index (schema) named ${this.settings.base_index}. ` +
129
+ 'Set migrate_to_newer_schema to true to copy the existing data to a new index ' +
130
+ `named ${this._index}.`);
131
+ }
132
+ let attempt = 0;
133
+ while (true) {
134
+ tmpIndex = `${this._index}_${exists ? 'migrate_attempt_' : 'i'}${attempt++}`;
135
+ if (!(await client.indices.exists({ index: tmpIndex })))
136
+ break;
137
+ }
138
+ await client.indices.create({ index: tmpIndex, mappings: mappings });
139
+ if (exists)
140
+ await migrateToSchema2(client, this.settings.base_index, tmpIndex, this.logger);
141
+ await client.indices.putAlias({ index: tmpIndex, name: this._index });
142
+ }
143
+ const indices = Object.values((await client.indices.get({ index: this._index })));
144
+ assert$0.equal(indices.length, 1);
145
+ try {
146
+ assert$0.deepEqual(indices[0].mappings, mappings);
147
+ }
148
+ catch (err) {
149
+ this.logger.warn(`Index ${this._index} mappings does not match expected; ` +
150
+ `attempting to use index anyway. Details: ${err}`);
151
+ }
152
+ this._client = client;
153
+ }
154
+ /**
155
+ * This function provides read functionality to the database.
156
+ *
157
+ * @param {String} key Key
158
+ */
159
+ async get(key) {
160
+ const res = await this._client.get({ ...this._q, id: keyToId(key) }, { ignore: [404] });
161
+ if (!res.found)
162
+ return null;
163
+ return res._source.value;
164
+ }
165
+ /**
166
+ * @param key Search key, which uses an asterisk (*) as the wild card.
167
+ * @param notKey Used to filter the result set
168
+ */
169
+ async findKeys(key, notKey) {
170
+ await this._refreshIndex();
171
+ const q = {
172
+ ...this._q,
173
+ body: {
174
+ query: {
175
+ bool: {
176
+ filter: { wildcard: { key: { value: key } } },
177
+ ...notKey == null ? {} : {
178
+ must_not: { wildcard: { key: { value: notKey } } },
179
+ },
180
+ },
181
+ },
182
+ },
183
+ };
184
+ const { hits: hits } = await this._client.search(q);
185
+ return hits.hits.map((h) => h._source.key);
186
+ }
187
+ /**
188
+ * This function provides write functionality to the database.
189
+ *
190
+ * @param {String} key Record identifier.
191
+ * @param {JSON|String} value The value to store in the database.
192
+ */
193
+ async set(key, value) {
194
+ this._indexClean = false;
195
+ await this._client.index({ ...this._q, id: keyToId(key), body: { key, value } });
196
+ }
197
+ /**
198
+ * This function provides delete functionality to the database.
199
+ *
200
+ * The index, type, and ID will be parsed from the key, and this document will
201
+ * be deleted from the database.
202
+ *
203
+ * @param {String} key Record identifier.
204
+ */
205
+ async remove(key) {
206
+ this._indexClean = false;
207
+ await this._client.delete({ ...this._q, id: keyToId(key) }, { ignore: [404] });
208
+ }
209
+ /**
210
+ * This uses the bulk upload functionality of elasticsearch (url:port/_bulk).
211
+ *
212
+ * The CacheAndBufferLayer will periodically (every this.settings.writeInterval)
213
+ * flush writes that have already been done in the local cache out to the database.
214
+ *
215
+ * @param {Array} bulk An array of JSON data in the format:
216
+ * {"type":type, "key":key, "value":value}
217
+ */
218
+ async doBulk(bulk) {
219
+ // bulk is an array of JSON:
220
+ // example: [{"type":"set", "key":"sessionstorage:{id}", "value":{"cookie":{...}}]
221
+ const operations = [];
222
+ for (const { type, key, value } of bulk) {
223
+ this._indexClean = false;
224
+ switch (type) {
225
+ case 'set':
226
+ operations.push({ index: { _id: keyToId(key) } });
227
+ operations.push({ key, value });
228
+ break;
229
+ case 'remove':
230
+ operations.push({ delete: { _id: keyToId(key) } });
231
+ break;
232
+ }
233
+ }
234
+ await this._client.bulk({ ...this._q, body: operations });
235
+ }
236
+ async close() {
237
+ if (this._client != null)
238
+ this._client.close();
239
+ this._client = null;
240
+ }
268
241
  };
242
+
269
243
  exports.Database = Database;
@@ -1,39 +1,37 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Database = void 0;
7
- const AbstractDatabase_1 = __importDefault(require("../lib/AbstractDatabase"));
8
- const Database = class MemoryDB extends AbstractDatabase_1.default {
9
- _data;
10
- constructor(settings) {
11
- super();
12
- this.settings = settings;
13
- settings.json = false;
14
- settings.cache = 0;
15
- settings.writeInterval = 0;
16
- this._data = null;
17
- }
18
- get isAsync() { return true; }
19
- close() {
20
- this._data = null;
21
- }
22
- findKeys(key, notKey) {
23
- const regex = this.createFindRegex(key, notKey);
24
- return [...this._data.keys()].filter((k) => regex.test(k));
25
- }
26
- get(key) {
27
- return this._data.get(key);
28
- }
29
- init() {
30
- this._data = this.settings.data || new Map();
31
- }
32
- remove(key) {
33
- this._data.delete(key);
34
- }
35
- set(key, value) {
36
- this._data.set(key, value);
37
- }
1
+ 'use strict';
2
+
3
+ var AbstractDatabase = require('../lib/AbstractDatabase.js');
4
+
5
+ const Database = class MemoryDB extends AbstractDatabase {
6
+ _data;
7
+ constructor(settings) {
8
+ super();
9
+ this.settings = settings;
10
+ settings.json = false;
11
+ settings.cache = 0;
12
+ settings.writeInterval = 0;
13
+ this._data = null;
14
+ }
15
+ get isAsync() { return true; }
16
+ close() {
17
+ this._data = null;
18
+ }
19
+ findKeys(key, notKey) {
20
+ const regex = this.createFindRegex(key, notKey);
21
+ return [...this._data.keys()].filter((k) => regex.test(k));
22
+ }
23
+ get(key) {
24
+ return this._data.get(key);
25
+ }
26
+ init() {
27
+ this._data = this.settings.data || new Map();
28
+ }
29
+ remove(key) {
30
+ this._data.delete(key);
31
+ }
32
+ set(key, value) {
33
+ this._data.set(key, value);
34
+ }
38
35
  };
36
+
39
37
  exports.Database = Database;