@warlock.js/cascade 1.0.4 → 1.0.5
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/cjs/commands/create-new-app/get-app-path.d.ts +2 -0
- package/cjs/commands/create-new-app/get-app-path.d.ts.map +1 -0
- package/cjs/commands/create-new-app/get-app-path.js +8 -0
- package/cjs/commands/create-new-app/get-app-path.js.map +1 -0
- package/cjs/commands/create-new-app/index.d.ts +2 -0
- package/cjs/commands/create-new-app/index.d.ts.map +1 -0
- package/cjs/commands/create-new-app/index.js +44 -0
- package/cjs/commands/create-new-app/index.js.map +1 -0
- package/cjs/commands/create-new-app/select-app-type.d.ts +2 -0
- package/cjs/commands/create-new-app/select-app-type.d.ts.map +1 -0
- package/cjs/commands/create-new-app/types.d.ts +9 -0
- package/cjs/commands/create-new-app/types.d.ts.map +1 -0
- package/cjs/commands/create-warlock-app/index.d.ts +3 -0
- package/cjs/commands/create-warlock-app/index.d.ts.map +1 -0
- package/cjs/commands/create-warlock-app/index.js +18 -0
- package/cjs/commands/create-warlock-app/index.js.map +1 -0
- package/cjs/helpers/app.d.ts +51 -0
- package/cjs/helpers/app.d.ts.map +1 -0
- package/cjs/helpers/app.js +127 -0
- package/cjs/helpers/app.js.map +1 -0
- package/cjs/helpers/exec.d.ts +10 -0
- package/cjs/helpers/exec.d.ts.map +1 -0
- package/cjs/helpers/exec.js +69 -0
- package/cjs/helpers/exec.js.map +1 -0
- package/cjs/helpers/package-manager.d.ts +6 -0
- package/cjs/helpers/package-manager.d.ts.map +1 -0
- package/cjs/helpers/package-manager.js +22 -0
- package/cjs/helpers/package-manager.js.map +1 -0
- package/cjs/helpers/paths.d.ts +4 -0
- package/cjs/helpers/paths.d.ts.map +1 -0
- package/cjs/helpers/paths.js +8 -0
- package/cjs/helpers/paths.js.map +1 -0
- package/cjs/helpers/project-builder-helpers.d.ts +7 -0
- package/cjs/helpers/project-builder-helpers.d.ts.map +1 -0
- package/cjs/helpers/project-builder-helpers.js +44 -0
- package/cjs/helpers/project-builder-helpers.js.map +1 -0
- package/cjs/index.d.ts +1 -11
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +5 -1
- package/cjs/index.js.map +1 -1
- package/esm/blueprint/blueprint.d.ts +2 -2
- package/esm/blueprint/blueprint.d.ts.map +1 -1
- package/esm/blueprint/blueprint.js +2 -0
- package/esm/blueprint/blueprint.js.map +1 -1
- package/esm/migration/migrate.d.ts.map +1 -1
- package/esm/migration/migrate.js +2 -6
- package/esm/migration/migrate.js.map +1 -1
- package/esm/migration/migration-office.d.ts +3 -3
- package/esm/migration/migration-office.d.ts.map +1 -1
- package/esm/migration/migration-office.js +15 -1
- package/esm/migration/migration-office.js.map +1 -1
- package/esm/migration/types.d.ts +10 -0
- package/esm/migration/types.d.ts.map +1 -1
- package/esm/model/ModelAggregate.js +1 -1
- package/esm/model/ModelAggregate.js.map +1 -1
- package/esm/model/base-model.d.ts +2 -2
- package/esm/model/base-model.d.ts.map +1 -1
- package/esm/model/base-model.js +14 -4
- package/esm/model/base-model.js.map +1 -1
- package/esm/model/model.d.ts +13 -2
- package/esm/model/model.d.ts.map +1 -1
- package/esm/model/model.js +41 -3
- package/esm/model/model.js.map +1 -1
- package/esm/types.d.ts +30 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/utils/deep-diff.d.ts +9 -0
- package/esm/utils/deep-diff.d.ts.map +1 -0
- package/esm/utils/deep-diff.js +42 -0
- package/esm/utils/deep-diff.js.map +1 -0
- package/package.json +1 -1
- package/cjs/aggregate/DeselectPipeline.d.ts +0 -10
- package/cjs/aggregate/DeselectPipeline.d.ts.map +0 -1
- package/cjs/aggregate/DeselectPipeline.js +0 -17
- package/cjs/aggregate/DeselectPipeline.js.map +0 -1
- package/cjs/aggregate/GeoNearPipeline.d.ts +0 -34
- package/cjs/aggregate/GeoNearPipeline.d.ts.map +0 -1
- package/cjs/aggregate/GeoNearPipeline.js +0 -44
- package/cjs/aggregate/GeoNearPipeline.js.map +0 -1
- package/cjs/aggregate/GroupByPipeline.d.ts +0 -12
- package/cjs/aggregate/GroupByPipeline.d.ts.map +0 -1
- package/cjs/aggregate/GroupByPipeline.js +0 -28
- package/cjs/aggregate/GroupByPipeline.js.map +0 -1
- package/cjs/aggregate/LimitPipeline.d.ts +0 -10
- package/cjs/aggregate/LimitPipeline.d.ts.map +0 -1
- package/cjs/aggregate/LimitPipeline.js +0 -14
- package/cjs/aggregate/LimitPipeline.js.map +0 -1
- package/cjs/aggregate/LookupPipeline.d.ts +0 -19
- package/cjs/aggregate/LookupPipeline.d.ts.map +0 -1
- package/cjs/aggregate/LookupPipeline.js +0 -19
- package/cjs/aggregate/LookupPipeline.js.map +0 -1
- package/cjs/aggregate/OrWherePipeline.d.ts +0 -23
- package/cjs/aggregate/OrWherePipeline.d.ts.map +0 -1
- package/cjs/aggregate/OrWherePipeline.js +0 -42
- package/cjs/aggregate/OrWherePipeline.js.map +0 -1
- package/cjs/aggregate/SelectPipeline.d.ts +0 -10
- package/cjs/aggregate/SelectPipeline.d.ts.map +0 -1
- package/cjs/aggregate/SelectPipeline.js +0 -26
- package/cjs/aggregate/SelectPipeline.js.map +0 -1
- package/cjs/aggregate/SkipPipeline.d.ts +0 -10
- package/cjs/aggregate/SkipPipeline.d.ts.map +0 -1
- package/cjs/aggregate/SkipPipeline.js +0 -14
- package/cjs/aggregate/SkipPipeline.js.map +0 -1
- package/cjs/aggregate/SortByPipeline.d.ts +0 -10
- package/cjs/aggregate/SortByPipeline.d.ts.map +0 -1
- package/cjs/aggregate/SortByPipeline.js +0 -18
- package/cjs/aggregate/SortByPipeline.js.map +0 -1
- package/cjs/aggregate/SortPipeline.d.ts +0 -11
- package/cjs/aggregate/SortPipeline.d.ts.map +0 -1
- package/cjs/aggregate/SortPipeline.js +0 -18
- package/cjs/aggregate/SortPipeline.js.map +0 -1
- package/cjs/aggregate/SortRandomPipeline.d.ts +0 -10
- package/cjs/aggregate/SortRandomPipeline.d.ts.map +0 -1
- package/cjs/aggregate/SortRandomPipeline.js +0 -16
- package/cjs/aggregate/SortRandomPipeline.js.map +0 -1
- package/cjs/aggregate/UnwindPipeline.d.ts +0 -14
- package/cjs/aggregate/UnwindPipeline.d.ts.map +0 -1
- package/cjs/aggregate/UnwindPipeline.js +0 -19
- package/cjs/aggregate/UnwindPipeline.js.map +0 -1
- package/cjs/aggregate/WhereExpression.d.ts +0 -17
- package/cjs/aggregate/WhereExpression.d.ts.map +0 -1
- package/cjs/aggregate/WhereExpression.js +0 -149
- package/cjs/aggregate/WhereExpression.js.map +0 -1
- package/cjs/aggregate/WhereExpressionPipeline.d.ts +0 -10
- package/cjs/aggregate/WhereExpressionPipeline.d.ts.map +0 -1
- package/cjs/aggregate/WhereExpressionPipeline.js +0 -15
- package/cjs/aggregate/WhereExpressionPipeline.js.map +0 -1
- package/cjs/aggregate/WherePipeline.d.ts +0 -13
- package/cjs/aggregate/WherePipeline.d.ts.map +0 -1
- package/cjs/aggregate/WherePipeline.js +0 -12
- package/cjs/aggregate/WherePipeline.js.map +0 -1
- package/cjs/aggregate/aggregate.d.ts +0 -358
- package/cjs/aggregate/aggregate.d.ts.map +0 -1
- package/cjs/aggregate/aggregate.js +0 -680
- package/cjs/aggregate/aggregate.js.map +0 -1
- package/cjs/aggregate/expressions.d.ts +0 -361
- package/cjs/aggregate/expressions.d.ts.map +0 -1
- package/cjs/aggregate/expressions.js +0 -536
- package/cjs/aggregate/expressions.js.map +0 -1
- package/cjs/aggregate/index.d.ts +0 -21
- package/cjs/aggregate/index.d.ts.map +0 -1
- package/cjs/aggregate/parsePipelines.d.ts +0 -5
- package/cjs/aggregate/parsePipelines.d.ts.map +0 -1
- package/cjs/aggregate/parsePipelines.js +0 -6
- package/cjs/aggregate/parsePipelines.js.map +0 -1
- package/cjs/aggregate/pipeline.d.ts +0 -31
- package/cjs/aggregate/pipeline.d.ts.map +0 -1
- package/cjs/aggregate/pipeline.js +0 -42
- package/cjs/aggregate/pipeline.js.map +0 -1
- package/cjs/aggregate/types.d.ts +0 -8
- package/cjs/aggregate/types.d.ts.map +0 -1
- package/cjs/aggregate/types.js +0 -31
- package/cjs/aggregate/types.js.map +0 -1
- package/cjs/blueprint/blueprint-column.d.ts +0 -82
- package/cjs/blueprint/blueprint-column.d.ts.map +0 -1
- package/cjs/blueprint/blueprint.d.ts +0 -162
- package/cjs/blueprint/blueprint.d.ts.map +0 -1
- package/cjs/blueprint/blueprint.js +0 -338
- package/cjs/blueprint/blueprint.js.map +0 -1
- package/cjs/blueprint/index.d.ts +0 -3
- package/cjs/blueprint/index.d.ts.map +0 -1
- package/cjs/blueprint/model-blueprint.d.ts +0 -15
- package/cjs/blueprint/model-blueprint.d.ts.map +0 -1
- package/cjs/blueprint/model-blueprint.js +0 -16
- package/cjs/blueprint/model-blueprint.js.map +0 -1
- package/cjs/casts/arrayOf.d.ts +0 -5
- package/cjs/casts/arrayOf.d.ts.map +0 -1
- package/cjs/casts/arrayOf.js +0 -18
- package/cjs/casts/arrayOf.js.map +0 -1
- package/cjs/casts/cast-email.d.ts +0 -2
- package/cjs/casts/cast-email.d.ts.map +0 -1
- package/cjs/casts/cast-email.js +0 -5
- package/cjs/casts/cast-email.js.map +0 -1
- package/cjs/casts/castModel.d.ts +0 -3
- package/cjs/casts/castModel.d.ts.map +0 -1
- package/cjs/casts/castModel.js +0 -49
- package/cjs/casts/castModel.js.map +0 -1
- package/cjs/casts/expiresAfter.d.ts +0 -3
- package/cjs/casts/expiresAfter.d.ts.map +0 -1
- package/cjs/casts/expiresAfter.js +0 -5
- package/cjs/casts/expiresAfter.js.map +0 -1
- package/cjs/casts/index.d.ts +0 -9
- package/cjs/casts/index.d.ts.map +0 -1
- package/cjs/casts/oneOf.d.ts +0 -3
- package/cjs/casts/oneOf.d.ts.map +0 -1
- package/cjs/casts/oneOf.js +0 -22
- package/cjs/casts/oneOf.js.map +0 -1
- package/cjs/casts/random-string.d.ts +0 -2
- package/cjs/casts/random-string.d.ts.map +0 -1
- package/cjs/casts/random-string.js +0 -5
- package/cjs/casts/random-string.js.map +0 -1
- package/cjs/casts/randomInteger.d.ts +0 -2
- package/cjs/casts/randomInteger.d.ts.map +0 -1
- package/cjs/casts/randomInteger.js +0 -7
- package/cjs/casts/randomInteger.js.map +0 -1
- package/cjs/casts/shapedArray.d.ts +0 -26
- package/cjs/casts/shapedArray.d.ts.map +0 -1
- package/cjs/casts/shapedArray.js +0 -100
- package/cjs/casts/shapedArray.js.map +0 -1
- package/cjs/config.d.ts +0 -4
- package/cjs/config.d.ts.map +0 -1
- package/cjs/config.js +0 -10
- package/cjs/config.js.map +0 -1
- package/cjs/connection.d.ts +0 -45
- package/cjs/connection.d.ts.map +0 -1
- package/cjs/connection.js +0 -98
- package/cjs/connection.js.map +0 -1
- package/cjs/database.d.ts +0 -49
- package/cjs/database.d.ts.map +0 -1
- package/cjs/database.js +0 -85
- package/cjs/database.js.map +0 -1
- package/cjs/migration/index.d.ts +0 -4
- package/cjs/migration/index.d.ts.map +0 -1
- package/cjs/migration/migrate.d.ts +0 -13
- package/cjs/migration/migrate.d.ts.map +0 -1
- package/cjs/migration/migrate.js +0 -76
- package/cjs/migration/migrate.js.map +0 -1
- package/cjs/migration/migration-office.d.ts +0 -21
- package/cjs/migration/migration-office.d.ts.map +0 -1
- package/cjs/migration/migration-office.js +0 -26
- package/cjs/migration/migration-office.js.map +0 -1
- package/cjs/migration/migration-officer.d.ts +0 -33
- package/cjs/migration/migration-officer.d.ts.map +0 -1
- package/cjs/migration/migration-officer.js +0 -55
- package/cjs/migration/migration-officer.js.map +0 -1
- package/cjs/migration/types.d.ts +0 -24
- package/cjs/migration/types.d.ts.map +0 -1
- package/cjs/model/ModelAggregate.d.ts +0 -84
- package/cjs/model/ModelAggregate.d.ts.map +0 -1
- package/cjs/model/ModelAggregate.js +0 -164
- package/cjs/model/ModelAggregate.js.map +0 -1
- package/cjs/model/ModelSync.d.ts +0 -74
- package/cjs/model/ModelSync.d.ts.map +0 -1
- package/cjs/model/ModelSync.js +0 -215
- package/cjs/model/ModelSync.js.map +0 -1
- package/cjs/model/RelationshipWithMany.d.ts +0 -24
- package/cjs/model/RelationshipWithMany.d.ts.map +0 -1
- package/cjs/model/RelationshipWithMany.js +0 -42
- package/cjs/model/RelationshipWithMany.js.map +0 -1
- package/cjs/model/base-model.d.ts +0 -155
- package/cjs/model/base-model.d.ts.map +0 -1
- package/cjs/model/base-model.js +0 -220
- package/cjs/model/base-model.js.map +0 -1
- package/cjs/model/crud-model.d.ts +0 -117
- package/cjs/model/crud-model.d.ts.map +0 -1
- package/cjs/model/crud-model.js +0 -352
- package/cjs/model/crud-model.js.map +0 -1
- package/cjs/model/index.d.ts +0 -10
- package/cjs/model/index.d.ts.map +0 -1
- package/cjs/model/joinable.d.ts +0 -87
- package/cjs/model/joinable.d.ts.map +0 -1
- package/cjs/model/joinable.js +0 -155
- package/cjs/model/joinable.js.map +0 -1
- package/cjs/model/master-mind.d.ts +0 -44
- package/cjs/model/master-mind.d.ts.map +0 -1
- package/cjs/model/master-mind.js +0 -129
- package/cjs/model/master-mind.js.map +0 -1
- package/cjs/model/model-events.d.ts +0 -57
- package/cjs/model/model-events.d.ts.map +0 -1
- package/cjs/model/model-events.js +0 -102
- package/cjs/model/model-events.js.map +0 -1
- package/cjs/model/model.d.ts +0 -390
- package/cjs/model/model.d.ts.map +0 -1
- package/cjs/model/model.js +0 -990
- package/cjs/model/model.js.map +0 -1
- package/cjs/model/types.d.ts +0 -113
- package/cjs/model/types.d.ts.map +0 -1
- package/cjs/model/types.js +0 -11
- package/cjs/model/types.js.map +0 -1
- package/cjs/query/index.d.ts +0 -2
- package/cjs/query/index.d.ts.map +0 -1
- package/cjs/query/query.d.ts +0 -228
- package/cjs/query/query.d.ts.map +0 -1
- package/cjs/query/query.js +0 -675
- package/cjs/query/query.js.map +0 -1
- package/cjs/query/types.d.ts +0 -56
- package/cjs/query/types.d.ts.map +0 -1
- package/cjs/types.d.ts +0 -32
- package/cjs/types.d.ts.map +0 -1
- package/cjs/utils/connectToDatabase.d.ts +0 -3
- package/cjs/utils/connectToDatabase.d.ts.map +0 -1
- package/cjs/utils/connectToDatabase.js +0 -3
- package/cjs/utils/connectToDatabase.js.map +0 -1
- package/cjs/utils/dropAllDatabaseIndexes.d.ts +0 -2
- package/cjs/utils/dropAllDatabaseIndexes.d.ts.map +0 -1
- package/cjs/utils/dropAllDatabaseIndexes.js +0 -10
- package/cjs/utils/dropAllDatabaseIndexes.js.map +0 -1
- package/cjs/utils/index.d.ts +0 -5
- package/cjs/utils/index.d.ts.map +0 -1
- package/cjs/utils/joinable-proxy.d.ts +0 -3
- package/cjs/utils/joinable-proxy.d.ts.map +0 -1
- package/cjs/utils/joinable-proxy.js +0 -14
- package/cjs/utils/joinable-proxy.js.map +0 -1
- package/cjs/utils/listDatabaseIndexes.d.ts +0 -4
- package/cjs/utils/listDatabaseIndexes.d.ts.map +0 -1
- package/cjs/utils/listDatabaseIndexes.js +0 -101
- package/cjs/utils/listDatabaseIndexes.js.map +0 -1
- package/cjs/utils/onceConnected.d.ts +0 -2
- package/cjs/utils/onceConnected.d.ts.map +0 -1
- package/cjs/utils/onceConnected.js +0 -8
- package/cjs/utils/onceConnected.js.map +0 -1
package/cjs/model/model.js
DELETED
|
@@ -1,990 +0,0 @@
|
|
|
1
|
-
'use strict';var reinforcements=require('@mongez/reinforcements'),supportiveIs=require('@mongez/supportive-is'),timeWizard=require('@mongez/time-wizard'),dayjs=require('dayjs'),mongodb=require('mongodb'),castModel=require('../casts/castModel.js'),oneOf=require('../casts/oneOf.js'),joinableProxy=require('../utils/joinable-proxy.js'),ModelAggregate=require('./ModelAggregate.js'),ModelSync=require('./ModelSync.js'),RelationshipWithMany=require('./RelationshipWithMany.js'),crudModel=require('./crud-model.js'),joinable=require('./joinable.js'),types=require('./types.js');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var dayjs__default=/*#__PURE__*/_interopDefault(dayjs);class Model
|
|
2
|
-
// <
|
|
3
|
-
// ModelDocument extends Document = any,
|
|
4
|
-
// ModelSchema extends Schema<ModelDocument> = any,
|
|
5
|
-
// >
|
|
6
|
-
extends crudModel.CrudModel {
|
|
7
|
-
/**
|
|
8
|
-
* Model Initial Document data
|
|
9
|
-
*/
|
|
10
|
-
initialData = {};
|
|
11
|
-
/**
|
|
12
|
-
* Joinings list
|
|
13
|
-
*/
|
|
14
|
-
static joinings = {};
|
|
15
|
-
/**
|
|
16
|
-
* Model Document data
|
|
17
|
-
*/
|
|
18
|
-
data;
|
|
19
|
-
/**
|
|
20
|
-
* Define Default value data that will be merged with the models' data
|
|
21
|
-
* on the create process
|
|
22
|
-
*/
|
|
23
|
-
defaultValue = {};
|
|
24
|
-
/**
|
|
25
|
-
* A flag to determine if the model is being restored
|
|
26
|
-
*/
|
|
27
|
-
isRestored = false;
|
|
28
|
-
/**
|
|
29
|
-
* Model casts types
|
|
30
|
-
*/
|
|
31
|
-
casts = {};
|
|
32
|
-
/**
|
|
33
|
-
* Sync with list
|
|
34
|
-
*/
|
|
35
|
-
syncWith = [];
|
|
36
|
-
/**
|
|
37
|
-
* Set custom casts that will be used to cast the model's data are not related to the current value of the collection's column
|
|
38
|
-
*
|
|
39
|
-
* For example: `name` is not a column in the given data, but it will be concatenation of `firstName` and `lastName`
|
|
40
|
-
*/
|
|
41
|
-
customCasts = {};
|
|
42
|
-
/**
|
|
43
|
-
* Guarded fields
|
|
44
|
-
*/
|
|
45
|
-
guarded = [];
|
|
46
|
-
/**
|
|
47
|
-
* Fillable fields
|
|
48
|
-
*/
|
|
49
|
-
filled = [];
|
|
50
|
-
/**
|
|
51
|
-
* Embedded columns
|
|
52
|
-
*/
|
|
53
|
-
embedded = [];
|
|
54
|
-
/**
|
|
55
|
-
* Embed all columns except the given columns
|
|
56
|
-
*/
|
|
57
|
-
embedAllExcept = [];
|
|
58
|
-
/**
|
|
59
|
-
* Embed all columns except timestamps and created|updated|deleted by columns
|
|
60
|
-
*/
|
|
61
|
-
embedAllExceptTimestampsAndUserColumns = false;
|
|
62
|
-
/**
|
|
63
|
-
* Created at column
|
|
64
|
-
*/
|
|
65
|
-
createdAtColumn = "createdAt";
|
|
66
|
-
/**
|
|
67
|
-
* Updated at column
|
|
68
|
-
*/
|
|
69
|
-
updatedAtColumn = "updatedAt";
|
|
70
|
-
/**
|
|
71
|
-
* Deleted at column
|
|
72
|
-
*/
|
|
73
|
-
deletedAtColumn = "deletedAt";
|
|
74
|
-
/**
|
|
75
|
-
* Created by column
|
|
76
|
-
*/
|
|
77
|
-
createdByColumn = "createdBy";
|
|
78
|
-
/**
|
|
79
|
-
* Updated by column
|
|
80
|
-
*/
|
|
81
|
-
updatedByColumn = "updatedBy";
|
|
82
|
-
/**
|
|
83
|
-
* Deleted by column
|
|
84
|
-
*/
|
|
85
|
-
deletedByColumn = "deletedBy";
|
|
86
|
-
/**
|
|
87
|
-
* Date format
|
|
88
|
-
*/
|
|
89
|
-
dateFormat = "DD-MM-YYYY";
|
|
90
|
-
/**
|
|
91
|
-
* A flag to determine if the id is auto generated not added manually
|
|
92
|
-
*/
|
|
93
|
-
autoGeneratedId = false;
|
|
94
|
-
/**
|
|
95
|
-
* Is active column
|
|
96
|
-
*/
|
|
97
|
-
isActiveColumn = "isActive";
|
|
98
|
-
/**
|
|
99
|
-
* Original data
|
|
100
|
-
*/
|
|
101
|
-
originalData = {};
|
|
102
|
-
/**
|
|
103
|
-
* Constructor
|
|
104
|
-
*/
|
|
105
|
-
constructor(originalData = {}) {
|
|
106
|
-
super();
|
|
107
|
-
if (originalData instanceof Model) {
|
|
108
|
-
this.originalData = reinforcements.clone(originalData.data);
|
|
109
|
-
}
|
|
110
|
-
else {
|
|
111
|
-
this.originalData = reinforcements.clone(originalData);
|
|
112
|
-
}
|
|
113
|
-
if (typeof originalData?._id === "string") {
|
|
114
|
-
try {
|
|
115
|
-
originalData._id = new mongodb.ObjectId(originalData._id);
|
|
116
|
-
}
|
|
117
|
-
catch (error) {
|
|
118
|
-
originalData._id = new mongodb.ObjectId();
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
this.data = reinforcements.clone(this.originalData);
|
|
122
|
-
this.initialData = reinforcements.clone(this.originalData);
|
|
123
|
-
// this is necessary as clone() will generate a new _id for the data
|
|
124
|
-
// so we need to keep the original _id
|
|
125
|
-
if (originalData?._id) {
|
|
126
|
-
this.originalData._id = new mongodb.ObjectId(originalData._id);
|
|
127
|
-
this.data._id = new mongodb.ObjectId(originalData._id);
|
|
128
|
-
this.initialData._id = new mongodb.ObjectId(originalData._id);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
/**
|
|
132
|
-
* Get save columns which are the casts keys
|
|
133
|
-
*/
|
|
134
|
-
get castColumns() {
|
|
135
|
-
return Object.keys(this.casts);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Get value from original data
|
|
139
|
-
*/
|
|
140
|
-
original(key, defaultValue) {
|
|
141
|
-
return reinforcements.get(this.originalData, key, defaultValue);
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Get all data except the guarded fields
|
|
145
|
-
*/
|
|
146
|
-
get publicData() {
|
|
147
|
-
return reinforcements.except(this.data, this.guarded);
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Get guarded data
|
|
151
|
-
*/
|
|
152
|
-
get guardedData() {
|
|
153
|
-
return reinforcements.only(this.data, this.guarded);
|
|
154
|
-
}
|
|
155
|
-
/**
|
|
156
|
-
* Get the model's id
|
|
157
|
-
*/
|
|
158
|
-
get id() {
|
|
159
|
-
return this.get("id");
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Check if current user is active
|
|
163
|
-
*/
|
|
164
|
-
get isActive() {
|
|
165
|
-
return Boolean(this.get(this.isActiveColumn));
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Get mongodb id
|
|
169
|
-
*/
|
|
170
|
-
get _id() {
|
|
171
|
-
return this.get("_id");
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Mark the current model as being restored
|
|
175
|
-
*/
|
|
176
|
-
markAsRestored() {
|
|
177
|
-
this.isRestored = true;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Set a column in the model data
|
|
181
|
-
*/
|
|
182
|
-
set(column, value) {
|
|
183
|
-
this.data = reinforcements.set(this.data, column, value);
|
|
184
|
-
return this;
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Increment the given column by the given value
|
|
188
|
-
*/
|
|
189
|
-
increment(column, value = 1) {
|
|
190
|
-
return this.set(column, this.get(column, 0) + value);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Decrement the given column by the given value
|
|
194
|
-
*/
|
|
195
|
-
decrement(column, value = 1) {
|
|
196
|
-
return this.set(column, this.get(column, 0) - value);
|
|
197
|
-
}
|
|
198
|
-
/**
|
|
199
|
-
* Get initial value of the given column
|
|
200
|
-
*/
|
|
201
|
-
getInitial(column, defaultValue) {
|
|
202
|
-
return reinforcements.get(this.initialData, column, defaultValue);
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Get value of the given column
|
|
206
|
-
*/
|
|
207
|
-
get(column, defaultValue) {
|
|
208
|
-
return reinforcements.get(this.data, column, defaultValue);
|
|
209
|
-
}
|
|
210
|
-
/**
|
|
211
|
-
* Return the value of the given column as a string
|
|
212
|
-
*/
|
|
213
|
-
string(column, defaultValue) {
|
|
214
|
-
return String(this.get(column, defaultValue));
|
|
215
|
-
}
|
|
216
|
-
/**
|
|
217
|
-
* Return the value of the given column as an integer
|
|
218
|
-
*/
|
|
219
|
-
int(column, defaultValue) {
|
|
220
|
-
return parseInt(this.get(column, defaultValue));
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Return the value of the given column as a float
|
|
224
|
-
*/
|
|
225
|
-
float(column, defaultValue) {
|
|
226
|
-
return parseFloat(this.get(column, defaultValue));
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Return the value of the given column as a number
|
|
230
|
-
*/
|
|
231
|
-
number(column, defaultValue) {
|
|
232
|
-
return Number(this.get(column, defaultValue));
|
|
233
|
-
}
|
|
234
|
-
/**
|
|
235
|
-
* Return the value of the given column as a boolean
|
|
236
|
-
*/
|
|
237
|
-
bool(column, defaultValue) {
|
|
238
|
-
return Boolean(this.get(column, defaultValue));
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
* Determine whether the given column exists in the document
|
|
242
|
-
*/
|
|
243
|
-
has(column) {
|
|
244
|
-
return reinforcements.get(this.data, column) !== undefined;
|
|
245
|
-
}
|
|
246
|
-
/**
|
|
247
|
-
* Get all columns except the given ones
|
|
248
|
-
*/
|
|
249
|
-
except(columns) {
|
|
250
|
-
return reinforcements.except(this.data, columns);
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Get only the given columns
|
|
254
|
-
*/
|
|
255
|
-
only(columns) {
|
|
256
|
-
return reinforcements.only(this.data, columns);
|
|
257
|
-
}
|
|
258
|
-
/**
|
|
259
|
-
* Get only id
|
|
260
|
-
*/
|
|
261
|
-
get onlyId() {
|
|
262
|
-
return this.only(["id"]);
|
|
263
|
-
}
|
|
264
|
-
/**
|
|
265
|
-
* Unset or remove the given columns from the data
|
|
266
|
-
*/
|
|
267
|
-
unset(...columns) {
|
|
268
|
-
this.data = reinforcements.except(this.data, columns);
|
|
269
|
-
return this;
|
|
270
|
-
}
|
|
271
|
-
/**
|
|
272
|
-
* Replace the entire document data with the given new data
|
|
273
|
-
*/
|
|
274
|
-
replaceWith(data) {
|
|
275
|
-
if (!data.id && this.data.id) {
|
|
276
|
-
data.id = this.data.id;
|
|
277
|
-
}
|
|
278
|
-
if (!data._id && this.data._id) {
|
|
279
|
-
data._id = this.data._id;
|
|
280
|
-
}
|
|
281
|
-
this.data = data;
|
|
282
|
-
return this;
|
|
283
|
-
}
|
|
284
|
-
/**
|
|
285
|
-
* Merge the given documents to current document
|
|
286
|
-
*/
|
|
287
|
-
merge(data) {
|
|
288
|
-
this.data = reinforcements.merge(this.data, data);
|
|
289
|
-
return this;
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Push the given values to the given column
|
|
293
|
-
* If the given column does not exists, it will be created
|
|
294
|
-
* If the given value exists but not an array it will be ignored
|
|
295
|
-
*/
|
|
296
|
-
push(column, ...values) {
|
|
297
|
-
const currentValue = this.get(column);
|
|
298
|
-
if (Array.isArray(currentValue)) {
|
|
299
|
-
this.set(column, [...currentValue, ...values]);
|
|
300
|
-
}
|
|
301
|
-
else if (!currentValue) {
|
|
302
|
-
this.set(column, values);
|
|
303
|
-
}
|
|
304
|
-
return this;
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Push the given values to the given column only if not exists
|
|
308
|
-
*/
|
|
309
|
-
pushOnce(column, ...values) {
|
|
310
|
-
const currentValue = this.get(column);
|
|
311
|
-
if (Array.isArray(currentValue)) {
|
|
312
|
-
const newValues = Array.from(new Set([...currentValue, ...values]));
|
|
313
|
-
this.set(column, newValues);
|
|
314
|
-
}
|
|
315
|
-
else if (!currentValue) {
|
|
316
|
-
this.set(column, values);
|
|
317
|
-
}
|
|
318
|
-
return this;
|
|
319
|
-
}
|
|
320
|
-
/**
|
|
321
|
-
* Add the given values to the beginning of the given column
|
|
322
|
-
* If the given column does not exists, it will be created
|
|
323
|
-
* If the given value exists but not an array it will be ignored
|
|
324
|
-
*/
|
|
325
|
-
unshift(column, ...values) {
|
|
326
|
-
const currentValue = this.get(column);
|
|
327
|
-
if (Array.isArray(currentValue)) {
|
|
328
|
-
this.set(column, [...values, ...currentValue]);
|
|
329
|
-
}
|
|
330
|
-
else if (!currentValue) {
|
|
331
|
-
this.set(column, values);
|
|
332
|
-
}
|
|
333
|
-
return this;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Add the given values to the beginning of the given column only if not exists
|
|
337
|
-
*/
|
|
338
|
-
unshiftOnce(column, ...values) {
|
|
339
|
-
const currentValue = this.get(column);
|
|
340
|
-
if (Array.isArray(currentValue)) {
|
|
341
|
-
const newValues = Array.from(new Set([...values, ...currentValue]));
|
|
342
|
-
this.set(column, newValues);
|
|
343
|
-
}
|
|
344
|
-
else if (!currentValue) {
|
|
345
|
-
this.set(column, values);
|
|
346
|
-
}
|
|
347
|
-
return this;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Perform saving operation either by updating or creating a new record in database
|
|
351
|
-
*/
|
|
352
|
-
async save(mergedData, { triggerEvents = true, cast = true, forceUpdate = false, } = {}) {
|
|
353
|
-
const saveEvents = [];
|
|
354
|
-
try {
|
|
355
|
-
if (mergedData) {
|
|
356
|
-
this.merge(mergedData);
|
|
357
|
-
}
|
|
358
|
-
let mode = "create";
|
|
359
|
-
let currentModel;
|
|
360
|
-
// check if the data contains the primary id column
|
|
361
|
-
if (!this.isNewModel()) {
|
|
362
|
-
// perform an update operation
|
|
363
|
-
// check if the data has changed
|
|
364
|
-
// if not changed, then do not do anything
|
|
365
|
-
if (cast) {
|
|
366
|
-
await this.castData(forceUpdate);
|
|
367
|
-
}
|
|
368
|
-
if (this.shouldUpdate(this.originalData, this.data) === false) {
|
|
369
|
-
return this;
|
|
370
|
-
}
|
|
371
|
-
currentModel = new this.constructor(this.originalData);
|
|
372
|
-
mode = "update";
|
|
373
|
-
const updatedAtColumn = this.updatedAtColumn;
|
|
374
|
-
if (updatedAtColumn) {
|
|
375
|
-
// updateAtColumn is supposed to be part of the Schema
|
|
376
|
-
this.data[updatedAtColumn] = new Date();
|
|
377
|
-
}
|
|
378
|
-
if (triggerEvents) {
|
|
379
|
-
const selfModelEvents = this.getModelEvents();
|
|
380
|
-
const ModelEvents = this.getBaseModelEvents();
|
|
381
|
-
await this.onSaving();
|
|
382
|
-
await this.onUpdating();
|
|
383
|
-
await selfModelEvents.trigger("updating", this, currentModel);
|
|
384
|
-
await selfModelEvents.trigger("saving", this, currentModel);
|
|
385
|
-
await ModelEvents.trigger("updating", this, currentModel);
|
|
386
|
-
await ModelEvents.trigger("saving", this, currentModel);
|
|
387
|
-
}
|
|
388
|
-
await this.getQuery().replace(this.getCollection(), {
|
|
389
|
-
_id: this.data._id,
|
|
390
|
-
}, this.data);
|
|
391
|
-
if (triggerEvents) {
|
|
392
|
-
const selfModelEvents = this.getModelEvents();
|
|
393
|
-
const ModelEvents = this.getBaseModelEvents();
|
|
394
|
-
saveEvents.push(this.onSaved(), this.onUpdated(), selfModelEvents.trigger("updated", this, currentModel), selfModelEvents.trigger("saved", this, currentModel), ModelEvents.trigger("updated", this, currentModel), ModelEvents.trigger("saved", this, currentModel));
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
else {
|
|
398
|
-
let tries = 3;
|
|
399
|
-
while (tries > 0) {
|
|
400
|
-
try {
|
|
401
|
-
// await this.getDatabase().startSessionContext(async ({ session }) => {
|
|
402
|
-
// check for default values and merge it with the data
|
|
403
|
-
await this.checkDefaultValues();
|
|
404
|
-
// if the column does not exist, then create it
|
|
405
|
-
if ((!this.data.id || !this.autoGeneratedId) &&
|
|
406
|
-
this.getStaticProperty("autoGenerateId")) {
|
|
407
|
-
this.autoGeneratedId = true;
|
|
408
|
-
await this.generateNextId();
|
|
409
|
-
}
|
|
410
|
-
const now = new Date();
|
|
411
|
-
const createdAtColumn = this.createdAtColumn;
|
|
412
|
-
// if the column does not exist, then create it
|
|
413
|
-
if (this.data[createdAtColumn]) {
|
|
414
|
-
this.data[createdAtColumn] = new Date(this.data[createdAtColumn]);
|
|
415
|
-
}
|
|
416
|
-
else if (createdAtColumn) {
|
|
417
|
-
this.data[createdAtColumn] = now;
|
|
418
|
-
}
|
|
419
|
-
// if the column does not exist, then create it
|
|
420
|
-
const updatedAtColumn = this.updatedAtColumn;
|
|
421
|
-
if (updatedAtColumn) {
|
|
422
|
-
this.data[updatedAtColumn] = now;
|
|
423
|
-
}
|
|
424
|
-
if (cast) {
|
|
425
|
-
await this.castData();
|
|
426
|
-
}
|
|
427
|
-
if (triggerEvents) {
|
|
428
|
-
const selfModelEvents = this.getModelEvents();
|
|
429
|
-
const ModelEvents = this.getBaseModelEvents();
|
|
430
|
-
await this.onSaving();
|
|
431
|
-
await this.onCreating();
|
|
432
|
-
await selfModelEvents.trigger("creating", this);
|
|
433
|
-
await selfModelEvents.trigger("saving", this);
|
|
434
|
-
await ModelEvents.trigger("creating", this);
|
|
435
|
-
await ModelEvents.trigger("saving", this);
|
|
436
|
-
}
|
|
437
|
-
this.data = (await this.getQuery().create(this.getCollection(), this.data));
|
|
438
|
-
if (triggerEvents) {
|
|
439
|
-
const selfModelEvents = this.getModelEvents();
|
|
440
|
-
const ModelEvents = this.getBaseModelEvents();
|
|
441
|
-
saveEvents.push(this.onSaved(), this.onCreated(), selfModelEvents.trigger("created", this), selfModelEvents.trigger("saved", this), ModelEvents.trigger("created", this), ModelEvents.trigger("saved", this));
|
|
442
|
-
}
|
|
443
|
-
break;
|
|
444
|
-
}
|
|
445
|
-
catch (error) {
|
|
446
|
-
console.log(error.message);
|
|
447
|
-
console.log(error.trace);
|
|
448
|
-
// Handle duplicate key error
|
|
449
|
-
if (error instanceof mongodb.MongoServerError && error.code === 11000) {
|
|
450
|
-
if (tries < 2) {
|
|
451
|
-
const duplicateField = error.keyValue;
|
|
452
|
-
const fieldName = Object.keys(duplicateField)[0];
|
|
453
|
-
const errorMessage = `A record with the same ${fieldName} already exists.`;
|
|
454
|
-
if (this.autoGeneratedId) {
|
|
455
|
-
this.unset("id");
|
|
456
|
-
}
|
|
457
|
-
throw new Error(errorMessage);
|
|
458
|
-
}
|
|
459
|
-
else {
|
|
460
|
-
tries--;
|
|
461
|
-
}
|
|
462
|
-
}
|
|
463
|
-
throw error;
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
if (!this.data.id)
|
|
468
|
-
return this;
|
|
469
|
-
this.originalData = reinforcements.clone(this.data);
|
|
470
|
-
// @see constructor
|
|
471
|
-
this.originalData._id = this.data._id;
|
|
472
|
-
if (triggerEvents) {
|
|
473
|
-
await Promise.all(saveEvents);
|
|
474
|
-
this.startSyncing(mode, currentModel);
|
|
475
|
-
}
|
|
476
|
-
return this;
|
|
477
|
-
}
|
|
478
|
-
catch (error) {
|
|
479
|
-
console.log("Error in " + this.constructor.name + ".save()");
|
|
480
|
-
console.log(error);
|
|
481
|
-
throw error;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
/**
|
|
485
|
-
* Generate and return next id
|
|
486
|
-
*/
|
|
487
|
-
async generateNextId() {
|
|
488
|
-
this.set("id", await this.getStaticProperty("genNextId").bind(this.constructor)());
|
|
489
|
-
return this.id;
|
|
490
|
-
}
|
|
491
|
-
/**
|
|
492
|
-
* Perform saving but without any events triggers
|
|
493
|
-
*/
|
|
494
|
-
async silentSaving(mergedData, options) {
|
|
495
|
-
return await this.save(mergedData, {
|
|
496
|
-
triggerEvents: false,
|
|
497
|
-
...(options || {}),
|
|
498
|
-
});
|
|
499
|
-
}
|
|
500
|
-
/**
|
|
501
|
-
* Determine whether the model should be updated or not
|
|
502
|
-
*/
|
|
503
|
-
shouldUpdate(originalData, data) {
|
|
504
|
-
return reinforcements.areEqual(originalData, data) === false;
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* Triggered before saving the model either by creating or updating
|
|
508
|
-
*/
|
|
509
|
-
async onSaving() {
|
|
510
|
-
//
|
|
511
|
-
}
|
|
512
|
-
/**
|
|
513
|
-
* Triggered after saving the model either by creating or updating
|
|
514
|
-
*/
|
|
515
|
-
async onSaved() {
|
|
516
|
-
//
|
|
517
|
-
}
|
|
518
|
-
/**
|
|
519
|
-
* Triggered before creating the model
|
|
520
|
-
*/
|
|
521
|
-
async onCreating() {
|
|
522
|
-
//
|
|
523
|
-
}
|
|
524
|
-
/**
|
|
525
|
-
* Triggered after creating the model
|
|
526
|
-
*/
|
|
527
|
-
async onCreated() {
|
|
528
|
-
//
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* Triggered before updating the model
|
|
532
|
-
*/
|
|
533
|
-
async onUpdating() {
|
|
534
|
-
//
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* Triggered after updating the model
|
|
538
|
-
*/
|
|
539
|
-
async onUpdated() {
|
|
540
|
-
//
|
|
541
|
-
}
|
|
542
|
-
/**
|
|
543
|
-
* Triggered before deleting the model
|
|
544
|
-
*/
|
|
545
|
-
async onDeleting() {
|
|
546
|
-
//
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Triggered after deleting the model
|
|
550
|
-
*/
|
|
551
|
-
async onDeleted() {
|
|
552
|
-
//
|
|
553
|
-
}
|
|
554
|
-
/**
|
|
555
|
-
* Cast data before saving
|
|
556
|
-
*/
|
|
557
|
-
async castData(forceUpdate = false) {
|
|
558
|
-
for (const column in this.casts) {
|
|
559
|
-
if (!forceUpdate && !this.isDirty(column)) {
|
|
560
|
-
continue;
|
|
561
|
-
}
|
|
562
|
-
let value = this.get(column);
|
|
563
|
-
if (value === undefined)
|
|
564
|
-
continue;
|
|
565
|
-
const castType = this.casts[column];
|
|
566
|
-
const castValue = async (value) => {
|
|
567
|
-
if (castType.prototype instanceof Model) {
|
|
568
|
-
// if cast type is passed as model class, then get its embedded data
|
|
569
|
-
value = await castModel.castModel(castType)(value);
|
|
570
|
-
}
|
|
571
|
-
else if (castType?.model) {
|
|
572
|
-
// it means the user is passing a custom model embedding i.e Model.embed('embedToProduct') => Product to embed from the getter property
|
|
573
|
-
// embedToProduct
|
|
574
|
-
// @see EmbeddedModel
|
|
575
|
-
value = await castModel.castModel(castType.model, castType.embeddedKey)(value);
|
|
576
|
-
}
|
|
577
|
-
else if (typeof castType === "object") {
|
|
578
|
-
// it means the user is passing an enum object
|
|
579
|
-
value = await oneOf.castEnum(castType)(value);
|
|
580
|
-
}
|
|
581
|
-
else if (value instanceof Model) {
|
|
582
|
-
value = value.embeddedData;
|
|
583
|
-
}
|
|
584
|
-
else if (typeof castType === "function") {
|
|
585
|
-
value = await castType(value, column, this);
|
|
586
|
-
}
|
|
587
|
-
else {
|
|
588
|
-
value = this.castValue(value, castType);
|
|
589
|
-
}
|
|
590
|
-
return value;
|
|
591
|
-
};
|
|
592
|
-
// if the cast type is passed in array
|
|
593
|
-
// it means we just need to pass the value to the first function
|
|
594
|
-
// second argument will be the column name
|
|
595
|
-
// and the third argument will be the model instance
|
|
596
|
-
if (Array.isArray(castType)) {
|
|
597
|
-
value = await castType[0](value, column, this);
|
|
598
|
-
}
|
|
599
|
-
else if (Array.isArray(value) && castType !== "localized") {
|
|
600
|
-
// if cast type is array, then we'll keep the value as it is
|
|
601
|
-
// now we want to add a new validation rule that to check
|
|
602
|
-
// if the value is an array of localized objects
|
|
603
|
-
// if so, then each value in the array should have `localeCode` and `value` keys
|
|
604
|
-
// if so, then it will be cast only to the value key inside each object
|
|
605
|
-
// so the final output will be localeCode and `castValue` of the value key
|
|
606
|
-
if (castType === "array") ;
|
|
607
|
-
else if (value[0]?.localeCode && value[0]?.value) {
|
|
608
|
-
value = await Promise.all(value.map(async (item) => {
|
|
609
|
-
return {
|
|
610
|
-
localeCode: item.localeCode,
|
|
611
|
-
value: await castValue(item.value),
|
|
612
|
-
};
|
|
613
|
-
}));
|
|
614
|
-
}
|
|
615
|
-
else {
|
|
616
|
-
value = await Promise.all(value.map(async (item) => {
|
|
617
|
-
return await castValue(item);
|
|
618
|
-
}));
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
else {
|
|
622
|
-
value = await castValue(value);
|
|
623
|
-
}
|
|
624
|
-
if (Array.isArray(value)) {
|
|
625
|
-
value = value.filter(value => value !== null && value !== undefined);
|
|
626
|
-
}
|
|
627
|
-
if (value !== undefined) {
|
|
628
|
-
this.set(column, value);
|
|
629
|
-
}
|
|
630
|
-
else {
|
|
631
|
-
this.unset(column);
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
for (const column in this.customCasts) {
|
|
635
|
-
const castType = this.customCasts[column];
|
|
636
|
-
const value = await castType(this, column);
|
|
637
|
-
if (value !== undefined) {
|
|
638
|
-
this.set(column, value);
|
|
639
|
-
}
|
|
640
|
-
else {
|
|
641
|
-
this.unset(column);
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
/**
|
|
646
|
-
* Return only the given columns to be used in output
|
|
647
|
-
*/
|
|
648
|
-
outputOnly(columns) {
|
|
649
|
-
return this.clone(this.only(columns));
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* Return all columns except the given columns to be used in output
|
|
653
|
-
*/
|
|
654
|
-
outputExcept(columns) {
|
|
655
|
-
return this.clone(this.except(columns));
|
|
656
|
-
}
|
|
657
|
-
/**
|
|
658
|
-
* Cast the given value based on the given cast type
|
|
659
|
-
*/
|
|
660
|
-
castValue(value, castType) {
|
|
661
|
-
const isEmptyValue = supportiveIs.isEmpty(value);
|
|
662
|
-
if (typeof value === "object") {
|
|
663
|
-
if (value === null)
|
|
664
|
-
return undefined;
|
|
665
|
-
}
|
|
666
|
-
else if (isEmptyValue)
|
|
667
|
-
return undefined;
|
|
668
|
-
switch (castType) {
|
|
669
|
-
case "string":
|
|
670
|
-
return isEmptyValue ? "" : String(value).trim();
|
|
671
|
-
case "localized":
|
|
672
|
-
// if (isEmptyValue) return [];
|
|
673
|
-
// if (!Array.isArray(value)) return [];
|
|
674
|
-
if (!Array.isArray(value))
|
|
675
|
-
return undefined;
|
|
676
|
-
return value
|
|
677
|
-
.filter(value => !supportiveIs.isEmpty(value) && supportiveIs.isPlainObject(value))
|
|
678
|
-
.map(item => {
|
|
679
|
-
return {
|
|
680
|
-
localeCode: item.localeCode,
|
|
681
|
-
value: item.value,
|
|
682
|
-
};
|
|
683
|
-
});
|
|
684
|
-
case "number":
|
|
685
|
-
// return isEmptyValue ? 0 : Number(value);
|
|
686
|
-
return Number(value);
|
|
687
|
-
case "int":
|
|
688
|
-
case "integer":
|
|
689
|
-
// return isEmptyValue ? 0 : parseInt(value);
|
|
690
|
-
return parseInt(value);
|
|
691
|
-
case "float":
|
|
692
|
-
// return isEmptyValue ? 0 : parseFloat(value);
|
|
693
|
-
return parseFloat(value);
|
|
694
|
-
case "bool":
|
|
695
|
-
case "boolean": {
|
|
696
|
-
// if (isEmptyValue) return false;
|
|
697
|
-
if (value === "true")
|
|
698
|
-
return true;
|
|
699
|
-
if (value === "false" || value === "0" || value === 0)
|
|
700
|
-
return false;
|
|
701
|
-
return Boolean(value);
|
|
702
|
-
}
|
|
703
|
-
case "date": {
|
|
704
|
-
if (dayjs__default.default.isDayjs(value)) {
|
|
705
|
-
value = value.toDate();
|
|
706
|
-
}
|
|
707
|
-
if (value instanceof Date) {
|
|
708
|
-
return timeWizard.toUTC(value);
|
|
709
|
-
}
|
|
710
|
-
return timeWizard.toUTC(new Date(value));
|
|
711
|
-
}
|
|
712
|
-
case "location": {
|
|
713
|
-
// if (isEmptyValue) return null;
|
|
714
|
-
const lat = value?.[0] || value?.lat;
|
|
715
|
-
const lng = value?.[1] || value?.lng;
|
|
716
|
-
const address = value?.address || value?.formattedAddress;
|
|
717
|
-
return {
|
|
718
|
-
type: "Point",
|
|
719
|
-
coordinates: [Number(lat), Number(lng)],
|
|
720
|
-
address,
|
|
721
|
-
};
|
|
722
|
-
}
|
|
723
|
-
case "object": {
|
|
724
|
-
// if (isEmptyValue) return {};
|
|
725
|
-
if (typeof value === "string") {
|
|
726
|
-
try {
|
|
727
|
-
return JSON.parse(value);
|
|
728
|
-
}
|
|
729
|
-
catch (error) {
|
|
730
|
-
return undefined;
|
|
731
|
-
}
|
|
732
|
-
}
|
|
733
|
-
return value;
|
|
734
|
-
}
|
|
735
|
-
case "array": {
|
|
736
|
-
// if (isEmptyValue) return [];
|
|
737
|
-
if (typeof value === "string") {
|
|
738
|
-
return JSON.parse(value);
|
|
739
|
-
}
|
|
740
|
-
return value;
|
|
741
|
-
}
|
|
742
|
-
case "mixed":
|
|
743
|
-
case "any":
|
|
744
|
-
default:
|
|
745
|
-
return value;
|
|
746
|
-
}
|
|
747
|
-
}
|
|
748
|
-
/**
|
|
749
|
-
* Check for default values
|
|
750
|
-
*/
|
|
751
|
-
checkDefaultValues() {
|
|
752
|
-
// if default value is empty, then do nothing
|
|
753
|
-
if (supportiveIs.isEmpty(this.defaultValue))
|
|
754
|
-
return;
|
|
755
|
-
const defaultValue = { ...this.defaultValue };
|
|
756
|
-
for (const key in defaultValue) {
|
|
757
|
-
const value = defaultValue[key];
|
|
758
|
-
if (typeof value === "function") {
|
|
759
|
-
defaultValue[key] = value(this);
|
|
760
|
-
}
|
|
761
|
-
}
|
|
762
|
-
// merge the data with default value
|
|
763
|
-
this.data = reinforcements.merge(defaultValue, this.data);
|
|
764
|
-
}
|
|
765
|
-
/**
|
|
766
|
-
* Destroy the model and delete it from database collection
|
|
767
|
-
*/
|
|
768
|
-
async destroy() {
|
|
769
|
-
if (!this.data._id)
|
|
770
|
-
return;
|
|
771
|
-
if (this.deletedAtColumn) {
|
|
772
|
-
this.set(this.deletedAtColumn, new Date());
|
|
773
|
-
}
|
|
774
|
-
const deleteStrategy = this.getStaticProperty("deleteStrategy");
|
|
775
|
-
if (deleteStrategy === types.ModelDeleteStrategy.moveToTrash) {
|
|
776
|
-
const collectionName = this.getCollection();
|
|
777
|
-
class Trash extends Model {
|
|
778
|
-
static collection = collectionName + "Trash";
|
|
779
|
-
}
|
|
780
|
-
// we need to wrap the trash collection inside a model class so it get a generated timestamps and id
|
|
781
|
-
Trash.create({
|
|
782
|
-
document: this.data,
|
|
783
|
-
});
|
|
784
|
-
}
|
|
785
|
-
const selfModelEvents = this.getModelEvents();
|
|
786
|
-
const ModelEvents = this.getBaseModelEvents();
|
|
787
|
-
await this.onDeleting();
|
|
788
|
-
await selfModelEvents.trigger("deleting", this);
|
|
789
|
-
await ModelEvents.trigger("deleting", this);
|
|
790
|
-
// the document will be deleted from database collection if the delete strategy is not soft delete
|
|
791
|
-
if (deleteStrategy !== types.ModelDeleteStrategy.softDelete) {
|
|
792
|
-
await this.getQuery().deleteOne(this.getCollection(), {
|
|
793
|
-
_id: this.data._id,
|
|
794
|
-
});
|
|
795
|
-
}
|
|
796
|
-
else if (deleteStrategy === types.ModelDeleteStrategy.softDelete) {
|
|
797
|
-
await this.getQuery().updateOne(this.getCollection(), {
|
|
798
|
-
_id: this.data._id,
|
|
799
|
-
}, this.data);
|
|
800
|
-
}
|
|
801
|
-
this.onDeleted();
|
|
802
|
-
selfModelEvents.trigger("deleted", this);
|
|
803
|
-
ModelEvents.trigger("deleted", this);
|
|
804
|
-
this.syncDestruction();
|
|
805
|
-
}
|
|
806
|
-
/**
|
|
807
|
-
* Determine if the given column is dirty column
|
|
808
|
-
*
|
|
809
|
-
* Dirty columns are columns that their values have been changed from the original data
|
|
810
|
-
*/
|
|
811
|
-
isDirty(column) {
|
|
812
|
-
if (!column) {
|
|
813
|
-
return reinforcements.areEqual(reinforcements.clone(this.data), reinforcements.clone(this.originalData)) === false;
|
|
814
|
-
}
|
|
815
|
-
if (this.isNewModel())
|
|
816
|
-
return true;
|
|
817
|
-
const currentValue = reinforcements.get(this.data, column);
|
|
818
|
-
const originalValue = reinforcements.get(this.originalData, column);
|
|
819
|
-
return reinforcements.areEqual(reinforcements.clone(currentValue), reinforcements.clone(originalValue)) === false;
|
|
820
|
-
}
|
|
821
|
-
/**
|
|
822
|
-
* Check if current model is a new model
|
|
823
|
-
*/
|
|
824
|
-
isNewModel() {
|
|
825
|
-
return !this.data._id || (this.data._id && this.isRestored);
|
|
826
|
-
}
|
|
827
|
-
/**
|
|
828
|
-
* Get embedded data
|
|
829
|
-
*/
|
|
830
|
-
get embeddedData() {
|
|
831
|
-
if (this.embedAllExcept.length > 0) {
|
|
832
|
-
return reinforcements.except(this.data, this.embedAllExcept);
|
|
833
|
-
}
|
|
834
|
-
if (this.embedAllExceptTimestampsAndUserColumns) {
|
|
835
|
-
return reinforcements.except(this.data, [
|
|
836
|
-
this.createdAtColumn,
|
|
837
|
-
this.updatedAtColumn,
|
|
838
|
-
this.deletedAtColumn,
|
|
839
|
-
this.createdByColumn,
|
|
840
|
-
this.updatedByColumn,
|
|
841
|
-
this.deletedByColumn,
|
|
842
|
-
]);
|
|
843
|
-
}
|
|
844
|
-
return this.embedded.length > 0 ? this.only(this.embedded) : this.data;
|
|
845
|
-
}
|
|
846
|
-
/**
|
|
847
|
-
* Clone the model
|
|
848
|
-
*/
|
|
849
|
-
clone(data = this.data) {
|
|
850
|
-
return new this.constructor(reinforcements.clone(data));
|
|
851
|
-
}
|
|
852
|
-
/**
|
|
853
|
-
* Get relationship with the given model class
|
|
854
|
-
*/
|
|
855
|
-
hasMany(modelClass, column) {
|
|
856
|
-
return new RelationshipWithMany.RelationshipWithMany(this, modelClass, column);
|
|
857
|
-
}
|
|
858
|
-
/**
|
|
859
|
-
* Get new aggregate for current model
|
|
860
|
-
*/
|
|
861
|
-
static aggregate() {
|
|
862
|
-
return new ModelAggregate.ModelAggregate(this);
|
|
863
|
-
}
|
|
864
|
-
/**
|
|
865
|
-
* Get query builder
|
|
866
|
-
* @alias aggregate
|
|
867
|
-
*/
|
|
868
|
-
static queryBuilder() {
|
|
869
|
-
return new ModelAggregate.ModelAggregate(this);
|
|
870
|
-
}
|
|
871
|
-
/**
|
|
872
|
-
* Sync with the given model
|
|
873
|
-
*/
|
|
874
|
-
static sync(columns, embedMethod = "embedData") {
|
|
875
|
-
return new ModelSync.ModelSync(this, columns, embedMethod);
|
|
876
|
-
}
|
|
877
|
-
/**
|
|
878
|
-
* Sync data on saving
|
|
879
|
-
*/
|
|
880
|
-
startSyncing(saveMode, oldModel) {
|
|
881
|
-
for (const modelSync of this.syncWith) {
|
|
882
|
-
modelSync.sync(this, saveMode, oldModel);
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
/**
|
|
886
|
-
* Sync destruction
|
|
887
|
-
* Called when destroy method is called
|
|
888
|
-
*/
|
|
889
|
-
syncDestruction() {
|
|
890
|
-
for (const modelSync of this.syncWith) {
|
|
891
|
-
modelSync.syncDestruction(this);
|
|
892
|
-
}
|
|
893
|
-
}
|
|
894
|
-
/**
|
|
895
|
-
* The syncing model (That calls startSyncing) is being embedded in multiple documents of current model
|
|
896
|
-
* I.e Country.syncMany('cities') while current model is City
|
|
897
|
-
*/
|
|
898
|
-
static syncMany(columns, embedMethod = "embedData") {
|
|
899
|
-
return new ModelSync.ModelSync(this, columns, embedMethod).syncMany();
|
|
900
|
-
}
|
|
901
|
-
/**
|
|
902
|
-
* Reassociate a model/object/document with the current model
|
|
903
|
-
* If the model is already associated, it will be updated
|
|
904
|
-
* If not, it will be associated
|
|
905
|
-
* the model/document must have an id
|
|
906
|
-
*
|
|
907
|
-
* If it is a model, you can set the embed method to use
|
|
908
|
-
*/
|
|
909
|
-
reassociate(column, model, embedWith) {
|
|
910
|
-
const columnValue = model instanceof Model
|
|
911
|
-
? embedWith
|
|
912
|
-
? model[embedWith]()
|
|
913
|
-
: model.embeddedData
|
|
914
|
-
: model;
|
|
915
|
-
if (columnValue === undefined)
|
|
916
|
-
return this;
|
|
917
|
-
// make a deep copy so when changing the data, it won't affect the original data
|
|
918
|
-
const documentsList = reinforcements.clone(this.get(column, []));
|
|
919
|
-
const index = documentsList.findIndex((doc) => (doc?.id || doc) === (columnValue?.id || columnValue));
|
|
920
|
-
if (index === -1) {
|
|
921
|
-
documentsList.push(columnValue);
|
|
922
|
-
}
|
|
923
|
-
else {
|
|
924
|
-
documentsList[index] = columnValue;
|
|
925
|
-
}
|
|
926
|
-
this.set(column, [...documentsList]);
|
|
927
|
-
return this;
|
|
928
|
-
}
|
|
929
|
-
/**
|
|
930
|
-
* Associate a model with the current model
|
|
931
|
-
*/
|
|
932
|
-
associate(column, model, embedWith) {
|
|
933
|
-
const columnValue = model instanceof Model
|
|
934
|
-
? embedWith
|
|
935
|
-
? model[embedWith]()
|
|
936
|
-
: model.embeddedData
|
|
937
|
-
: model;
|
|
938
|
-
if (columnValue === undefined)
|
|
939
|
-
return this;
|
|
940
|
-
const documentsList = this.get(column, []);
|
|
941
|
-
documentsList.push(columnValue);
|
|
942
|
-
this.set(column, documentsList);
|
|
943
|
-
return this;
|
|
944
|
-
}
|
|
945
|
-
/**
|
|
946
|
-
* Disassociate a model with the current model
|
|
947
|
-
*/
|
|
948
|
-
disassociate(column, model) {
|
|
949
|
-
const columnValue = model instanceof Model ? model.embeddedData : model;
|
|
950
|
-
if (columnValue === undefined)
|
|
951
|
-
return this;
|
|
952
|
-
const documentsList = this.get(column, []);
|
|
953
|
-
if (!Array.isArray(documentsList))
|
|
954
|
-
return this;
|
|
955
|
-
const index = documentsList.findIndex((doc) => (doc?.id || doc) === (columnValue?.id || columnValue));
|
|
956
|
-
if (index !== -1) {
|
|
957
|
-
documentsList.splice(index, 1);
|
|
958
|
-
}
|
|
959
|
-
this.set(column, documentsList);
|
|
960
|
-
return this;
|
|
961
|
-
}
|
|
962
|
-
/**
|
|
963
|
-
* Make a wrapper to list when models should be updated when only one of the given columns is updated
|
|
964
|
-
*/
|
|
965
|
-
syncUpdateWhenChange(columns, syncModels) {
|
|
966
|
-
return syncModels.map(syncModel => {
|
|
967
|
-
syncModel.updateWhenChange(columns);
|
|
968
|
-
return syncModel;
|
|
969
|
-
});
|
|
970
|
-
}
|
|
971
|
-
/**
|
|
972
|
-
* Get a Joinable instance for current model
|
|
973
|
-
*/
|
|
974
|
-
static joinable(localField, foreignField, single, as) {
|
|
975
|
-
const joinable$1 = new joinable.Joinable(this);
|
|
976
|
-
if (localField) {
|
|
977
|
-
joinable$1.localField(localField);
|
|
978
|
-
}
|
|
979
|
-
if (foreignField) {
|
|
980
|
-
joinable$1.foreignField(foreignField);
|
|
981
|
-
}
|
|
982
|
-
if (single) {
|
|
983
|
-
joinable$1.single(single);
|
|
984
|
-
}
|
|
985
|
-
if (as) {
|
|
986
|
-
joinable$1.as(as);
|
|
987
|
-
}
|
|
988
|
-
return joinableProxy.joinableProxy(joinable$1);
|
|
989
|
-
}
|
|
990
|
-
}exports.Model=Model;//# sourceMappingURL=model.js.map
|