@lykmapipo/mongoose-common 0.39.0 → 0.40.0
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/.babelrc +12 -0
- package/.editorconfig +14 -0
- package/.eslintignore +1 -0
- package/.eslintrc +11 -0
- package/.prettierignore +0 -1
- package/CHANGELOG.md +16 -0
- package/LICENSE +2 -2
- package/README.md +22 -8
- package/SECURITY.md +3 -0
- package/es/index.js +1151 -0
- package/lib/index.js +1290 -0
- package/package.json +101 -38
- package/rollup.config.js +26 -0
- package/{lib → src}/.gitkeep +0 -0
- package/{index.js → src/index.js} +209 -226
- package/src/plugins/.gitkeep +0 -0
- package/src/plugins/error.plugin.js +183 -0
- package/src/plugins/path.plugin.js +39 -0
- package/{lib → src/plugins}/seed.plugin.js +120 -70
- package/.jsbeautifyrc +0 -7
- package/lib/error.plugin.js +0 -163
- package/lib/path.plugin.js +0 -43
@@ -1,5 +1,3 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
1
|
/**
|
4
2
|
* @module mongoose-common
|
5
3
|
* @name mongoose-common
|
@@ -22,16 +20,24 @@
|
|
22
20
|
* clear((error) => { ... });
|
23
21
|
* drop((error) => { ... });
|
24
22
|
* disconnect((error) => { ... });
|
25
|
-
*
|
26
23
|
*/
|
27
24
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
25
|
+
import {
|
26
|
+
forEach,
|
27
|
+
get,
|
28
|
+
head,
|
29
|
+
isEmpty,
|
30
|
+
isFunction,
|
31
|
+
isNull,
|
32
|
+
map,
|
33
|
+
tail,
|
34
|
+
toPlainObject,
|
35
|
+
} from 'lodash';
|
36
|
+
import { createHmac } from 'crypto';
|
37
|
+
import { compact, idOf, join, mergeObjects } from '@lykmapipo/common';
|
38
|
+
import mongoose from 'mongoose-valid8';
|
39
|
+
import { toObject } from 'mongoose/lib/utils';
|
40
|
+
import {
|
35
41
|
SCHEMA_OPTIONS,
|
36
42
|
SUB_SCHEMA_OPTIONS,
|
37
43
|
enableDebug,
|
@@ -53,23 +59,14 @@ const {
|
|
53
59
|
createSchema,
|
54
60
|
createVarySubSchema,
|
55
61
|
createModel,
|
56
|
-
}
|
57
|
-
|
62
|
+
} from '@lykmapipo/mongoose-connection';
|
63
|
+
import errorPlugin from './plugins/error.plugin';
|
64
|
+
import pathPlugin from './plugins/path.plugin';
|
65
|
+
import seedPlugin from './plugins/seed.plugin';
|
58
66
|
|
59
|
-
|
67
|
+
// set global mongoose promise
|
60
68
|
mongoose.Promise = global.Promise;
|
61
69
|
|
62
|
-
/**
|
63
|
-
* @description register jsonschema schema plugin
|
64
|
-
* @since 0.1.0
|
65
|
-
* @version 0.1.0
|
66
|
-
* @public
|
67
|
-
* @example
|
68
|
-
*
|
69
|
-
* const jsonSchema = User.jsonSchema();
|
70
|
-
*/
|
71
|
-
require('mongoose-schema-jsonschema')(mongoose); // TODO: ignore global
|
72
|
-
|
73
70
|
/**
|
74
71
|
* @name path
|
75
72
|
* @description register path schema plugin
|
@@ -80,9 +77,8 @@ require('mongoose-schema-jsonschema')(mongoose); // TODO: ignore global
|
|
80
77
|
*
|
81
78
|
* const name = User.path('name');
|
82
79
|
* //=> SchemaString { path: 'name', instance: 'String', ... }
|
83
|
-
*
|
84
80
|
*/
|
85
|
-
mongoose.plugin(
|
81
|
+
mongoose.plugin(pathPlugin); // TODO: ignore global
|
86
82
|
|
87
83
|
/**
|
88
84
|
* @name error
|
@@ -91,7 +87,7 @@ mongoose.plugin(require('./lib/path.plugin')); // TODO: ignore global
|
|
91
87
|
* @version 0.1.0
|
92
88
|
* @public
|
93
89
|
*/
|
94
|
-
mongoose.plugin(
|
90
|
+
mongoose.plugin(errorPlugin); // TODO: ignore global
|
95
91
|
|
96
92
|
/**
|
97
93
|
* @name seed
|
@@ -100,37 +96,47 @@ mongoose.plugin(require('./lib/error.plugin')); // TODO: ignore global
|
|
100
96
|
* @version 0.1.0
|
101
97
|
* @public
|
102
98
|
*/
|
103
|
-
mongoose.plugin(
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
99
|
+
mongoose.plugin(seedPlugin); // TODO: ignore global
|
100
|
+
|
101
|
+
// expose shortcuts
|
102
|
+
export const { STATES } = mongoose;
|
103
|
+
export const { Aggregate } = mongoose;
|
104
|
+
export const { Collection } = mongoose;
|
105
|
+
export const { Connection } = mongoose;
|
106
|
+
export const { Schema } = mongoose;
|
107
|
+
export const { SchemaType } = mongoose;
|
108
|
+
export const { SchemaTypes } = mongoose;
|
109
|
+
export const { VirtualType } = mongoose;
|
110
|
+
export const { Types } = mongoose;
|
111
|
+
export const MongooseTypes = mongoose.Types;
|
112
|
+
export const { Query } = mongoose;
|
113
|
+
export const MongooseError = mongoose.Error;
|
114
|
+
export const { CastError } = mongoose;
|
115
|
+
export const modelNames = () => mongoose.modelNames();
|
116
|
+
export const { GridFSBucket } = mongoose.mongo;
|
117
|
+
|
118
|
+
// schema types shortcuts
|
119
|
+
|
120
|
+
export const SchemaString = Schema.Types.String;
|
121
|
+
export const SchemaNumber = Schema.Types.Number;
|
122
|
+
export const SchemaBoolean = Schema.Types.Boolean;
|
123
|
+
export const { DocumentArray } = Schema.Types;
|
124
|
+
export const SchemaDocumentArray = Schema.Types.DocumentArray;
|
125
|
+
export const SubDocument = Schema.Types.Subdocument;
|
126
|
+
export const SchemaSubDocument = Schema.Types.Subdocument;
|
127
|
+
export const Embedded = SubDocument;
|
128
|
+
export const SchemaEmbedded = SubDocument;
|
129
|
+
export const SchemaArray = Schema.Types.Array;
|
130
|
+
export const SchemaBuffer = Schema.Types.Buffer;
|
131
|
+
export const SchemaDate = Schema.Types.Date;
|
132
|
+
export const { ObjectId } = Schema.Types;
|
133
|
+
export const SchemaObjectId = Schema.Types.ObjectId;
|
134
|
+
export const { Mixed } = Schema.Types;
|
135
|
+
export const SchemaMixed = Schema.Types.Mixed;
|
136
|
+
export const { Decimal128 } = Schema.Types;
|
137
|
+
export const SchemaDecimal = Decimal128;
|
138
|
+
export const SchemaDecimal128 = Decimal128;
|
139
|
+
export const SchemaMap = Schema.Types.Map;
|
134
140
|
|
135
141
|
/**
|
136
142
|
* @name LOOKUP_FIELDS
|
@@ -143,9 +149,8 @@ exports.Map = exports.SchemaMap = Schema.Types.Map;
|
|
143
149
|
*
|
144
150
|
* const { LOOKUP_FIELDS } = require('@lykmapipo/mongoose-common');
|
145
151
|
* //=> ['from', 'localField', 'foreignField', 'as']
|
146
|
-
*
|
147
152
|
*/
|
148
|
-
|
153
|
+
export const LOOKUP_FIELDS = ['from', 'localField', 'foreignField', 'as'];
|
149
154
|
|
150
155
|
/**
|
151
156
|
* @name SCHEMA_OPTIONS
|
@@ -158,9 +163,8 @@ exports.LOOKUP_FIELDS = ['from', 'localField', 'foreignField', 'as'];
|
|
158
163
|
*
|
159
164
|
* const { SCHEMA_OPTIONS } = require('@lykmapipo/mongoose-common');
|
160
165
|
* //=> { timestamps: true, ... }
|
161
|
-
*
|
162
166
|
*/
|
163
|
-
|
167
|
+
export { SCHEMA_OPTIONS };
|
164
168
|
|
165
169
|
/**
|
166
170
|
* @name SUB_SCHEMA_OPTIONS
|
@@ -174,7 +178,7 @@ exports.SCHEMA_OPTIONS = SCHEMA_OPTIONS;
|
|
174
178
|
* const { SUB_SCHEMA_OPTIONS } = require('@lykmapipo/mongoose-common');
|
175
179
|
* //=> { timestamps: false, ... }
|
176
180
|
*/
|
177
|
-
|
181
|
+
export { SUB_SCHEMA_OPTIONS };
|
178
182
|
|
179
183
|
/**
|
180
184
|
* @function isConnection
|
@@ -189,9 +193,8 @@ exports.SUB_SCHEMA_OPTIONS = SUB_SCHEMA_OPTIONS;
|
|
189
193
|
*
|
190
194
|
* isConnection(conn);
|
191
195
|
* //=> true
|
192
|
-
*
|
193
196
|
*/
|
194
|
-
|
197
|
+
export { isConnection };
|
195
198
|
|
196
199
|
/**
|
197
200
|
* @function isConnected
|
@@ -206,9 +209,8 @@ exports.isConnection = isConnection;
|
|
206
209
|
*
|
207
210
|
* isConnected(conn);
|
208
211
|
* //=> true
|
209
|
-
*
|
210
212
|
*/
|
211
|
-
|
213
|
+
export { isConnected };
|
212
214
|
|
213
215
|
/**
|
214
216
|
* @function isSchema
|
@@ -223,9 +225,8 @@ exports.isConnected = isConnected;
|
|
223
225
|
*
|
224
226
|
* isSchema(schema);
|
225
227
|
* //=> true
|
226
|
-
*
|
227
228
|
*/
|
228
|
-
|
229
|
+
export { isSchema };
|
229
230
|
|
230
231
|
/**
|
231
232
|
* @function isModel
|
@@ -240,9 +241,8 @@ exports.isSchema = isSchema;
|
|
240
241
|
*
|
241
242
|
* isModel(model);
|
242
243
|
* //=> true
|
243
|
-
*
|
244
244
|
*/
|
245
|
-
|
245
|
+
export { isModel };
|
246
246
|
|
247
247
|
/**
|
248
248
|
* @function isQuery
|
@@ -257,9 +257,8 @@ exports.isModel = isModel;
|
|
257
257
|
*
|
258
258
|
* isQuery(query);
|
259
259
|
* //=> true
|
260
|
-
*
|
261
260
|
*/
|
262
|
-
|
261
|
+
export { isQuery };
|
263
262
|
|
264
263
|
/**
|
265
264
|
* @function isAggregate
|
@@ -274,9 +273,8 @@ exports.isQuery = isQuery;
|
|
274
273
|
*
|
275
274
|
* isAggregate(query);
|
276
275
|
* //=> true
|
277
|
-
*
|
278
276
|
*/
|
279
|
-
|
277
|
+
export { isAggregate };
|
280
278
|
|
281
279
|
/**
|
282
280
|
* @function enableDebug
|
@@ -289,9 +287,8 @@ exports.isAggregate = isAggregate;
|
|
289
287
|
* @example
|
290
288
|
*
|
291
289
|
* enableDebug();
|
292
|
-
*
|
293
290
|
*/
|
294
|
-
|
291
|
+
export { enableDebug };
|
295
292
|
|
296
293
|
/**
|
297
294
|
* @function disableDebug
|
@@ -304,16 +301,15 @@ exports.enableDebug = enableDebug;
|
|
304
301
|
* @example
|
305
302
|
*
|
306
303
|
* disableDebug();
|
307
|
-
*
|
308
304
|
*/
|
309
|
-
|
305
|
+
export { disableDebug };
|
310
306
|
|
311
307
|
/**
|
312
308
|
* @function toCollectionName
|
313
309
|
* @name toCollectionName
|
314
310
|
* @description Produces a collection name of provided model name
|
315
|
-
* @param {
|
316
|
-
* @
|
311
|
+
* @param {string} modelName a model name
|
312
|
+
* @returns {string} a collection name
|
317
313
|
* @author lally elias <lallyelias87@mail.com>
|
318
314
|
* @since 0.8.0
|
319
315
|
* @version 0.1.0
|
@@ -322,11 +318,10 @@ exports.disableDebug = disableDebug;
|
|
322
318
|
*
|
323
319
|
* const collectionName = toCollectionName('User');
|
324
320
|
* //=> users
|
325
|
-
*
|
326
321
|
*/
|
327
|
-
|
322
|
+
export const toCollectionName = (modelName) => {
|
328
323
|
let collectionName = modelName;
|
329
|
-
if (!
|
324
|
+
if (!isEmpty(modelName)) {
|
330
325
|
collectionName = mongoose.pluralize()(modelName);
|
331
326
|
}
|
332
327
|
return collectionName;
|
@@ -338,6 +333,7 @@ exports.toCollectionName = (modelName) => {
|
|
338
333
|
* @description Check if provided value is an instance of ObjectId
|
339
334
|
* @param {Mixed} val value to check if its an ObjectId
|
340
335
|
* @author lally elias <lallyelias87@mail.com>
|
336
|
+
* @returns {boolean} whether a val is ObjectId instance
|
341
337
|
* @since 0.2.0
|
342
338
|
* @version 0.1.0
|
343
339
|
* @public
|
@@ -345,11 +341,10 @@ exports.toCollectionName = (modelName) => {
|
|
345
341
|
*
|
346
342
|
* isObjectId(val);
|
347
343
|
* //=> true
|
348
|
-
*
|
349
344
|
*/
|
350
|
-
|
351
|
-
const
|
352
|
-
return
|
345
|
+
export const isObjectId = (val) => {
|
346
|
+
const $isObjectId = val instanceof mongoose.Types.ObjectId;
|
347
|
+
return $isObjectId;
|
353
348
|
};
|
354
349
|
|
355
350
|
/**
|
@@ -358,6 +353,7 @@ exports.isObjectId = (val) => {
|
|
358
353
|
* @description Check if provided value is an instance of Map
|
359
354
|
* @param {Mixed} val value to check if its a Map
|
360
355
|
* @author lally elias <lallyelias87@mail.com>
|
356
|
+
* @returns {boolean} whether a val is Map instance
|
361
357
|
* @since 0.2.0
|
362
358
|
* @version 0.1.0
|
363
359
|
* @public
|
@@ -365,11 +361,10 @@ exports.isObjectId = (val) => {
|
|
365
361
|
*
|
366
362
|
* isMap(val);
|
367
363
|
* //=> true
|
368
|
-
*
|
369
364
|
*/
|
370
|
-
|
371
|
-
const
|
372
|
-
return
|
365
|
+
export const isMap = (val) => {
|
366
|
+
const $isMap = val instanceof mongoose.Types.Map;
|
367
|
+
return $isMap;
|
373
368
|
};
|
374
369
|
|
375
370
|
/**
|
@@ -378,6 +373,7 @@ exports.isMap = (val) => {
|
|
378
373
|
* @description Check if provided value is an instance of String schema type
|
379
374
|
* @param {Mixed} val value to check if its a String schema type
|
380
375
|
* @author lally elias <lallyelias87@mail.com>
|
376
|
+
* @returns {boolean} whether a val is String instance
|
381
377
|
* @since 0.10.0
|
382
378
|
* @version 0.1.0
|
383
379
|
* @public
|
@@ -386,9 +382,9 @@ exports.isMap = (val) => {
|
|
386
382
|
* isString(val);
|
387
383
|
* //=> true
|
388
384
|
*/
|
389
|
-
|
390
|
-
const
|
391
|
-
return
|
385
|
+
export const isString = (val) => {
|
386
|
+
const $isString = val instanceof Schema.Types.String;
|
387
|
+
return $isString;
|
392
388
|
};
|
393
389
|
|
394
390
|
/**
|
@@ -396,7 +392,7 @@ exports.isString = (val) => {
|
|
396
392
|
* @name isArraySchemaType
|
397
393
|
* @description check if schema type is array
|
398
394
|
* @param {SchemaType} val valid mongoose schema type
|
399
|
-
* @
|
395
|
+
* @returns {boolean} whether schema type is array
|
400
396
|
* @author lally elias <lallyelias87@mail.com>
|
401
397
|
* @since 0.16.0
|
402
398
|
* @version 0.1.0
|
@@ -405,9 +401,8 @@ exports.isString = (val) => {
|
|
405
401
|
*
|
406
402
|
* isArraySchemaType(val)
|
407
403
|
* //=> true
|
408
|
-
*
|
409
404
|
*/
|
410
|
-
|
405
|
+
export const isArraySchemaType = (val = {}) => {
|
411
406
|
const { $isMongooseArray = false, instance } = val;
|
412
407
|
const isArray =
|
413
408
|
val instanceof Schema.Types.Array ||
|
@@ -423,6 +418,7 @@ exports.isArraySchemaType = (val = {}) => {
|
|
423
418
|
* schema type
|
424
419
|
* @param {Mixed} val value to check if its a StringArray schema type
|
425
420
|
* @author lally elias <lallyelias87@mail.com>
|
421
|
+
* @returns {boolean} whether a val is String Array instance
|
426
422
|
* @since 0.11.0
|
427
423
|
* @version 0.1.0
|
428
424
|
* @public
|
@@ -430,14 +426,13 @@ exports.isArraySchemaType = (val = {}) => {
|
|
430
426
|
*
|
431
427
|
* isStringArray(val);
|
432
428
|
* //=> true
|
433
|
-
*
|
434
429
|
*/
|
435
|
-
|
436
|
-
const
|
430
|
+
export const isStringArray = (val) => {
|
431
|
+
const $isStringArray =
|
437
432
|
val &&
|
438
433
|
val instanceof Schema.Types.Array &&
|
439
434
|
val.caster instanceof Schema.Types.String;
|
440
|
-
return
|
435
|
+
return $isStringArray;
|
441
436
|
};
|
442
437
|
|
443
438
|
/**
|
@@ -446,6 +441,7 @@ exports.isStringArray = (val) => {
|
|
446
441
|
* @description Check if provided value is an instance of Number schema type
|
447
442
|
* @param {Mixed} val value to check if its a Number schema type
|
448
443
|
* @author lally elias <lallyelias87@mail.com>
|
444
|
+
* @returns {boolean} whether a val is Number instance
|
449
445
|
* @since 0.10.0
|
450
446
|
* @version 0.1.0
|
451
447
|
* @public
|
@@ -453,11 +449,10 @@ exports.isStringArray = (val) => {
|
|
453
449
|
*
|
454
450
|
* isNumber(<val>);
|
455
451
|
* //=> true
|
456
|
-
*
|
457
452
|
*/
|
458
|
-
|
459
|
-
const
|
460
|
-
return
|
453
|
+
export const isNumber = (val) => {
|
454
|
+
const $isNumber = val instanceof Schema.Types.Number;
|
455
|
+
return $isNumber;
|
461
456
|
};
|
462
457
|
|
463
458
|
/**
|
@@ -467,6 +462,7 @@ exports.isNumber = (val) => {
|
|
467
462
|
* schema type
|
468
463
|
* @param {Mixed} val value to check if its a NumberArray schema type
|
469
464
|
* @author lally elias <lallyelias87@mail.com>
|
465
|
+
* @returns {boolean} whether a val is Number Array instance
|
470
466
|
* @since 0.11.0
|
471
467
|
* @version 0.1.0
|
472
468
|
* @public
|
@@ -474,22 +470,21 @@ exports.isNumber = (val) => {
|
|
474
470
|
*
|
475
471
|
* isNumberArray(val);
|
476
472
|
* //=> true
|
477
|
-
*
|
478
473
|
*/
|
479
|
-
|
480
|
-
const
|
474
|
+
export const isNumberArray = (val) => {
|
475
|
+
const $isNumberArray =
|
481
476
|
val &&
|
482
477
|
val instanceof Schema.Types.Array &&
|
483
478
|
val.caster instanceof Schema.Types.Number;
|
484
|
-
return
|
479
|
+
return $isNumberArray;
|
485
480
|
};
|
486
481
|
|
487
482
|
/**
|
488
483
|
* @function isInstance
|
489
484
|
* @name isInstance
|
490
485
|
* @description check if object is valid mongoose model instance
|
491
|
-
* @param {
|
492
|
-
* @returns {
|
486
|
+
* @param {object} value valid object
|
487
|
+
* @returns {boolean} whether object is valid model instance
|
493
488
|
* @author lally elias <lallyelias87@mail.com>
|
494
489
|
* @since 0.4.0
|
495
490
|
* @version 0.2.0
|
@@ -498,14 +493,13 @@ exports.isNumberArray = (val) => {
|
|
498
493
|
*
|
499
494
|
* isInstance(val);
|
500
495
|
* //=> true
|
501
|
-
*
|
502
496
|
*/
|
503
|
-
|
497
|
+
export const isInstance = (value) => {
|
504
498
|
if (value) {
|
505
|
-
const
|
506
|
-
|
507
|
-
!
|
508
|
-
return
|
499
|
+
const $isInstance =
|
500
|
+
isFunction(get(value, 'toObject', null)) &&
|
501
|
+
!isNull(get(value, '$__', null));
|
502
|
+
return $isInstance;
|
509
503
|
}
|
510
504
|
return false;
|
511
505
|
};
|
@@ -513,8 +507,8 @@ exports.isInstance = (value) => {
|
|
513
507
|
/**
|
514
508
|
* @name copyInstance
|
515
509
|
* @description copy and return plain object of mongoose model instance
|
516
|
-
* @param {
|
517
|
-
* @returns {
|
510
|
+
* @param {object} value valid object
|
511
|
+
* @returns {object} plain object from mongoose model instance
|
518
512
|
* @author lally elias <lallyelias87@mail.com>
|
519
513
|
* @since 0.4.0
|
520
514
|
* @version 0.1.0
|
@@ -523,16 +517,15 @@ exports.isInstance = (value) => {
|
|
523
517
|
*
|
524
518
|
* const instance = copyInstance(val);
|
525
519
|
* //=> { ... }
|
526
|
-
*
|
527
520
|
*/
|
528
|
-
|
521
|
+
export const copyInstance = (value = {}) => mergeObjects(toObject(value));
|
529
522
|
|
530
523
|
/**
|
531
524
|
* @function schemaTypeOptionOf
|
532
525
|
* @name schemaTypeOptionOf
|
533
526
|
* @description obtain schema type options
|
534
527
|
* @param {SchemaType} schemaType valid mongoose schema type
|
535
|
-
* @
|
528
|
+
* @returns {object} schema type options
|
536
529
|
* @author lally elias <lallyelias87@mail.com>
|
537
530
|
* @since 0.14.0
|
538
531
|
* @version 0.1.0
|
@@ -541,15 +534,14 @@ exports.copyInstance = (value = {}) => mergeObjects(toObject(value));
|
|
541
534
|
*
|
542
535
|
* const options = schemaTypeOptionOf(schemaType)
|
543
536
|
* //=> { trim: true, ... }
|
544
|
-
*
|
545
537
|
*/
|
546
|
-
|
538
|
+
export const schemaTypeOptionOf = (schemaType = {}) => {
|
547
539
|
// grab options
|
548
540
|
const options = mergeObjects(
|
549
541
|
// grub schema caster options
|
550
|
-
|
542
|
+
toPlainObject(get(schemaType, 'caster.options')),
|
551
543
|
// grab direct schema options
|
552
|
-
|
544
|
+
toPlainObject(get(schemaType, 'options'))
|
553
545
|
);
|
554
546
|
// return options
|
555
547
|
return options;
|
@@ -559,8 +551,8 @@ exports.schemaTypeOptionOf = (schemaType = {}) => {
|
|
559
551
|
* @function collectionNameOf
|
560
552
|
* @name collectionNameOf
|
561
553
|
* @description obtain collection name of provided model name
|
562
|
-
* @param {
|
563
|
-
* @
|
554
|
+
* @param {string} modelName valid model name
|
555
|
+
* @returns {string} underlying collection of the model
|
564
556
|
* @author lally elias <lallyelias87@mail.com>
|
565
557
|
* @since 0.16.0
|
566
558
|
* @version 0.1.0
|
@@ -569,9 +561,8 @@ exports.schemaTypeOptionOf = (schemaType = {}) => {
|
|
569
561
|
*
|
570
562
|
* const collectionName = collectionNameOf('User');
|
571
563
|
* //=> 'users'
|
572
|
-
*
|
573
564
|
*/
|
574
|
-
|
565
|
+
export { collectionNameOf };
|
575
566
|
|
576
567
|
// TODO return default connection
|
577
568
|
// TODO return created connection
|
@@ -581,7 +572,7 @@ exports.collectionNameOf = collectionNameOf;
|
|
581
572
|
* @function connect
|
582
573
|
* @name connect
|
583
574
|
* @description Opens the default mongoose connection
|
584
|
-
* @param {
|
575
|
+
* @param {string} [url] valid mongodb conenction string. if not provided it
|
585
576
|
* will be obtained from process.env.MONGODB_URI or package name prefixed with
|
586
577
|
* current execution environment name
|
587
578
|
* @param {Function} done a callback to invoke on success or failure
|
@@ -593,9 +584,8 @@ exports.collectionNameOf = collectionNameOf;
|
|
593
584
|
*
|
594
585
|
* connect(done);
|
595
586
|
* connect(url, done);
|
596
|
-
*
|
597
587
|
*/
|
598
|
-
|
588
|
+
export { connect };
|
599
589
|
|
600
590
|
/**
|
601
591
|
* @function disconnect
|
@@ -609,9 +599,8 @@ exports.connect = connect;
|
|
609
599
|
* @example
|
610
600
|
*
|
611
601
|
* disconnect(done);
|
612
|
-
*
|
613
602
|
*/
|
614
|
-
|
603
|
+
export { disconnect };
|
615
604
|
|
616
605
|
/**
|
617
606
|
* @function clear
|
@@ -619,7 +608,7 @@ exports.disconnect = disconnect;
|
|
619
608
|
* @description Clear provided collection or all if none give
|
620
609
|
* @param {Connection} [connection] valid mongoose database connection. If not
|
621
610
|
* provide default connection will be used.
|
622
|
-
* @param {
|
611
|
+
* @param {string[] | string | ...string} modelNames name of models to clear
|
623
612
|
* @param {Function} done a callback to invoke on success or failure
|
624
613
|
* @author lally elias <lallyelias87@mail.com>
|
625
614
|
* @since 0.1.0
|
@@ -630,9 +619,8 @@ exports.disconnect = disconnect;
|
|
630
619
|
* clear(done);
|
631
620
|
* clear('User', done);
|
632
621
|
* clear('User', 'Profile', done);
|
633
|
-
*
|
634
622
|
*/
|
635
|
-
|
623
|
+
export { clear };
|
636
624
|
|
637
625
|
/**
|
638
626
|
* @function drop
|
@@ -649,15 +637,14 @@ exports.clear = clear;
|
|
649
637
|
* @example
|
650
638
|
*
|
651
639
|
* drop(done);
|
652
|
-
*
|
653
640
|
*/
|
654
|
-
|
641
|
+
export { drop };
|
655
642
|
|
656
643
|
/**
|
657
644
|
* @function model
|
658
645
|
* @name model
|
659
646
|
* @description Try obtain already registered or register new model safely.
|
660
|
-
* @param {
|
647
|
+
* @param {string} [modelName] valid model name
|
661
648
|
* @param {Schema} [schema] valid mongoose schema instance
|
662
649
|
* @param {Connection} [connection] valid mongoose database connection. If not
|
663
650
|
* provide default connection will be used.
|
@@ -669,16 +656,15 @@ exports.drop = drop;
|
|
669
656
|
*
|
670
657
|
* const User = model('User');
|
671
658
|
* const User = model('User', Schema);
|
672
|
-
*
|
673
659
|
*/
|
674
|
-
|
660
|
+
export { model };
|
675
661
|
|
676
662
|
/**
|
677
663
|
* @function eachPath
|
678
664
|
* @name eachPath
|
679
665
|
* @description iterate recursively on schema primitive paths and invoke
|
680
666
|
* provided iteratee function.
|
681
|
-
* @param {
|
667
|
+
* @param {object} schema valid instance of mongoose schema
|
682
668
|
* @param {Function} iteratee callback function invoked per each path found.
|
683
669
|
* The callback is passed the pathName, parentPath and schemaType as arguments
|
684
670
|
* on each iteration.
|
@@ -690,28 +676,34 @@ exports.model = model;
|
|
690
676
|
* @example
|
691
677
|
*
|
692
678
|
* eachPath(schema, (path, schemaType) => { ... });
|
693
|
-
*
|
694
679
|
*/
|
695
|
-
|
680
|
+
export const eachPath = (schema, iteratee) => {
|
681
|
+
/**
|
682
|
+
* @name iterateRecursive
|
683
|
+
* @description recursivily search for a schema path
|
684
|
+
* @param {string} pathName valid schema path name
|
685
|
+
* @param {object} schemaType valid schema type
|
686
|
+
* @param {string} parentPath parent schema path
|
687
|
+
*/
|
696
688
|
function iterateRecursive(pathName, schemaType, parentPath) {
|
697
689
|
// compute path name
|
698
|
-
const
|
690
|
+
const $path = compact([parentPath, pathName]).join('.');
|
699
691
|
|
700
692
|
// check if is sub schema
|
701
|
-
const isSchema =
|
702
|
-
schemaType.schema &&
|
693
|
+
const $isSchema =
|
694
|
+
schemaType.schema && isFunction(schemaType.schema.eachPath);
|
703
695
|
|
704
696
|
// iterate over sub schema
|
705
|
-
if (isSchema) {
|
697
|
+
if ($isSchema) {
|
706
698
|
const { schema: subSchema } = schemaType;
|
707
|
-
subSchema.eachPath(function iterateSubSchema(
|
708
|
-
iterateRecursive(
|
699
|
+
subSchema.eachPath(function iterateSubSchema($pathName, $schemaType) {
|
700
|
+
iterateRecursive($pathName, $schemaType, $path);
|
709
701
|
});
|
710
702
|
}
|
711
703
|
|
712
704
|
// invoke iteratee
|
713
705
|
else {
|
714
|
-
iteratee(
|
706
|
+
iteratee($path, schemaType);
|
715
707
|
}
|
716
708
|
}
|
717
709
|
|
@@ -725,7 +717,9 @@ exports.eachPath = (schema, iteratee) => {
|
|
725
717
|
* @function jsonSchema
|
726
718
|
* @name jsonSchema
|
727
719
|
* @description Produces valid json schema of all available models
|
720
|
+
* if `mongoose-schema-jsonschema` has been applied
|
728
721
|
* @author lally elias <lallyelias87@mail.com>
|
722
|
+
* @returns {object[]} models json schema
|
729
723
|
* @since 0.8.0
|
730
724
|
* @version 0.1.0
|
731
725
|
* @public
|
@@ -733,23 +727,22 @@ exports.eachPath = (schema, iteratee) => {
|
|
733
727
|
*
|
734
728
|
* const jsonSchema = jsonSchema();
|
735
729
|
* //=> {"user": {title: "User", type: "object", properties: {..} } }
|
736
|
-
*
|
737
730
|
*/
|
738
|
-
|
731
|
+
export const jsonSchema = () => {
|
739
732
|
// initialize schemas dictionary
|
740
|
-
|
733
|
+
const schemas = {};
|
741
734
|
// get model names
|
742
|
-
const modelNames = mongoose.modelNames();
|
735
|
+
const $modelNames = mongoose.modelNames();
|
743
736
|
// loop model names to get schemas
|
744
|
-
|
737
|
+
forEach($modelNames, function getJsonSchema(modelName) {
|
745
738
|
// get model
|
746
|
-
const Model =
|
739
|
+
const Model = model(modelName);
|
747
740
|
// collect model json schema
|
748
|
-
if (Model &&
|
741
|
+
if (Model && isFunction(Model.jsonSchema)) {
|
749
742
|
schemas[modelName] = Model.jsonSchema();
|
750
743
|
}
|
751
744
|
});
|
752
|
-
//return available schemas
|
745
|
+
// return available schemas
|
753
746
|
return schemas;
|
754
747
|
};
|
755
748
|
|
@@ -765,17 +758,16 @@ exports.jsonSchema = () => {
|
|
765
758
|
* @example
|
766
759
|
*
|
767
760
|
* syncIndexes(done);
|
768
|
-
*
|
769
761
|
*/
|
770
|
-
|
762
|
+
export { syncIndexes };
|
771
763
|
|
772
764
|
/**
|
773
765
|
* @function createSubSchema
|
774
766
|
* @name createSubSchema
|
775
767
|
* @description Create mongoose sub schema with no id and timestamp
|
776
|
-
* @param {
|
777
|
-
* @param {
|
778
|
-
* @
|
768
|
+
* @param {object} definition valid model schema definition
|
769
|
+
* @param {object} [optns] valid schema options
|
770
|
+
* @returns {object} valid mongoose sub schema
|
779
771
|
* @author lally elias <lallyelias87@mail.com>
|
780
772
|
* @since 0.21.0
|
781
773
|
* @version 0.3.0
|
@@ -783,18 +775,17 @@ exports.syncIndexes = syncIndexes;
|
|
783
775
|
* @example
|
784
776
|
*
|
785
777
|
* const User = createSubSchema({ name: { type: String } });
|
786
|
-
*
|
787
778
|
*/
|
788
|
-
|
779
|
+
export { createSubSchema };
|
789
780
|
|
790
781
|
/**
|
791
782
|
* @function createSchema
|
792
783
|
* @name createSchema
|
793
784
|
* @description Create mongoose schema with timestamps
|
794
|
-
* @param {
|
795
|
-
* @param {
|
785
|
+
* @param {object} definition valid model schema definition
|
786
|
+
* @param {object} [optns] valid schema options
|
796
787
|
* @param {...Function} [plugins] list of valid mongoose plugin to apply
|
797
|
-
* @
|
788
|
+
* @returns {object} valid mongoose schema
|
798
789
|
* @author lally elias <lallyelias87@mail.com>
|
799
790
|
* @since 0.23.0
|
800
791
|
* @version 0.1.0
|
@@ -802,21 +793,20 @@ exports.createSubSchema = createSubSchema;
|
|
802
793
|
* @example
|
803
794
|
*
|
804
795
|
* const User = createSchema({ name: { type: String } });
|
805
|
-
*
|
806
796
|
*/
|
807
|
-
|
797
|
+
export { createSchema };
|
808
798
|
|
809
799
|
/**
|
810
800
|
* @function createModel
|
811
801
|
* @name createModel
|
812
802
|
* @description Create and register mongoose model
|
813
|
-
* @param {
|
814
|
-
* @param {
|
815
|
-
* @param {
|
803
|
+
* @param {object} schema valid model schema definition
|
804
|
+
* @param {object} options valid model schema options
|
805
|
+
* @param {string} options.modelName valid model name
|
816
806
|
* @param {...Function} [plugins] list of valid mongoose plugin to apply
|
817
807
|
* @param {Connection} [connection] valid mongoose database connection. If not
|
818
808
|
* provide default connection will be used.
|
819
|
-
* @
|
809
|
+
* @returns {object} valid mongoose model
|
820
810
|
* @author lally elias <lallyelias87@mail.com>
|
821
811
|
* @since 0.21.0
|
822
812
|
* @version 0.2.0
|
@@ -829,17 +819,16 @@ exports.createSchema = createSchema;
|
|
829
819
|
* { name: 'User' },
|
830
820
|
* autopopulate, hidden
|
831
821
|
* );
|
832
|
-
*
|
833
822
|
*/
|
834
|
-
|
823
|
+
export { createModel };
|
835
824
|
|
836
825
|
/**
|
837
826
|
* @function createVarySubSchema
|
838
827
|
* @name createVarySubSchema
|
839
828
|
* @description Create sub schema with variable paths
|
840
|
-
* @param {
|
841
|
-
* @param {...
|
842
|
-
* @
|
829
|
+
* @param {object} optns valid schema type options
|
830
|
+
* @param {...object | ...string} paths variable paths to include on schema
|
831
|
+
* @returns {object} valid mongoose schema
|
843
832
|
* @author lally elias <lallyelias87@mail.com>
|
844
833
|
* @since 0.22.0
|
845
834
|
* @version 0.1.0
|
@@ -852,19 +841,18 @@ exports.createModel = createModel;
|
|
852
841
|
* { name: 'en': required: true },
|
853
842
|
* 'sw'
|
854
843
|
* );
|
855
|
-
*
|
856
844
|
*/
|
857
|
-
|
845
|
+
export { createVarySubSchema };
|
858
846
|
|
859
847
|
/**
|
860
848
|
* @function validationErrorFor
|
861
849
|
* @name validationErrorFor
|
862
850
|
* @description Create mongoose validation error for specified options
|
863
|
-
* @param {
|
864
|
-
* @param {
|
865
|
-
* @param {
|
866
|
-
* @param {
|
867
|
-
* @
|
851
|
+
* @param {object} optns valid error options
|
852
|
+
* @param {number | string} [optns.status] valid error status
|
853
|
+
* @param {number | string} [optns.code] valid error code
|
854
|
+
* @param {object} [optns.paths] paths with validator error properties
|
855
|
+
* @returns {object} valid instance of mongoose validation error
|
868
856
|
* @author lally elias <lallyelias87@mail.com>
|
869
857
|
* @since 0.24.0
|
870
858
|
* @version 0.1.0
|
@@ -877,9 +865,8 @@ exports.createVarySubSchema = createVarySubSchema;
|
|
877
865
|
* };
|
878
866
|
* const error = validationErrorFor({ status, paths });
|
879
867
|
* //=> error
|
880
|
-
*
|
881
868
|
*/
|
882
|
-
|
869
|
+
export const validationErrorFor = (optns) => {
|
883
870
|
// obtain options
|
884
871
|
const { status = 400, code = 400, paths = {} } = mergeObjects(optns);
|
885
872
|
|
@@ -887,12 +874,13 @@ exports.validationErrorFor = (optns) => {
|
|
887
874
|
const error = new mongoose.Error.ValidationError();
|
888
875
|
error.status = status;
|
889
876
|
error.code = code || status;
|
877
|
+
// eslint-disable-next-line no-underscore-dangle
|
890
878
|
error.message = error.message || error._message;
|
891
879
|
|
892
880
|
// attach path validator error
|
893
|
-
if (!
|
881
|
+
if (!isEmpty(paths)) {
|
894
882
|
const errors = {};
|
895
|
-
|
883
|
+
forEach(paths, (props, path) => {
|
896
884
|
let pathError = mergeObjects({ path }, props);
|
897
885
|
pathError = new mongoose.Error.ValidatorError(pathError);
|
898
886
|
errors[path] = pathError;
|
@@ -908,9 +896,9 @@ exports.validationErrorFor = (optns) => {
|
|
908
896
|
* @function areSameInstance
|
909
897
|
* @name areSameInstance
|
910
898
|
* @description check if given two mongoose model instances are same
|
911
|
-
* @param {
|
912
|
-
* @param {
|
913
|
-
* @returns {
|
899
|
+
* @param {object} a valid model instance
|
900
|
+
* @param {object} b valid model instance
|
901
|
+
* @returns {boolean} whether model instance are same
|
914
902
|
* @author lally elias <lallyelias87@mail.com>
|
915
903
|
* @since 0.31.0
|
916
904
|
* @version 0.1.0
|
@@ -918,9 +906,8 @@ exports.validationErrorFor = (optns) => {
|
|
918
906
|
* @example
|
919
907
|
*
|
920
908
|
* areSameInstance(a, a); //=> true
|
921
|
-
*
|
922
909
|
*/
|
923
|
-
|
910
|
+
export const areSameInstance = (a, b) => {
|
924
911
|
try {
|
925
912
|
const areSame = !!(a && b && a.equals(b));
|
926
913
|
return areSame;
|
@@ -933,9 +920,9 @@ exports.areSameInstance = (a, b) => {
|
|
933
920
|
* @function areSameObjectId
|
934
921
|
* @name areSameObjectId
|
935
922
|
* @description check if given two mongoose objectid are same
|
936
|
-
* @param {
|
937
|
-
* @param {
|
938
|
-
* @returns {
|
923
|
+
* @param {object} a valid object id
|
924
|
+
* @param {object} b valid object
|
925
|
+
* @returns {boolean} whether objectid's are same
|
939
926
|
* @author lally elias <lallyelias87@mail.com>
|
940
927
|
* @since 0.31.0
|
941
928
|
* @version 0.1.0
|
@@ -943,17 +930,16 @@ exports.areSameInstance = (a, b) => {
|
|
943
930
|
* @example
|
944
931
|
*
|
945
932
|
* areSameObjectId(a, a); //=> true
|
946
|
-
*
|
947
933
|
*/
|
948
|
-
|
934
|
+
export const areSameObjectId = (a, b) => {
|
949
935
|
try {
|
950
936
|
// grab actual ids
|
951
937
|
const idOfA = idOf(a) || a;
|
952
938
|
const idOfB = idOf(b) || b;
|
953
939
|
|
954
940
|
// convert to string
|
955
|
-
const idA =
|
956
|
-
const idB =
|
941
|
+
const idA = isObjectId(idOfA) ? idOfA.toString() : idOfA;
|
942
|
+
const idB = isObjectId(idOfB) ? idOfB.toString() : idOfB;
|
957
943
|
|
958
944
|
// check if are equal
|
959
945
|
const areSame = idA === idB;
|
@@ -967,8 +953,8 @@ exports.areSameObjectId = (a, b) => {
|
|
967
953
|
* @function toObjectIds
|
968
954
|
* @name toObjectIds
|
969
955
|
* @description convert given model instances into object ids
|
970
|
-
* @param {...
|
971
|
-
* @returns {
|
956
|
+
* @param {...object} instances valid model instances
|
957
|
+
* @returns {object[]} objectid's of model instances
|
972
958
|
* @author lally elias <lallyelias87@mail.com>
|
973
959
|
* @since 0.31.0
|
974
960
|
* @version 0.1.0
|
@@ -976,10 +962,9 @@ exports.areSameObjectId = (a, b) => {
|
|
976
962
|
* @example
|
977
963
|
*
|
978
964
|
* toObjectIds(a, b); //=> [ '5e90486301de071ca4ebc03d', ... ]
|
979
|
-
*
|
980
965
|
*/
|
981
|
-
|
982
|
-
const ids =
|
966
|
+
export const toObjectIds = (...instances) => {
|
967
|
+
const ids = map([...instances], (instance) => {
|
983
968
|
const id = idOf(instance) || instance;
|
984
969
|
return id;
|
985
970
|
});
|
@@ -990,8 +975,8 @@ exports.toObjectIds = (...instances) => {
|
|
990
975
|
* @function toObjectIdStrings
|
991
976
|
* @name toObjectIdStrings
|
992
977
|
* @description convert given model instances objectid's into strings
|
993
|
-
* @param {...
|
994
|
-
* @returns {
|
978
|
+
* @param {...object} instances valid model instances
|
979
|
+
* @returns {string[]} objectid's as strings
|
995
980
|
* @author lally elias <lallyelias87@mail.com>
|
996
981
|
* @since 0.31.0
|
997
982
|
* @version 0.1.0
|
@@ -999,12 +984,11 @@ exports.toObjectIds = (...instances) => {
|
|
999
984
|
* @example
|
1000
985
|
*
|
1001
986
|
* toObjectIdStrings(a, b); //=> [ '5e90486301de071ca4ebc03d', ... ]
|
1002
|
-
*
|
1003
987
|
*/
|
1004
|
-
|
1005
|
-
const ids =
|
1006
|
-
const idStrings =
|
1007
|
-
const idString =
|
988
|
+
export const toObjectIdStrings = (...instances) => {
|
989
|
+
const ids = toObjectIds(...instances);
|
990
|
+
const idStrings = map([...ids], (id) => {
|
991
|
+
const idString = isObjectId(id) ? id.toString() : id;
|
1008
992
|
return idString;
|
1009
993
|
});
|
1010
994
|
return idStrings;
|
@@ -1014,9 +998,9 @@ exports.toObjectIdStrings = (...instances) => {
|
|
1014
998
|
* @function objectIdFor
|
1015
999
|
* @name objectIdFor
|
1016
1000
|
* @description create a unique objectid of a given model values
|
1017
|
-
* @param {...
|
1018
|
-
* @param {...
|
1019
|
-
* @returns {
|
1001
|
+
* @param {...string} modelName valid model name
|
1002
|
+
* @param {...string} parts values to generate object id for
|
1003
|
+
* @returns {object} valid objectid
|
1020
1004
|
* @author lally elias <lallyelias87@mail.com>
|
1021
1005
|
* @since 0.36.0
|
1022
1006
|
* @version 0.1.0
|
@@ -1025,15 +1009,14 @@ exports.toObjectIdStrings = (...instances) => {
|
|
1025
1009
|
*
|
1026
1010
|
* objectIdFor('Party', 'TZ-0101');
|
1027
1011
|
* //=> '5e90486301de071ca4ebc03d'
|
1028
|
-
*
|
1029
1012
|
*/
|
1030
|
-
|
1013
|
+
export const objectIdFor = (modelName, ...parts) => {
|
1031
1014
|
// ensure parts
|
1032
|
-
const values = compact([].concat(
|
1015
|
+
const values = compact([].concat(modelName).concat(...parts));
|
1033
1016
|
|
1034
1017
|
// ensure secret & message
|
1035
|
-
const secret =
|
1036
|
-
const data = join(
|
1018
|
+
const secret = head(values);
|
1019
|
+
const data = join(tail(values), ':');
|
1037
1020
|
|
1038
1021
|
// generate 24-byte hex hash
|
1039
1022
|
const hash = createHmac('md5', secret)
|