@storyous/mongodb 1.0.7 → 1.0.8

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 (60) hide show
  1. package/dist/appData/index.d.ts +12 -0
  2. package/dist/appData/index.js +40 -0
  3. package/dist/concurrentTask.d.ts +9 -0
  4. package/dist/concurrentTask.js +20 -0
  5. package/dist/fixtures.d.ts +5 -0
  6. package/dist/fixtures.js +28 -0
  7. package/dist/getCollection.d.ts +3 -0
  8. package/dist/getCollection.js +9 -0
  9. package/dist/getMongoLocker.d.ts +10 -0
  10. package/dist/getMongoLocker.js +53 -0
  11. package/dist/index.d.ts +11 -0
  12. package/dist/index.js +27 -0
  13. package/dist/migrations/Migration.d.ts +6 -0
  14. package/dist/migrations/Migration.js +22 -0
  15. package/dist/migrations/MigrationsStore.d.ts +13 -0
  16. package/dist/migrations/MigrationsStore.js +65 -0
  17. package/dist/migrations/runMigrations.d.ts +12 -0
  18. package/dist/migrations/runMigrations.js +50 -0
  19. package/dist/migrations/template.d.ts +3 -0
  20. package/dist/migrations/template.js +11 -0
  21. package/dist/mongoClient.d.ts +8 -0
  22. package/dist/mongoClient.js +18 -0
  23. package/dist/mongoErrorCodes.d.ts +6 -0
  24. package/dist/mongoErrorCodes.js +11 -0
  25. package/dist/mongoLocker.d.ts +3 -0
  26. package/dist/mongoLocker.js +17 -0
  27. package/dist/withTransaction.d.ts +3 -0
  28. package/dist/withTransaction.js +21 -0
  29. package/lib/appData/index.d.ts +12 -0
  30. package/lib/concurrentTask.d.ts +9 -0
  31. package/lib/fixtures.d.ts +5 -0
  32. package/lib/getCollection.d.ts +3 -0
  33. package/lib/getMongoLocker.d.ts +10 -0
  34. package/lib/index.d.ts +11 -0
  35. package/lib/migrations/Migration.d.ts +6 -0
  36. package/lib/migrations/MigrationsStore.d.ts +13 -0
  37. package/lib/migrations/runMigrations.d.ts +12 -0
  38. package/lib/migrations/template.d.ts +3 -0
  39. package/lib/migrations/template.js +8 -4
  40. package/lib/migrations/template.ts +2 -1
  41. package/lib/mongoClient.d.ts +8 -0
  42. package/lib/mongoErrorCodes.d.ts +6 -0
  43. package/lib/mongoLocker.d.ts +3 -0
  44. package/lib/withTransaction.d.ts +3 -0
  45. package/package.json +9 -4
  46. package/__tests__/appData.test.js +0 -33
  47. package/__tests__/appData.test.ts +0 -35
  48. package/__tests__/fixtures.test.js +0 -40
  49. package/__tests__/fixtures.test.ts +0 -44
  50. package/__tests__/getMongoClient.js +0 -19
  51. package/__tests__/getMongoClient.ts +0 -24
  52. package/__tests__/migrations/1642152064249-test.js +0 -7
  53. package/__tests__/migrations/1642152064249-test.ts +0 -7
  54. package/__tests__/mongoLocker.test.js +0 -151
  55. package/__tests__/mongoLocker.test.ts +0 -191
  56. package/__tests__/runMigration.test.js +0 -21
  57. package/__tests__/runMigration.test.ts +0 -22
  58. package/__tests__/withTransaction.test.js +0 -40
  59. package/__tests__/withTransaction.test.ts +0 -67
  60. package/tsconfig.json +0 -7
@@ -1,191 +0,0 @@
1
- import assert from 'assert';
2
- import { noop, pick, times } from 'lodash';
3
- import { beforeEach, describe, it } from 'mocha';
4
- import { Collection, MongoClient } from 'mongodb';
5
- import sinon from 'sinon';
6
- import {
7
- getMongoLocker, LockCallback, LockerOptions, mongoLocker,
8
- } from '../lib';
9
- import { Key } from '../lib/mongoClient';
10
- import connectAndGetMongoClient from './getMongoClient';
11
-
12
- describe('packages/mongodb/mongoLocker', () => {
13
-
14
- let mongoClient: MongoClient;
15
- let collection: Collection;
16
- const collectionName = 'customLocks';
17
-
18
- beforeEach(async () => {
19
- mongoClient = await connectAndGetMongoClient();
20
- collection = mongoClient.db().collection(collectionName);
21
- await collection.drop().catch(noop);
22
- });
23
-
24
- [
25
- { description: 'default mongoLocker instance', locker: () => mongoLocker },
26
- { description: 'custom getMongoLocker', locker: () => getMongoLocker(collection) },
27
- ]
28
- .forEach((suite) => {
29
-
30
- describe(suite.description, () => {
31
-
32
- let locker: (key: Key, callback: LockCallback<any>, options?: LockerOptions) => Promise<any>;
33
-
34
- beforeEach(async () => {
35
- locker = await suite.locker();
36
- });
37
-
38
- it('should acquire and release a lock', async () => {
39
-
40
- let i = 0;
41
- const callback: any = sinon.spy(() => ++i);
42
-
43
- const result1 = await locker('AAA', callback);
44
- assert.strictEqual(result1, 1);
45
-
46
- const result2 = await locker('AAA', callback);
47
- assert.strictEqual(result2, 2);
48
-
49
- assert.deepStrictEqual(callback.callCount, 2);
50
- });
51
-
52
- it('should not be possible to acquire the lock twice at once', async () => {
53
-
54
- const callback = sinon.spy(() => new Promise((resolve) => { setTimeout(resolve, 50); }));
55
- const callLocker = () => locker('AAA', callback, { noLaterThan: 0 });
56
-
57
- const concurrency = 10;
58
- const promises = times(concurrency, callLocker);
59
-
60
- // Node 10 or less does not support allSettled. Going through good old cycle in such case
61
- if (Promise.allSettled) {
62
- await Promise.allSettled(promises); // just to get rid of UnhandledRejectionErrors
63
- } else {
64
- // just to get rid of UnhandledRejectionErrors
65
- promises.forEach((promise) => promise.catch(() => {}));
66
-
67
- for (let i = 0; i < promises.length; i++) {
68
- try {
69
- await promises[i]; // eslint-disable-line
70
- } catch (err) {
71
- // That's ok, we expect there can be errors
72
- }
73
- }
74
- }
75
-
76
- const stats = {
77
- resolved: 0,
78
- failed: 0,
79
- };
80
-
81
- for (const promise of promises) {
82
- try {
83
- await promise; // eslint-disable-line no-await-in-loop
84
- stats.resolved++;
85
- } catch (err: any) {
86
- assert(/The lock is already acquired./.test(err?.message));
87
- stats.failed++;
88
- }
89
- }
90
-
91
- assert.deepStrictEqual(callback.callCount, 1, 'The callback should be called only once');
92
- assert.deepStrictEqual(stats, {
93
- resolved: 1,
94
- failed: concurrency - 1,
95
- });
96
- });
97
-
98
- it('should propagate an error and release the lock if the callback fails', async () => {
99
-
100
- await assert.rejects(() => locker('AAA', async () => {
101
- throw new Error('Some error occurred');
102
- }), /Some error occurred/);
103
-
104
- const callback = sinon.spy();
105
- await locker('AAA', callback);
106
-
107
- assert.strictEqual(callback.callCount, 1);
108
- });
109
-
110
- it(
111
- 'should be able to handle few concurrent tasks by waiting some time and acquire retries',
112
- async () => {
113
- const callTimes: Date[] = [];
114
- const callback = sinon.spy(async () => {
115
- callTimes.push(new Date());
116
- await new Promise((resolve) => { setTimeout(resolve, 150); });
117
- callTimes.push(new Date());
118
- }); // default retryDelay is 200
119
-
120
- const callLocker = () => locker('AAA', callback);
121
-
122
- await Promise.all(times(3, callLocker));
123
-
124
- assert.strictEqual(callback.callCount, 3);
125
- callTimes.forEach((time, i) => {
126
- if (i > 0) {
127
- assert(time > callTimes[i - 1], 'There was a parallel callback execution!');
128
- }
129
- });
130
- });
131
-
132
- it('should be possible to override a lock which expired yet', async () => {
133
- const expireIn = 500;
134
- const array: number[] = [];
135
- const key = 'AAA';
136
-
137
- const firstCallPromise = locker(key, async () => {
138
- array.push(1);
139
- // the method last too long
140
- await new Promise((resolve) => { setTimeout(resolve, 2 * expireIn); });
141
- array.push(3);
142
- }, { expireIn });
143
-
144
- await new Promise((resolve) => { setTimeout(resolve, expireIn); });
145
-
146
- const secondCallPromise = locker('AAA', async () => {
147
- array.push(2);
148
- }, { expireIn });
149
-
150
- await Promise.all([firstCallPromise, secondCallPromise]);
151
- assert.deepStrictEqual(array, [1, 2, 3]);
152
- });
153
- });
154
- });
155
-
156
- describe('getMongoLocker', () => {
157
- it('should create and TTL index on the right field', async () => {
158
- await mongoClient.db()
159
- .createCollection(collectionName);
160
-
161
- let indexes = await collection.indexes();
162
- assert.deepStrictEqual(indexes.length, 1); // ensure there is only mandatory _id index
163
-
164
- const locker = await getMongoLocker(collection);
165
- indexes = await collection.indexes();
166
- assert.deepStrictEqual(indexes.length, 2, 'No or wrong indexes has been created!');
167
- assert.deepStrictEqual(pick(indexes[1], 'key', 'expireAfterSeconds'), {
168
- expireAfterSeconds: 0,
169
- key: {
170
- expiresAt: 1,
171
- },
172
- }, 'No or wrong indexes has been created!');
173
-
174
- const startedAt = new Date();
175
- const someKey = 'someKey';
176
- const expireIn = 55000;
177
- await locker(someKey, async () => {
178
- const document = await collection.findOne({});
179
- assert.strictEqual(document?._id, someKey);
180
- assert(
181
- document.expiresAt >= new Date(startedAt.getTime() + expireIn),
182
- 'The expiresAt property has to be greater then startedAt + expiresIn',
183
- );
184
- assert(
185
- document.expiresAt < new Date(Date.now() + expireIn),
186
- 'The acquiredAt property has to be younger then present + expiresIn',
187
- );
188
- }, { expireIn });
189
- });
190
- });
191
- });
@@ -1,21 +0,0 @@
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
- require("./getMongoClient");
7
- const assert_1 = __importDefault(require("assert"));
8
- const mocha_1 = require("mocha");
9
- const winston_1 = require("winston");
10
- const lib_1 = require("../lib");
11
- (0, mocha_1.describe)('packages/mongodb/migrations', () => {
12
- (0, mocha_1.before)(async () => {
13
- await lib_1.appData._collection.deleteOne({ _id: 'migrations' });
14
- });
15
- (0, mocha_1.it)('should runMigrations works', async () => {
16
- await (0, lib_1.runMigrations)((0, winston_1.createLogger)(), `${__dirname}/migrations`, { safeMigration: false });
17
- const migration = await lib_1.appData.getDocument('migrations');
18
- assert_1.default.deepStrictEqual(migration?.lastRun, '1642152064249-test.js');
19
- });
20
- });
21
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicnVuTWlncmF0aW9uLnRlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJydW5NaWdyYXRpb24udGVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLDRCQUEwQjtBQUMxQixvREFBNEI7QUFDNUIsaUNBQTZDO0FBQzdDLHFDQUF1QztBQUN2QyxnQ0FBZ0Q7QUFFaEQsSUFBQSxnQkFBUSxFQUFDLDZCQUE2QixFQUFFLEdBQUcsRUFBRTtJQUN6QyxJQUFBLGNBQU0sRUFBQyxLQUFLLElBQUksRUFBRTtRQUNkLE1BQU0sYUFBTyxDQUFDLFdBQVcsQ0FBQyxTQUFTLENBQUMsRUFBRSxHQUFHLEVBQUUsWUFBWSxFQUFFLENBQUMsQ0FBQztJQUMvRCxDQUFDLENBQUMsQ0FBQztJQUVILElBQUEsVUFBRSxFQUFDLDRCQUE0QixFQUFFLEtBQUssSUFBSSxFQUFFO1FBQ3hDLE1BQU0sSUFBQSxtQkFBYSxFQUNmLElBQUEsc0JBQVksR0FBRSxFQUNkLEdBQUcsU0FBUyxhQUFhLEVBQ3pCLEVBQUUsYUFBYSxFQUFFLEtBQUssRUFBRSxDQUMzQixDQUFDO1FBRUYsTUFBTSxTQUFTLEdBQUcsTUFBTSxhQUFPLENBQUMsV0FBVyxDQUFDLFlBQVksQ0FBQyxDQUFDO1FBQzFELGdCQUFNLENBQUMsZUFBZSxDQUFDLFNBQVMsRUFBRSxPQUFPLEVBQUUsdUJBQXVCLENBQUMsQ0FBQztJQUN4RSxDQUFDLENBQUMsQ0FBQztBQUNQLENBQUMsQ0FBQyxDQUFDIn0=
@@ -1,22 +0,0 @@
1
- import './getMongoClient';
2
- import assert from 'assert';
3
- import { before, describe, it } from 'mocha';
4
- import { createLogger } from 'winston';
5
- import { appData, runMigrations } from '../lib';
6
-
7
- describe('packages/mongodb/migrations', () => {
8
- before(async () => {
9
- await appData._collection.deleteOne({ _id: 'migrations' });
10
- });
11
-
12
- it('should runMigrations works', async () => {
13
- await runMigrations(
14
- createLogger(),
15
- `${__dirname}/migrations`,
16
- { safeMigration: false },
17
- );
18
-
19
- const migration = await appData.getDocument('migrations');
20
- assert.deepStrictEqual(migration?.lastRun, '1642152064249-test.js');
21
- });
22
- });
@@ -1,40 +0,0 @@
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 lodash_1 = require("lodash");
8
- const mocha_1 = require("mocha");
9
- const lib_1 = require("../lib");
10
- require('./getMongoClient');
11
- (0, mocha_1.describe)('packages/mongodb/withTransaction', () => {
12
- let testCollection;
13
- (0, mocha_1.beforeEach)(async () => {
14
- testCollection = (0, lib_1.getCollection)('withTransactionTests');
15
- await testCollection.drop().catch(lodash_1.noop);
16
- await testCollection.insertOne({}); // ensure the collection is created before tests
17
- });
18
- (0, mocha_1.it)('should commit the transaction', async () => {
19
- const expectedReturnValue = { some: 'value' };
20
- const returnValue = await (0, lib_1.withTransaction)(async (session) => {
21
- await testCollection.insertOne({}, { session });
22
- await testCollection.insertOne({}, { session });
23
- assert_1.default.equal(await testCollection.countDocuments({}, { session }), 3);
24
- return expectedReturnValue;
25
- });
26
- assert_1.default.equal(await testCollection.countDocuments({}), 3, 'there should available documents after a commit');
27
- assert_1.default.equal(returnValue, expectedReturnValue);
28
- });
29
- (0, mocha_1.it)('should rollback the transaction', async () => {
30
- const error = new Error('some error');
31
- await assert_1.default.rejects(() => (0, lib_1.withTransaction)(async (session) => {
32
- await testCollection.insertOne({}, { session });
33
- await testCollection.insertOne({}, { session });
34
- assert_1.default.equal(await testCollection.countDocuments({}, { session }), 3);
35
- throw error;
36
- }), error);
37
- assert_1.default.equal(await testCollection.countDocuments({}), 1, 'there should be no document after a rollback');
38
- });
39
- });
40
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoid2l0aFRyYW5zYWN0aW9uLnRlc3QuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJ3aXRoVHJhbnNhY3Rpb24udGVzdC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7OztBQUFBLG9EQUE0QjtBQUM1QixtQ0FBOEI7QUFDOUIsaUNBQWlEO0FBRWpELGdDQUF3RDtBQUV4RCxPQUFPLENBQUMsa0JBQWtCLENBQUMsQ0FBQztBQUU1QixJQUFBLGdCQUFRL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0FBQyxFQUN2QyxDQUFDLEVBQ0QsaURBQWlELENBQ3BELENBQUM7UUFFRixnQkFBTSxDQUFDLEtBQUssQ0FBQyxXQUFXLEVBQUUsbUJBQW1CLENBQUMsQ0FBQztJQUNuRCxDQUFDLENBQUMsQ0FBQztJQUVILElBQUEsVUFBRSxFQUFDLGlDQUFpQyxFQUFFLEtBQUssSUFBSSxFQUFFO1FBQzdDLE1BQU0sS0FBSyxHQUFHLElBQUksS0FBSyxDQUFDLFlBQVksQ0FBQyxDQUFDO1FBRXRDLE1BQU0sZ0JBQU0sQ0FBQyxPQUFPLENBQUMsR0FBRyxFQUFFLENBQUMsSUFBQSxxQkFBZSxFQUFDLEtBQUssRUFBRSxPQUFPLEVBQUUsRUFBRTtZQUV6RCxNQUFNLGNBQWMsQ0FBQyxTQUFTLENBQUMsRUFBRSxFQUFFLEVBQUUsT0FBTyxFQUFFLENBQUMsQ0FBQztZQUNoRCxNQUFNLGNBQWMsQ0FBQyxTQUFTLENBQUMsRUFBRSxFQUFFLEVBQUUsT0FBTyxFQUFFLENBQUMsQ0FBQztZQUVoRCxnQkFBTSxDQUFDLEtBQUssQ0FDUixNQUFNLGNBQWMsQ0FBQyxjQUFjLENBQUMsRUFBRSxFQUFFLEVBQUUsT0FBTyxFQUFFLENBQUMsRUFDcEQsQ0FBQyxDQUNKLENBQUM7WUFFRixNQUFNLEtBQUssQ0FBQztRQUVoQixDQUFDLENBQUMsRUFBRSxLQUFLLENBQUMsQ0FBQztRQUVYLGdCQUFNLENBQUMsS0FBSyxDQUNSLE1BQU0sY0FBYyxDQUFDLGNBQWMsQ0FBQyxFQUFFLENBQUMsRUFDdkMsQ0FBQyxFQUNELDhDQUE4QyxDQUNqRCxDQUFDO0lBQ04sQ0FBQyxDQUFDLENBQUM7QUFDUCxDQUFDLENBQUMsQ0FBQyJ9
@@ -1,67 +0,0 @@
1
- import assert from 'assert';
2
- import { noop } from 'lodash';
3
- import { beforeEach, describe, it } from 'mocha';
4
- import { Collection } from 'mongodb';
5
- import { getCollection, withTransaction } from '../lib';
6
-
7
- require('./getMongoClient');
8
-
9
- describe('packages/mongodb/withTransaction', () => {
10
-
11
- let testCollection: Collection<any>;
12
- beforeEach(async () => {
13
- testCollection = getCollection('withTransactionTests');
14
- await testCollection.drop().catch(noop);
15
- await testCollection.insertOne({}) as any; // ensure the collection is created before tests
16
- });
17
-
18
- it('should commit the transaction', async () => {
19
-
20
- const expectedReturnValue = { some: 'value' };
21
-
22
- const returnValue = await withTransaction(async (session) => {
23
-
24
- await testCollection.insertOne({}, { session });
25
- await testCollection.insertOne({}, { session });
26
-
27
- assert.equal(
28
- await testCollection.countDocuments({}, { session }),
29
- 3,
30
- );
31
-
32
- return expectedReturnValue;
33
- });
34
-
35
- assert.equal(
36
- await testCollection.countDocuments({}),
37
- 3,
38
- 'there should available documents after a commit',
39
- );
40
-
41
- assert.equal(returnValue, expectedReturnValue);
42
- });
43
-
44
- it('should rollback the transaction', async () => {
45
- const error = new Error('some error');
46
-
47
- await assert.rejects(() => withTransaction(async (session) => {
48
-
49
- await testCollection.insertOne({}, { session });
50
- await testCollection.insertOne({}, { session });
51
-
52
- assert.equal(
53
- await testCollection.countDocuments({}, { session }),
54
- 3,
55
- );
56
-
57
- throw error;
58
-
59
- }), error);
60
-
61
- assert.equal(
62
- await testCollection.countDocuments({}),
63
- 1,
64
- 'there should be no document after a rollback',
65
- );
66
- });
67
- });
package/tsconfig.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "include": [
4
- "lib",
5
- "__tests__"
6
- ]
7
- }