@orion-js/mongodb 3.0.38 → 3.0.44

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.
@@ -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 __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
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 = __importDefault(require("."));
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,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
+ });
@@ -1,3 +1,4 @@
1
1
  import { CreateCollection } from '../types';
2
+ export declare const createIndexesPromises: any[];
2
3
  declare const createCollection: CreateCollection;
3
4
  export default createCollection;
@@ -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,16 @@ 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
- collection.createIndexesPromise = (0, createIndexes_1.loadIndexes)(collection);
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
+ }
67
78
  return collection;
68
79
  };
69
80
  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 = __importDefault(require("./createCollection"));
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);
@@ -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();
@@ -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.38",
3
+ "version": "3.0.44",
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": "00903f1488b72e13b7d5031a916eb897d0d80e0c"
41
+ "gitHead": "f4ddba085eebb33ad62cb0e0fbd9b687fafae864"
42
42
  }