ueberdb2 4.0.11 → 4.0.17

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 (81) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc.cjs +44 -5
  3. package/.github/workflows/npmpublish.yml +3 -3
  4. package/databases/{cassandra_db.js → cassandra_db.ts} +45 -30
  5. package/databases/{couch_db.js → couch_db.ts} +78 -31
  6. package/databases/{dirty_db.js → dirty_db.ts} +19 -14
  7. package/databases/{dirty_git_db.js → dirty_git_db.ts} +19 -15
  8. package/databases/{elasticsearch_db.js → elasticsearch_db.ts} +30 -21
  9. package/databases/{memory_db.js → memory_db.ts} +8 -8
  10. package/databases/mock_db.ts +43 -0
  11. package/databases/{mongodb_db.js → mongodb_db.ts} +22 -16
  12. package/databases/{mssql_db.js → mssql_db.ts} +29 -21
  13. package/databases/{mysql_db.js → mysql_db.ts} +20 -15
  14. package/databases/{postgres_db.js → postgres_db.ts} +37 -22
  15. package/databases/{postgrespool_db.js → postgrespool_db.ts} +3 -3
  16. package/databases/redis_db.ts +129 -0
  17. package/databases/{rethink_db.js → rethink_db.ts} +35 -19
  18. package/databases/{sqlite_db.js → sqlite_db.ts} +37 -36
  19. package/dist/databases/cassandra_db.js +237 -0
  20. package/dist/databases/couch_db.js +181 -0
  21. package/dist/databases/dirty_db.js +78 -0
  22. package/dist/databases/dirty_git_db.js +77 -0
  23. package/dist/databases/elasticsearch_db.js +251 -0
  24. package/dist/databases/memory_db.js +39 -0
  25. package/dist/databases/mock_db.js +40 -0
  26. package/dist/databases/mongodb_db.js +127 -0
  27. package/dist/databases/mssql_db.js +187 -0
  28. package/dist/databases/mysql_db.js +170 -0
  29. package/dist/databases/postgres_db.js +192 -0
  30. package/dist/databases/postgrespool_db.js +12 -0
  31. package/dist/databases/redis_db.js +105 -0
  32. package/dist/databases/rethink_db.js +123 -0
  33. package/dist/databases/sqlite_db.js +140 -0
  34. package/dist/index.js +215 -0
  35. package/dist/lib/AbstractDatabase.js +38 -0
  36. package/dist/lib/CacheAndBufferLayer.js +657 -0
  37. package/dist/lib/logging.js +34 -0
  38. package/dist/test/lib/databases.js +72 -0
  39. package/dist/test/test.js +373 -0
  40. package/dist/test/test_bulk.js +74 -0
  41. package/dist/test/test_elasticsearch.js +157 -0
  42. package/dist/test/test_findKeys.js +69 -0
  43. package/dist/test/test_flush.js +83 -0
  44. package/dist/test/test_getSub.js +57 -0
  45. package/dist/test/test_lru.js +155 -0
  46. package/dist/test/test_memory.js +59 -0
  47. package/dist/test/test_metrics.js +772 -0
  48. package/dist/test/test_mysql.js +91 -0
  49. package/dist/test/test_postgres.js +40 -0
  50. package/dist/test/test_setSub.js +48 -0
  51. package/dist/test/test_tojson.js +62 -0
  52. package/docker-compose.yml +44 -0
  53. package/{index.js → index.ts} +76 -25
  54. package/lib/AbstractDatabase.ts +79 -0
  55. package/lib/{CacheAndBufferLayer.js → CacheAndBufferLayer.ts} +17 -16
  56. package/lib/{logging.js → logging.ts} +10 -6
  57. package/package.json +18 -3
  58. package/test/lib/{databases.js → databases.ts} +8 -5
  59. package/test/test.ts +328 -0
  60. package/test/test_bulk.ts +69 -0
  61. package/test/{test_elasticsearch.js → test_elasticsearch.ts} +48 -53
  62. package/test/{test_findKeys.js → test_findKeys.ts} +15 -17
  63. package/test/{test_flush.js → test_flush.ts} +16 -22
  64. package/test/test_getSub.ts +28 -0
  65. package/test/test_lru.ts +151 -0
  66. package/test/test_memory.ts +32 -0
  67. package/test/{test_metrics.js → test_metrics.ts} +73 -68
  68. package/test/{test_mysql.js → test_mysql.ts} +16 -22
  69. package/test/test_postgres.ts +16 -0
  70. package/test/{test_setSub.js → test_setSub.ts} +8 -12
  71. package/test/test_tojson.ts +34 -0
  72. package/databases/mock_db.js +0 -42
  73. package/databases/redis_db.js +0 -96
  74. package/lib/AbstractDatabase.js +0 -37
  75. package/test/test.js +0 -328
  76. package/test/test_bulk.js +0 -69
  77. package/test/test_getSub.js +0 -31
  78. package/test/test_lru.js +0 -145
  79. package/test/test_memory.js +0 -31
  80. package/test/test_postgres.js +0 -16
  81. package/test/test_tojson.js +0 -37
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.databases = void 0;
4
+ const os = require('os');
5
+ exports.databases = {
6
+ memory: {},
7
+ dirty: {
8
+ filename: `${os.tmpdir()}/ueberdb-test.db`,
9
+ speeds: {
10
+ setMax: 1,
11
+ getMax: 0.1,
12
+ findKeysMax: 0.5,
13
+ },
14
+ },
15
+ sqlite: {
16
+ filename: `${os.tmpdir()}/ueberdb-test.sqlite`,
17
+ speeds: {
18
+ setMax: 0.6,
19
+ getMax: 0.5,
20
+ findKeysMax: 2.5,
21
+ removeMax: 0.5,
22
+ },
23
+ },
24
+ mysql: {
25
+ user: 'ueberdb',
26
+ host: '127.0.0.1',
27
+ password: 'ueberdb',
28
+ database: 'ueberdb',
29
+ charset: 'utf8mb4',
30
+ speeds: {
31
+ findKeysMax: 6,
32
+ getMax: 1,
33
+ },
34
+ },
35
+ postgres: {
36
+ user: 'ueberdb',
37
+ host: 'localhost',
38
+ password: 'ueberdb',
39
+ database: 'ueberdb',
40
+ charset: 'utf8mb4',
41
+ speeds: {
42
+ setMax: 6,
43
+ },
44
+ },
45
+ redis: {},
46
+ mongodb: {
47
+ url: 'mongodb://127.0.0.1:27017',
48
+ database: 'mydb_test',
49
+ speeds: {
50
+ setMax: 0.2,
51
+ getMax: 0.05,
52
+ removeMax: 0.3,
53
+ },
54
+ },
55
+ couch: {
56
+ host: '127.0.0.1',
57
+ port: 5984,
58
+ database: 'ueberdb',
59
+ user: 'ueberdb',
60
+ password: 'ueberdb',
61
+ speeds: {
62
+ findKeysMax: 30,
63
+ },
64
+ },
65
+ elasticsearch: {
66
+ base_index: 'ueberdb_test',
67
+ speeds: {
68
+ findKeysMax: 30,
69
+ }, host: '127.0.0.1',
70
+ port: '9200',
71
+ },
72
+ };
@@ -0,0 +1,373 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ // @ts-expect-error TS(7016): Could not find a declaration file for module 'wtfn... Remove this comment to see the full error message
30
+ const wtfnode_1 = __importDefault(require("wtfnode"));
31
+ // @ts-expect-error TS(7016): Could not find a declaration file for module 'cli-... Remove this comment to see the full error message
32
+ const cli_table_1 = __importDefault(require("cli-table"));
33
+ // @ts-ignore
34
+ const randexp_1 = __importDefault(require("randexp"));
35
+ const assert_1 = __importDefault(require("assert"));
36
+ const databases_1 = require("./lib/databases");
37
+ const fs_1 = require("fs");
38
+ const logging_1 = __importDefault(require("../lib/logging"));
39
+ const ueberdb = __importStar(require("../index"));
40
+ 'use strict';
41
+ const assert = assert_1.default.strict;
42
+ const databases = { databases: databases_1.databases }.databases;
43
+ const fs = { promises: fs_1.promises }.promises;
44
+ const maxKeyLength = 100;
45
+ const randomString = (length = maxKeyLength) => new randexp_1.default(new RegExp(`.{${length}}`)).gen();
46
+ // eslint-disable-next-line mocha/no-top-level-hooks
47
+ after(async () => {
48
+ // Add a timeout to forcibly exit if something is keeping node from exiting cleanly.
49
+ // The timeout is unref()ed so that it doesn't prevent node from exiting when done.
50
+ setTimeout(() => {
51
+ console.error('node should have exited by now but something is keeping it open ' +
52
+ 'such as an open connection or active timer');
53
+ wtfnode_1.default.dump();
54
+ process.exit(1); // eslint-disable-line n/no-process-exit
55
+ }, 5000).unref();
56
+ });
57
+ describe(__filename, () => {
58
+ let speedTable;
59
+ let db;
60
+ before(async () => {
61
+ speedTable = new cli_table_1.default({
62
+ head: [
63
+ 'Database',
64
+ 'read cache',
65
+ 'write buffer',
66
+ '#',
67
+ 'ms/set',
68
+ 'ms/get',
69
+ 'ms/findKeys',
70
+ 'ms/remove',
71
+ 'total ms',
72
+ 'total ms/#',
73
+ ],
74
+ colWidths: [15, 15, 15, 8, 13, 13, 13, 13, 13, 13],
75
+ });
76
+ });
77
+ after(async () => {
78
+ console.log(speedTable.toString());
79
+ });
80
+ Object.keys(databases).filter((v) => v === "sqlite")
81
+ .forEach((database) => {
82
+ // @ts-ignore
83
+ const dbSettings = databases[database];
84
+ describe(database, () => {
85
+ for (const readCache of [false, true]) {
86
+ describe(`${readCache ? '' : 'no '}read cache`, () => {
87
+ for (const writeBuffer of [false, true]) {
88
+ describe(`${writeBuffer ? '' : 'no '}write buffer`, function () {
89
+ this.timeout(5000);
90
+ before(async () => {
91
+ if (dbSettings.filename) {
92
+ await fs.unlink(dbSettings.filename).catch(() => { });
93
+ }
94
+ db = new ueberdb.Database(database, dbSettings, {
95
+ ...(readCache ? {} : { cache: 0 }),
96
+ ...(writeBuffer ? {} : { writeInterval: 0 }),
97
+ }, new logging_1.default.ConsoleLogger());
98
+ await db.init();
99
+ });
100
+ after(async () => {
101
+ await db.close();
102
+ if (dbSettings.filename) {
103
+ await fs.unlink(dbSettings.filename).catch(() => { });
104
+ }
105
+ });
106
+ describe('white space in key is not ignored', () => {
107
+ for (const space of [false, true]) {
108
+ describe(`key ${space ? 'has' : 'does not have'} a trailing space`, () => {
109
+ let input;
110
+ let key;
111
+ before(async () => {
112
+ input = { a: 1, b: new randexp_1.default(/.+/).gen() };
113
+ key = randomString(maxKeyLength - 1) + (space ? ' ' : '');
114
+ await db.set(key, input);
115
+ });
116
+ it('get(key) -> record', async () => {
117
+ const output = await db.get(key);
118
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
119
+ assert.equal(JSON.stringify(output), JSON.stringify(input));
120
+ });
121
+ it('get(`${key} `) -> nullish', async () => {
122
+ const output = await db.get(`${key} `);
123
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
124
+ assert(output == null);
125
+ });
126
+ if (space) {
127
+ it('get(key.slice(0, -1)) -> nullish', async () => {
128
+ const output = await db.get(key.slice(0, -1));
129
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
130
+ assert(output == null);
131
+ });
132
+ }
133
+ });
134
+ }
135
+ });
136
+ it('get of unknown key -> nullish', async () => {
137
+ const key = randomString();
138
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
139
+ assert((await db.get(key)) == null);
140
+ });
141
+ it('set+get works', async () => {
142
+ const input = { a: 1, b: new randexp_1.default(/.+/).gen() };
143
+ const key = randomString();
144
+ await db.set(key, input);
145
+ const output = await db.get(key);
146
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
147
+ assert.equal(JSON.stringify(output), JSON.stringify(input));
148
+ });
149
+ it('set+get with random key/value works', async () => {
150
+ const input = { testLongString: new randexp_1.default(/[a-f0-9]{50000}/).gen() };
151
+ const key = randomString();
152
+ await db.set(key, input);
153
+ const output = await db.get(key);
154
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
155
+ assert.equal(JSON.stringify(output), JSON.stringify(input));
156
+ });
157
+ it('findKeys works', async function () {
158
+ if (database === 'mongodb') {
159
+ this.skip();
160
+ } // TODO: Fix mongodb.
161
+ // TODO setting a key with non ascii chars
162
+ const key = new randexp_1.default(/([a-z]\w{0,20})foo\1/).gen();
163
+ await Promise.all([
164
+ db.set(key, true),
165
+ db.set(`${key}a`, true),
166
+ db.set(`nonmatching_${key}`, false),
167
+ ]);
168
+ const keys = await db.findKeys(`${key}*`, null);
169
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
170
+ assert.deepEqual(keys.sort(), [key, `${key}a`]);
171
+ });
172
+ it('findKeys with exclusion works', async function () {
173
+ if (database === 'mongodb') {
174
+ this.skip();
175
+ } // TODO: Fix mongodb.
176
+ const key = new randexp_1.default(/([a-z]\w{0,20})foo\1/).gen();
177
+ await Promise.all([
178
+ db.set(key, true),
179
+ db.set(`${key}a`, true),
180
+ db.set(`${key}b`, false),
181
+ db.set(`${key}b2`, false),
182
+ db.set(`nonmatching_${key}`, false),
183
+ ]);
184
+ const keys = await db.findKeys(`${key}*`, `${key}b*`);
185
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
186
+ assert.deepEqual(keys.sort(), [key, `${key}a`].sort());
187
+ });
188
+ it('findKeys with no matches works', async () => {
189
+ const key = new randexp_1.default(/([a-z]\w{0,20})foo\1/).gen();
190
+ await db.set(key, true);
191
+ const keys = await db.findKeys(`${key}_nomatch_*`, null);
192
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
193
+ assert.deepEqual(keys, []);
194
+ });
195
+ it('findKeys with no wildcard works', async () => {
196
+ const key = new randexp_1.default(/([a-z]\w{0,20})foo\1/).gen();
197
+ await db.set(key, true);
198
+ const keys = await db.findKeys(key, null);
199
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
200
+ assert.deepEqual(keys, [key]);
201
+ });
202
+ it('remove works', async () => {
203
+ const input = { a: 1, b: new randexp_1.default(/.+/).gen() };
204
+ const key = randomString();
205
+ await db.set(key, input);
206
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
207
+ assert.equal(JSON.stringify(await db.get(key)), JSON.stringify(input));
208
+ await db.remove(key);
209
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
210
+ assert((await db.get(key)) == null);
211
+ });
212
+ it('getSub of existing property works', async () => {
213
+ await db.set('k', { sub1: { sub2: 'v' } });
214
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
215
+ assert.equal(await db.getSub('k', ['sub1', 'sub2']), 'v');
216
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
217
+ assert.deepEqual(await db.getSub('k', ['sub1']), { sub2: 'v' });
218
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
219
+ assert.deepEqual(await db.getSub('k', []), { sub1: { sub2: 'v' } });
220
+ });
221
+ it('getSub of missing property returns nullish', async () => {
222
+ await db.set('k', { sub1: {} });
223
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
224
+ assert((await db.getSub('k', ['sub1', 'sub2'])) == null);
225
+ await db.set('k', {});
226
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
227
+ assert((await db.getSub('k', ['sub1', 'sub2'])) == null);
228
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
229
+ assert((await db.getSub('k', ['sub1'])) == null);
230
+ await db.remove('k');
231
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
232
+ assert((await db.getSub('k', ['sub1', 'sub2'])) == null);
233
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
234
+ assert((await db.getSub('k', ['sub1'])) == null);
235
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
236
+ assert((await db.getSub('k', [])) == null);
237
+ });
238
+ it('setSub can modify an existing property', async () => {
239
+ await db.set('k', { sub1: { sub2: 'v' } });
240
+ await db.setSub('k', ['sub1', 'sub2'], 'v2');
241
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
242
+ assert.deepEqual(await db.get('k'), { sub1: { sub2: 'v2' } });
243
+ await db.setSub('k', ['sub1'], 'v2');
244
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
245
+ assert.deepEqual(await db.get('k'), { sub1: 'v2' });
246
+ await db.setSub('k', [], 'v3');
247
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
248
+ assert.equal(await db.get('k'), 'v3');
249
+ });
250
+ it('setSub can add a new property', async () => {
251
+ await db.remove('k');
252
+ await db.setSub('k', [], {});
253
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
254
+ assert.deepEqual(await db.get('k'), {});
255
+ await db.setSub('k', ['sub1'], {});
256
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
257
+ assert.deepEqual(await db.get('k'), { sub1: {} });
258
+ await db.setSub('k', ['sub1', 'sub2'], 'v');
259
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
260
+ assert.deepEqual(await db.get('k'), { sub1: { sub2: 'v' } });
261
+ await db.remove('k');
262
+ await db.setSub('k', ['sub1', 'sub2'], 'v');
263
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
264
+ assert.deepEqual(await db.get('k'), { sub1: { sub2: 'v' } });
265
+ });
266
+ it('setSub rejects attempts to set properties on primitives', async () => {
267
+ for (const v of ['hello world', 42, true]) {
268
+ await db.set('k', v);
269
+ assert.rejects(db.setSub('k', ['sub'], 'x'), {
270
+ name: 'TypeError',
271
+ message: /property "sub" on non-object/,
272
+ });
273
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
274
+ assert.deepEqual(await db.get('k'), v);
275
+ }
276
+ });
277
+ it('speed is acceptable', async function () {
278
+ this.timeout(180000);
279
+ const { speeds: { count = 1000, setMax = 3, getMax = 0.1, findKeysMax = 3, removeMax = 1 } = {} } = dbSettings || {};
280
+ const input = { a: 1, b: new randexp_1.default(/.+/).gen() };
281
+ // TODO setting a key with non ascii chars
282
+ const key = new randexp_1.default(/([a-z]\w{0,20})foo\1/).gen();
283
+ // Pre-allocate an array before starting the timer so that time spent growing the
284
+ // array doesn't throw off the benchmarks.
285
+ const promises = [...Array(count + 1)].map(() => null);
286
+ const timers = { start: Date.now() };
287
+ for (let i = 0; i < count; ++i) {
288
+ promises[i] = db.set(key + i, input);
289
+ }
290
+ promises[count] = db.flush();
291
+ await Promise.all(promises);
292
+ // @ts-expect-error TS(2339): Property 'set' does not exist on type '{ start: nu... Remove this comment to see the full error message
293
+ timers.set = Date.now();
294
+ for (let i = 0; i < count; ++i) {
295
+ promises[i] = db.get(key + i);
296
+ }
297
+ await Promise.all(promises);
298
+ // @ts-expect-error TS(2339): Property 'get' does not exist on type '{ start: nu... Remove this comment to see the full error message
299
+ timers.get = Date.now();
300
+ for (let i = 0; i < count; ++i) {
301
+ promises[i] = db.findKeys(key + i, null);
302
+ }
303
+ await Promise.all(promises);
304
+ // @ts-expect-error TS(2339): Property 'findKeys' does not exist on type '{ star... Remove this comment to see the full error message
305
+ timers.findKeys = Date.now();
306
+ for (let i = 0; i < count; ++i) {
307
+ promises[i] = db.remove(key + i);
308
+ }
309
+ promises[count] = db.flush();
310
+ await Promise.all(promises);
311
+ // @ts-expect-error TS(2339): Property 'remove' does not exist on type '{ start:... Remove this comment to see the full error message
312
+ timers.remove = Date.now();
313
+ const timePerOp = {
314
+ // @ts-expect-error TS(2339): Property 'set' does not exist on type '{ start: nu... Remove this comment to see the full error message
315
+ set: (timers.set - timers.start) / count,
316
+ // @ts-expect-error TS(2339): Property 'get' does not exist on type '{ start: nu... Remove this comment to see the full error message
317
+ get: (timers.get - timers.set) / count,
318
+ // @ts-expect-error TS(2339): Property 'findKeys' does not exist on type '{ star... Remove this comment to see the full error message
319
+ findKeys: (timers.findKeys - timers.get) / count,
320
+ // @ts-expect-error TS(2339): Property 'remove' does not exist on type '{ start:... Remove this comment to see the full error message
321
+ remove: (timers.remove - timers.findKeys) / count,
322
+ };
323
+ speedTable.push([
324
+ database,
325
+ readCache ? 'yes' : 'no',
326
+ writeBuffer ? 'yes' : 'no',
327
+ count,
328
+ timePerOp.set,
329
+ timePerOp.get,
330
+ timePerOp.findKeys,
331
+ timePerOp.remove,
332
+ // @ts-expect-error TS(2339): Property 'remove' does not exist on type '{ start:... Remove this comment to see the full error message
333
+ timers.remove - timers.start,
334
+ // @ts-expect-error TS(2339): Property 'remove' does not exist on type '{ start:... Remove this comment to see the full error message
335
+ (timers.remove - timers.start) / count,
336
+ ]);
337
+ // Removes the "Acceptable ms/op" column if there is no enforced limit.
338
+ const filterColumn = (row) => {
339
+ if (readCache && writeBuffer) {
340
+ return row;
341
+ }
342
+ row.splice(1, 1);
343
+ return row;
344
+ };
345
+ const acceptableTable = new cli_table_1.default({
346
+ head: filterColumn(['op', 'Acceptable ms/op', 'Actual ms/op']),
347
+ colWidths: filterColumn([10, 18, 18]),
348
+ });
349
+ acceptableTable.push(...[
350
+ ['set', setMax, timePerOp.set],
351
+ ['get', getMax, timePerOp.get],
352
+ ['findKeys', findKeysMax, timePerOp.findKeys],
353
+ ['remove', removeMax, timePerOp.remove],
354
+ ].map(filterColumn));
355
+ console.log(acceptableTable.toString());
356
+ if (readCache && writeBuffer) {
357
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
358
+ assert(setMax >= timePerOp.set);
359
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
360
+ assert(getMax >= timePerOp.get);
361
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
362
+ assert(findKeysMax >= timePerOp.findKeys);
363
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
364
+ assert(removeMax >= timePerOp.remove);
365
+ }
366
+ });
367
+ });
368
+ }
369
+ });
370
+ }
371
+ });
372
+ });
373
+ });
@@ -0,0 +1,74 @@
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
+ const assert_1 = __importDefault(require("assert"));
7
+ const index_1 = require("../index");
8
+ const util_1 = __importDefault(require("util"));
9
+ 'use strict';
10
+ const assert = assert_1.default.strict;
11
+ const range = (N) => [...Array(N).keys()];
12
+ describe(__filename, () => {
13
+ let db = null;
14
+ let mock = null;
15
+ const createDb = async (wrapperSettings) => {
16
+ const settings = {};
17
+ db = new index_1.Database('mock', settings, wrapperSettings);
18
+ // @ts-expect-error TS(2339): Property 'mock' does not exist on type '{}'.
19
+ mock = settings.mock;
20
+ mock.once('init', (cb) => cb());
21
+ await db.init();
22
+ };
23
+ afterEach(async () => {
24
+ if (mock != null) {
25
+ mock.removeAllListeners();
26
+ mock.once('close', (cb) => cb());
27
+ mock = null;
28
+ }
29
+ if (db != null) {
30
+ await db.close();
31
+ db = null;
32
+ }
33
+ });
34
+ describe('bulkLimit', () => {
35
+ const bulkLimits = [0, false, null, undefined, '', 1, 2];
36
+ for (const bulkLimit of bulkLimits) {
37
+ it(bulkLimit === undefined ? 'undefined' : JSON.stringify(bulkLimit), async () => {
38
+ await createDb({ bulkLimit });
39
+ const gotWrites = [];
40
+ mock.on('set', util_1.default.callbackify(async (k, v) => gotWrites.push(1)));
41
+ mock.on('doBulk', util_1.default.callbackify(async (ops) => gotWrites.push(ops.length)));
42
+ const N = 10;
43
+ await Promise.all(range(N).map((i) => db.set(`key${i}`, `val${i}`)));
44
+ const wantLimit = bulkLimit || N;
45
+ // @ts-expect-error TS(2363): The right-hand side of an arithmetic operation mus... Remove this comment to see the full error message
46
+ const wantWrites = range(N / wantLimit).map((i) => wantLimit);
47
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
48
+ assert.deepEqual(gotWrites, wantWrites);
49
+ });
50
+ }
51
+ });
52
+ it('bulk failures are retried individually', async () => {
53
+ await createDb({});
54
+ const gotDoBulkCalls = [];
55
+ mock.on('doBulk', util_1.default.callbackify(async (ops) => {
56
+ gotDoBulkCalls.push(ops.length);
57
+ throw new Error('test');
58
+ }));
59
+ const gotWrites = new Map();
60
+ const wantWrites = new Map();
61
+ mock.on('set', util_1.default.callbackify(async (k, v) => gotWrites.set(k, v)));
62
+ const N = 10;
63
+ await Promise.all(range(N).map(async (i) => {
64
+ const k = `key${i}`;
65
+ const v = `val${i}`;
66
+ wantWrites.set(k, JSON.stringify(v));
67
+ await db.set(k, v);
68
+ }));
69
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
70
+ assert.deepEqual(gotDoBulkCalls, [N]);
71
+ // @ts-expect-error TS(2775): Assertions require every name in the call target t... Remove this comment to see the full error message
72
+ assert.deepEqual(gotWrites, wantWrites);
73
+ });
74
+ });