@e22m4u/js-repository-mongodb-adapter 0.8.0 → 0.8.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/.mocharc.json +5 -0
- package/dist/cjs/index.cjs +68 -82
- package/eslint.config.js +14 -0
- package/jsconfig.json +1 -1
- package/mocha.setup.js +1 -1
- package/package.json +11 -7
- package/src/mongodb-adapter.js +11 -24
- package/src/mongodb-adapter.spec.js +14 -7
- package/src/utils/create-mongodb-url.js +16 -0
- package/src/utils/is-iso-date.js +2 -2
- package/src/utils/is-object-id.js +2 -2
- package/src/utils/transform-values-deep.js +9 -3
- package/.mocharc.cjs +0 -7
package/.mocharc.json
ADDED
package/dist/cjs/index.cjs
CHANGED
|
@@ -28,7 +28,6 @@ module.exports = __toCommonJS(index_exports);
|
|
|
28
28
|
|
|
29
29
|
// src/mongodb-adapter.js
|
|
30
30
|
var import_mongodb2 = require("mongodb");
|
|
31
|
-
var import_mongodb3 = require("mongodb");
|
|
32
31
|
|
|
33
32
|
// node_modules/@e22m4u/js-service/src/errors/invalid-argument-error.js
|
|
34
33
|
var import_js_format = require("@e22m4u/js-format");
|
|
@@ -386,65 +385,8 @@ function generateRandomHex(length = 4) {
|
|
|
386
385
|
}
|
|
387
386
|
__name(generateRandomHex, "generateRandomHex");
|
|
388
387
|
|
|
389
|
-
// node_modules/@e22m4u/js-debug/src/debuggable.js
|
|
390
|
-
var _Debuggable = class _Debuggable {
|
|
391
|
-
/**
|
|
392
|
-
* Debug.
|
|
393
|
-
*
|
|
394
|
-
* @type {Function}
|
|
395
|
-
*/
|
|
396
|
-
debug;
|
|
397
|
-
/**
|
|
398
|
-
* Ctor Debug.
|
|
399
|
-
*
|
|
400
|
-
* @type {Function}
|
|
401
|
-
*/
|
|
402
|
-
ctorDebug;
|
|
403
|
-
/**
|
|
404
|
-
* Возвращает функцию-отладчик с сегментом пространства имен
|
|
405
|
-
* указанного в параметре метода.
|
|
406
|
-
*
|
|
407
|
-
* @param {Function} method
|
|
408
|
-
* @returns {Function}
|
|
409
|
-
*/
|
|
410
|
-
getDebuggerFor(method) {
|
|
411
|
-
const name = method.name || "anonymous";
|
|
412
|
-
return this.debug.withHash().withNs(name);
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Constructor.
|
|
416
|
-
*
|
|
417
|
-
* @param {DebuggableOptions|undefined} options
|
|
418
|
-
*/
|
|
419
|
-
constructor(options = void 0) {
|
|
420
|
-
const className = toCamelCase(this.constructor.name);
|
|
421
|
-
options = typeof options === "object" && options || {};
|
|
422
|
-
const namespace = options.namespace && String(options.namespace) || void 0;
|
|
423
|
-
if (namespace) {
|
|
424
|
-
this.debug = createDebugger(namespace, className);
|
|
425
|
-
} else {
|
|
426
|
-
this.debug = createDebugger(className);
|
|
427
|
-
}
|
|
428
|
-
const noEnvironmentNamespace = Boolean(options.noEnvironmentNamespace);
|
|
429
|
-
if (noEnvironmentNamespace) this.debug = this.debug.withoutEnvNs();
|
|
430
|
-
this.ctorDebug = this.debug.withNs("constructor").withHash();
|
|
431
|
-
const noInstantiationMessage = Boolean(options.noInstantiationMessage);
|
|
432
|
-
if (!noInstantiationMessage)
|
|
433
|
-
this.ctorDebug(_Debuggable.INSTANTIATION_MESSAGE);
|
|
434
|
-
}
|
|
435
|
-
};
|
|
436
|
-
__name(_Debuggable, "Debuggable");
|
|
437
|
-
/**
|
|
438
|
-
* Instantiation message;
|
|
439
|
-
*
|
|
440
|
-
* @type {string}
|
|
441
|
-
*/
|
|
442
|
-
__publicField(_Debuggable, "INSTANTIATION_MESSAGE", "Instantiated.");
|
|
443
|
-
var Debuggable = _Debuggable;
|
|
444
|
-
|
|
445
388
|
// node_modules/@e22m4u/js-debug/src/create-debugger.js
|
|
446
389
|
var import_js_format2 = require("@e22m4u/js-format");
|
|
447
|
-
var import_js_format3 = require("@e22m4u/js-format");
|
|
448
390
|
|
|
449
391
|
// node_modules/@e22m4u/js-debug/src/create-colorized-dump.js
|
|
450
392
|
var import_util = require("util");
|
|
@@ -650,7 +592,7 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
|
|
|
650
592
|
function debugFn(messageOrData, ...args) {
|
|
651
593
|
if (!isDebuggerEnabled()) return;
|
|
652
594
|
const prefix = getPrefix();
|
|
653
|
-
const multiString = (0,
|
|
595
|
+
const multiString = (0, import_js_format2.format)(messageOrData, ...args);
|
|
654
596
|
const rows = multiString.split("\n");
|
|
655
597
|
rows.forEach((message) => {
|
|
656
598
|
prefix ? console.log(`${prefix} ${message}`) : console.log(message);
|
|
@@ -719,6 +661,62 @@ function createDebugger(namespaceOrOptions = void 0, ...namespaceSegments) {
|
|
|
719
661
|
}
|
|
720
662
|
__name(createDebugger, "createDebugger");
|
|
721
663
|
|
|
664
|
+
// node_modules/@e22m4u/js-debug/src/debuggable.js
|
|
665
|
+
var _Debuggable = class _Debuggable {
|
|
666
|
+
/**
|
|
667
|
+
* Debug.
|
|
668
|
+
*
|
|
669
|
+
* @type {*}
|
|
670
|
+
*/
|
|
671
|
+
debug;
|
|
672
|
+
/**
|
|
673
|
+
* Ctor Debug.
|
|
674
|
+
*
|
|
675
|
+
* @type {Function}
|
|
676
|
+
*/
|
|
677
|
+
ctorDebug;
|
|
678
|
+
/**
|
|
679
|
+
* Возвращает функцию-отладчик с сегментом пространства имен
|
|
680
|
+
* указанного в параметре метода.
|
|
681
|
+
*
|
|
682
|
+
* @param {Function} method
|
|
683
|
+
* @returns {Function}
|
|
684
|
+
*/
|
|
685
|
+
getDebuggerFor(method) {
|
|
686
|
+
const name = method.name || "anonymous";
|
|
687
|
+
return this.debug.withHash().withNs(name);
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* Constructor.
|
|
691
|
+
*
|
|
692
|
+
* @param {DebuggableOptions} [options]
|
|
693
|
+
*/
|
|
694
|
+
constructor(options = void 0) {
|
|
695
|
+
const className = toCamelCase(this.constructor.name);
|
|
696
|
+
options = typeof options === "object" && options || {};
|
|
697
|
+
const namespace = options.namespace && String(options.namespace) || void 0;
|
|
698
|
+
if (namespace) {
|
|
699
|
+
this.debug = createDebugger(namespace, className);
|
|
700
|
+
} else {
|
|
701
|
+
this.debug = createDebugger(className);
|
|
702
|
+
}
|
|
703
|
+
const noEnvironmentNamespace = Boolean(options.noEnvironmentNamespace);
|
|
704
|
+
if (noEnvironmentNamespace) this.debug = this.debug.withoutEnvNs();
|
|
705
|
+
this.ctorDebug = this.debug.withNs("constructor").withHash();
|
|
706
|
+
const noInstantiationMessage = Boolean(options.noInstantiationMessage);
|
|
707
|
+
if (!noInstantiationMessage)
|
|
708
|
+
this.ctorDebug(_Debuggable.INSTANTIATION_MESSAGE);
|
|
709
|
+
}
|
|
710
|
+
};
|
|
711
|
+
__name(_Debuggable, "Debuggable");
|
|
712
|
+
/**
|
|
713
|
+
* Instantiation message.
|
|
714
|
+
*
|
|
715
|
+
* @type {string}
|
|
716
|
+
*/
|
|
717
|
+
__publicField(_Debuggable, "INSTANTIATION_MESSAGE", "Instantiated.");
|
|
718
|
+
var Debuggable = _Debuggable;
|
|
719
|
+
|
|
722
720
|
// node_modules/@e22m4u/js-service/src/debuggable-service.js
|
|
723
721
|
var _DebuggableService = class _DebuggableService extends Debuggable {
|
|
724
722
|
/**
|
|
@@ -1098,7 +1096,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1098
1096
|
* Mongodb instance.
|
|
1099
1097
|
*
|
|
1100
1098
|
* @type {MongoClient}
|
|
1101
|
-
* @private
|
|
1102
1099
|
*/
|
|
1103
1100
|
_client;
|
|
1104
1101
|
/**
|
|
@@ -1113,14 +1110,13 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1113
1110
|
* Collections.
|
|
1114
1111
|
*
|
|
1115
1112
|
* @type {Map<any, any>}
|
|
1116
|
-
* @private
|
|
1117
1113
|
*/
|
|
1118
1114
|
_collections = /* @__PURE__ */ new Map();
|
|
1119
1115
|
/**
|
|
1120
1116
|
* Constructor.
|
|
1121
1117
|
*
|
|
1122
1118
|
* @param {ServiceContainer} container
|
|
1123
|
-
* @param settings
|
|
1119
|
+
* @param {object} settings
|
|
1124
1120
|
*/
|
|
1125
1121
|
constructor(container, settings) {
|
|
1126
1122
|
settings = Object.assign({}, DEFAULT_SETTINGS, settings || {});
|
|
@@ -1131,13 +1127,13 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1131
1127
|
super(container, settings);
|
|
1132
1128
|
const options = (0, import_js_repository3.selectObjectKeys)(this.settings, MONGODB_OPTION_NAMES);
|
|
1133
1129
|
const url = createMongodbUrl(this.settings);
|
|
1134
|
-
this._client = new
|
|
1130
|
+
this._client = new import_mongodb2.MongoClient(url, options);
|
|
1135
1131
|
}
|
|
1136
1132
|
/**
|
|
1137
1133
|
* Get id prop name.
|
|
1138
1134
|
*
|
|
1139
|
-
* @param modelName
|
|
1140
|
-
* @
|
|
1135
|
+
* @param {string} modelName
|
|
1136
|
+
* @returns {string}
|
|
1141
1137
|
*/
|
|
1142
1138
|
_getIdPropName(modelName) {
|
|
1143
1139
|
return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
|
|
@@ -1147,8 +1143,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1147
1143
|
/**
|
|
1148
1144
|
* Get id col name.
|
|
1149
1145
|
*
|
|
1150
|
-
* @param modelName
|
|
1151
|
-
* @
|
|
1146
|
+
* @param {string} modelName
|
|
1147
|
+
* @returns {string}
|
|
1152
1148
|
*/
|
|
1153
1149
|
_getIdColName(modelName) {
|
|
1154
1150
|
return this.getService(import_js_repository3.ModelDefinitionUtils).getPrimaryKeyAsColumnName(
|
|
@@ -1158,9 +1154,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1158
1154
|
/**
|
|
1159
1155
|
* Coerce id.
|
|
1160
1156
|
*
|
|
1161
|
-
* @param value
|
|
1157
|
+
* @param {*} value
|
|
1162
1158
|
* @returns {ObjectId|*}
|
|
1163
|
-
* @private
|
|
1164
1159
|
*/
|
|
1165
1160
|
_coerceId(value) {
|
|
1166
1161
|
if (value == null) return value;
|
|
@@ -1170,9 +1165,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1170
1165
|
/**
|
|
1171
1166
|
* Coerce date.
|
|
1172
1167
|
*
|
|
1173
|
-
* @param value
|
|
1168
|
+
* @param {Date|string|*} value
|
|
1174
1169
|
* @returns {Date|*}
|
|
1175
|
-
* @private
|
|
1176
1170
|
*/
|
|
1177
1171
|
_coerceDate(value) {
|
|
1178
1172
|
if (value == null) return value;
|
|
@@ -1186,7 +1180,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1186
1180
|
* @param {string} modelName
|
|
1187
1181
|
* @param {object} modelData
|
|
1188
1182
|
* @returns {object}
|
|
1189
|
-
* @private
|
|
1190
1183
|
*/
|
|
1191
1184
|
_toDatabase(modelName, modelData) {
|
|
1192
1185
|
const tableData = this.getService(
|
|
@@ -1216,7 +1209,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1216
1209
|
* @param {string} modelName
|
|
1217
1210
|
* @param {object} tableData
|
|
1218
1211
|
* @returns {object}
|
|
1219
|
-
* @private
|
|
1220
1212
|
*/
|
|
1221
1213
|
_fromDatabase(modelName, tableData) {
|
|
1222
1214
|
if ("_id" in tableData) {
|
|
@@ -1244,6 +1236,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1244
1236
|
* Get collection name by model name.
|
|
1245
1237
|
*
|
|
1246
1238
|
* @param {string} modelName
|
|
1239
|
+
* @returns {string}
|
|
1247
1240
|
*/
|
|
1248
1241
|
_getCollectionNameByModelName(modelName) {
|
|
1249
1242
|
const modelDef = this.getService(import_js_repository3.DefinitionRegistry).getModel(modelName);
|
|
@@ -1255,7 +1248,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1255
1248
|
*
|
|
1256
1249
|
* @param {string} modelName
|
|
1257
1250
|
* @returns {*}
|
|
1258
|
-
* @private
|
|
1259
1251
|
*/
|
|
1260
1252
|
_getCollection(modelName) {
|
|
1261
1253
|
let collection = this._collections.get(modelName);
|
|
@@ -1268,9 +1260,8 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1268
1260
|
/**
|
|
1269
1261
|
* Get id type.
|
|
1270
1262
|
*
|
|
1271
|
-
* @param modelName
|
|
1263
|
+
* @param {string} modelName
|
|
1272
1264
|
* @returns {string|*}
|
|
1273
|
-
* @private
|
|
1274
1265
|
*/
|
|
1275
1266
|
_getIdType(modelName) {
|
|
1276
1267
|
const utils = this.getService(import_js_repository3.ModelDefinitionUtils);
|
|
@@ -1283,7 +1274,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1283
1274
|
* @param {string} modelName
|
|
1284
1275
|
* @param {string} propName
|
|
1285
1276
|
* @returns {string}
|
|
1286
|
-
* @private
|
|
1287
1277
|
*/
|
|
1288
1278
|
_getColName(modelName, propName) {
|
|
1289
1279
|
if (!propName || typeof propName !== "string")
|
|
@@ -1308,7 +1298,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1308
1298
|
* @param {string} modelName
|
|
1309
1299
|
* @param {string} propsChain
|
|
1310
1300
|
* @returns {string}
|
|
1311
|
-
* @private
|
|
1312
1301
|
*/
|
|
1313
1302
|
_convertPropNamesChainToColNamesChain(modelName, propsChain) {
|
|
1314
1303
|
if (!modelName || typeof modelName !== "string")
|
|
@@ -1341,7 +1330,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1341
1330
|
* @param {string} modelName
|
|
1342
1331
|
* @param {string|string[]} fields
|
|
1343
1332
|
* @returns {Record<string, number>|undefined}
|
|
1344
|
-
* @private
|
|
1345
1333
|
*/
|
|
1346
1334
|
_buildProjection(modelName, fields) {
|
|
1347
1335
|
if (fields == null) return;
|
|
@@ -1368,7 +1356,6 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1368
1356
|
* @param {string} modelName
|
|
1369
1357
|
* @param {string|string[]} clause
|
|
1370
1358
|
* @returns {object|undefined}
|
|
1371
|
-
* @private
|
|
1372
1359
|
*/
|
|
1373
1360
|
_buildSort(modelName, clause) {
|
|
1374
1361
|
if (clause == null) return;
|
|
@@ -1403,8 +1390,7 @@ var _MongodbAdapter = class _MongodbAdapter extends import_js_repository3.Adapte
|
|
|
1403
1390
|
*
|
|
1404
1391
|
* @param {string} modelName
|
|
1405
1392
|
* @param {object} clause
|
|
1406
|
-
* @returns {object}
|
|
1407
|
-
* @private
|
|
1393
|
+
* @returns {object|undefined}
|
|
1408
1394
|
*/
|
|
1409
1395
|
_buildQuery(modelName, clause) {
|
|
1410
1396
|
if (clause == null) return;
|
package/eslint.config.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import globals from 'globals';
|
|
2
2
|
import eslintJs from '@eslint/js';
|
|
3
|
+
import eslintJsdocPlugin from 'eslint-plugin-jsdoc';
|
|
3
4
|
import eslintMochaPlugin from 'eslint-plugin-mocha';
|
|
5
|
+
import eslintImportPlugin from 'eslint-plugin-import';
|
|
4
6
|
import eslintPrettierConfig from 'eslint-config-prettier';
|
|
5
7
|
import eslintChaiExpectPlugin from 'eslint-plugin-chai-expect';
|
|
6
8
|
|
|
@@ -13,14 +15,26 @@ export default [{
|
|
|
13
15
|
},
|
|
14
16
|
},
|
|
15
17
|
plugins: {
|
|
18
|
+
'jsdoc': eslintJsdocPlugin,
|
|
16
19
|
'mocha': eslintMochaPlugin,
|
|
20
|
+
'import': eslintImportPlugin,
|
|
17
21
|
'chai-expect': eslintChaiExpectPlugin,
|
|
18
22
|
},
|
|
19
23
|
rules: {
|
|
20
24
|
...eslintJs.configs.recommended.rules,
|
|
21
25
|
...eslintPrettierConfig.rules,
|
|
26
|
+
...eslintImportPlugin.flatConfigs.recommended.rules,
|
|
22
27
|
...eslintMochaPlugin.configs.recommended.rules,
|
|
23
28
|
...eslintChaiExpectPlugin.configs['recommended-flat'].rules,
|
|
29
|
+
...eslintJsdocPlugin.configs['flat/recommended-error'].rules,
|
|
30
|
+
'no-duplicate-imports': 'error',
|
|
31
|
+
'import/export': 0,
|
|
32
|
+
'jsdoc/reject-any-type': 0,
|
|
33
|
+
'jsdoc/reject-function-type': 0,
|
|
34
|
+
'jsdoc/require-param-description': 0,
|
|
35
|
+
'jsdoc/require-returns-description': 0,
|
|
36
|
+
'jsdoc/require-property-description': 0,
|
|
37
|
+
'jsdoc/tag-lines': ['error', 'any', {startLines: 1}],
|
|
24
38
|
},
|
|
25
39
|
files: ['src/**/*.js'],
|
|
26
40
|
}];
|
package/jsconfig.json
CHANGED
package/mocha.setup.js
CHANGED
|
@@ -6,6 +6,6 @@ import chaiAsPromised from 'chai-as-promised';
|
|
|
6
6
|
process.env['NODE_ENV'] = 'test';
|
|
7
7
|
const dirname = url.fileURLToPath(new URL('.', import.meta.url));
|
|
8
8
|
const envFile = `${dirname}/${process.env['NODE_ENV']}.env`;
|
|
9
|
-
dotenv.config({path: envFile});
|
|
9
|
+
dotenv.config({path: envFile, quiet: true});
|
|
10
10
|
|
|
11
11
|
chai.use(chaiAsPromised);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@e22m4u/js-repository-mongodb-adapter",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"description": "MongoDB адаптер для @e22m4u/js-repository",
|
|
5
5
|
"author": "Mikhail Evstropov <e22m4u@yandex.ru>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,10 +13,10 @@
|
|
|
13
13
|
"Datasource",
|
|
14
14
|
"Relations"
|
|
15
15
|
],
|
|
16
|
-
"homepage": "https://
|
|
16
|
+
"homepage": "https://gitrepos.ru/e22m4u/js-repository-mongodb-adapter",
|
|
17
17
|
"repository": {
|
|
18
18
|
"type": "git",
|
|
19
|
-
"url": "git+https://
|
|
19
|
+
"url": "git+https://gitrepos.ru/e22m4u/js-repository-mongodb-adapter.git"
|
|
20
20
|
},
|
|
21
21
|
"type": "module",
|
|
22
22
|
"module": "./src/index.js",
|
|
@@ -34,20 +34,22 @@
|
|
|
34
34
|
"format": "prettier --write \"./src/**/*.js\"",
|
|
35
35
|
"test": "npm run lint && c8 --reporter=text-summary mocha --bail",
|
|
36
36
|
"test:coverage": "npm run lint && c8 --reporter=text mocha --bail",
|
|
37
|
-
"build:cjs": "rimraf ./dist/cjs && node
|
|
37
|
+
"build:cjs": "rimraf ./dist/cjs && node build-cjs.js",
|
|
38
38
|
"prepare": "husky"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@e22m4u/js-format": "~0.
|
|
41
|
+
"@e22m4u/js-format": "~0.3.1",
|
|
42
42
|
"mongodb": "6.20.0"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"@e22m4u/js-repository": "~0.
|
|
45
|
+
"@e22m4u/js-repository": "~0.8.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@commitlint/cli": "~20.1.0",
|
|
49
49
|
"@commitlint/config-conventional": "~20.0.0",
|
|
50
50
|
"@eslint/js": "~9.39.1",
|
|
51
|
+
"@types/chai": "~5.2.3",
|
|
52
|
+
"@types/mocha": "~10.0.10",
|
|
51
53
|
"c8": "~10.1.3",
|
|
52
54
|
"chai": "~6.2.1",
|
|
53
55
|
"chai-as-promised": "~8.0.2",
|
|
@@ -56,11 +58,13 @@
|
|
|
56
58
|
"eslint": "~9.39.1",
|
|
57
59
|
"eslint-config-prettier": "~10.1.8",
|
|
58
60
|
"eslint-plugin-chai-expect": "~3.1.0",
|
|
61
|
+
"eslint-plugin-import": "~2.32.0",
|
|
62
|
+
"eslint-plugin-jsdoc": "~61.4.1",
|
|
59
63
|
"eslint-plugin-mocha": "~11.2.0",
|
|
60
64
|
"globals": "~16.5.0",
|
|
61
65
|
"husky": "~9.1.7",
|
|
62
66
|
"mocha": "~11.7.5",
|
|
63
|
-
"prettier": "~3.
|
|
67
|
+
"prettier": "~3.7.4",
|
|
64
68
|
"rimraf": "~6.1.2"
|
|
65
69
|
}
|
|
66
70
|
}
|
package/src/mongodb-adapter.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/* eslint no-unused-vars: 0 */
|
|
2
|
-
import {ObjectId} from 'mongodb';
|
|
3
|
-
import {MongoClient} from 'mongodb';
|
|
2
|
+
import {ObjectId, MongoClient} from 'mongodb';
|
|
4
3
|
import {ServiceContainer} from '@e22m4u/js-service';
|
|
5
4
|
|
|
6
5
|
import {
|
|
@@ -149,7 +148,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
149
148
|
* Mongodb instance.
|
|
150
149
|
*
|
|
151
150
|
* @type {MongoClient}
|
|
152
|
-
* @private
|
|
153
151
|
*/
|
|
154
152
|
_client;
|
|
155
153
|
|
|
@@ -166,7 +164,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
166
164
|
* Collections.
|
|
167
165
|
*
|
|
168
166
|
* @type {Map<any, any>}
|
|
169
|
-
* @private
|
|
170
167
|
*/
|
|
171
168
|
_collections = new Map();
|
|
172
169
|
|
|
@@ -174,7 +171,7 @@ export class MongodbAdapter extends Adapter {
|
|
|
174
171
|
* Constructor.
|
|
175
172
|
*
|
|
176
173
|
* @param {ServiceContainer} container
|
|
177
|
-
* @param settings
|
|
174
|
+
* @param {object} settings
|
|
178
175
|
*/
|
|
179
176
|
constructor(container, settings) {
|
|
180
177
|
settings = Object.assign({}, DEFAULT_SETTINGS, settings || {});
|
|
@@ -191,8 +188,8 @@ export class MongodbAdapter extends Adapter {
|
|
|
191
188
|
/**
|
|
192
189
|
* Get id prop name.
|
|
193
190
|
*
|
|
194
|
-
* @param modelName
|
|
195
|
-
* @
|
|
191
|
+
* @param {string} modelName
|
|
192
|
+
* @returns {string}
|
|
196
193
|
*/
|
|
197
194
|
_getIdPropName(modelName) {
|
|
198
195
|
return this.getService(ModelDefinitionUtils).getPrimaryKeyAsPropertyName(
|
|
@@ -203,8 +200,8 @@ export class MongodbAdapter extends Adapter {
|
|
|
203
200
|
/**
|
|
204
201
|
* Get id col name.
|
|
205
202
|
*
|
|
206
|
-
* @param modelName
|
|
207
|
-
* @
|
|
203
|
+
* @param {string} modelName
|
|
204
|
+
* @returns {string}
|
|
208
205
|
*/
|
|
209
206
|
_getIdColName(modelName) {
|
|
210
207
|
return this.getService(ModelDefinitionUtils).getPrimaryKeyAsColumnName(
|
|
@@ -215,9 +212,8 @@ export class MongodbAdapter extends Adapter {
|
|
|
215
212
|
/**
|
|
216
213
|
* Coerce id.
|
|
217
214
|
*
|
|
218
|
-
* @param value
|
|
215
|
+
* @param {*} value
|
|
219
216
|
* @returns {ObjectId|*}
|
|
220
|
-
* @private
|
|
221
217
|
*/
|
|
222
218
|
_coerceId(value) {
|
|
223
219
|
if (value == null) return value;
|
|
@@ -228,9 +224,8 @@ export class MongodbAdapter extends Adapter {
|
|
|
228
224
|
/**
|
|
229
225
|
* Coerce date.
|
|
230
226
|
*
|
|
231
|
-
* @param value
|
|
227
|
+
* @param {Date|string|*} value
|
|
232
228
|
* @returns {Date|*}
|
|
233
|
-
* @private
|
|
234
229
|
*/
|
|
235
230
|
_coerceDate(value) {
|
|
236
231
|
if (value == null) return value;
|
|
@@ -245,7 +240,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
245
240
|
* @param {string} modelName
|
|
246
241
|
* @param {object} modelData
|
|
247
242
|
* @returns {object}
|
|
248
|
-
* @private
|
|
249
243
|
*/
|
|
250
244
|
_toDatabase(modelName, modelData) {
|
|
251
245
|
const tableData = this.getService(
|
|
@@ -279,7 +273,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
279
273
|
* @param {string} modelName
|
|
280
274
|
* @param {object} tableData
|
|
281
275
|
* @returns {object}
|
|
282
|
-
* @private
|
|
283
276
|
*/
|
|
284
277
|
_fromDatabase(modelName, tableData) {
|
|
285
278
|
if ('_id' in tableData) {
|
|
@@ -311,6 +304,7 @@ export class MongodbAdapter extends Adapter {
|
|
|
311
304
|
* Get collection name by model name.
|
|
312
305
|
*
|
|
313
306
|
* @param {string} modelName
|
|
307
|
+
* @returns {string}
|
|
314
308
|
*/
|
|
315
309
|
_getCollectionNameByModelName(modelName) {
|
|
316
310
|
const modelDef = this.getService(DefinitionRegistry).getModel(modelName);
|
|
@@ -327,7 +321,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
327
321
|
*
|
|
328
322
|
* @param {string} modelName
|
|
329
323
|
* @returns {*}
|
|
330
|
-
* @private
|
|
331
324
|
*/
|
|
332
325
|
_getCollection(modelName) {
|
|
333
326
|
let collection = this._collections.get(modelName);
|
|
@@ -343,9 +336,8 @@ export class MongodbAdapter extends Adapter {
|
|
|
343
336
|
/**
|
|
344
337
|
* Get id type.
|
|
345
338
|
*
|
|
346
|
-
* @param modelName
|
|
339
|
+
* @param {string} modelName
|
|
347
340
|
* @returns {string|*}
|
|
348
|
-
* @private
|
|
349
341
|
*/
|
|
350
342
|
_getIdType(modelName) {
|
|
351
343
|
const utils = this.getService(ModelDefinitionUtils);
|
|
@@ -359,7 +351,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
359
351
|
* @param {string} modelName
|
|
360
352
|
* @param {string} propName
|
|
361
353
|
* @returns {string}
|
|
362
|
-
* @private
|
|
363
354
|
*/
|
|
364
355
|
_getColName(modelName, propName) {
|
|
365
356
|
if (!propName || typeof propName !== 'string')
|
|
@@ -388,7 +379,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
388
379
|
* @param {string} modelName
|
|
389
380
|
* @param {string} propsChain
|
|
390
381
|
* @returns {string}
|
|
391
|
-
* @private
|
|
392
382
|
*/
|
|
393
383
|
_convertPropNamesChainToColNamesChain(modelName, propsChain) {
|
|
394
384
|
if (!modelName || typeof modelName !== 'string')
|
|
@@ -429,7 +419,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
429
419
|
* @param {string} modelName
|
|
430
420
|
* @param {string|string[]} fields
|
|
431
421
|
* @returns {Record<string, number>|undefined}
|
|
432
|
-
* @private
|
|
433
422
|
*/
|
|
434
423
|
_buildProjection(modelName, fields) {
|
|
435
424
|
if (fields == null) return;
|
|
@@ -458,7 +447,6 @@ export class MongodbAdapter extends Adapter {
|
|
|
458
447
|
* @param {string} modelName
|
|
459
448
|
* @param {string|string[]} clause
|
|
460
449
|
* @returns {object|undefined}
|
|
461
|
-
* @private
|
|
462
450
|
*/
|
|
463
451
|
_buildSort(modelName, clause) {
|
|
464
452
|
if (clause == null) return;
|
|
@@ -498,8 +486,7 @@ export class MongodbAdapter extends Adapter {
|
|
|
498
486
|
*
|
|
499
487
|
* @param {string} modelName
|
|
500
488
|
* @param {object} clause
|
|
501
|
-
* @returns {object}
|
|
502
|
-
* @private
|
|
489
|
+
* @returns {object|undefined}
|
|
503
490
|
*/
|
|
504
491
|
_buildQuery(modelName, clause) {
|
|
505
492
|
if (clause == null) return;
|
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
import {expect} from 'chai';
|
|
2
|
-
import {ObjectId} from 'mongodb';
|
|
3
|
-
import {MongoClient} from 'mongodb';
|
|
4
2
|
import {format} from '@e22m4u/js-format';
|
|
5
|
-
import {
|
|
3
|
+
import {ObjectId, MongoClient} from 'mongodb';
|
|
6
4
|
import {createMongodbUrl} from './utils/index.js';
|
|
7
5
|
import {MongodbAdapter} from './mongodb-adapter.js';
|
|
8
|
-
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
DataType,
|
|
9
|
+
DatabaseSchema,
|
|
10
|
+
AdapterRegistry,
|
|
11
|
+
InvalidOperatorValueError,
|
|
12
|
+
DEFAULT_PRIMARY_KEY_PROPERTY_NAME as DEF_PK,
|
|
13
|
+
} from '@e22m4u/js-repository';
|
|
12
14
|
|
|
13
15
|
const CONFIG = {
|
|
14
16
|
host: process.env.MONGODB_HOST || 'localhost',
|
|
@@ -19,6 +21,11 @@ const CONFIG = {
|
|
|
19
21
|
const MDB_CLIENT = new MongoClient(createMongodbUrl(CONFIG));
|
|
20
22
|
const ADAPTERS_STACK = [];
|
|
21
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Database schema factory.
|
|
26
|
+
*
|
|
27
|
+
* @returns {DatabaseSchema}
|
|
28
|
+
*/
|
|
22
29
|
function createSchema() {
|
|
23
30
|
const schema = new DatabaseSchema();
|
|
24
31
|
const adapter = new MongodbAdapter(schema.container, CONFIG);
|
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
import {InvalidArgumentError} from '@e22m4u/js-repository';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {object} MongoDBUrlOptions
|
|
5
|
+
* @property {string} [protocol]
|
|
6
|
+
* @property {string} [hostname]
|
|
7
|
+
* @property {string} [host]
|
|
8
|
+
* @property {number|string} [port]
|
|
9
|
+
* @property {string} [database]
|
|
10
|
+
* @property {string} [db]
|
|
11
|
+
* @property {string} [username]
|
|
12
|
+
* @property {string} [password]
|
|
13
|
+
* @property {string} [pass]
|
|
14
|
+
*/
|
|
15
|
+
|
|
3
16
|
/**
|
|
4
17
|
* Generate the mongodb URL from the options.
|
|
18
|
+
*
|
|
19
|
+
* @param {MongoDBUrlOptions} options
|
|
20
|
+
* @returns {string}
|
|
5
21
|
*/
|
|
6
22
|
export function createMongodbUrl(options = {}) {
|
|
7
23
|
if (!options || typeof options !== 'object' || Array.isArray(options))
|
package/src/utils/is-iso-date.js
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import {InvalidArgumentError} from '@e22m4u/js-repository';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @callback Transformer
|
|
5
|
+
* @param {*} value
|
|
6
|
+
* @returns {*}
|
|
7
|
+
*/
|
|
8
|
+
|
|
3
9
|
/**
|
|
4
10
|
* Transform values deep.
|
|
5
11
|
*
|
|
6
|
-
* @param value
|
|
7
|
-
* @param transformer
|
|
8
|
-
* @
|
|
12
|
+
* @param {*} value
|
|
13
|
+
* @param {Transformer} transformer
|
|
14
|
+
* @returns {*}
|
|
9
15
|
*/
|
|
10
16
|
export function transformValuesDeep(value, transformer) {
|
|
11
17
|
if (!transformer || typeof transformer !== 'function')
|