@orion-js/mongodb 3.0.41 → 3.0.45
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/connect/getMongoConnection.d.ts +1 -0
- package/lib/connect/getMongoConnection.js +3 -1
- package/lib/connect/index.d.ts +2 -2
- package/lib/connect/index.js +2 -1
- package/lib/createCollection/createIndexPromisesTest.d.ts +1 -0
- package/lib/createCollection/createIndexPromisesTest.js +12 -0
- package/lib/createCollection/createIndexes.js +5 -0
- package/lib/createCollection/createIndexesPromise.test.d.ts +1 -0
- package/lib/createCollection/createIndexesPromise.test.js +32 -0
- package/lib/createCollection/index.js +10 -3
- package/lib/tests/setup.js +4 -0
- package/lib/types/index.d.ts +6 -0
- package/package.json +2 -2
|
@@ -3,17 +3,19 @@ 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.getMongoConnection = void 0;
|
|
6
|
+
exports.getMongoConnection = exports.allConnectionPromises = void 0;
|
|
7
7
|
const mongodb_1 = require("mongodb");
|
|
8
8
|
const connections_1 = require("./connections");
|
|
9
9
|
const getDBName_1 = __importDefault(require("./getDBName"));
|
|
10
10
|
const getMongoURLFromEnv_1 = require("./getMongoURLFromEnv");
|
|
11
|
+
exports.allConnectionPromises = [];
|
|
11
12
|
const getMongoConnection = ({ name, uri }) => {
|
|
12
13
|
uri = uri || (0, getMongoURLFromEnv_1.getMongoURLFromEnv)(name);
|
|
13
14
|
if (connections_1.connections[name])
|
|
14
15
|
return connections_1.connections[name];
|
|
15
16
|
const client = new mongodb_1.MongoClient(uri);
|
|
16
17
|
const connectionPromise = client.connect();
|
|
18
|
+
exports.allConnectionPromises.push(connectionPromise);
|
|
17
19
|
const dbName = (0, getDBName_1.default)(uri);
|
|
18
20
|
const db = client.db(dbName);
|
|
19
21
|
const mongoClient = {
|
package/lib/connect/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { connections } from './connections';
|
|
2
2
|
import { OrionMongoClient } from './connections';
|
|
3
|
-
import { getMongoConnection } from './getMongoConnection';
|
|
4
|
-
export { OrionMongoClient, getMongoConnection, connections };
|
|
3
|
+
import { allConnectionPromises, getMongoConnection } from './getMongoConnection';
|
|
4
|
+
export { OrionMongoClient, getMongoConnection, connections, allConnectionPromises };
|
package/lib/connect/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.connections = exports.getMongoConnection = void 0;
|
|
3
|
+
exports.allConnectionPromises = exports.connections = exports.getMongoConnection = void 0;
|
|
4
4
|
const connections_1 = require("./connections");
|
|
5
5
|
Object.defineProperty(exports, "connections", { enumerable: true, get: function () { return connections_1.connections; } });
|
|
6
6
|
const getMongoConnection_1 = require("./getMongoConnection");
|
|
7
|
+
Object.defineProperty(exports, "allConnectionPromises", { enumerable: true, get: function () { return getMongoConnection_1.allConnectionPromises; } });
|
|
7
8
|
Object.defineProperty(exports, "getMongoConnection", { enumerable: true, get: function () { return getMongoConnection_1.getMongoConnection; } });
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const MockTests: import("..").Collection<any>;
|
|
@@ -0,0 +1,12 @@
|
|
|
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.MockTests = void 0;
|
|
7
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
8
|
+
const _1 = __importDefault(require("."));
|
|
9
|
+
exports.MockTests = (0, _1.default)({
|
|
10
|
+
name: (0, helpers_1.generateId)(),
|
|
11
|
+
indexes: [{ keys: { a: 1 }, options: { unique: true } }]
|
|
12
|
+
});
|
|
@@ -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);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const Tests: import("..").Collection<any>;
|
|
@@ -0,0 +1,32 @@
|
|
|
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.Tests = void 0;
|
|
7
|
+
const helpers_1 = require("@orion-js/helpers");
|
|
8
|
+
const _1 = __importDefault(require("."));
|
|
9
|
+
const __1 = require("..");
|
|
10
|
+
jest.mock('./createIndexPromisesTest');
|
|
11
|
+
exports.Tests = (0, _1.default)({
|
|
12
|
+
name: (0, helpers_1.generateId)(),
|
|
13
|
+
indexes: [{ keys: { a: 1 }, options: { unique: true } }]
|
|
14
|
+
});
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
await Promise.all(__1.allConnectionPromises);
|
|
17
|
+
});
|
|
18
|
+
describe('createIndexesPromise', () => {
|
|
19
|
+
it('should correctly handle the promise for tests', async () => {
|
|
20
|
+
const userId = await exports.Tests.insertOne({
|
|
21
|
+
name: 'Nico'
|
|
22
|
+
});
|
|
23
|
+
const user = await exports.Tests.findOne(userId);
|
|
24
|
+
expect(user.name).toBe('Nico');
|
|
25
|
+
});
|
|
26
|
+
it('should be able to close the index handlers on the global config', async () => {
|
|
27
|
+
(0, _1.default)({
|
|
28
|
+
name: (0, helpers_1.generateId)(),
|
|
29
|
+
indexes: [{ keys: { a: 1 }, options: { unique: true } }]
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -65,9 +65,16 @@ const createCollection = (options) => {
|
|
|
65
65
|
collection.loadById = (0, dataLoader_1.loadById)(collection);
|
|
66
66
|
collection.loadOne = (0, dataLoader_1.loadOne)(collection);
|
|
67
67
|
collection.loadMany = (0, dataLoader_1.loadMany)(collection);
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
const createIndexes = async () => {
|
|
69
|
+
const createIndexPromise = (0, createIndexes_1.loadIndexes)(collection);
|
|
70
|
+
exports.createIndexesPromises.push(createIndexPromise);
|
|
71
|
+
collection.createIndexesPromise = createIndexPromise;
|
|
72
|
+
return createIndexPromise;
|
|
73
|
+
};
|
|
74
|
+
collection.createIndexes = createIndexes;
|
|
75
|
+
if (!process.env.DONT_CREATE_INDEXES_AUTOMATICALLY) {
|
|
76
|
+
createIndexes();
|
|
77
|
+
}
|
|
71
78
|
return collection;
|
|
72
79
|
};
|
|
73
80
|
exports.default = createCollection;
|
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/lib/types/index.d.ts
CHANGED
|
@@ -116,6 +116,12 @@ export interface Collection<ModelClass = any> {
|
|
|
116
116
|
loadOne: DataLoader.LoadOne<ModelClass>;
|
|
117
117
|
loadMany: DataLoader.LoadMany<ModelClass>;
|
|
118
118
|
loadById: DataLoader.LoadById<ModelClass>;
|
|
119
|
+
/**
|
|
120
|
+
* Use this function if you are using tests and you pass the
|
|
121
|
+
* env var DONT_CREATE_INDEXES_AUTOMATICALLY and you need to
|
|
122
|
+
* create the indexes for this collection
|
|
123
|
+
*/
|
|
124
|
+
createIndexes: () => Promise<string[]>;
|
|
119
125
|
createIndexesPromise: Promise<string[]>;
|
|
120
126
|
connectionPromise: Promise<MongoDB.MongoClient>;
|
|
121
127
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/mongodb",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.45",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"types": "lib/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"publishConfig": {
|
|
39
39
|
"access": "public"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "76213e5017e576d6a30e6d8af3580814bac028b2"
|
|
42
42
|
}
|