e22m4u 4 дней назад
Родитель
Сommit
24d63c33fa

+ 0 - 7
.mocharc.cjs

@@ -1,7 +0,0 @@
-const path = require('path');
-
-module.exports = {
-  extension: ['js'],
-  spec: 'src/**/*.spec.js',
-  require: [path.join(__dirname, 'mocha.setup.js')],
-}

+ 5 - 0
.mocharc.json

@@ -0,0 +1,5 @@
+{
+  "extension": ["js"],
+  "spec": "src/**/*.spec.js",
+  "require": "./mocha.setup.js"
+}

+ 11 - 24
dist/cjs/index.cjs

@@ -28,7 +28,6 @@ module.exports = __toCommonJS(index_exports);
 
 // src/mongodb-adapter.js
 var import_mongodb2 = require("mongodb");
-var import_mongodb3 = require("mongodb");
 
 // node_modules/@e22m4u/js-service/src/errors/invalid-argument-error.js
 var import_js_format = require("@e22m4u/js-format");
@@ -1098,7 +1097,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * Mongodb instance.
    *
    * @type {MongoClient}
-   * @private
    */
   _client;
   /**
@@ -1113,14 +1111,13 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * Collections.
    *
    * @type {Map<any, any>}
-   * @private
    */
   _collections = /* @__PURE__ */ new Map();
   /**
    * Constructor.
    *
    * @param {ServiceContainer} container
-   * @param settings
+   * @param {object} settings
    */
   constructor(container, settings) {
     settings = Object.assign({}, DEFAULT_SETTINGS, settings || {});
@@ -1131,13 +1128,13 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
     super(container, settings);
     const options = (0, import_js_repository3.selectObjectKeys)(this.settings, MONGODB_OPTION_NAMES);
     const url = createMongodbUrl(this.settings);
-    this._client = new import_mongodb3.MongoClient(url, options);
+    this._client = new import_mongodb2.MongoClient(url, options);
   }
   /**
    * Get id prop name.
    *
-   * @param modelName
-   * @private
+   * @param {string} modelName
+   * @returns {string}
    */
   _getIdPropName(modelName) {
     return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
@@ -1147,8 +1144,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
   /**
    * Get id col name.
    *
-   * @param modelName
-   * @private
+   * @param {string} modelName
+   * @returns {string}
    */
   _getIdColName(modelName) {
     return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsColumnName(
@@ -1158,9 +1155,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
   /**
    * Coerce id.
    *
-   * @param value
+   * @param {*} value
    * @returns {ObjectId|*}
-   * @private
    */
   _coerceId(value) {
     if (value == null) return value;
@@ -1170,9 +1166,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
   /**
    * Coerce date.
    *
-   * @param value
+   * @param {Date|string|*} value
    * @returns {Date|*}
-   * @private
    */
   _coerceDate(value) {
     if (value == null) return value;
@@ -1186,7 +1181,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * @param {string} modelName
    * @param {object} modelData
    * @returns {object}
-   * @private
    */
   _toDatabase(modelName, modelData) {
     const tableData = this.getService(
@@ -1216,7 +1210,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * @param {string} modelName
    * @param {object} tableData
    * @returns {object}
-   * @private
    */
   _fromDatabase(modelName, tableData) {
     if ("_id" in tableData) {
@@ -1244,6 +1237,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * Get collection name by model name.
    *
    * @param {string} modelName
+   * @returns {string}
    */
   _getCollectionNameByModelName(modelName) {
     const modelDef = this.getService(import_js_repository3.DefinitionRegistry).getModel(modelName);
@@ -1255,7 +1249,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    *
    * @param {string} modelName
    * @returns {*}
-   * @private
    */
   _getCollection(modelName) {
     let collection = this._collections.get(modelName);
@@ -1268,9 +1261,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
   /**
    * Get id type.
    *
-   * @param modelName
+   * @param {string} modelName
    * @returns {string|*}
-   * @private
    */
   _getIdType(modelName) {
     const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
@@ -1283,7 +1275,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * @param {string} modelName
    * @param {string} propName
    * @returns {string}
-   * @private
    */
   _getColName(modelName, propName) {
     if (!propName || typeof propName !== "string")
@@ -1308,7 +1299,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * @param {string} modelName
    * @param {string} propsChain
    * @returns {string}
-   * @private
    */
   _convertPropNamesChainToColNamesChain(modelName, propsChain) {
     if (!modelName || typeof modelName !== "string")
@@ -1341,7 +1331,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * @param {string} modelName
    * @param {string|string[]} fields
    * @returns {Record<string, number>|undefined}
-   * @private
    */
   _buildProjection(modelName, fields) {
     if (fields == null) return;
@@ -1368,7 +1357,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    * @param {string} modelName
    * @param {string|string[]} clause
    * @returns {object|undefined}
-   * @private
    */
   _buildSort(modelName, clause) {
     if (clause == null) return;
@@ -1403,8 +1391,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
    *
    * @param {string} modelName
    * @param {object} clause
-   * @returns {object}
-   * @private
+   * @returns {object|undefined}
    */
   _buildQuery(modelName, clause) {
     if (clause == null) return;

+ 13 - 0
eslint.config.js

@@ -1,6 +1,8 @@
 import globals from 'globals';
 import eslintJs from '@eslint/js';
+import eslintJsdocPlugin from 'eslint-plugin-jsdoc';
 import eslintMochaPlugin from 'eslint-plugin-mocha';
+import eslintImportPlugin from 'eslint-plugin-import';
 import eslintPrettierConfig from 'eslint-config-prettier';
 import eslintChaiExpectPlugin from 'eslint-plugin-chai-expect';
 
@@ -13,14 +15,25 @@ export default [{
     },
   },
   plugins: {
+    'jsdoc': eslintJsdocPlugin,
     'mocha': eslintMochaPlugin,
+    'import': eslintImportPlugin,
     'chai-expect': eslintChaiExpectPlugin,
   },
   rules: {
     ...eslintJs.configs.recommended.rules,
     ...eslintPrettierConfig.rules,
+    ...eslintImportPlugin.flatConfigs.recommended.rules,
     ...eslintMochaPlugin.configs.recommended.rules,
     ...eslintChaiExpectPlugin.configs['recommended-flat'].rules,
+    ...eslintJsdocPlugin.configs['flat/recommended-error'].rules,
+    'no-duplicate-imports': 'error',
+    'jsdoc/reject-any-type': 0,
+    'jsdoc/reject-function-type': 0,
+    'jsdoc/require-param-description': 0,
+    'jsdoc/require-returns-description': 0,
+    'jsdoc/require-property-description': 0,
+    'jsdoc/tag-lines': ['error', 'any', {startLines: 1}],
   },
   files: ['src/**/*.js'],
 }];

+ 1 - 1
jsconfig.json

@@ -4,4 +4,4 @@
     "module": "NodeNext",
     "moduleResolution": "NodeNext"
   }
-}
+}

+ 7 - 5
package.json

@@ -13,10 +13,10 @@
     "Datasource",
     "Relations"
   ],
-  "homepage": "https://github.com/e22m4u/js-repository-mongodb-adapter",
+  "homepage": "https://gitrepos.ru/e22m4u/js-repository-mongodb-adapter",
   "repository": {
     "type": "git",
-    "url": "git+https://github.com/e22m4u/js-repository-mongodb-adapter.git"
+    "url": "git+https://gitrepos.ru/e22m4u/js-repository-mongodb-adapter.git"
   },
   "type": "module",
   "module": "./src/index.js",
@@ -38,11 +38,11 @@
     "prepare": "husky"
   },
   "dependencies": {
-    "@e22m4u/js-format": "~0.2.1",
+    "@e22m4u/js-format": "~0.3.0",
     "mongodb": "6.20.0"
   },
   "peerDependencies": {
-    "@e22m4u/js-repository": "~0.7.0"
+    "@e22m4u/js-repository": "~0.8.0"
   },
   "devDependencies": {
     "@commitlint/cli": "~20.1.0",
@@ -56,11 +56,13 @@
     "eslint": "~9.39.1",
     "eslint-config-prettier": "~10.1.8",
     "eslint-plugin-chai-expect": "~3.1.0",
+    "eslint-plugin-import": "~2.32.0",
+    "eslint-plugin-jsdoc": "~61.4.1",
     "eslint-plugin-mocha": "~11.2.0",
     "globals": "~16.5.0",
     "husky": "~9.1.7",
     "mocha": "~11.7.5",
-    "prettier": "~3.6.2",
+    "prettier": "~3.7.3",
     "rimraf": "~6.1.2"
   }
 }

+ 11 - 24
src/mongodb-adapter.js

@@ -1,6 +1,5 @@
 /* eslint no-unused-vars: 0 */
-import {ObjectId} from 'mongodb';
-import {MongoClient} from 'mongodb';
+import {ObjectId, MongoClient} from 'mongodb';
 import {ServiceContainer} from '@e22m4u/js-service';
 
 import {
@@ -149,7 +148,6 @@ export class MongodbAdapter extends Adapter {
    * Mongodb instance.
    *
    * @type {MongoClient}
-   * @private
    */
   _client;
 
@@ -166,7 +164,6 @@ export class MongodbAdapter extends Adapter {
    * Collections.
    *
    * @type {Map<any, any>}
-   * @private
    */
   _collections = new Map();
 
@@ -174,7 +171,7 @@ export class MongodbAdapter extends Adapter {
    * Constructor.
    *
    * @param {ServiceContainer} container
-   * @param settings
+   * @param {object} settings
    */
   constructor(container, settings) {
     settings = Object.assign({}, DEFAULT_SETTINGS, settings || {});
@@ -191,8 +188,8 @@ export class MongodbAdapter extends Adapter {
   /**
    * Get id prop name.
    *
-   * @param modelName
-   * @private
+   * @param {string} modelName
+   * @returns {string}
    */
   _getIdPropName(modelName) {
     return this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
@@ -203,8 +200,8 @@ export class MongodbAdapter extends Adapter {
   /**
    * Get id col name.
    *
-   * @param modelName
-   * @private
+   * @param {string} modelName
+   * @returns {string}
    */
   _getIdColName(modelName) {
     return this.getService(ModelDefinitionUtils).getPrimaryKeyAsColumnName(
@@ -215,9 +212,8 @@ export class MongodbAdapter extends Adapter {
   /**
    * Coerce id.
    *
-   * @param value
+   * @param {*} value
    * @returns {ObjectId|*}
-   * @private
    */
   _coerceId(value) {
     if (value == null) return value;
@@ -228,9 +224,8 @@ export class MongodbAdapter extends Adapter {
   /**
    * Coerce date.
    *
-   * @param value
+   * @param {Date|string|*} value
    * @returns {Date|*}
-   * @private
    */
   _coerceDate(value) {
     if (value == null) return value;
@@ -245,7 +240,6 @@ export class MongodbAdapter extends Adapter {
    * @param {string} modelName
    * @param {object} modelData
    * @returns {object}
-   * @private
    */
   _toDatabase(modelName, modelData) {
     const tableData = this.getService(
@@ -279,7 +273,6 @@ export class MongodbAdapter extends Adapter {
    * @param {string} modelName
    * @param {object} tableData
    * @returns {object}
-   * @private
    */
   _fromDatabase(modelName, tableData) {
     if ('_id' in tableData) {
@@ -311,6 +304,7 @@ export class MongodbAdapter extends Adapter {
    * Get collection name by model name.
    *
    * @param {string} modelName
+   * @returns {string}
    */
   _getCollectionNameByModelName(modelName) {
     const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
@@ -327,7 +321,6 @@ export class MongodbAdapter extends Adapter {
    *
    * @param {string} modelName
    * @returns {*}
-   * @private
    */
   _getCollection(modelName) {
     let collection = this._collections.get(modelName);
@@ -343,9 +336,8 @@ export class MongodbAdapter extends Adapter {
   /**
    * Get id type.
    *
-   * @param modelName
+   * @param {string} modelName
    * @returns {string|*}
-   * @private
    */
   _getIdType(modelName) {
     const utils = this.getService(ModelDefinitionUtils);
@@ -359,7 +351,6 @@ export class MongodbAdapter extends Adapter {
    * @param {string} modelName
    * @param {string} propName
    * @returns {string}
-   * @private
    */
   _getColName(modelName, propName) {
     if (!propName || typeof propName !== 'string')
@@ -388,7 +379,6 @@ export class MongodbAdapter extends Adapter {
    * @param {string} modelName
    * @param {string} propsChain
    * @returns {string}
-   * @private
    */
   _convertPropNamesChainToColNamesChain(modelName, propsChain) {
     if (!modelName || typeof modelName !== 'string')
@@ -429,7 +419,6 @@ export class MongodbAdapter extends Adapter {
    * @param {string} modelName
    * @param {string|string[]} fields
    * @returns {Record<string, number>|undefined}
-   * @private
    */
   _buildProjection(modelName, fields) {
     if (fields == null) return;
@@ -458,7 +447,6 @@ export class MongodbAdapter extends Adapter {
    * @param {string} modelName
    * @param {string|string[]} clause
    * @returns {object|undefined}
-   * @private
    */
   _buildSort(modelName, clause) {
     if (clause == null) return;
@@ -498,8 +486,7 @@ export class MongodbAdapter extends Adapter {
    *
    * @param {string} modelName
    * @param {object} clause
-   * @returns {object}
-   * @private
+   * @returns {object|undefined}
    */
   _buildQuery(modelName, clause) {
     if (clause == null) return;

+ 14 - 7
src/mongodb-adapter.spec.js

@@ -1,14 +1,16 @@
 import {expect} from 'chai';
-import {ObjectId} from 'mongodb';
-import {MongoClient} from 'mongodb';
 import {format} from '@e22m4u/js-format';
-import {DataType} from '@e22m4u/js-repository';
+import {ObjectId, MongoClient} from 'mongodb';
 import {createMongodbUrl} from './utils/index.js';
 import {MongodbAdapter} from './mongodb-adapter.js';
-import {DatabaseSchema} from '@e22m4u/js-repository';
-import {AdapterRegistry} from '@e22m4u/js-repository';
-import {InvalidOperatorValueError} from '@e22m4u/js-repository';
-import {DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK} from '@e22m4u/js-repository';
+
+import {
+  DataType,
+  DatabaseSchema,
+  AdapterRegistry,
+  InvalidOperatorValueError,
+  DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK,
+} from '@e22m4u/js-repository';
 
 const CONFIG = {
   host: process.env.MONGODB_HOST || 'localhost',
@@ -19,6 +21,11 @@ const CONFIG = {
 const MDB_CLIENT = new MongoClient(createMongodbUrl(CONFIG));
 const ADAPTERS_STACK = [];
 
+/**
+ * Database schema factory.
+ *
+ * @returns {DatabaseSchema}
+ */
 function createSchema() {
   const schema = new DatabaseSchema();
   const adapter = new MongodbAdapter(schema.container, CONFIG);

+ 16 - 0
src/utils/create-mongodb-url.js

@@ -1,7 +1,23 @@
 import {InvalidArgumentError} from '@e22m4u/js-repository';
 
+/**
+ * @typedef {object} MongoDBUrlOptions
+ * @property {string} [protocol]
+ * @property {string} [hostname]
+ * @property {string} [host]
+ * @property {number|string} [port]
+ * @property {string} [database]
+ * @property {string} [db]
+ * @property {string} [username]
+ * @property {string} [password]
+ * @property {string} [pass]
+ */
+
 /**
  * Generate the mongodb URL from the options.
+ *
+ * @param {MongoDBUrlOptions} options
+ * @returns {string}
  */
 export function createMongodbUrl(options = {}) {
   if (!options || typeof options !== 'object' || Array.isArray(options))

+ 2 - 2
src/utils/is-iso-date.js

@@ -1,8 +1,8 @@
 /**
  * Is iso date.
  *
- * @param value
- * @return {boolean}
+ * @param {*} value
+ * @returns {boolean}
  */
 export function isIsoDate(value) {
   if (!value) return false;

+ 2 - 2
src/utils/is-object-id.js

@@ -3,8 +3,8 @@ import {ObjectId} from 'mongodb';
 /**
  * Is object id.
  *
- * @param value
- * @return {boolean}
+ * @param {*} value
+ * @returns {boolean}
  */
 export function isObjectId(value) {
   if (!value) return false;

+ 9 - 3
src/utils/transform-values-deep.js

@@ -1,11 +1,17 @@
 import {InvalidArgumentError} from '@e22m4u/js-repository';
 
+/**
+ * @callback Transformer
+ * @param {*} value
+ * @returns {*}
+ */
+
 /**
  * Transform values deep.
  *
- * @param value
- * @param transformer
- * @return {*}
+ * @param {*} value
+ * @param {Transformer} transformer
+ * @returns {*}
  */
 export function transformValuesDeep(value, transformer) {
   if (!transformer || typeof transformer !== 'function')