@orion-js/mongodb 3.0.37 → 3.0.42
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.
- package/lib/createCollection/createIndexes.js +5 -0
- package/lib/createCollection/createIndexes.test.js +27 -3
- package/lib/createCollection/createIndexesPromise.test.d.ts +1 -0
- package/lib/createCollection/createIndexesPromise.test.js +26 -0
- package/lib/createCollection/getMethods/update.test.js +38 -0
- package/lib/createCollection/index.d.ts +1 -0
- package/lib/createCollection/index.js +5 -1
- package/lib/index.d.ts +2 -2
- package/lib/index.js +15 -5
- package/lib/tests/setup.js +4 -0
- package/package.json +4 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.loadIndexes = exports.checkIndexes = void 0;
|
|
4
|
+
const mongodb_1 = require("mongodb");
|
|
4
5
|
function matchingDefinition(defIndex, curIndex) {
|
|
5
6
|
if (defIndex.options && defIndex.options.name === curIndex.name)
|
|
6
7
|
return true;
|
|
@@ -49,6 +50,10 @@ async function loadIndexes(collection) {
|
|
|
49
50
|
console.info('Index updated correctly');
|
|
50
51
|
return result;
|
|
51
52
|
}
|
|
53
|
+
else if (error instanceof mongodb_1.MongoExpiredSessionError) {
|
|
54
|
+
// this errors is thrown when we are on tests environment
|
|
55
|
+
// but it's not a problem never, index will be created on the next connection
|
|
56
|
+
}
|
|
52
57
|
else {
|
|
53
58
|
console.error(`Error creating index for collection ${collection.name}: ${error.message}`);
|
|
54
59
|
console.error(error);
|
|
@@ -1,11 +1,35 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
4
20
|
};
|
|
5
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
22
|
const helpers_1 = require("@orion-js/helpers");
|
|
7
|
-
const _1 =
|
|
23
|
+
const _1 = __importStar(require("."));
|
|
8
24
|
describe('Test indexes', () => {
|
|
25
|
+
it('Should store all create indexes promises in the array', async () => {
|
|
26
|
+
const collection = (0, _1.default)({
|
|
27
|
+
name: (0, helpers_1.generateId)(),
|
|
28
|
+
indexes: [{ keys: { a: 1 }, options: { unique: true } }]
|
|
29
|
+
});
|
|
30
|
+
expect(_1.createIndexesPromises[0]).toBe(collection.createIndexesPromise);
|
|
31
|
+
expect(_1.createIndexesPromises.length).toBe(1);
|
|
32
|
+
});
|
|
9
33
|
it('Should create collection indexes correctly', async () => {
|
|
10
34
|
const collection = (0, _1.default)({
|
|
11
35
|
name: (0, helpers_1.generateId)(),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
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 helpers_1 = require("@orion-js/helpers");
|
|
7
|
+
const _1 = __importDefault(require("."));
|
|
8
|
+
const Tests = (0, _1.default)({
|
|
9
|
+
name: (0, helpers_1.generateId)(),
|
|
10
|
+
indexes: [{ keys: { a: 1 }, options: { unique: true } }]
|
|
11
|
+
});
|
|
12
|
+
describe('createIndexesPromise', () => {
|
|
13
|
+
it('should correctly handle the promise for tests', async () => {
|
|
14
|
+
const userId = await Tests.insertOne({
|
|
15
|
+
name: 'Nico'
|
|
16
|
+
});
|
|
17
|
+
const user = await Tests.findOne(userId);
|
|
18
|
+
expect(user.name).toBe('Nico');
|
|
19
|
+
});
|
|
20
|
+
it('should be able to close the index handlers on the global config', async () => {
|
|
21
|
+
(0, _1.default)({
|
|
22
|
+
name: (0, helpers_1.generateId)(),
|
|
23
|
+
indexes: [{ keys: { a: 1 }, options: { unique: true } }]
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -247,3 +247,41 @@ it('Should allow custom clean function on a blackbox field', async () => {
|
|
|
247
247
|
const item = await Tests.findOne(itemId);
|
|
248
248
|
expect(item).toEqual({ _id: itemId, info: { hello: 'world' } });
|
|
249
249
|
});
|
|
250
|
+
it('Should be able to use custom clean for models on update', async () => {
|
|
251
|
+
const modelFile = (0, models_1.createModel)({
|
|
252
|
+
name: 'File',
|
|
253
|
+
schema: {
|
|
254
|
+
name: { type: String },
|
|
255
|
+
lastName: { type: String, optional: true }
|
|
256
|
+
},
|
|
257
|
+
async clean(value) {
|
|
258
|
+
if (!value)
|
|
259
|
+
return null;
|
|
260
|
+
expect(typeof value.name).toBe('string');
|
|
261
|
+
return {
|
|
262
|
+
...value,
|
|
263
|
+
name: value.name.toUpperCase(),
|
|
264
|
+
lastName: '1'
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
const model = (0, models_1.createModel)({
|
|
269
|
+
name: 'Item',
|
|
270
|
+
schema: {
|
|
271
|
+
file: { type: modelFile }
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
const Tests = (0, __1.default)({ name: (0, helpers_1.generateId)(), model });
|
|
275
|
+
const docId = await Tests.insertOne({
|
|
276
|
+
file: { name: '1' }
|
|
277
|
+
});
|
|
278
|
+
await Tests.updateOne(docId, {
|
|
279
|
+
$set: {
|
|
280
|
+
file: { name: 'Hello' }
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
const result = await Tests.findOne(docId);
|
|
284
|
+
expect(result.file.name).toBe('HELLO');
|
|
285
|
+
expect(result.file.lastName).toBe('1');
|
|
286
|
+
expect.assertions(4);
|
|
287
|
+
});
|
|
@@ -3,6 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createIndexesPromises = void 0;
|
|
6
7
|
const initItem_1 = __importDefault(require("./initItem"));
|
|
7
8
|
const getMethods_1 = require("./getMethods");
|
|
8
9
|
const dataLoader_1 = require("./getMethods/dataLoader");
|
|
@@ -10,6 +11,7 @@ const generateId_1 = __importDefault(require("./generateId"));
|
|
|
10
11
|
const createIndexes_1 = require("./createIndexes");
|
|
11
12
|
const lodash_1 = require("lodash");
|
|
12
13
|
const __1 = require("..");
|
|
14
|
+
exports.createIndexesPromises = [];
|
|
13
15
|
const createCollection = (options) => {
|
|
14
16
|
const connectionName = options.connectionName || 'main';
|
|
15
17
|
const orionConnection = (0, __1.getMongoConnection)({ name: connectionName });
|
|
@@ -63,7 +65,9 @@ const createCollection = (options) => {
|
|
|
63
65
|
collection.loadById = (0, dataLoader_1.loadById)(collection);
|
|
64
66
|
collection.loadOne = (0, dataLoader_1.loadOne)(collection);
|
|
65
67
|
collection.loadMany = (0, dataLoader_1.loadMany)(collection);
|
|
66
|
-
|
|
68
|
+
const createIndexPromise = (0, createIndexes_1.loadIndexes)(collection);
|
|
69
|
+
exports.createIndexesPromises.push(createIndexPromise);
|
|
70
|
+
collection.createIndexesPromise = createIndexPromise;
|
|
67
71
|
return collection;
|
|
68
72
|
};
|
|
69
73
|
exports.default = createCollection;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import createCollection from './createCollection';
|
|
2
|
-
export { createCollection };
|
|
1
|
+
import createCollection, { createIndexesPromises } from './createCollection';
|
|
2
|
+
export { createCollection, createIndexesPromises };
|
|
3
3
|
export * from './connect';
|
|
4
4
|
export * from './types';
|
package/lib/index.js
CHANGED
|
@@ -6,15 +6,25 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
6
6
|
if (k2 === undefined) k2 = k;
|
|
7
7
|
o[k2] = m[k];
|
|
8
8
|
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
9
21
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
22
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
23
|
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.createCollection = void 0;
|
|
17
|
-
const createCollection_1 =
|
|
25
|
+
exports.createIndexesPromises = exports.createCollection = void 0;
|
|
26
|
+
const createCollection_1 = __importStar(require("./createCollection"));
|
|
18
27
|
exports.createCollection = createCollection_1.default;
|
|
28
|
+
Object.defineProperty(exports, "createIndexesPromises", { enumerable: true, get: function () { return createCollection_1.createIndexesPromises; } });
|
|
19
29
|
__exportStar(require("./connect"), exports);
|
|
20
30
|
__exportStar(require("./types"), exports);
|
package/lib/tests/setup.js
CHANGED
|
@@ -9,6 +9,10 @@ beforeAll(async () => {
|
|
|
9
9
|
await connection.connectionPromise;
|
|
10
10
|
});
|
|
11
11
|
afterAll(async () => {
|
|
12
|
+
/**
|
|
13
|
+
* We need to wait on indexes promises to be resolved to close all the handlers
|
|
14
|
+
*/
|
|
15
|
+
await Promise.all(__1.createIndexesPromises);
|
|
12
16
|
for (const connectionName in connections_1.connections) {
|
|
13
17
|
const connection = connections_1.connections[connectionName];
|
|
14
18
|
await connection.client.close();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/mongodb",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.42",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -18,10 +18,10 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@orion-js/helpers": "^3.0.36",
|
|
21
|
-
"@orion-js/models": "^3.0.
|
|
21
|
+
"@orion-js/models": "^3.0.38",
|
|
22
22
|
"@orion-js/resolvers": "^3.0.37",
|
|
23
23
|
"@orion-js/schema": "^3.0.37",
|
|
24
|
-
"@orion-js/typed-model": "^3.0.
|
|
24
|
+
"@orion-js/typed-model": "^3.0.38",
|
|
25
25
|
"dataloader": "2.0.0",
|
|
26
26
|
"dot-object": "2.1.4",
|
|
27
27
|
"mongodb": "4.1.4"
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "ef0e7a088474c899e13146be867dc574032ac588"
|
|
42
42
|
}
|