@e22m4u/js-repository-mongodb-adapter 0.5.6 → 0.6.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@e22m4u/js-repository-mongodb-adapter",
3
- "version": "0.5.6",
3
+ "version": "0.6.2",
4
4
  "description": "MongoDB адаптер для @e22m4u/js-repository",
5
5
  "author": "Mikhail Evstropov <e22m4u@yandex.ru>",
6
6
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@e22m4u/js-format": "~0.1.0",
45
- "@e22m4u/js-repository": "~0.3.0"
45
+ "@e22m4u/js-repository": "~0.4.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@commitlint/cli": "~19.8.1",
@@ -11,8 +11,10 @@ import {ServiceContainer} from '@e22m4u/js-service';
11
11
  import {transformValuesDeep} from './utils/index.js';
12
12
  import {stringToRegexp} from '@e22m4u/js-repository';
13
13
  import {selectObjectKeys} from '@e22m4u/js-repository';
14
+ import {DefinitionRegistry} from '@e22m4u/js-repository';
14
15
  import {ModelDefinitionUtils} from '@e22m4u/js-repository';
15
16
  import {InvalidArgumentError} from '@e22m4u/js-repository';
17
+ import {modelNameToCollectionName} from './utils/index.js';
16
18
  import {InvalidOperatorValueError} from '@e22m4u/js-repository';
17
19
 
18
20
  /**
@@ -297,6 +299,17 @@ export class MongodbAdapter extends Adapter {
297
299
  });
298
300
  }
299
301
 
302
+ /**
303
+ * Get collection name by model name.
304
+ *
305
+ * @param {string} modelName
306
+ */
307
+ _getCollectionNameByModelName(modelName) {
308
+ const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
309
+ if (modelDef.tableName != null) return modelDef.tableName;
310
+ return modelNameToCollectionName(modelDef.name);
311
+ }
312
+
300
313
  /**
301
314
  * Get collection.
302
315
  *
@@ -307,9 +320,10 @@ export class MongodbAdapter extends Adapter {
307
320
  _getCollection(modelName) {
308
321
  let collection = this._collections.get(modelName);
309
322
  if (collection) return collection;
310
- const tableName =
311
- this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName);
312
- collection = this.client.db(this.settings.database).collection(tableName);
323
+ const collectionName = this._getCollectionNameByModelName(modelName);
324
+ collection = this.client
325
+ .db(this.settings.database)
326
+ .collection(collectionName);
313
327
  this._collections.set(modelName, collection);
314
328
  return collection;
315
329
  }
@@ -929,6 +943,6 @@ export class MongodbAdapter extends Adapter {
929
943
  async count(modelName, where = undefined) {
930
944
  const query = this._buildQuery(modelName, where);
931
945
  const table = this._getCollection(modelName);
932
- return await table.count(query);
946
+ return await table.countDocuments(query);
933
947
  }
934
948
  }