@warlock.js/cascade 4.0.135 → 4.0.137
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/context/database-transaction-context.d.ts +4 -0
- package/cjs/context/database-transaction-context.d.ts.map +1 -1
- package/cjs/context/database-transaction-context.js +6 -0
- package/cjs/context/database-transaction-context.js.map +1 -1
- package/cjs/contracts/database-driver.contract.d.ts +106 -4
- package/cjs/contracts/database-driver.contract.d.ts.map +1 -1
- package/cjs/contracts/migration-driver.contract.d.ts +49 -1
- package/cjs/contracts/migration-driver.contract.d.ts.map +1 -1
- package/cjs/contracts/query-builder.contract.d.ts +76 -4
- package/cjs/contracts/query-builder.contract.d.ts.map +1 -1
- package/cjs/data-source/data-source.d.ts +40 -1
- package/cjs/data-source/data-source.d.ts.map +1 -1
- package/cjs/data-source/data-source.js +6 -0
- package/cjs/data-source/data-source.js.map +1 -1
- package/cjs/database-dirty-tracker.d.ts +21 -22
- package/cjs/database-dirty-tracker.d.ts.map +1 -1
- package/cjs/database-dirty-tracker.js +14 -17
- package/cjs/database-dirty-tracker.js.map +1 -1
- package/cjs/drivers/mongodb/mongodb-driver.d.ts +29 -1
- package/cjs/drivers/mongodb/mongodb-driver.d.ts.map +1 -1
- package/cjs/drivers/mongodb/mongodb-driver.js +116 -4
- package/cjs/drivers/mongodb/mongodb-driver.js.map +1 -1
- package/cjs/drivers/mongodb/mongodb-migration-driver.d.ts +22 -1
- package/cjs/drivers/mongodb/mongodb-migration-driver.d.ts.map +1 -1
- package/cjs/drivers/mongodb/mongodb-migration-driver.js +27 -0
- package/cjs/drivers/mongodb/mongodb-migration-driver.js.map +1 -1
- package/cjs/drivers/mongodb/mongodb-query-builder.d.ts +41 -9
- package/cjs/drivers/mongodb/mongodb-query-builder.d.ts.map +1 -1
- package/cjs/drivers/mongodb/mongodb-query-builder.js +60 -18
- package/cjs/drivers/mongodb/mongodb-query-builder.js.map +1 -1
- package/cjs/drivers/mongodb/types.d.ts +1 -1
- package/cjs/drivers/mongodb/types.d.ts.map +1 -1
- package/cjs/drivers/postgres/postgres-dialect.d.ts +1 -0
- package/cjs/drivers/postgres/postgres-dialect.d.ts.map +1 -1
- package/cjs/drivers/postgres/postgres-dialect.js +24 -1
- package/cjs/drivers/postgres/postgres-dialect.js.map +1 -1
- package/cjs/drivers/postgres/postgres-driver.d.ts +24 -3
- package/cjs/drivers/postgres/postgres-driver.d.ts.map +1 -1
- package/cjs/drivers/postgres/postgres-driver.js +101 -8
- package/cjs/drivers/postgres/postgres-driver.js.map +1 -1
- package/cjs/drivers/postgres/postgres-migration-driver.d.ts +30 -1
- package/cjs/drivers/postgres/postgres-migration-driver.d.ts.map +1 -1
- package/cjs/drivers/postgres/postgres-migration-driver.js +61 -2
- package/cjs/drivers/postgres/postgres-migration-driver.js.map +1 -1
- package/cjs/drivers/postgres/postgres-query-builder.d.ts +152 -722
- package/cjs/drivers/postgres/postgres-query-builder.d.ts.map +1 -1
- package/cjs/drivers/postgres/postgres-query-builder.js +592 -1459
- package/cjs/drivers/postgres/postgres-query-builder.js.map +1 -1
- package/cjs/drivers/postgres/postgres-query-parser.d.ts +34 -14
- package/cjs/drivers/postgres/postgres-query-parser.d.ts.map +1 -1
- package/cjs/drivers/postgres/postgres-query-parser.js +197 -39
- package/cjs/drivers/postgres/postgres-query-parser.js.map +1 -1
- package/cjs/drivers/sql/sql-dialect.contract.d.ts +1 -0
- package/cjs/drivers/sql/sql-dialect.contract.d.ts.map +1 -1
- package/cjs/errors/transaction-rollback.error.d.ts +20 -0
- package/cjs/errors/transaction-rollback.error.d.ts.map +1 -0
- package/cjs/errors/transaction-rollback.error.js +27 -0
- package/cjs/errors/transaction-rollback.error.js.map +1 -0
- package/cjs/events/model-events.d.ts +1 -1
- package/cjs/events/model-events.d.ts.map +1 -1
- package/cjs/index.d.ts +1 -0
- package/cjs/index.d.ts.map +1 -1
- package/cjs/index.js +1 -1
- package/cjs/migration/column-builder.d.ts +4 -0
- package/cjs/migration/column-builder.d.ts.map +1 -1
- package/cjs/migration/column-builder.js +23 -6
- package/cjs/migration/column-builder.js.map +1 -1
- package/cjs/migration/migration-runner.d.ts.map +1 -1
- package/cjs/migration/migration-runner.js +137 -23
- package/cjs/migration/migration-runner.js.map +1 -1
- package/cjs/migration/migration.d.ts +209 -35
- package/cjs/migration/migration.d.ts.map +1 -1
- package/cjs/migration/migration.js +192 -27
- package/cjs/migration/migration.js.map +1 -1
- package/cjs/model/methods/accessor-methods.d.ts +13 -0
- package/cjs/model/methods/accessor-methods.d.ts.map +1 -0
- package/cjs/model/methods/accessor-methods.js +51 -0
- package/cjs/model/methods/accessor-methods.js.map +1 -0
- package/cjs/model/methods/delete-methods.d.ts +10 -0
- package/cjs/model/methods/delete-methods.d.ts.map +1 -0
- package/cjs/model/methods/delete-methods.js +10 -0
- package/cjs/model/methods/delete-methods.js.map +1 -0
- package/cjs/model/methods/dirty-methods.d.ts +10 -0
- package/cjs/model/methods/dirty-methods.d.ts.map +1 -0
- package/cjs/model/methods/dirty-methods.js +15 -0
- package/cjs/model/methods/dirty-methods.js.map +1 -0
- package/cjs/model/methods/hydration-methods.d.ts +10 -0
- package/cjs/model/methods/hydration-methods.d.ts.map +1 -0
- package/cjs/model/methods/hydration-methods.js +57 -0
- package/cjs/model/methods/hydration-methods.js.map +1 -0
- package/cjs/model/methods/instance-event-methods.d.ts +7 -0
- package/cjs/model/methods/instance-event-methods.d.ts.map +1 -0
- package/cjs/model/methods/instance-event-methods.js +15 -0
- package/cjs/model/methods/instance-event-methods.js.map +1 -0
- package/cjs/model/methods/meta-methods.d.ts +7 -0
- package/cjs/model/methods/meta-methods.d.ts.map +1 -0
- package/cjs/model/methods/meta-methods.js +78 -0
- package/cjs/model/methods/meta-methods.js.map +1 -0
- package/cjs/model/methods/query-methods.d.ts +24 -0
- package/cjs/model/methods/query-methods.d.ts.map +1 -0
- package/cjs/model/methods/query-methods.js +161 -0
- package/cjs/model/methods/query-methods.js.map +1 -0
- package/cjs/model/methods/restore-methods.d.ts +10 -0
- package/cjs/model/methods/restore-methods.d.ts.map +1 -0
- package/cjs/model/methods/restore-methods.js +13 -0
- package/cjs/model/methods/restore-methods.js.map +1 -0
- package/cjs/model/methods/scope-methods.d.ts +7 -0
- package/cjs/model/methods/scope-methods.d.ts.map +1 -0
- package/cjs/model/methods/scope-methods.js +15 -0
- package/cjs/model/methods/scope-methods.js.map +1 -0
- package/cjs/model/methods/serialization-methods.d.ts +3 -0
- package/cjs/model/methods/serialization-methods.d.ts.map +1 -0
- package/cjs/model/methods/serialization-methods.js +27 -0
- package/cjs/model/methods/serialization-methods.js.map +1 -0
- package/cjs/model/methods/static-event-methods.d.ts +9 -0
- package/cjs/model/methods/static-event-methods.d.ts.map +1 -0
- package/cjs/model/methods/static-event-methods.js +29 -0
- package/cjs/model/methods/static-event-methods.js.map +1 -0
- package/cjs/model/methods/write-methods.d.ts +10 -0
- package/cjs/model/methods/write-methods.d.ts.map +1 -0
- package/cjs/model/methods/write-methods.js +52 -0
- package/cjs/model/methods/write-methods.js.map +1 -0
- package/cjs/model/model.d.ts +89 -58
- package/cjs/model/model.d.ts.map +1 -1
- package/cjs/model/model.js +166 -424
- package/cjs/model/model.js.map +1 -1
- package/cjs/model/model.types.d.ts +44 -0
- package/cjs/model/model.types.d.ts.map +1 -0
- package/cjs/model/register-model.d.ts +3 -3
- package/cjs/model/register-model.d.ts.map +1 -1
- package/cjs/query-builder/query-builder.d.ts +556 -0
- package/cjs/query-builder/query-builder.d.ts.map +1 -0
- package/cjs/query-builder/query-builder.js +1070 -0
- package/cjs/query-builder/query-builder.js.map +1 -0
- package/cjs/relations/helpers.d.ts.map +1 -1
- package/cjs/relations/helpers.js +4 -0
- package/cjs/relations/helpers.js.map +1 -1
- package/cjs/relations/index.d.ts +2 -0
- package/cjs/relations/index.d.ts.map +1 -1
- package/cjs/relations/relation-hydrator.d.ts +68 -0
- package/cjs/relations/relation-hydrator.d.ts.map +1 -0
- package/cjs/relations/relation-hydrator.js +81 -0
- package/cjs/relations/relation-hydrator.js.map +1 -0
- package/cjs/relations/relation-loader.js +1 -1
- package/cjs/relations/relation-loader.js.map +1 -1
- package/cjs/relations/types.d.ts +26 -0
- package/cjs/relations/types.d.ts.map +1 -1
- package/cjs/sql-database-dirty-tracker.d.ts +13 -0
- package/cjs/sql-database-dirty-tracker.d.ts.map +1 -0
- package/cjs/sql-database-dirty-tracker.js +14 -0
- package/cjs/sql-database-dirty-tracker.js.map +1 -0
- package/cjs/types.d.ts +59 -0
- package/cjs/types.d.ts.map +1 -1
- package/cjs/utils/connect-to-database.d.ts +50 -1
- package/cjs/utils/connect-to-database.d.ts.map +1 -1
- package/cjs/utils/connect-to-database.js +14 -1
- package/cjs/utils/connect-to-database.js.map +1 -1
- package/cjs/utils/database-writer.utils.d.ts +1 -1
- package/cjs/utils/database-writer.utils.d.ts.map +1 -1
- package/cjs/utils/is-valid-date-value.d.ts +5 -0
- package/cjs/utils/is-valid-date-value.d.ts.map +1 -0
- package/cjs/utils/is-valid-date-value.js +25 -0
- package/cjs/utils/is-valid-date-value.js.map +1 -0
- package/cjs/utils/once-connected.d.ts.map +1 -1
- package/cjs/utils/once-connected.js +4 -5
- package/cjs/utils/once-connected.js.map +1 -1
- package/cjs/writer/database-writer.d.ts.map +1 -1
- package/cjs/writer/database-writer.js +7 -6
- package/cjs/writer/database-writer.js.map +1 -1
- package/esm/context/database-transaction-context.d.ts +4 -0
- package/esm/context/database-transaction-context.d.ts.map +1 -1
- package/esm/context/database-transaction-context.js +6 -0
- package/esm/context/database-transaction-context.js.map +1 -1
- package/esm/contracts/database-driver.contract.d.ts +106 -4
- package/esm/contracts/database-driver.contract.d.ts.map +1 -1
- package/esm/contracts/migration-driver.contract.d.ts +49 -1
- package/esm/contracts/migration-driver.contract.d.ts.map +1 -1
- package/esm/contracts/query-builder.contract.d.ts +76 -4
- package/esm/contracts/query-builder.contract.d.ts.map +1 -1
- package/esm/data-source/data-source.d.ts +40 -1
- package/esm/data-source/data-source.d.ts.map +1 -1
- package/esm/data-source/data-source.js +6 -0
- package/esm/data-source/data-source.js.map +1 -1
- package/esm/database-dirty-tracker.d.ts +21 -22
- package/esm/database-dirty-tracker.d.ts.map +1 -1
- package/esm/database-dirty-tracker.js +14 -17
- package/esm/database-dirty-tracker.js.map +1 -1
- package/esm/drivers/mongodb/mongodb-driver.d.ts +29 -1
- package/esm/drivers/mongodb/mongodb-driver.d.ts.map +1 -1
- package/esm/drivers/mongodb/mongodb-driver.js +116 -4
- package/esm/drivers/mongodb/mongodb-driver.js.map +1 -1
- package/esm/drivers/mongodb/mongodb-migration-driver.d.ts +22 -1
- package/esm/drivers/mongodb/mongodb-migration-driver.d.ts.map +1 -1
- package/esm/drivers/mongodb/mongodb-migration-driver.js +27 -0
- package/esm/drivers/mongodb/mongodb-migration-driver.js.map +1 -1
- package/esm/drivers/mongodb/mongodb-query-builder.d.ts +41 -9
- package/esm/drivers/mongodb/mongodb-query-builder.d.ts.map +1 -1
- package/esm/drivers/mongodb/mongodb-query-builder.js +60 -18
- package/esm/drivers/mongodb/mongodb-query-builder.js.map +1 -1
- package/esm/drivers/mongodb/types.d.ts +1 -1
- package/esm/drivers/mongodb/types.d.ts.map +1 -1
- package/esm/drivers/postgres/postgres-dialect.d.ts +1 -0
- package/esm/drivers/postgres/postgres-dialect.d.ts.map +1 -1
- package/esm/drivers/postgres/postgres-dialect.js +24 -1
- package/esm/drivers/postgres/postgres-dialect.js.map +1 -1
- package/esm/drivers/postgres/postgres-driver.d.ts +24 -3
- package/esm/drivers/postgres/postgres-driver.d.ts.map +1 -1
- package/esm/drivers/postgres/postgres-driver.js +101 -8
- package/esm/drivers/postgres/postgres-driver.js.map +1 -1
- package/esm/drivers/postgres/postgres-migration-driver.d.ts +30 -1
- package/esm/drivers/postgres/postgres-migration-driver.d.ts.map +1 -1
- package/esm/drivers/postgres/postgres-migration-driver.js +61 -2
- package/esm/drivers/postgres/postgres-migration-driver.js.map +1 -1
- package/esm/drivers/postgres/postgres-query-builder.d.ts +152 -722
- package/esm/drivers/postgres/postgres-query-builder.d.ts.map +1 -1
- package/esm/drivers/postgres/postgres-query-builder.js +592 -1459
- package/esm/drivers/postgres/postgres-query-builder.js.map +1 -1
- package/esm/drivers/postgres/postgres-query-parser.d.ts +34 -14
- package/esm/drivers/postgres/postgres-query-parser.d.ts.map +1 -1
- package/esm/drivers/postgres/postgres-query-parser.js +197 -39
- package/esm/drivers/postgres/postgres-query-parser.js.map +1 -1
- package/esm/drivers/sql/sql-dialect.contract.d.ts +1 -0
- package/esm/drivers/sql/sql-dialect.contract.d.ts.map +1 -1
- package/esm/errors/transaction-rollback.error.d.ts +20 -0
- package/esm/errors/transaction-rollback.error.d.ts.map +1 -0
- package/esm/errors/transaction-rollback.error.js +27 -0
- package/esm/errors/transaction-rollback.error.js.map +1 -0
- package/esm/events/model-events.d.ts +1 -1
- package/esm/events/model-events.d.ts.map +1 -1
- package/esm/index.d.ts +1 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -1
- package/esm/migration/column-builder.d.ts +4 -0
- package/esm/migration/column-builder.d.ts.map +1 -1
- package/esm/migration/column-builder.js +23 -6
- package/esm/migration/column-builder.js.map +1 -1
- package/esm/migration/migration-runner.d.ts.map +1 -1
- package/esm/migration/migration-runner.js +137 -23
- package/esm/migration/migration-runner.js.map +1 -1
- package/esm/migration/migration.d.ts +209 -35
- package/esm/migration/migration.d.ts.map +1 -1
- package/esm/migration/migration.js +192 -27
- package/esm/migration/migration.js.map +1 -1
- package/esm/model/methods/accessor-methods.d.ts +13 -0
- package/esm/model/methods/accessor-methods.d.ts.map +1 -0
- package/esm/model/methods/accessor-methods.js +51 -0
- package/esm/model/methods/accessor-methods.js.map +1 -0
- package/esm/model/methods/delete-methods.d.ts +10 -0
- package/esm/model/methods/delete-methods.d.ts.map +1 -0
- package/esm/model/methods/delete-methods.js +10 -0
- package/esm/model/methods/delete-methods.js.map +1 -0
- package/esm/model/methods/dirty-methods.d.ts +10 -0
- package/esm/model/methods/dirty-methods.d.ts.map +1 -0
- package/esm/model/methods/dirty-methods.js +15 -0
- package/esm/model/methods/dirty-methods.js.map +1 -0
- package/esm/model/methods/hydration-methods.d.ts +10 -0
- package/esm/model/methods/hydration-methods.d.ts.map +1 -0
- package/esm/model/methods/hydration-methods.js +57 -0
- package/esm/model/methods/hydration-methods.js.map +1 -0
- package/esm/model/methods/instance-event-methods.d.ts +7 -0
- package/esm/model/methods/instance-event-methods.d.ts.map +1 -0
- package/esm/model/methods/instance-event-methods.js +15 -0
- package/esm/model/methods/instance-event-methods.js.map +1 -0
- package/esm/model/methods/meta-methods.d.ts +7 -0
- package/esm/model/methods/meta-methods.d.ts.map +1 -0
- package/esm/model/methods/meta-methods.js +78 -0
- package/esm/model/methods/meta-methods.js.map +1 -0
- package/esm/model/methods/query-methods.d.ts +24 -0
- package/esm/model/methods/query-methods.d.ts.map +1 -0
- package/esm/model/methods/query-methods.js +161 -0
- package/esm/model/methods/query-methods.js.map +1 -0
- package/esm/model/methods/restore-methods.d.ts +10 -0
- package/esm/model/methods/restore-methods.d.ts.map +1 -0
- package/esm/model/methods/restore-methods.js +13 -0
- package/esm/model/methods/restore-methods.js.map +1 -0
- package/esm/model/methods/scope-methods.d.ts +7 -0
- package/esm/model/methods/scope-methods.d.ts.map +1 -0
- package/esm/model/methods/scope-methods.js +15 -0
- package/esm/model/methods/scope-methods.js.map +1 -0
- package/esm/model/methods/serialization-methods.d.ts +3 -0
- package/esm/model/methods/serialization-methods.d.ts.map +1 -0
- package/esm/model/methods/serialization-methods.js +27 -0
- package/esm/model/methods/serialization-methods.js.map +1 -0
- package/esm/model/methods/static-event-methods.d.ts +9 -0
- package/esm/model/methods/static-event-methods.d.ts.map +1 -0
- package/esm/model/methods/static-event-methods.js +29 -0
- package/esm/model/methods/static-event-methods.js.map +1 -0
- package/esm/model/methods/write-methods.d.ts +10 -0
- package/esm/model/methods/write-methods.d.ts.map +1 -0
- package/esm/model/methods/write-methods.js +52 -0
- package/esm/model/methods/write-methods.js.map +1 -0
- package/esm/model/model.d.ts +89 -58
- package/esm/model/model.d.ts.map +1 -1
- package/esm/model/model.js +166 -424
- package/esm/model/model.js.map +1 -1
- package/esm/model/model.types.d.ts +44 -0
- package/esm/model/model.types.d.ts.map +1 -0
- package/esm/model/register-model.d.ts +3 -3
- package/esm/model/register-model.d.ts.map +1 -1
- package/esm/query-builder/query-builder.d.ts +556 -0
- package/esm/query-builder/query-builder.d.ts.map +1 -0
- package/esm/query-builder/query-builder.js +1070 -0
- package/esm/query-builder/query-builder.js.map +1 -0
- package/esm/relations/helpers.d.ts.map +1 -1
- package/esm/relations/helpers.js +4 -0
- package/esm/relations/helpers.js.map +1 -1
- package/esm/relations/index.d.ts +2 -0
- package/esm/relations/index.d.ts.map +1 -1
- package/esm/relations/relation-hydrator.d.ts +68 -0
- package/esm/relations/relation-hydrator.d.ts.map +1 -0
- package/esm/relations/relation-hydrator.js +81 -0
- package/esm/relations/relation-hydrator.js.map +1 -0
- package/esm/relations/relation-loader.js +1 -1
- package/esm/relations/relation-loader.js.map +1 -1
- package/esm/relations/types.d.ts +26 -0
- package/esm/relations/types.d.ts.map +1 -1
- package/esm/sql-database-dirty-tracker.d.ts +13 -0
- package/esm/sql-database-dirty-tracker.d.ts.map +1 -0
- package/esm/sql-database-dirty-tracker.js +14 -0
- package/esm/sql-database-dirty-tracker.js.map +1 -0
- package/esm/types.d.ts +59 -0
- package/esm/types.d.ts.map +1 -1
- package/esm/utils/connect-to-database.d.ts +50 -1
- package/esm/utils/connect-to-database.d.ts.map +1 -1
- package/esm/utils/connect-to-database.js +14 -1
- package/esm/utils/connect-to-database.js.map +1 -1
- package/esm/utils/database-writer.utils.d.ts +1 -1
- package/esm/utils/database-writer.utils.d.ts.map +1 -1
- package/esm/utils/is-valid-date-value.d.ts +5 -0
- package/esm/utils/is-valid-date-value.d.ts.map +1 -0
- package/esm/utils/is-valid-date-value.js +25 -0
- package/esm/utils/is-valid-date-value.js.map +1 -0
- package/esm/utils/once-connected.d.ts.map +1 -1
- package/esm/utils/once-connected.js +4 -5
- package/esm/utils/once-connected.js.map +1 -1
- package/esm/writer/database-writer.d.ts.map +1 -1
- package/esm/writer/database-writer.js +7 -6
- package/esm/writer/database-writer.js.map +1 -1
- package/package.json +4 -4
package/esm/model/model.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
* Sentinel value used to distinguish between undefined and missing fields.
|
|
3
|
-
*/
|
|
4
|
-
const MISSING_VALUE = Symbol("missing");
|
|
5
|
-
/**
|
|
6
|
-
* WeakMap registry that associates each model constructor with its own event emitter.
|
|
7
|
-
*/
|
|
8
|
-
const modelEventsRegistry = new WeakMap();
|
|
9
|
-
/**
|
|
1
|
+
import {ModelEvents}from'../events/model-events.js';import {RelationLoader}from'../relations/relation-loader.js';import {modelSync}from'../sync/model-sync.js';import {getFieldValue,getOnlyFields,getStringField,getNumberField,getBooleanField,setFieldValue,hasField,incrementField,decrementField,unsetFields,mergeFields}from'./methods/accessor-methods.js';import {destroyModel,deleteRecords,deleteOneRecord}from'./methods/delete-methods.js';import {checkHasChanges,checkIsDirty,getDirtyColumnsWithValues,getRemovedColumns,getDirtyColumns}from'./methods/dirty-methods.js';import {cloneModel,deepFreezeObject,replaceModelData,serializeModel,modelToSnapshot,modelFromSnapshot,hydrateModel}from'./methods/hydration-methods.js';import {emitModelEvent,onModelEvent,onceModelEvent,offModelEvent}from'./methods/instance-event-methods.js';import {performAtomicUpdate,performAtomicIncrement,performAtomicDecrement,generateModelNextId,applyDefaultsToModel}from'./methods/meta-methods.js';import {resolveDataSource,buildQuery,buildNewQueryBuilder,findFirst,findLast,countRecords,findById,findAll,paginateRecords,findLatest,increaseField,decreaseField,performAtomic,updateById,findAndUpdateRecords,findOneAndUpdateRecord,findAndReplaceRecord,findOneAndDeleteRecord}from'./methods/query-methods.js';import {restoreRecord,restoreAllRecords}from'./methods/restore-methods.js';import {addGlobalModelScope,removeGlobalModelScope,addLocalModelScope,removeLocalModelScope}from'./methods/scope-methods.js';import {modelToJSON}from'./methods/serialization-methods.js';import {cleanupModelEvents,getModelEvents,onStaticEvent,onceStaticEvent,offStaticEvent,getGlobalEvents}from'./methods/static-event-methods.js';import {createRecord,createManyRecords,findOrCreateRecord,upsertRecord,saveModel}from'./methods/write-methods.js';import {getModelFromRegistry,getAllModelsFromRegistry}from'./register-model.js';/**
|
|
10
2
|
* Base class that powers all Cascade models.
|
|
11
3
|
*
|
|
12
4
|
* Provides:
|
|
@@ -261,11 +253,11 @@ class Model {
|
|
|
261
253
|
/**
|
|
262
254
|
* Created at column name.
|
|
263
255
|
*/
|
|
264
|
-
static createdAtColumn
|
|
256
|
+
static createdAtColumn;
|
|
265
257
|
/**
|
|
266
258
|
* Updated at column name.
|
|
267
259
|
*/
|
|
268
|
-
static updatedAtColumn
|
|
260
|
+
static updatedAtColumn;
|
|
269
261
|
/**
|
|
270
262
|
* Delete strategy for this model.
|
|
271
263
|
*
|
|
@@ -404,6 +396,22 @@ class Model {
|
|
|
404
396
|
* Column name for active status.
|
|
405
397
|
*/
|
|
406
398
|
isActiveColumn = "isActive";
|
|
399
|
+
/**
|
|
400
|
+
* Constructs a new model instance with optional initial data.
|
|
401
|
+
*
|
|
402
|
+
* Initializes the dirty tracker with a snapshot of the provided data.
|
|
403
|
+
*
|
|
404
|
+
* @param initialData - Partial data to populate the model
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```typescript
|
|
408
|
+
* const user = new User({ name: "Alice", email: "alice@example.com" });
|
|
409
|
+
* ```
|
|
410
|
+
*/
|
|
411
|
+
constructor(initialData = {}) {
|
|
412
|
+
this.data = initialData;
|
|
413
|
+
this.dirtyTracker = this.self().getDriver().getDirtyTracker(this.data);
|
|
414
|
+
}
|
|
407
415
|
/**
|
|
408
416
|
* Lazily load one or more relations for this model instance.
|
|
409
417
|
*
|
|
@@ -472,22 +480,6 @@ class Model {
|
|
|
472
480
|
getRelation(relationName) {
|
|
473
481
|
return this.loadedRelations.get(relationName);
|
|
474
482
|
}
|
|
475
|
-
/**
|
|
476
|
-
* Constructs a new model instance with optional initial data.
|
|
477
|
-
*
|
|
478
|
-
* Initializes the dirty tracker with a snapshot of the provided data.
|
|
479
|
-
*
|
|
480
|
-
* @param initialData - Partial data to populate the model
|
|
481
|
-
*
|
|
482
|
-
* @example
|
|
483
|
-
* ```typescript
|
|
484
|
-
* const user = new User({ name: "Alice", email: "alice@example.com" });
|
|
485
|
-
* ```
|
|
486
|
-
*/
|
|
487
|
-
constructor(initialData = {}) {
|
|
488
|
-
this.data = initialData;
|
|
489
|
-
this.dirtyTracker = new DatabaseDirtyTracker(this.data);
|
|
490
|
-
}
|
|
491
483
|
/**
|
|
492
484
|
* Get a model class by its name from the global registry.
|
|
493
485
|
*
|
|
@@ -572,60 +564,53 @@ class Model {
|
|
|
572
564
|
get id() {
|
|
573
565
|
return this.get("id");
|
|
574
566
|
}
|
|
567
|
+
/**
|
|
568
|
+
* Get uuid
|
|
569
|
+
*/
|
|
570
|
+
get uuid() {
|
|
571
|
+
return this.get("id");
|
|
572
|
+
}
|
|
575
573
|
get(field, defaultValue) {
|
|
576
|
-
return
|
|
574
|
+
return getFieldValue(this, field, defaultValue);
|
|
577
575
|
}
|
|
578
576
|
only(fields) {
|
|
579
|
-
return
|
|
577
|
+
return getOnlyFields(this, fields);
|
|
580
578
|
}
|
|
581
579
|
/**
|
|
582
580
|
* Get a string value
|
|
583
581
|
*/
|
|
584
582
|
string(key, defaultValue) {
|
|
585
|
-
return this
|
|
583
|
+
return getStringField(this, key, defaultValue);
|
|
586
584
|
}
|
|
587
585
|
/**
|
|
588
586
|
* Get a number value
|
|
589
587
|
*/
|
|
590
588
|
number(key, defaultValue) {
|
|
591
|
-
return this
|
|
589
|
+
return getNumberField(this, key, defaultValue);
|
|
592
590
|
}
|
|
593
591
|
/**
|
|
594
592
|
* Get a boolean value
|
|
595
593
|
*/
|
|
596
594
|
boolean(key, defaultValue) {
|
|
597
|
-
return this
|
|
595
|
+
return getBooleanField(this, key, defaultValue);
|
|
598
596
|
}
|
|
599
597
|
set(field, value) {
|
|
600
|
-
|
|
601
|
-
set(this.data, path, value);
|
|
602
|
-
const partial = {};
|
|
603
|
-
set(partial, path, value);
|
|
604
|
-
this.dirtyTracker.mergeChanges(partial);
|
|
605
|
-
return this;
|
|
598
|
+
return setFieldValue(this, field, value);
|
|
606
599
|
}
|
|
607
600
|
has(field) {
|
|
608
|
-
return
|
|
601
|
+
return hasField(this, field);
|
|
609
602
|
}
|
|
610
603
|
increment(field, amount) {
|
|
611
|
-
|
|
612
|
-
const incrementedValue = value + (amount ?? 1);
|
|
613
|
-
return this.set(field, incrementedValue);
|
|
604
|
+
return incrementField(this, field, amount);
|
|
614
605
|
}
|
|
615
606
|
decrement(field, amount) {
|
|
616
|
-
|
|
617
|
-
const decrementedValue = value - (amount ?? 1);
|
|
618
|
-
return this.set(field, decrementedValue);
|
|
607
|
+
return decrementField(this, field, amount);
|
|
619
608
|
}
|
|
620
609
|
unset(...fields) {
|
|
621
|
-
|
|
622
|
-
this.dirtyTracker.unset(fields);
|
|
623
|
-
return this;
|
|
610
|
+
return unsetFields(this, ...fields);
|
|
624
611
|
}
|
|
625
612
|
merge(values) {
|
|
626
|
-
|
|
627
|
-
this.dirtyTracker.mergeChanges(values);
|
|
628
|
-
return this;
|
|
613
|
+
return mergeFields(this, values);
|
|
629
614
|
}
|
|
630
615
|
/**
|
|
631
616
|
* Perform atomoic update from current model instance
|
|
@@ -634,7 +619,7 @@ class Model {
|
|
|
634
619
|
* @returns number of affected records
|
|
635
620
|
*/
|
|
636
621
|
async atomicUpdate(operations) {
|
|
637
|
-
return
|
|
622
|
+
return performAtomicUpdate(this, operations);
|
|
638
623
|
}
|
|
639
624
|
/**
|
|
640
625
|
* Perform atomic increment
|
|
@@ -642,12 +627,7 @@ class Model {
|
|
|
642
627
|
* saving the model
|
|
643
628
|
*/
|
|
644
629
|
async atomicIncrement(field, amount = 1) {
|
|
645
|
-
this
|
|
646
|
-
return this.atomicUpdate({
|
|
647
|
-
$inc: {
|
|
648
|
-
[field]: amount,
|
|
649
|
-
},
|
|
650
|
-
});
|
|
630
|
+
return performAtomicIncrement(this, field, amount);
|
|
651
631
|
}
|
|
652
632
|
/**
|
|
653
633
|
* Perform atomic decrement
|
|
@@ -655,12 +635,7 @@ class Model {
|
|
|
655
635
|
* saving the model
|
|
656
636
|
*/
|
|
657
637
|
async atomicDecrement(field, amount = 1) {
|
|
658
|
-
this
|
|
659
|
-
return this.atomicUpdate({
|
|
660
|
-
$inc: {
|
|
661
|
-
[field]: -amount,
|
|
662
|
-
},
|
|
663
|
-
});
|
|
638
|
+
return performAtomicDecrement(this, field, amount);
|
|
664
639
|
}
|
|
665
640
|
/**
|
|
666
641
|
* Determine if current model is active
|
|
@@ -706,7 +681,7 @@ class Model {
|
|
|
706
681
|
* ```
|
|
707
682
|
*/
|
|
708
683
|
hasChanges() {
|
|
709
|
-
return this
|
|
684
|
+
return checkHasChanges(this);
|
|
710
685
|
}
|
|
711
686
|
/**
|
|
712
687
|
* Check if the given column has been modified.
|
|
@@ -721,7 +696,7 @@ class Model {
|
|
|
721
696
|
* ```
|
|
722
697
|
*/
|
|
723
698
|
isDirty(column) {
|
|
724
|
-
return this
|
|
699
|
+
return checkIsDirty(this, column);
|
|
725
700
|
}
|
|
726
701
|
/**
|
|
727
702
|
* Retrieves all dirty columns with their old and new values.
|
|
@@ -736,7 +711,7 @@ class Model {
|
|
|
736
711
|
* ```
|
|
737
712
|
*/
|
|
738
713
|
getDirtyColumnsWithValues() {
|
|
739
|
-
return
|
|
714
|
+
return getDirtyColumnsWithValues(this);
|
|
740
715
|
}
|
|
741
716
|
/**
|
|
742
717
|
* Lists all columns that have been removed from the model's data.
|
|
@@ -750,7 +725,7 @@ class Model {
|
|
|
750
725
|
* ```
|
|
751
726
|
*/
|
|
752
727
|
getRemovedColumns() {
|
|
753
|
-
return
|
|
728
|
+
return getRemovedColumns(this);
|
|
754
729
|
}
|
|
755
730
|
/**
|
|
756
731
|
* Lists all columns that have been modified since instantiation or last reset.
|
|
@@ -764,7 +739,7 @@ class Model {
|
|
|
764
739
|
* ```
|
|
765
740
|
*/
|
|
766
741
|
getDirtyColumns() {
|
|
767
|
-
return
|
|
742
|
+
return getDirtyColumns(this);
|
|
768
743
|
}
|
|
769
744
|
/**
|
|
770
745
|
* Emits a lifecycle event to both per-model and global listeners.
|
|
@@ -782,13 +757,7 @@ class Model {
|
|
|
782
757
|
* ```
|
|
783
758
|
*/
|
|
784
759
|
async emitEvent(event, context) {
|
|
785
|
-
|
|
786
|
-
// Trigger instance events
|
|
787
|
-
await this.events.emit(event, this, context);
|
|
788
|
-
// Trigger static events
|
|
789
|
-
await ctor.events().emit(event, this, context);
|
|
790
|
-
// Trigger global events
|
|
791
|
-
await globalModelEvents.emit(event, this, context);
|
|
760
|
+
return emitModelEvent(this, event, context);
|
|
792
761
|
}
|
|
793
762
|
/**
|
|
794
763
|
* Register a listener for a model lifecycle event on this instance.
|
|
@@ -798,7 +767,7 @@ class Model {
|
|
|
798
767
|
* @returns Unsubscribe function
|
|
799
768
|
*/
|
|
800
769
|
on(event, listener) {
|
|
801
|
-
return this
|
|
770
|
+
return onModelEvent(this, event, listener);
|
|
802
771
|
}
|
|
803
772
|
/**
|
|
804
773
|
* Register a one-time listener for a model lifecycle event on this instance.
|
|
@@ -808,7 +777,7 @@ class Model {
|
|
|
808
777
|
* @returns Unsubscribe function
|
|
809
778
|
*/
|
|
810
779
|
once(event, listener) {
|
|
811
|
-
return this
|
|
780
|
+
return onceModelEvent(this, event, listener);
|
|
812
781
|
}
|
|
813
782
|
/**
|
|
814
783
|
* Remove a listener from this instance.
|
|
@@ -817,7 +786,7 @@ class Model {
|
|
|
817
786
|
* @param listener - Callback function to remove
|
|
818
787
|
*/
|
|
819
788
|
off(event, listener) {
|
|
820
|
-
this
|
|
789
|
+
offModelEvent(this, event, listener);
|
|
821
790
|
}
|
|
822
791
|
/**
|
|
823
792
|
* Resolves the data source associated with this model.
|
|
@@ -840,42 +809,19 @@ class Model {
|
|
|
840
809
|
* ```
|
|
841
810
|
*/
|
|
842
811
|
static getDataSource() {
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
}
|
|
851
|
-
else {
|
|
852
|
-
dataSource = dataSourceRegistry.get();
|
|
853
|
-
}
|
|
854
|
-
// Apply model defaults from data source (only once per model class)
|
|
855
|
-
if (!this.hasOwnProperty("_defaultsApplied")) {
|
|
856
|
-
// Merge defaults hierarchy: driver defaults < dataSource modelDefaults
|
|
857
|
-
const driverDefaults = dataSource.driver.modelDefaults || {};
|
|
858
|
-
const dataSourceDefaults = dataSource.modelDefaults || {};
|
|
859
|
-
// Merge with dataSource modelDefaults taking priority over driver defaults
|
|
860
|
-
const mergedDefaults = {
|
|
861
|
-
...driverDefaults,
|
|
862
|
-
...dataSourceDefaults,
|
|
863
|
-
};
|
|
864
|
-
// Apply merged defaults to model
|
|
865
|
-
if (Object.keys(mergedDefaults).length > 0) {
|
|
866
|
-
this.applyModelDefaults(mergedDefaults);
|
|
867
|
-
}
|
|
868
|
-
this._defaultsApplied = true;
|
|
869
|
-
}
|
|
870
|
-
return dataSource;
|
|
812
|
+
return resolveDataSource(this);
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Get driver instance
|
|
816
|
+
*/
|
|
817
|
+
static getDriver() {
|
|
818
|
+
return this.getDataSource().driver;
|
|
871
819
|
}
|
|
872
820
|
/**
|
|
873
821
|
* Generate next id and set it to current model's id
|
|
874
822
|
*/
|
|
875
823
|
async generateNextId() {
|
|
876
|
-
|
|
877
|
-
await writer.generateNextId();
|
|
878
|
-
return this.id;
|
|
824
|
+
return generateModelNextId(this);
|
|
879
825
|
}
|
|
880
826
|
/**
|
|
881
827
|
* Apply model defaults from data source configuration.
|
|
@@ -893,58 +839,7 @@ class Model {
|
|
|
893
839
|
* @param defaults - Model default configuration from data source
|
|
894
840
|
*/
|
|
895
841
|
static applyModelDefaults(defaults) {
|
|
896
|
-
|
|
897
|
-
// ============================================================================
|
|
898
|
-
// ID Generation
|
|
899
|
-
// ============================================================================
|
|
900
|
-
if (defaults.autoGenerateId !== undefined && this.autoGenerateId === undefined) {
|
|
901
|
-
this.autoGenerateId = defaults.autoGenerateId;
|
|
902
|
-
}
|
|
903
|
-
if (defaults.initialId !== undefined && this.initialId === undefined) {
|
|
904
|
-
this.initialId = defaults.initialId;
|
|
905
|
-
}
|
|
906
|
-
if (defaults.randomInitialId !== undefined && this.randomInitialId === undefined) {
|
|
907
|
-
this.randomInitialId = defaults.randomInitialId;
|
|
908
|
-
}
|
|
909
|
-
if (defaults.incrementIdBy !== undefined && this.incrementIdBy === undefined) {
|
|
910
|
-
this.incrementIdBy = defaults.incrementIdBy;
|
|
911
|
-
}
|
|
912
|
-
if (defaults.randomIncrement !== undefined && this.randomIncrement === undefined) {
|
|
913
|
-
this.randomIncrement = defaults.randomIncrement;
|
|
914
|
-
}
|
|
915
|
-
// ============================================================================
|
|
916
|
-
// Timestamps
|
|
917
|
-
// ============================================================================
|
|
918
|
-
if (defaults.createdAtColumn !== undefined && this.createdAtColumn === undefined) {
|
|
919
|
-
this.createdAtColumn = defaults.createdAtColumn;
|
|
920
|
-
}
|
|
921
|
-
if (defaults.updatedAtColumn !== undefined && this.updatedAtColumn === undefined) {
|
|
922
|
-
this.updatedAtColumn = defaults.updatedAtColumn;
|
|
923
|
-
}
|
|
924
|
-
// ============================================================================
|
|
925
|
-
// Deletion
|
|
926
|
-
// ============================================================================
|
|
927
|
-
if (defaults.deleteStrategy !== undefined && this.deleteStrategy === undefined) {
|
|
928
|
-
this.deleteStrategy = defaults.deleteStrategy;
|
|
929
|
-
}
|
|
930
|
-
if (defaults.deletedAtColumn !== undefined && this.deletedAtColumn === undefined) {
|
|
931
|
-
this.deletedAtColumn = defaults.deletedAtColumn;
|
|
932
|
-
}
|
|
933
|
-
if (defaults.trashTable !== undefined && this.trashTable === undefined) {
|
|
934
|
-
// Handle function-based trash table
|
|
935
|
-
if (typeof defaults.trashTable === "function") {
|
|
936
|
-
this.trashTable = defaults.trashTable(this.table);
|
|
937
|
-
}
|
|
938
|
-
else {
|
|
939
|
-
this.trashTable = defaults.trashTable;
|
|
940
|
-
}
|
|
941
|
-
}
|
|
942
|
-
// ============================================================================
|
|
943
|
-
// Validation
|
|
944
|
-
// ============================================================================
|
|
945
|
-
if (defaults.strictMode !== undefined && this.strictMode === undefined) {
|
|
946
|
-
this.strictMode = defaults.strictMode;
|
|
947
|
-
}
|
|
842
|
+
applyDefaultsToModel(this, defaults);
|
|
948
843
|
}
|
|
949
844
|
/**
|
|
950
845
|
* Add a global scope that is automatically applied to all queries.
|
|
@@ -970,10 +865,7 @@ class Model {
|
|
|
970
865
|
* ```
|
|
971
866
|
*/
|
|
972
867
|
static addGlobalScope(name, callback, options = {}) {
|
|
973
|
-
this
|
|
974
|
-
callback,
|
|
975
|
-
timing: options.timing || "before",
|
|
976
|
-
});
|
|
868
|
+
addGlobalModelScope(this, name, callback, options);
|
|
977
869
|
}
|
|
978
870
|
/**
|
|
979
871
|
* Remove a global scope by name.
|
|
@@ -986,7 +878,7 @@ class Model {
|
|
|
986
878
|
* ```
|
|
987
879
|
*/
|
|
988
880
|
static removeGlobalScope(name) {
|
|
989
|
-
this
|
|
881
|
+
removeGlobalModelScope(this, name);
|
|
990
882
|
}
|
|
991
883
|
/**
|
|
992
884
|
* Add a local scope that can be manually applied to queries.
|
|
@@ -1014,7 +906,7 @@ class Model {
|
|
|
1014
906
|
* ```
|
|
1015
907
|
*/
|
|
1016
908
|
static addScope(name, callback) {
|
|
1017
|
-
this
|
|
909
|
+
addLocalModelScope(this, name, callback);
|
|
1018
910
|
}
|
|
1019
911
|
/**
|
|
1020
912
|
* Remove a local scope by name.
|
|
@@ -1027,51 +919,13 @@ class Model {
|
|
|
1027
919
|
* ```
|
|
1028
920
|
*/
|
|
1029
921
|
static removeScope(name) {
|
|
1030
|
-
this
|
|
922
|
+
removeLocalModelScope(this, name);
|
|
1031
923
|
}
|
|
1032
924
|
/**
|
|
1033
925
|
* Create a new query builder for this model
|
|
1034
926
|
*/
|
|
1035
927
|
static query() {
|
|
1036
|
-
|
|
1037
|
-
const queryBuilder = this.newQueryBuilder();
|
|
1038
|
-
const ModelClass = this;
|
|
1039
|
-
const qb = queryBuilder; // Capture for closure access
|
|
1040
|
-
// Collect global scopes from base Model and child model
|
|
1041
|
-
const allGlobalScopes = new Map([
|
|
1042
|
-
...Model.globalScopes,
|
|
1043
|
-
...this.globalScopes,
|
|
1044
|
-
]);
|
|
1045
|
-
// Pass scopes to query builder
|
|
1046
|
-
queryBuilder.pendingGlobalScopes = allGlobalScopes;
|
|
1047
|
-
queryBuilder.availableLocalScopes = this.localScopes;
|
|
1048
|
-
queryBuilder.disabledGlobalScopes = new Set();
|
|
1049
|
-
// Pass relation definitions for joinWith() support
|
|
1050
|
-
queryBuilder.relationDefinitions = this.relations;
|
|
1051
|
-
queryBuilder.modelClass = ModelClass;
|
|
1052
|
-
// Emit fetching event
|
|
1053
|
-
this.events().emitFetching(queryBuilder, { table: this.table, modelClass: this });
|
|
1054
|
-
queryBuilder.hydrate((data) => {
|
|
1055
|
-
return this.readFrom(data);
|
|
1056
|
-
});
|
|
1057
|
-
// Wire up onFetched callback to load relations and emit model-level event
|
|
1058
|
-
queryBuilder.onFetched(async (models) => {
|
|
1059
|
-
// Load eager relations if any were requested
|
|
1060
|
-
const eagerRelations = qb.eagerLoadRelations;
|
|
1061
|
-
if (eagerRelations && eagerRelations.size > 0 && models.length > 0) {
|
|
1062
|
-
// Build constraints object from the Map
|
|
1063
|
-
const constraints = {};
|
|
1064
|
-
for (const [name, constraint] of eagerRelations) {
|
|
1065
|
-
if (typeof constraint === "function") {
|
|
1066
|
-
constraints[name] = constraint;
|
|
1067
|
-
}
|
|
1068
|
-
}
|
|
1069
|
-
const loader = new RelationLoader(models, ModelClass);
|
|
1070
|
-
await loader.load([...eagerRelations.keys()], constraints);
|
|
1071
|
-
}
|
|
1072
|
-
await this.events().emit("fetched", models, {});
|
|
1073
|
-
});
|
|
1074
|
-
return queryBuilder;
|
|
928
|
+
return buildQuery(this, Model);
|
|
1075
929
|
}
|
|
1076
930
|
static with(...args) {
|
|
1077
931
|
return this.query().with(...args);
|
|
@@ -1124,35 +978,19 @@ class Model {
|
|
|
1124
978
|
* ```
|
|
1125
979
|
*/
|
|
1126
980
|
static newQueryBuilder() {
|
|
1127
|
-
|
|
1128
|
-
// Check if model has a custom builder class
|
|
1129
|
-
if (this.builder) {
|
|
1130
|
-
const BuilderClass = this.builder;
|
|
1131
|
-
return new BuilderClass(this.table, dataSource);
|
|
1132
|
-
}
|
|
1133
|
-
// Use default driver query builder
|
|
1134
|
-
const queryBuilder = dataSource.driver.queryBuilder(this.table);
|
|
1135
|
-
return queryBuilder;
|
|
981
|
+
return buildNewQueryBuilder(this);
|
|
1136
982
|
}
|
|
1137
983
|
/**
|
|
1138
984
|
* Get First matched record for the given filter
|
|
1139
985
|
*/
|
|
1140
986
|
static async first(filter) {
|
|
1141
|
-
|
|
1142
|
-
if (filter) {
|
|
1143
|
-
query.where(filter);
|
|
1144
|
-
}
|
|
1145
|
-
return query.first();
|
|
987
|
+
return findFirst(this, filter);
|
|
1146
988
|
}
|
|
1147
989
|
/**
|
|
1148
990
|
* Get last matched record for the given filter
|
|
1149
991
|
*/
|
|
1150
992
|
static async last(filter) {
|
|
1151
|
-
|
|
1152
|
-
if (filter) {
|
|
1153
|
-
query.where(filter);
|
|
1154
|
-
}
|
|
1155
|
-
return query.last();
|
|
993
|
+
return findLast(this, filter);
|
|
1156
994
|
}
|
|
1157
995
|
static where(...args) {
|
|
1158
996
|
return this.query().where(...args);
|
|
@@ -1162,18 +1000,13 @@ class Model {
|
|
|
1162
1000
|
* @param filter - The filter to apply to the query
|
|
1163
1001
|
*/
|
|
1164
1002
|
static count(filter) {
|
|
1165
|
-
|
|
1166
|
-
if (filter) {
|
|
1167
|
-
query.where(filter);
|
|
1168
|
-
}
|
|
1169
|
-
return query.count();
|
|
1003
|
+
return countRecords(this, filter);
|
|
1170
1004
|
}
|
|
1171
1005
|
/**
|
|
1172
1006
|
* Find record by id
|
|
1173
1007
|
*/
|
|
1174
1008
|
static async find(id) {
|
|
1175
|
-
|
|
1176
|
-
return query.where(this.primaryKey, id).first();
|
|
1009
|
+
return findById(this, id);
|
|
1177
1010
|
}
|
|
1178
1011
|
/**
|
|
1179
1012
|
* Get all records from the table
|
|
@@ -1182,24 +1015,13 @@ class Model {
|
|
|
1182
1015
|
* @returns All records from the table
|
|
1183
1016
|
*/
|
|
1184
1017
|
static async all(filter) {
|
|
1185
|
-
|
|
1186
|
-
if (filter) {
|
|
1187
|
-
query.where(filter);
|
|
1188
|
-
}
|
|
1189
|
-
return query.get();
|
|
1018
|
+
return findAll(this, filter);
|
|
1190
1019
|
}
|
|
1191
1020
|
/**
|
|
1192
1021
|
* Perform pagination
|
|
1193
1022
|
*/
|
|
1194
|
-
static async paginate(
|
|
1195
|
-
|
|
1196
|
-
if (filter) {
|
|
1197
|
-
query.where(filter);
|
|
1198
|
-
}
|
|
1199
|
-
return query.paginate({
|
|
1200
|
-
limit: limit,
|
|
1201
|
-
page: page,
|
|
1202
|
-
});
|
|
1023
|
+
static async paginate(options = {}) {
|
|
1024
|
+
return paginateRecords(this, options);
|
|
1203
1025
|
}
|
|
1204
1026
|
/**
|
|
1205
1027
|
* Get latest records from the table
|
|
@@ -1207,14 +1029,10 @@ class Model {
|
|
|
1207
1029
|
* @param filter - The filter to apply to the query
|
|
1208
1030
|
*/
|
|
1209
1031
|
static async latest(filter) {
|
|
1210
|
-
|
|
1211
|
-
if (filter) {
|
|
1212
|
-
query.where(filter);
|
|
1213
|
-
}
|
|
1214
|
-
return (await query.latest());
|
|
1032
|
+
return findLatest(this, filter);
|
|
1215
1033
|
}
|
|
1216
1034
|
/**
|
|
1217
|
-
* Increment the given field by the given amount
|
|
1035
|
+
* Increment the given field by the given amount using atomic update
|
|
1218
1036
|
*
|
|
1219
1037
|
* @example ```typescript
|
|
1220
1038
|
* // Increase age by 1 for user id 1
|
|
@@ -1224,11 +1042,10 @@ class Model {
|
|
|
1224
1042
|
* ```
|
|
1225
1043
|
*/
|
|
1226
1044
|
static increase(filter, field, amount) {
|
|
1227
|
-
|
|
1228
|
-
return query.increment(field, amount);
|
|
1045
|
+
return increaseField(this, filter, field, amount);
|
|
1229
1046
|
}
|
|
1230
1047
|
/**
|
|
1231
|
-
* Decrement the given field by the given amount
|
|
1048
|
+
* Decrement the given field by the given amount using atomic update
|
|
1232
1049
|
* @example ```typescript
|
|
1233
1050
|
* // Decrease age by 1 for user id 1
|
|
1234
1051
|
* User.decrement({id: 1}, "age", 1);
|
|
@@ -1237,16 +1054,7 @@ class Model {
|
|
|
1237
1054
|
* ```
|
|
1238
1055
|
*/
|
|
1239
1056
|
static decrease(filter, field, amount) {
|
|
1240
|
-
|
|
1241
|
-
return query.decrement(field, amount);
|
|
1242
|
-
}
|
|
1243
|
-
/**
|
|
1244
|
-
* Create a new instance from the given data
|
|
1245
|
-
*/
|
|
1246
|
-
static readFrom(data) {
|
|
1247
|
-
const model = new this(data);
|
|
1248
|
-
model.isNew = false;
|
|
1249
|
-
return model;
|
|
1057
|
+
return decreaseField(this, filter, field, amount);
|
|
1250
1058
|
}
|
|
1251
1059
|
/**
|
|
1252
1060
|
* Perform atomic operation
|
|
@@ -1257,9 +1065,13 @@ class Model {
|
|
|
1257
1065
|
* Returns user model with updated age
|
|
1258
1066
|
*/
|
|
1259
1067
|
static async atomic(filter, operations) {
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1068
|
+
return performAtomic(this, filter, operations);
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Perform a direct update $set for the given id
|
|
1072
|
+
*/
|
|
1073
|
+
static async update(id, data) {
|
|
1074
|
+
return updateById(this, id, data);
|
|
1263
1075
|
}
|
|
1264
1076
|
/**
|
|
1265
1077
|
* Find one and update multiple records that matches the provided filter and return the updated record
|
|
@@ -1268,8 +1080,7 @@ class Model {
|
|
|
1268
1080
|
* @returns The updated records
|
|
1269
1081
|
*/
|
|
1270
1082
|
static async findAndUpdate(filter, update) {
|
|
1271
|
-
|
|
1272
|
-
return await this.query().where(filter).get();
|
|
1083
|
+
return findAndUpdateRecords(this, filter, update);
|
|
1273
1084
|
}
|
|
1274
1085
|
/**
|
|
1275
1086
|
* Find one and update a single record that matches the provided filter and return the updated record
|
|
@@ -1278,17 +1089,13 @@ class Model {
|
|
|
1278
1089
|
* @returns The updated record or null
|
|
1279
1090
|
*/
|
|
1280
1091
|
static async findOneAndUpdate(filter, update) {
|
|
1281
|
-
|
|
1282
|
-
const result = await dataSource.driver.findOneAndUpdate(this.table, filter, update);
|
|
1283
|
-
return result ? new this(result) : null;
|
|
1092
|
+
return findOneAndUpdateRecord(this, filter, update);
|
|
1284
1093
|
}
|
|
1285
1094
|
/**
|
|
1286
1095
|
* Find and replace the entire document that matches the provided filter and return the replaced document
|
|
1287
1096
|
*/
|
|
1288
1097
|
static async findAndReplace(filter, document) {
|
|
1289
|
-
|
|
1290
|
-
const result = await dataSource.driver.replace(this.table, filter, document);
|
|
1291
|
-
return result ? new this(result) : null;
|
|
1098
|
+
return findAndReplaceRecord(this, filter, document);
|
|
1292
1099
|
}
|
|
1293
1100
|
/**
|
|
1294
1101
|
* Destroy (delete) the current model instance from the database.
|
|
@@ -1309,8 +1116,7 @@ class Model {
|
|
|
1309
1116
|
* ```
|
|
1310
1117
|
*/
|
|
1311
1118
|
async destroy(options) {
|
|
1312
|
-
|
|
1313
|
-
return await remover.destroy(options);
|
|
1119
|
+
return destroyModel(this, options);
|
|
1314
1120
|
}
|
|
1315
1121
|
/**
|
|
1316
1122
|
* Get the class constructor from an instance.
|
|
@@ -1365,21 +1171,7 @@ class Model {
|
|
|
1365
1171
|
* ```
|
|
1366
1172
|
*/
|
|
1367
1173
|
clone() {
|
|
1368
|
-
|
|
1369
|
-
// This ensures nested objects are also copied
|
|
1370
|
-
const clonedData = JSON.parse(JSON.stringify(this.data));
|
|
1371
|
-
// Create a new instance of the same model class
|
|
1372
|
-
const ModelClass = this.self();
|
|
1373
|
-
const clonedModel = new ModelClass(clonedData);
|
|
1374
|
-
// Preserve the isNew state
|
|
1375
|
-
clonedModel.isNew = this.isNew;
|
|
1376
|
-
// Freeze the data to make it immutable
|
|
1377
|
-
// This recursively freezes all nested objects
|
|
1378
|
-
this.deepFreeze(clonedModel.data);
|
|
1379
|
-
// Reset the dirty tracker to have no changes
|
|
1380
|
-
// The clone represents a clean snapshot
|
|
1381
|
-
clonedModel.dirtyTracker.reset();
|
|
1382
|
-
return clonedModel;
|
|
1174
|
+
return cloneModel(this);
|
|
1383
1175
|
}
|
|
1384
1176
|
/**
|
|
1385
1177
|
* Recursively freezes an object and all its nested properties.
|
|
@@ -1388,19 +1180,7 @@ class Model {
|
|
|
1388
1180
|
* @returns The frozen object
|
|
1389
1181
|
*/
|
|
1390
1182
|
deepFreeze(obj) {
|
|
1391
|
-
|
|
1392
|
-
Object.freeze(obj);
|
|
1393
|
-
// Recursively freeze all properties
|
|
1394
|
-
Object.getOwnPropertyNames(obj).forEach((prop) => {
|
|
1395
|
-
const value = obj[prop];
|
|
1396
|
-
// Only freeze objects and arrays, skip primitives and null
|
|
1397
|
-
if (value !== null &&
|
|
1398
|
-
(typeof value === "object" || typeof value === "function") &&
|
|
1399
|
-
!Object.isFrozen(value)) {
|
|
1400
|
-
this.deepFreeze(value);
|
|
1401
|
-
}
|
|
1402
|
-
});
|
|
1403
|
-
return obj;
|
|
1183
|
+
return deepFreezeObject(obj);
|
|
1404
1184
|
}
|
|
1405
1185
|
/**
|
|
1406
1186
|
* Get table name
|
|
@@ -1442,13 +1222,13 @@ class Model {
|
|
|
1442
1222
|
* Delete all matching documents from the table.
|
|
1443
1223
|
*/
|
|
1444
1224
|
static async delete(filter) {
|
|
1445
|
-
return
|
|
1225
|
+
return deleteRecords(this, filter);
|
|
1446
1226
|
}
|
|
1447
1227
|
/**
|
|
1448
1228
|
* Delete a single matching document from the table.
|
|
1449
1229
|
*/
|
|
1450
1230
|
static async deleteOne(filter) {
|
|
1451
|
-
return
|
|
1231
|
+
return deleteOneRecord(this, filter);
|
|
1452
1232
|
}
|
|
1453
1233
|
/**
|
|
1454
1234
|
* Restore a single deleted record by its ID.
|
|
@@ -1476,12 +1256,7 @@ class Model {
|
|
|
1476
1256
|
* ```
|
|
1477
1257
|
*/
|
|
1478
1258
|
static async restore(id, options) {
|
|
1479
|
-
|
|
1480
|
-
const result = await restorer.restore(id, options);
|
|
1481
|
-
if (!result.restoredRecord) {
|
|
1482
|
-
throw new Error(`Failed to restore ${this.name} with ${this.primaryKey} ${id}: no record returned.`);
|
|
1483
|
-
}
|
|
1484
|
-
return result.restoredRecord;
|
|
1259
|
+
return restoreRecord(this, id, options);
|
|
1485
1260
|
}
|
|
1486
1261
|
/**
|
|
1487
1262
|
* Restore all deleted records for the model's table.
|
|
@@ -1502,12 +1277,7 @@ class Model {
|
|
|
1502
1277
|
* ```
|
|
1503
1278
|
*/
|
|
1504
1279
|
static async restoreAll(options) {
|
|
1505
|
-
|
|
1506
|
-
const result = await restorer.restoreAll(options);
|
|
1507
|
-
if (result.restoredCount === 0) {
|
|
1508
|
-
return [];
|
|
1509
|
-
}
|
|
1510
|
-
return result.restoredRecords;
|
|
1280
|
+
return restoreAllRecords(this, options);
|
|
1511
1281
|
}
|
|
1512
1282
|
/**
|
|
1513
1283
|
* Create a new record in database and return the model instance.
|
|
@@ -1529,15 +1299,13 @@ class Model {
|
|
|
1529
1299
|
* ```
|
|
1530
1300
|
*/
|
|
1531
1301
|
static async create(data) {
|
|
1532
|
-
|
|
1533
|
-
await model.save();
|
|
1534
|
-
return model;
|
|
1302
|
+
return createRecord(this, data);
|
|
1535
1303
|
}
|
|
1536
1304
|
/**
|
|
1537
1305
|
* Create many documents and return an array of created models
|
|
1538
1306
|
*/
|
|
1539
1307
|
static async createMany(data) {
|
|
1540
|
-
return
|
|
1308
|
+
return createManyRecords(this, data);
|
|
1541
1309
|
}
|
|
1542
1310
|
/**
|
|
1543
1311
|
* Find a record or create it if not found.
|
|
@@ -1561,13 +1329,7 @@ class Model {
|
|
|
1561
1329
|
* ```
|
|
1562
1330
|
*/
|
|
1563
1331
|
static async findOrCreate(filter, data) {
|
|
1564
|
-
|
|
1565
|
-
const existing = await this.first(filter);
|
|
1566
|
-
if (existing) {
|
|
1567
|
-
return existing; // Return as-is, no update
|
|
1568
|
-
}
|
|
1569
|
-
// Create new record with merged data
|
|
1570
|
-
return await this.create({ ...filter, ...data });
|
|
1332
|
+
return findOrCreateRecord(this, filter, data);
|
|
1571
1333
|
}
|
|
1572
1334
|
/**
|
|
1573
1335
|
* Upsert (insert or update) a record atomically.
|
|
@@ -1612,42 +1374,7 @@ class Model {
|
|
|
1612
1374
|
* ```
|
|
1613
1375
|
*/
|
|
1614
1376
|
static async upsert(filter, data, options) {
|
|
1615
|
-
|
|
1616
|
-
const mergedData = { ...filter, ...data };
|
|
1617
|
-
// Create a temporary model instance for validation and data preparation
|
|
1618
|
-
const tempModel = new this(mergedData);
|
|
1619
|
-
tempModel.isNew = true;
|
|
1620
|
-
// Emit saving event for validation context
|
|
1621
|
-
await tempModel.emitEvent("saving", {
|
|
1622
|
-
isInsert: true,
|
|
1623
|
-
options,
|
|
1624
|
-
mode: "upsert",
|
|
1625
|
-
});
|
|
1626
|
-
// Add timestamps
|
|
1627
|
-
const createdAtColumn = this.createdAtColumn;
|
|
1628
|
-
const updatedAtColumn = this.updatedAtColumn;
|
|
1629
|
-
if (createdAtColumn !== false && createdAtColumn !== undefined) {
|
|
1630
|
-
// Only set createdAt if not already set (for new records)
|
|
1631
|
-
const createdAtKey = createdAtColumn;
|
|
1632
|
-
if (!mergedData[createdAtKey]) {
|
|
1633
|
-
mergedData[createdAtKey] = new Date();
|
|
1634
|
-
}
|
|
1635
|
-
}
|
|
1636
|
-
if (updatedAtColumn !== false && updatedAtColumn !== undefined) {
|
|
1637
|
-
const updatedAtKey = updatedAtColumn;
|
|
1638
|
-
mergedData[updatedAtKey] = new Date();
|
|
1639
|
-
}
|
|
1640
|
-
// Emit saving event (using existing event name)
|
|
1641
|
-
await tempModel.emitEvent("saving", { filter, data: mergedData, options, mode: "upsert" });
|
|
1642
|
-
// Perform upsert via driver
|
|
1643
|
-
const result = await dataSource.driver.upsert(this.table, filter, mergedData, options);
|
|
1644
|
-
// Create model instance from result
|
|
1645
|
-
const model = new this(result);
|
|
1646
|
-
model.isNew = false;
|
|
1647
|
-
model.dirtyTracker.reset();
|
|
1648
|
-
// Emit saved event (using existing event name)
|
|
1649
|
-
await model.emitEvent("saved", { filter, data: result, options, mode: "upsert" });
|
|
1650
|
-
return model;
|
|
1377
|
+
return upsertRecord(this, filter, data, options);
|
|
1651
1378
|
}
|
|
1652
1379
|
/**
|
|
1653
1380
|
* Update a record or create it if not found (upsert).
|
|
@@ -1679,15 +1406,7 @@ class Model {
|
|
|
1679
1406
|
* ```
|
|
1680
1407
|
*/
|
|
1681
1408
|
static async findOneAndDelete(filter, options) {
|
|
1682
|
-
|
|
1683
|
-
const result = await dataSource.driver.findOneAndDelete(this.table, filter, options);
|
|
1684
|
-
if (!result) {
|
|
1685
|
-
return null;
|
|
1686
|
-
}
|
|
1687
|
-
const model = new this(result);
|
|
1688
|
-
model.isNew = false;
|
|
1689
|
-
model.dirtyTracker.reset();
|
|
1690
|
-
return model;
|
|
1409
|
+
return findOneAndDeleteRecord(this, filter, options);
|
|
1691
1410
|
}
|
|
1692
1411
|
/**
|
|
1693
1412
|
* Returns embedded data for sync operations.
|
|
@@ -1706,6 +1425,12 @@ class Model {
|
|
|
1706
1425
|
get embedData() {
|
|
1707
1426
|
return this.self().embed ? this.only(this.self().embed) : this.data;
|
|
1708
1427
|
}
|
|
1428
|
+
/**
|
|
1429
|
+
* Cleanup model events
|
|
1430
|
+
*/
|
|
1431
|
+
static $cleanup() {
|
|
1432
|
+
cleanupModelEvents(this);
|
|
1433
|
+
}
|
|
1709
1434
|
/**
|
|
1710
1435
|
* Accesses the event emitter dedicated to this model constructor.
|
|
1711
1436
|
*
|
|
@@ -1722,19 +1447,7 @@ class Model {
|
|
|
1722
1447
|
* ```
|
|
1723
1448
|
*/
|
|
1724
1449
|
static events() {
|
|
1725
|
-
|
|
1726
|
-
if (!events) {
|
|
1727
|
-
events = new ModelEvents();
|
|
1728
|
-
modelEventsRegistry.set(this, events);
|
|
1729
|
-
}
|
|
1730
|
-
return events;
|
|
1731
|
-
}
|
|
1732
|
-
/**
|
|
1733
|
-
* Cleanup model events
|
|
1734
|
-
*/
|
|
1735
|
-
static $cleanup() {
|
|
1736
|
-
modelEventsRegistry.delete(this);
|
|
1737
|
-
removeModelFromRegistery(this.name);
|
|
1450
|
+
return getModelEvents(this);
|
|
1738
1451
|
}
|
|
1739
1452
|
/**
|
|
1740
1453
|
* Registers an event listener for this model constructor.
|
|
@@ -1753,7 +1466,7 @@ class Model {
|
|
|
1753
1466
|
* ```
|
|
1754
1467
|
*/
|
|
1755
1468
|
static on(event, listener) {
|
|
1756
|
-
return this
|
|
1469
|
+
return onStaticEvent(this, event, listener);
|
|
1757
1470
|
}
|
|
1758
1471
|
/**
|
|
1759
1472
|
* Registers a one-time event listener for this model constructor.
|
|
@@ -1773,7 +1486,7 @@ class Model {
|
|
|
1773
1486
|
* ```
|
|
1774
1487
|
*/
|
|
1775
1488
|
static once(event, listener) {
|
|
1776
|
-
return this
|
|
1489
|
+
return onceStaticEvent(this, event, listener);
|
|
1777
1490
|
}
|
|
1778
1491
|
/**
|
|
1779
1492
|
* Removes an event listener from this model constructor.
|
|
@@ -1791,7 +1504,7 @@ class Model {
|
|
|
1791
1504
|
* ```
|
|
1792
1505
|
*/
|
|
1793
1506
|
static off(event, listener) {
|
|
1794
|
-
this
|
|
1507
|
+
offStaticEvent(this, event, listener);
|
|
1795
1508
|
}
|
|
1796
1509
|
/**
|
|
1797
1510
|
* Accesses the global event emitter shared by all model instances.
|
|
@@ -1809,7 +1522,7 @@ class Model {
|
|
|
1809
1522
|
* ```
|
|
1810
1523
|
*/
|
|
1811
1524
|
static globalEvents() {
|
|
1812
|
-
return
|
|
1525
|
+
return getGlobalEvents();
|
|
1813
1526
|
}
|
|
1814
1527
|
/**
|
|
1815
1528
|
* Replace the model's data entirely.
|
|
@@ -1829,8 +1542,7 @@ class Model {
|
|
|
1829
1542
|
* ```
|
|
1830
1543
|
*/
|
|
1831
1544
|
replaceData(data) {
|
|
1832
|
-
this
|
|
1833
|
-
this.dirtyTracker.replaceCurrentData(data);
|
|
1545
|
+
replaceModelData(this, data);
|
|
1834
1546
|
}
|
|
1835
1547
|
/**
|
|
1836
1548
|
* Save the model to the database.
|
|
@@ -1872,44 +1584,74 @@ class Model {
|
|
|
1872
1584
|
* ```
|
|
1873
1585
|
*/
|
|
1874
1586
|
async save(options) {
|
|
1875
|
-
|
|
1876
|
-
this.merge(options.merge);
|
|
1877
|
-
}
|
|
1878
|
-
const writer = new DatabaseWriter(this);
|
|
1879
|
-
await writer.save(options);
|
|
1880
|
-
return this;
|
|
1587
|
+
return saveModel(this, options);
|
|
1881
1588
|
}
|
|
1882
1589
|
/**
|
|
1883
|
-
* Serialize the model data for storage in database
|
|
1590
|
+
* Serialize the model data for storage in the database.
|
|
1591
|
+
*
|
|
1592
|
+
* Uses the driver's `serialize` to apply driver-specific type transformations
|
|
1593
|
+
* (e.g. Date → ISO string, BigInt → string for Postgres).
|
|
1594
|
+
*
|
|
1595
|
+
* **Not** the same as `toSnapshot` — this is a DB write concern, not a cache concern.
|
|
1884
1596
|
*/
|
|
1885
1597
|
serialize() {
|
|
1886
|
-
return
|
|
1598
|
+
return serializeModel(this);
|
|
1887
1599
|
}
|
|
1888
1600
|
/**
|
|
1889
|
-
*
|
|
1601
|
+
* Produce a plain-object snapshot of this model suitable for cache storage.
|
|
1602
|
+
*
|
|
1603
|
+
* - `data`: The model's own fields, serialized via the driver (handles Dates, BigInt, ObjectId).
|
|
1604
|
+
* - `relations`: Each entry in `loadedRelations` recursively snapshotted via `toSnapshot`.
|
|
1605
|
+
* A relation that was loaded but resolved to `null` is stored as `null` (not omitted),
|
|
1606
|
+
* so that `fromSnapshot` can distinguish "loaded + null" from "never loaded".
|
|
1607
|
+
*
|
|
1608
|
+
* Use `Model.fromSnapshot(snapshot)` to reconstruct.
|
|
1609
|
+
*
|
|
1610
|
+
* @example
|
|
1611
|
+
* ```typescript
|
|
1612
|
+
* await cache.set(key, chat.toSnapshot());
|
|
1613
|
+
* ```
|
|
1614
|
+
*/
|
|
1615
|
+
toSnapshot() {
|
|
1616
|
+
return modelToSnapshot(this);
|
|
1617
|
+
}
|
|
1618
|
+
/**
|
|
1619
|
+
* Reconstruct a model instance (with relations) from a cache snapshot.
|
|
1620
|
+
*
|
|
1621
|
+
* Counterpart to `toSnapshot`. Applies driver deserialization (e.g. ISO string → Date)
|
|
1622
|
+
* and recursively hydrates any nested relation snapshots via `RelationHydrator`.
|
|
1623
|
+
*
|
|
1624
|
+
* @example
|
|
1625
|
+
* ```typescript
|
|
1626
|
+
* const snapshot = await cache.get(key);
|
|
1627
|
+
* const chat = Chat.fromSnapshot(snapshot);
|
|
1628
|
+
* chat.unit; // Unit model instance, fully hydrated
|
|
1629
|
+
* ```
|
|
1630
|
+
*/
|
|
1631
|
+
static fromSnapshot(snapshot) {
|
|
1632
|
+
return modelFromSnapshot(this, snapshot);
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Create a model instance from raw data (no relations).
|
|
1636
|
+
*
|
|
1637
|
+
* This is the data-only hydration path, used by the query builder when
|
|
1638
|
+
* converting a raw DB row into a model instance. Relations are NOT restored
|
|
1639
|
+
* here — use `fromSnapshot` when restoring from a cache snapshot that
|
|
1640
|
+
* includes relation data.
|
|
1641
|
+
*
|
|
1642
|
+
* @example
|
|
1643
|
+
* ```typescript
|
|
1644
|
+
* // Query builder internals:
|
|
1645
|
+
* const user = User.hydrate(rawRow);
|
|
1646
|
+
* ```
|
|
1890
1647
|
*/
|
|
1891
|
-
static
|
|
1892
|
-
|
|
1893
|
-
const model = new this(deserializedData);
|
|
1894
|
-
model.isNew = false;
|
|
1895
|
-
return model;
|
|
1648
|
+
static hydrate(data) {
|
|
1649
|
+
return hydrateModel(this, data);
|
|
1896
1650
|
}
|
|
1897
1651
|
/**
|
|
1898
1652
|
* Convert the model into JSON
|
|
1899
1653
|
*/
|
|
1900
1654
|
toJSON() {
|
|
1901
|
-
|
|
1902
|
-
if (!resource) {
|
|
1903
|
-
const toJsonColumns = this.self().toJsonColumns;
|
|
1904
|
-
if (toJsonColumns && toJsonColumns.length > 0) {
|
|
1905
|
-
return this.only(toJsonColumns);
|
|
1906
|
-
}
|
|
1907
|
-
return this.data;
|
|
1908
|
-
}
|
|
1909
|
-
const resourceColumns = this.self().resourceColumns;
|
|
1910
|
-
const data = resourceColumns !== undefined && resourceColumns.length > 0
|
|
1911
|
-
? this.only(resourceColumns)
|
|
1912
|
-
: this.data;
|
|
1913
|
-
return new resource(data).toJSON();
|
|
1655
|
+
return modelToJSON(this);
|
|
1914
1656
|
}
|
|
1915
1657
|
}export{Model};//# sourceMappingURL=model.js.map
|