@warlock.js/cascade 4.0.162 → 4.0.164
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-data-source-context.d.ts +29 -0
- package/cjs/context/database-data-source-context.d.ts.map +1 -0
- package/cjs/context/database-data-source-context.js +28 -0
- package/cjs/context/database-data-source-context.js.map +1 -0
- package/cjs/context/database-transaction-context.d.ts +35 -0
- package/cjs/context/database-transaction-context.d.ts.map +1 -0
- package/cjs/context/database-transaction-context.js +40 -0
- package/cjs/context/database-transaction-context.js.map +1 -0
- package/cjs/contracts/database-driver.contract.d.ts +450 -0
- package/cjs/contracts/database-driver.contract.d.ts.map +1 -0
- package/cjs/contracts/database-id-generator.contract.d.ts +109 -0
- package/cjs/contracts/database-id-generator.contract.d.ts.map +1 -0
- package/cjs/contracts/database-remover.contract.d.ts +104 -0
- package/cjs/contracts/database-remover.contract.d.ts.map +1 -0
- package/cjs/contracts/database-restorer.contract.d.ts +143 -0
- package/cjs/contracts/database-restorer.contract.d.ts.map +1 -0
- package/cjs/contracts/database-writer.contract.d.ts +119 -0
- package/cjs/contracts/database-writer.contract.d.ts.map +1 -0
- package/cjs/contracts/driver-blueprint.contract.d.ts +49 -0
- package/cjs/contracts/driver-blueprint.contract.d.ts.map +1 -0
- package/cjs/contracts/index.d.ts +10 -0
- package/cjs/contracts/index.d.ts.map +1 -0
- package/cjs/contracts/migration-driver.contract.d.ts +522 -0
- package/cjs/contracts/migration-driver.contract.d.ts.map +1 -0
- package/cjs/contracts/query-builder.contract.d.ts +1609 -0
- package/cjs/contracts/query-builder.contract.d.ts.map +1 -0
- package/cjs/contracts/sync-adapter.contract.d.ts +58 -0
- package/cjs/contracts/sync-adapter.contract.d.ts.map +1 -0
- package/cjs/data-source/data-source-registry.d.ts +108 -0
- package/cjs/data-source/data-source-registry.d.ts.map +1 -0
- package/cjs/data-source/data-source-registry.js +145 -0
- package/cjs/data-source/data-source-registry.js.map +1 -0
- package/cjs/data-source/data-source.d.ts +147 -0
- package/cjs/data-source/data-source.d.ts.map +1 -0
- package/cjs/data-source/data-source.js +83 -0
- package/cjs/data-source/data-source.js.map +1 -0
- package/cjs/database-dirty-tracker.d.ts +252 -0
- package/cjs/database-dirty-tracker.d.ts.map +1 -0
- package/cjs/database-dirty-tracker.js +386 -0
- package/cjs/database-dirty-tracker.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-blueprint.d.ts +30 -0
- package/cjs/drivers/mongodb/mongodb-blueprint.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-blueprint.js +51 -0
- package/cjs/drivers/mongodb/mongodb-blueprint.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-driver.d.ts +325 -0
- package/cjs/drivers/mongodb/mongodb-driver.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-driver.js +838 -0
- package/cjs/drivers/mongodb/mongodb-driver.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-id-generator.d.ts +116 -0
- package/cjs/drivers/mongodb/mongodb-id-generator.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-id-generator.js +149 -0
- package/cjs/drivers/mongodb/mongodb-id-generator.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-migration-driver.d.ts +317 -0
- package/cjs/drivers/mongodb/mongodb-migration-driver.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-migration-driver.js +666 -0
- package/cjs/drivers/mongodb/mongodb-migration-driver.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-query-builder.d.ts +1122 -0
- package/cjs/drivers/mongodb/mongodb-query-builder.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-query-builder.js +1988 -0
- package/cjs/drivers/mongodb/mongodb-query-builder.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-query-operations.d.ts +226 -0
- package/cjs/drivers/mongodb/mongodb-query-operations.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-query-operations.js +270 -0
- package/cjs/drivers/mongodb/mongodb-query-operations.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-query-parser.d.ts +262 -0
- package/cjs/drivers/mongodb/mongodb-query-parser.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-query-parser.js +1351 -0
- package/cjs/drivers/mongodb/mongodb-query-parser.js.map +1 -0
- package/cjs/drivers/mongodb/mongodb-sync-adapter.d.ts +79 -0
- package/cjs/drivers/mongodb/mongodb-sync-adapter.d.ts.map +1 -0
- package/cjs/drivers/mongodb/mongodb-sync-adapter.js +146 -0
- package/cjs/drivers/mongodb/mongodb-sync-adapter.js.map +1 -0
- package/cjs/drivers/mongodb/types.d.ts +43 -0
- package/cjs/drivers/mongodb/types.d.ts.map +1 -0
- package/cjs/drivers/postgres/index.d.ts +16 -0
- package/cjs/drivers/postgres/index.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-blueprint.d.ts +64 -0
- package/cjs/drivers/postgres/postgres-blueprint.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-blueprint.js +121 -0
- package/cjs/drivers/postgres/postgres-blueprint.js.map +1 -0
- package/cjs/drivers/postgres/postgres-dialect.d.ts +136 -0
- package/cjs/drivers/postgres/postgres-dialect.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-dialect.js +268 -0
- package/cjs/drivers/postgres/postgres-dialect.js.map +1 -0
- package/cjs/drivers/postgres/postgres-driver.d.ts +432 -0
- package/cjs/drivers/postgres/postgres-driver.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-driver.js +1010 -0
- package/cjs/drivers/postgres/postgres-driver.js.map +1 -0
- package/cjs/drivers/postgres/postgres-migration-driver.d.ts +397 -0
- package/cjs/drivers/postgres/postgres-migration-driver.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-migration-driver.js +900 -0
- package/cjs/drivers/postgres/postgres-migration-driver.js.map +1 -0
- package/cjs/drivers/postgres/postgres-query-builder.d.ts +254 -0
- package/cjs/drivers/postgres/postgres-query-builder.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-query-builder.js +933 -0
- package/cjs/drivers/postgres/postgres-query-builder.js.map +1 -0
- package/cjs/drivers/postgres/postgres-query-parser.d.ts +328 -0
- package/cjs/drivers/postgres/postgres-query-parser.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-query-parser.js +868 -0
- package/cjs/drivers/postgres/postgres-query-parser.js.map +1 -0
- package/cjs/drivers/postgres/postgres-sql-serializer.d.ts +37 -0
- package/cjs/drivers/postgres/postgres-sql-serializer.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-sql-serializer.js +400 -0
- package/cjs/drivers/postgres/postgres-sql-serializer.js.map +1 -0
- package/cjs/drivers/postgres/postgres-sync-adapter.d.ts +83 -0
- package/cjs/drivers/postgres/postgres-sync-adapter.d.ts.map +1 -0
- package/cjs/drivers/postgres/postgres-sync-adapter.js +197 -0
- package/cjs/drivers/postgres/postgres-sync-adapter.js.map +1 -0
- package/cjs/drivers/postgres/types.d.ts +144 -0
- package/cjs/drivers/postgres/types.d.ts.map +1 -0
- package/cjs/drivers/sql/index.d.ts +10 -0
- package/cjs/drivers/sql/index.d.ts.map +1 -0
- package/cjs/drivers/sql/sql-dialect.contract.d.ts +204 -0
- package/cjs/drivers/sql/sql-dialect.contract.d.ts.map +1 -0
- package/cjs/drivers/sql/sql-types.d.ts +202 -0
- package/cjs/drivers/sql/sql-types.d.ts.map +1 -0
- package/cjs/errors/missing-data-source.error.d.ts +22 -0
- package/cjs/errors/missing-data-source.error.d.ts.map +1 -0
- package/cjs/errors/missing-data-source.error.js +29 -0
- package/cjs/errors/missing-data-source.error.js.map +1 -0
- 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 +231 -0
- package/cjs/events/model-events.d.ts.map +1 -0
- package/cjs/events/model-events.js +259 -0
- package/cjs/events/model-events.js.map +1 -0
- package/cjs/expressions/aggregate-expressions.d.ts +215 -0
- package/cjs/expressions/aggregate-expressions.d.ts.map +1 -0
- package/cjs/expressions/aggregate-expressions.js +221 -0
- package/cjs/expressions/aggregate-expressions.js.map +1 -0
- package/cjs/expressions/index.d.ts +2 -0
- package/cjs/expressions/index.d.ts.map +1 -0
- package/cjs/index.d.ts +45 -0
- package/cjs/index.d.ts.map +1 -0
- package/cjs/index.js +1 -0
- package/cjs/index.js.map +1 -0
- package/cjs/migration/column-builder.d.ts +417 -0
- package/cjs/migration/column-builder.d.ts.map +1 -0
- package/cjs/migration/column-builder.js +586 -0
- package/cjs/migration/column-builder.js.map +1 -0
- package/cjs/migration/column-helpers.d.ts +275 -0
- package/cjs/migration/column-helpers.d.ts.map +1 -0
- package/cjs/migration/column-helpers.js +389 -0
- package/cjs/migration/column-helpers.js.map +1 -0
- package/cjs/migration/foreign-key-builder.d.ts +103 -0
- package/cjs/migration/foreign-key-builder.d.ts.map +1 -0
- package/cjs/migration/foreign-key-builder.js +121 -0
- package/cjs/migration/foreign-key-builder.js.map +1 -0
- package/cjs/migration/index.d.ts +7 -0
- package/cjs/migration/index.d.ts.map +1 -0
- package/cjs/migration/migration-runner.d.ts +278 -0
- package/cjs/migration/migration-runner.d.ts.map +1 -0
- package/cjs/migration/migration-runner.js +815 -0
- package/cjs/migration/migration-runner.js.map +1 -0
- package/cjs/migration/migration.d.ts +1992 -0
- package/cjs/migration/migration.d.ts.map +1 -0
- package/cjs/migration/migration.js +2162 -0
- package/cjs/migration/migration.js.map +1 -0
- package/cjs/migration/sql-grammar.d.ts +61 -0
- package/cjs/migration/sql-grammar.d.ts.map +1 -0
- package/cjs/migration/sql-grammar.js +164 -0
- package/cjs/migration/sql-grammar.js.map +1 -0
- package/cjs/migration/sql-serializer.d.ts +22 -0
- package/cjs/migration/sql-serializer.d.ts.map +1 -0
- package/cjs/migration/sql-serializer.js +26 -0
- package/cjs/migration/sql-serializer.js.map +1 -0
- package/cjs/migration/types.d.ts +155 -0
- package/cjs/migration/types.d.ts.map +1 -0
- 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 +164 -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 +1647 -0
- package/cjs/model/model.d.ts.map +1 -0
- package/cjs/model/model.js +1657 -0
- package/cjs/model/model.js.map +1 -0
- 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 +81 -0
- package/cjs/model/register-model.d.ts.map +1 -0
- package/cjs/model/register-model.js +94 -0
- package/cjs/model/register-model.js.map +1 -0
- 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 +156 -0
- package/cjs/relations/helpers.d.ts.map +1 -0
- package/cjs/relations/helpers.js +202 -0
- package/cjs/relations/helpers.js.map +1 -0
- package/cjs/relations/index.d.ts +35 -0
- package/cjs/relations/index.d.ts.map +1 -0
- package/cjs/relations/pivot-operations.d.ts +160 -0
- package/cjs/relations/pivot-operations.d.ts.map +1 -0
- package/cjs/relations/pivot-operations.js +293 -0
- package/cjs/relations/pivot-operations.js.map +1 -0
- 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.d.ts +194 -0
- package/cjs/relations/relation-loader.d.ts.map +1 -0
- package/cjs/relations/relation-loader.js +466 -0
- package/cjs/relations/relation-loader.js.map +1 -0
- package/cjs/relations/types.d.ts +306 -0
- package/cjs/relations/types.d.ts.map +1 -0
- package/cjs/remover/database-remover.d.ts +100 -0
- package/cjs/remover/database-remover.d.ts.map +1 -0
- package/cjs/remover/database-remover.js +214 -0
- package/cjs/remover/database-remover.js.map +1 -0
- package/cjs/restorer/database-restorer.d.ts +131 -0
- package/cjs/restorer/database-restorer.d.ts.map +1 -0
- package/cjs/restorer/database-restorer.js +434 -0
- package/cjs/restorer/database-restorer.js.map +1 -0
- 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/sync/index.d.ts +12 -0
- package/cjs/sync/index.d.ts.map +1 -0
- package/cjs/sync/model-events.d.ts +62 -0
- package/cjs/sync/model-events.d.ts.map +1 -0
- package/cjs/sync/model-events.js +49 -0
- package/cjs/sync/model-events.js.map +1 -0
- package/cjs/sync/model-sync-operation.d.ts +163 -0
- package/cjs/sync/model-sync-operation.d.ts.map +1 -0
- package/cjs/sync/model-sync-operation.js +292 -0
- package/cjs/sync/model-sync-operation.js.map +1 -0
- package/cjs/sync/model-sync.d.ts +130 -0
- package/cjs/sync/model-sync.d.ts.map +1 -0
- package/cjs/sync/model-sync.js +178 -0
- package/cjs/sync/model-sync.js.map +1 -0
- package/cjs/sync/sync-context.d.ts +70 -0
- package/cjs/sync/sync-context.d.ts.map +1 -0
- package/cjs/sync/sync-context.js +101 -0
- package/cjs/sync/sync-context.js.map +1 -0
- package/cjs/sync/sync-manager.d.ts +213 -0
- package/cjs/sync/sync-manager.d.ts.map +1 -0
- package/cjs/sync/sync-manager.js +689 -0
- package/cjs/sync/sync-manager.js.map +1 -0
- package/cjs/sync/types.d.ts +289 -0
- package/cjs/sync/types.d.ts.map +1 -0
- package/cjs/test-migrations/test-enhanced-features.migration.d.ts +15 -0
- package/cjs/test-migrations/test-enhanced-features.migration.d.ts.map +1 -0
- package/cjs/types.d.ts +371 -0
- package/cjs/types.d.ts.map +1 -0
- package/cjs/utils/connect-to-database.d.ts +307 -0
- package/cjs/utils/connect-to-database.d.ts.map +1 -0
- package/cjs/utils/connect-to-database.js +130 -0
- package/cjs/utils/connect-to-database.js.map +1 -0
- package/cjs/utils/database-writer.utils.d.ts +15 -0
- package/cjs/utils/database-writer.utils.d.ts.map +1 -0
- package/cjs/utils/database-writer.utils.js +14 -0
- package/cjs/utils/database-writer.utils.js.map +1 -0
- package/cjs/utils/define-model.js +100 -0
- package/cjs/utils/define-model.js.map +1 -0
- 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 +146 -0
- package/cjs/utils/once-connected.d.ts.map +1 -0
- package/cjs/utils/once-connected.js +251 -0
- package/cjs/utils/once-connected.js.map +1 -0
- package/cjs/validation/database-seal-plugins.d.ts +12 -0
- package/cjs/validation/database-seal-plugins.d.ts.map +1 -0
- package/cjs/validation/database-seal-plugins.js +1 -0
- package/cjs/validation/database-seal-plugins.js.map +1 -0
- package/cjs/validation/database-writer-validation-error.d.ts +97 -0
- package/cjs/validation/database-writer-validation-error.d.ts.map +1 -0
- package/cjs/validation/database-writer-validation-error.js +160 -0
- package/cjs/validation/database-writer-validation-error.js.map +1 -0
- package/cjs/validation/index.d.ts +3 -0
- package/cjs/validation/index.d.ts.map +1 -0
- package/cjs/validation/mutators/embed-mutator.d.ts +9 -0
- package/cjs/validation/mutators/embed-mutator.d.ts.map +1 -0
- package/cjs/validation/mutators/embed-mutator.js +33 -0
- package/cjs/validation/mutators/embed-mutator.js.map +1 -0
- package/cjs/validation/plugins/embed-validator-plugin.d.ts +24 -0
- package/cjs/validation/plugins/embed-validator-plugin.d.ts.map +1 -0
- package/cjs/validation/plugins/embed-validator-plugin.js +18 -0
- package/cjs/validation/plugins/embed-validator-plugin.js.map +1 -0
- package/cjs/validation/rules/database-model-rule.d.ts +7 -0
- package/cjs/validation/rules/database-model-rule.d.ts.map +1 -0
- package/cjs/validation/rules/database-model-rule.js +27 -0
- package/cjs/validation/rules/database-model-rule.js.map +1 -0
- package/cjs/validation/transformers/embed-model-transformer.d.ts +3 -0
- package/cjs/validation/transformers/embed-model-transformer.d.ts.map +1 -0
- package/cjs/validation/transformers/embed-model-transformer.js +18 -0
- package/cjs/validation/transformers/embed-model-transformer.js.map +1 -0
- package/cjs/validation/validators/embed-validator.d.ts +21 -0
- package/cjs/validation/validators/embed-validator.d.ts.map +1 -0
- package/cjs/validation/validators/embed-validator.js +43 -0
- package/cjs/validation/validators/embed-validator.js.map +1 -0
- package/cjs/writer/database-writer.d.ts +181 -0
- package/cjs/writer/database-writer.d.ts.map +1 -0
- package/cjs/writer/database-writer.js +407 -0
- package/cjs/writer/database-writer.js.map +1 -0
- package/esm/data-source/data-source-registry.d.ts +108 -0
- package/esm/data-source/data-source-registry.d.ts.map +1 -0
- package/esm/data-source/data-source-registry.js +145 -0
- package/esm/data-source/data-source-registry.js.map +1 -0
- package/esm/data-source/data-source.d.ts +147 -0
- package/esm/data-source/data-source.d.ts.map +1 -0
- package/esm/data-source/data-source.js +83 -0
- package/esm/data-source/data-source.js.map +1 -0
- package/esm/database-dirty-tracker.d.ts +252 -0
- package/esm/database-dirty-tracker.d.ts.map +1 -0
- package/esm/database-dirty-tracker.js +386 -0
- package/esm/database-dirty-tracker.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-blueprint.d.ts +30 -0
- package/esm/drivers/mongodb/mongodb-blueprint.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-blueprint.js +51 -0
- package/esm/drivers/mongodb/mongodb-blueprint.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-driver.d.ts +325 -0
- package/esm/drivers/mongodb/mongodb-driver.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-driver.js +838 -0
- package/esm/drivers/mongodb/mongodb-driver.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-id-generator.d.ts +116 -0
- package/esm/drivers/mongodb/mongodb-id-generator.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-id-generator.js +149 -0
- package/esm/drivers/mongodb/mongodb-id-generator.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-migration-driver.d.ts +317 -0
- package/esm/drivers/mongodb/mongodb-migration-driver.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-migration-driver.js +666 -0
- package/esm/drivers/mongodb/mongodb-migration-driver.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-query-builder.d.ts +1122 -0
- package/esm/drivers/mongodb/mongodb-query-builder.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-query-builder.js +1988 -0
- package/esm/drivers/mongodb/mongodb-query-builder.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-query-operations.d.ts +226 -0
- package/esm/drivers/mongodb/mongodb-query-operations.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-query-operations.js +270 -0
- package/esm/drivers/mongodb/mongodb-query-operations.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-query-parser.d.ts +262 -0
- package/esm/drivers/mongodb/mongodb-query-parser.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-query-parser.js +1351 -0
- package/esm/drivers/mongodb/mongodb-query-parser.js.map +1 -0
- package/esm/drivers/mongodb/mongodb-sync-adapter.d.ts +79 -0
- package/esm/drivers/mongodb/mongodb-sync-adapter.d.ts.map +1 -0
- package/esm/drivers/mongodb/mongodb-sync-adapter.js +146 -0
- package/esm/drivers/mongodb/mongodb-sync-adapter.js.map +1 -0
- package/esm/drivers/mongodb/types.d.ts +43 -0
- package/esm/drivers/mongodb/types.d.ts.map +1 -0
- package/esm/drivers/postgres/index.d.ts +16 -0
- package/esm/drivers/postgres/index.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-blueprint.d.ts +64 -0
- package/esm/drivers/postgres/postgres-blueprint.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-blueprint.js +121 -0
- package/esm/drivers/postgres/postgres-blueprint.js.map +1 -0
- package/esm/drivers/postgres/postgres-dialect.d.ts +136 -0
- package/esm/drivers/postgres/postgres-dialect.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-dialect.js +268 -0
- package/esm/drivers/postgres/postgres-dialect.js.map +1 -0
- package/esm/drivers/postgres/postgres-driver.d.ts +432 -0
- package/esm/drivers/postgres/postgres-driver.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-driver.js +1010 -0
- package/esm/drivers/postgres/postgres-driver.js.map +1 -0
- package/esm/drivers/postgres/postgres-migration-driver.d.ts +397 -0
- package/esm/drivers/postgres/postgres-migration-driver.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-migration-driver.js +900 -0
- package/esm/drivers/postgres/postgres-migration-driver.js.map +1 -0
- package/esm/drivers/postgres/postgres-query-builder.d.ts +254 -0
- package/esm/drivers/postgres/postgres-query-builder.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-query-builder.js +933 -0
- package/esm/drivers/postgres/postgres-query-builder.js.map +1 -0
- package/esm/drivers/postgres/postgres-query-parser.d.ts +328 -0
- package/esm/drivers/postgres/postgres-query-parser.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-query-parser.js +868 -0
- package/esm/drivers/postgres/postgres-query-parser.js.map +1 -0
- package/esm/drivers/postgres/postgres-sql-serializer.d.ts +37 -0
- package/esm/drivers/postgres/postgres-sql-serializer.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-sql-serializer.js +400 -0
- package/esm/drivers/postgres/postgres-sql-serializer.js.map +1 -0
- package/esm/drivers/postgres/postgres-sync-adapter.d.ts +83 -0
- package/esm/drivers/postgres/postgres-sync-adapter.d.ts.map +1 -0
- package/esm/drivers/postgres/postgres-sync-adapter.js +197 -0
- package/esm/drivers/postgres/postgres-sync-adapter.js.map +1 -0
- package/esm/drivers/postgres/types.d.ts +144 -0
- package/esm/drivers/postgres/types.d.ts.map +1 -0
- package/esm/drivers/sql/index.d.ts +10 -0
- package/esm/drivers/sql/index.d.ts.map +1 -0
- package/esm/drivers/sql/sql-dialect.contract.d.ts +204 -0
- package/esm/drivers/sql/sql-dialect.contract.d.ts.map +1 -0
- package/esm/drivers/sql/sql-types.d.ts +202 -0
- package/esm/drivers/sql/sql-types.d.ts.map +1 -0
- package/esm/errors/missing-data-source.error.d.ts +22 -0
- package/esm/errors/missing-data-source.error.d.ts.map +1 -0
- package/esm/errors/missing-data-source.error.js +29 -0
- package/esm/errors/missing-data-source.error.js.map +1 -0
- 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 +231 -0
- package/esm/events/model-events.d.ts.map +1 -0
- package/esm/events/model-events.js +259 -0
- package/esm/events/model-events.js.map +1 -0
- package/esm/expressions/aggregate-expressions.d.ts +215 -0
- package/esm/expressions/aggregate-expressions.d.ts.map +1 -0
- package/esm/expressions/aggregate-expressions.js +221 -0
- package/esm/expressions/aggregate-expressions.js.map +1 -0
- package/esm/expressions/index.d.ts +2 -0
- package/esm/expressions/index.d.ts.map +1 -0
- package/esm/index.d.ts +45 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -0
- package/esm/migration/column-builder.d.ts +417 -0
- package/esm/migration/column-builder.d.ts.map +1 -0
- package/esm/migration/column-builder.js +586 -0
- package/esm/migration/column-builder.js.map +1 -0
- package/esm/migration/column-helpers.d.ts +275 -0
- package/esm/migration/column-helpers.d.ts.map +1 -0
- package/esm/migration/column-helpers.js +389 -0
- package/esm/migration/column-helpers.js.map +1 -0
- package/esm/migration/foreign-key-builder.d.ts +103 -0
- package/esm/migration/foreign-key-builder.d.ts.map +1 -0
- package/esm/migration/foreign-key-builder.js +121 -0
- package/esm/migration/foreign-key-builder.js.map +1 -0
- package/esm/migration/index.d.ts +7 -0
- package/esm/migration/index.d.ts.map +1 -0
- package/esm/migration/migration-runner.d.ts +278 -0
- package/esm/migration/migration-runner.d.ts.map +1 -0
- package/esm/migration/migration-runner.js +815 -0
- package/esm/migration/migration-runner.js.map +1 -0
- package/esm/migration/migration.d.ts +1992 -0
- package/esm/migration/migration.d.ts.map +1 -0
- package/esm/migration/migration.js +2162 -0
- package/esm/migration/migration.js.map +1 -0
- package/esm/migration/sql-grammar.d.ts +61 -0
- package/esm/migration/sql-grammar.d.ts.map +1 -0
- package/esm/migration/sql-grammar.js +164 -0
- package/esm/migration/sql-grammar.js.map +1 -0
- package/esm/migration/sql-serializer.d.ts +22 -0
- package/esm/migration/sql-serializer.d.ts.map +1 -0
- package/esm/migration/sql-serializer.js +26 -0
- package/esm/migration/sql-serializer.js.map +1 -0
- package/esm/migration/types.d.ts +155 -0
- package/esm/migration/types.d.ts.map +1 -0
- 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 +164 -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 +1647 -0
- package/esm/model/model.d.ts.map +1 -0
- package/esm/model/model.js +1657 -0
- package/esm/model/model.js.map +1 -0
- 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 +81 -0
- package/esm/model/register-model.d.ts.map +1 -0
- package/esm/model/register-model.js +94 -0
- package/esm/model/register-model.js.map +1 -0
- 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 +156 -0
- package/esm/relations/helpers.d.ts.map +1 -0
- package/esm/relations/helpers.js +202 -0
- package/esm/relations/helpers.js.map +1 -0
- package/esm/relations/index.d.ts +35 -0
- package/esm/relations/index.d.ts.map +1 -0
- package/esm/relations/pivot-operations.d.ts +160 -0
- package/esm/relations/pivot-operations.d.ts.map +1 -0
- package/esm/relations/pivot-operations.js +293 -0
- package/esm/relations/pivot-operations.js.map +1 -0
- 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.d.ts +194 -0
- package/esm/relations/relation-loader.d.ts.map +1 -0
- package/esm/relations/relation-loader.js +466 -0
- package/esm/relations/relation-loader.js.map +1 -0
- package/esm/relations/types.d.ts +306 -0
- package/esm/relations/types.d.ts.map +1 -0
- package/esm/remover/database-remover.d.ts +100 -0
- package/esm/remover/database-remover.d.ts.map +1 -0
- package/esm/remover/database-remover.js +214 -0
- package/esm/remover/database-remover.js.map +1 -0
- package/esm/restorer/database-restorer.d.ts +131 -0
- package/esm/restorer/database-restorer.d.ts.map +1 -0
- package/esm/restorer/database-restorer.js +434 -0
- package/esm/restorer/database-restorer.js.map +1 -0
- 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/sync/index.d.ts +12 -0
- package/esm/sync/index.d.ts.map +1 -0
- package/esm/sync/model-events.d.ts +62 -0
- package/esm/sync/model-events.d.ts.map +1 -0
- package/esm/sync/model-events.js +49 -0
- package/esm/sync/model-events.js.map +1 -0
- package/esm/sync/model-sync-operation.d.ts +163 -0
- package/esm/sync/model-sync-operation.d.ts.map +1 -0
- package/esm/sync/model-sync-operation.js +292 -0
- package/esm/sync/model-sync-operation.js.map +1 -0
- package/esm/sync/model-sync.d.ts +130 -0
- package/esm/sync/model-sync.d.ts.map +1 -0
- package/esm/sync/model-sync.js +178 -0
- package/esm/sync/model-sync.js.map +1 -0
- package/esm/sync/sync-context.d.ts +70 -0
- package/esm/sync/sync-context.d.ts.map +1 -0
- package/esm/sync/sync-context.js +101 -0
- package/esm/sync/sync-context.js.map +1 -0
- package/esm/sync/sync-manager.d.ts +213 -0
- package/esm/sync/sync-manager.d.ts.map +1 -0
- package/esm/sync/sync-manager.js +689 -0
- package/esm/sync/sync-manager.js.map +1 -0
- package/esm/sync/types.d.ts +289 -0
- package/esm/sync/types.d.ts.map +1 -0
- package/esm/test-migrations/test-enhanced-features.migration.d.ts +15 -0
- package/esm/test-migrations/test-enhanced-features.migration.d.ts.map +1 -0
- package/esm/types.d.ts +371 -0
- package/esm/types.d.ts.map +1 -0
- package/esm/utils/connect-to-database.d.ts +307 -0
- package/esm/utils/connect-to-database.d.ts.map +1 -0
- package/esm/utils/connect-to-database.js +130 -0
- package/esm/utils/connect-to-database.js.map +1 -0
- package/esm/utils/database-writer.utils.d.ts +15 -0
- package/esm/utils/database-writer.utils.d.ts.map +1 -0
- package/esm/utils/database-writer.utils.js +14 -0
- package/esm/utils/database-writer.utils.js.map +1 -0
- package/esm/utils/define-model.js +100 -0
- package/esm/utils/define-model.js.map +1 -0
- 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 +146 -0
- package/esm/utils/once-connected.d.ts.map +1 -0
- package/esm/utils/once-connected.js +251 -0
- package/esm/utils/once-connected.js.map +1 -0
- package/esm/validation/database-seal-plugins.d.ts +12 -0
- package/esm/validation/database-seal-plugins.d.ts.map +1 -0
- package/esm/validation/database-seal-plugins.js +1 -0
- package/esm/validation/database-seal-plugins.js.map +1 -0
- package/esm/validation/database-writer-validation-error.d.ts +97 -0
- package/esm/validation/database-writer-validation-error.d.ts.map +1 -0
- package/esm/validation/database-writer-validation-error.js +160 -0
- package/esm/validation/database-writer-validation-error.js.map +1 -0
- package/esm/validation/index.d.ts +3 -0
- package/esm/validation/index.d.ts.map +1 -0
- package/esm/validation/mutators/embed-mutator.d.ts +9 -0
- package/esm/validation/mutators/embed-mutator.d.ts.map +1 -0
- package/esm/validation/mutators/embed-mutator.js +33 -0
- package/esm/validation/mutators/embed-mutator.js.map +1 -0
- package/esm/validation/plugins/embed-validator-plugin.d.ts +24 -0
- package/esm/validation/plugins/embed-validator-plugin.d.ts.map +1 -0
- package/esm/validation/plugins/embed-validator-plugin.js +18 -0
- package/esm/validation/plugins/embed-validator-plugin.js.map +1 -0
- package/esm/validation/rules/database-model-rule.d.ts +7 -0
- package/esm/validation/rules/database-model-rule.d.ts.map +1 -0
- package/esm/validation/rules/database-model-rule.js +27 -0
- package/esm/validation/rules/database-model-rule.js.map +1 -0
- package/esm/validation/transformers/embed-model-transformer.d.ts +3 -0
- package/esm/validation/transformers/embed-model-transformer.d.ts.map +1 -0
- package/esm/validation/transformers/embed-model-transformer.js +18 -0
- package/esm/validation/transformers/embed-model-transformer.js.map +1 -0
- package/esm/validation/validators/embed-validator.d.ts +21 -0
- package/esm/validation/validators/embed-validator.d.ts.map +1 -0
- package/esm/validation/validators/embed-validator.js +43 -0
- package/esm/validation/validators/embed-validator.js.map +1 -0
- package/esm/writer/database-writer.d.ts +181 -0
- package/esm/writer/database-writer.d.ts.map +1 -0
- package/esm/writer/database-writer.js +407 -0
- package/esm/writer/database-writer.js.map +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,1647 @@
|
|
|
1
|
+
import { type GenericObject } from "@mongez/reinforcements";
|
|
2
|
+
import type { ObjectValidator } from "@warlock.js/seal";
|
|
3
|
+
import type { DriverContract, PaginationOptions, PaginationResult, RemoverResult, UpdateOperations, WriterOptions } from "../contracts";
|
|
4
|
+
import { QueryBuilderContract, WhereCallback, WhereObject, WhereOperator } from "../contracts";
|
|
5
|
+
import type { DataSource } from "../data-source/data-source";
|
|
6
|
+
import { DatabaseDirtyTracker } from "../database-dirty-tracker";
|
|
7
|
+
import type { ModelEventListener, ModelEventName } from "../events/model-events";
|
|
8
|
+
import { ModelEvents } from "../events/model-events";
|
|
9
|
+
import type { ModelSnapshot } from "../relations/relation-hydrator";
|
|
10
|
+
import type { ModelSyncOperationContract } from "../sync/types";
|
|
11
|
+
import type { DeleteStrategy, StrictMode } from "../types";
|
|
12
|
+
import type { ChildModel, GlobalScopeDefinition, GlobalScopeOptions, LocalScopeCallback, ModelSchema } from "./model.types";
|
|
13
|
+
export type { ChildModel, GlobalScopeDefinition, GlobalScopeOptions, LocalScopeCallback, ModelSchema, ScopeTiming, } from "./model.types";
|
|
14
|
+
/**
|
|
15
|
+
* Base class that powers all Cascade models.
|
|
16
|
+
*
|
|
17
|
+
* Provides:
|
|
18
|
+
* - Type-safe value accessors with dot-notation support (get, set, has, unset, merge)
|
|
19
|
+
* - Automatic dirty tracking for efficient partial updates
|
|
20
|
+
* - Lifecycle event hooks (saving, created, deleting, etc.)
|
|
21
|
+
* - Integration with the data-source registry for multi-database support
|
|
22
|
+
* - Support for both per-model and global event listeners
|
|
23
|
+
*
|
|
24
|
+
* @template TSchema - The shape of the model's underlying data
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* interface UserSchema {
|
|
29
|
+
* id: number;
|
|
30
|
+
* name: string;
|
|
31
|
+
* email: string;
|
|
32
|
+
* }
|
|
33
|
+
*
|
|
34
|
+
* class User extends Model<UserSchema> {
|
|
35
|
+
* public static table = "users";
|
|
36
|
+
* }
|
|
37
|
+
*
|
|
38
|
+
* const user = new User({ name: "Alice" });
|
|
39
|
+
* user.set("email", "alice@example.com");
|
|
40
|
+
* console.log(user.hasChanges()); // true
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare abstract class Model<TSchema extends ModelSchema = ModelSchema> {
|
|
44
|
+
/**
|
|
45
|
+
* The database table or collection name associated with this model.
|
|
46
|
+
*
|
|
47
|
+
* Must be defined by each concrete model subclass.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* class User extends Model {
|
|
52
|
+
* public static table = "users";
|
|
53
|
+
* }
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
static table: string;
|
|
57
|
+
/**
|
|
58
|
+
* Resource for this model.
|
|
59
|
+
* It is a class that holds a toJSON function
|
|
60
|
+
* Called when the model is being converted to JSON (by calling toJSON or JSON.stringify(model))
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```typescript
|
|
64
|
+
* class User extends Model {
|
|
65
|
+
* public static resource = UserResource;
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
static resource?: any;
|
|
70
|
+
/**
|
|
71
|
+
* Resource columns
|
|
72
|
+
* Define what columns should be sent to the resource (if any) when converting to JSON
|
|
73
|
+
*/
|
|
74
|
+
static resourceColumns?: string[];
|
|
75
|
+
/**
|
|
76
|
+
* JSON keys for this model.
|
|
77
|
+
* This could be used if resource is not passed
|
|
78
|
+
* It will select only these keys from the model
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* class User extends Model {
|
|
82
|
+
* public static toJsonColumns = ["id", "name"];
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
static toJsonColumns?: string[];
|
|
87
|
+
/**
|
|
88
|
+
* Data source reference for this model.
|
|
89
|
+
*
|
|
90
|
+
* Can be:
|
|
91
|
+
* - A string name registered in the data-source registry
|
|
92
|
+
* - A DataSource instance
|
|
93
|
+
* - Undefined (falls back to the default data source)
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* class User extends Model {
|
|
98
|
+
* public static dataSource = "primary";
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
static dataSource?: string | DataSource;
|
|
103
|
+
/**
|
|
104
|
+
* Query builder class
|
|
105
|
+
*/
|
|
106
|
+
static builder?: new (...args: any[]) => QueryBuilderContract<Model>;
|
|
107
|
+
/**
|
|
108
|
+
* Primary key field name used to identify records.
|
|
109
|
+
*
|
|
110
|
+
* @default "id"
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* class User extends Model {
|
|
115
|
+
* public static primaryKey = "_id"; // MongoDB
|
|
116
|
+
* }
|
|
117
|
+
*
|
|
118
|
+
* class Product extends Model {
|
|
119
|
+
* public static primaryKey = "id"; // SQL
|
|
120
|
+
* }
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
static primaryKey: string;
|
|
124
|
+
/**
|
|
125
|
+
* Embeded fields when document is Being embeded
|
|
126
|
+
*/
|
|
127
|
+
static embed?: string[];
|
|
128
|
+
/**
|
|
129
|
+
* Validation and casting schema using @warlock.js/seal.
|
|
130
|
+
*
|
|
131
|
+
* Defines validation rules and data transformations for the model.
|
|
132
|
+
* Used automatically during save operations.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```typescript
|
|
136
|
+
* import { v } from "@warlock.js/seal";
|
|
137
|
+
*
|
|
138
|
+
* class User extends Model {
|
|
139
|
+
* public static schema = v.object({
|
|
140
|
+
* name: v.string().required().trim(),
|
|
141
|
+
* age: v.number().min(0).max(120),
|
|
142
|
+
* email: v.string().email().required().toLowerCase(),
|
|
143
|
+
* createdAt: v.date().default(() => new Date()),
|
|
144
|
+
* });
|
|
145
|
+
* }
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
148
|
+
static schema?: ObjectValidator;
|
|
149
|
+
/**
|
|
150
|
+
* Strict mode behavior for unknown fields.
|
|
151
|
+
*
|
|
152
|
+
* - `"strip"`: Remove unknown fields silently (default, recommended for APIs)
|
|
153
|
+
* - `"fail"`: Throw validation error on unknown fields (strict validation)
|
|
154
|
+
* - `"allow"`: Allow unknown fields to pass through (permissive)
|
|
155
|
+
*
|
|
156
|
+
* @default "strip"
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* import { Model, type StrictMode } from "@warlock.js/cascade";
|
|
161
|
+
*
|
|
162
|
+
* class User extends Model {
|
|
163
|
+
* public static strictMode: StrictMode = "fail"; // Throw on unknown fields
|
|
164
|
+
* }
|
|
165
|
+
*
|
|
166
|
+
* const user = new User({ name: "Alice", unknownField: "value" });
|
|
167
|
+
* await user.save(); // DatabaseWriterValidationError: unknown field
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
static strictMode: StrictMode;
|
|
171
|
+
/**
|
|
172
|
+
* Auto-generate incremental `id` field on insert (NoSQL only).
|
|
173
|
+
*
|
|
174
|
+
* When enabled, the ID generator creates a sequential integer ID
|
|
175
|
+
* separate from the database's native ID (_id for MongoDB).
|
|
176
|
+
*
|
|
177
|
+
* **Note:** SQL databases use native AUTO_INCREMENT and don't need this.
|
|
178
|
+
*
|
|
179
|
+
* @default true
|
|
180
|
+
*
|
|
181
|
+
* @example
|
|
182
|
+
* ```typescript
|
|
183
|
+
* class User extends Model {
|
|
184
|
+
* public static autoGenerateId = true;
|
|
185
|
+
* }
|
|
186
|
+
*
|
|
187
|
+
* const user = new User({ name: "Alice" });
|
|
188
|
+
* await user.save();
|
|
189
|
+
* console.log(user.get("_id")); // ObjectId("...") - MongoDB
|
|
190
|
+
* console.log(user.get("id")); // 1 - Generated
|
|
191
|
+
* ```
|
|
192
|
+
*/
|
|
193
|
+
static autoGenerateId: boolean;
|
|
194
|
+
/**
|
|
195
|
+
* Initial ID value for the first record.
|
|
196
|
+
*
|
|
197
|
+
* If not set, defaults to 1 or uses `randomInitialId`.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```typescript
|
|
201
|
+
* class User extends Model {
|
|
202
|
+
* public static initialId = 1000; // Start from 1000
|
|
203
|
+
* }
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
static initialId?: number;
|
|
207
|
+
/**
|
|
208
|
+
* Randomly generate the initial ID.
|
|
209
|
+
*
|
|
210
|
+
* Can be:
|
|
211
|
+
* - `true`: Generate random ID between 10000-499999
|
|
212
|
+
* - Function: Custom random ID generator
|
|
213
|
+
* - `false`: Use `initialId` or default to 1
|
|
214
|
+
*
|
|
215
|
+
* @default false
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```typescript
|
|
219
|
+
* class User extends Model {
|
|
220
|
+
* public static randomInitialId = true; // Random 10000-499999
|
|
221
|
+
* }
|
|
222
|
+
*
|
|
223
|
+
* class Product extends Model {
|
|
224
|
+
* public static randomInitialId = () => Math.floor(Math.random() * 1000000);
|
|
225
|
+
* }
|
|
226
|
+
* ```
|
|
227
|
+
*/
|
|
228
|
+
static randomInitialId?: boolean | (() => number);
|
|
229
|
+
/**
|
|
230
|
+
* Amount to increment ID by for each new record.
|
|
231
|
+
*
|
|
232
|
+
* If not set, defaults to 1 or uses `randomIncrement`.
|
|
233
|
+
*
|
|
234
|
+
* @default 1
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```typescript
|
|
238
|
+
* class User extends Model {
|
|
239
|
+
* public static incrementIdBy = 5; // Increment by 5
|
|
240
|
+
* }
|
|
241
|
+
* ```
|
|
242
|
+
*/
|
|
243
|
+
static incrementIdBy?: number;
|
|
244
|
+
/**
|
|
245
|
+
* Randomly generate the increment amount.
|
|
246
|
+
*
|
|
247
|
+
* Can be:
|
|
248
|
+
* - `true`: Generate random increment between 1-10
|
|
249
|
+
* - Function: Custom random increment generator
|
|
250
|
+
* - `false`: Use `incrementIdBy` or default to 1
|
|
251
|
+
*
|
|
252
|
+
* @default false
|
|
253
|
+
*
|
|
254
|
+
* @example
|
|
255
|
+
* ```typescript
|
|
256
|
+
* class User extends Model {
|
|
257
|
+
* public static randomIncrement = true; // Random 1-10
|
|
258
|
+
* }
|
|
259
|
+
*
|
|
260
|
+
* class Product extends Model {
|
|
261
|
+
* public static randomIncrement = () => Math.floor(Math.random() * 100);
|
|
262
|
+
* }
|
|
263
|
+
* ```
|
|
264
|
+
*/
|
|
265
|
+
static randomIncrement?: boolean | (() => number);
|
|
266
|
+
/**
|
|
267
|
+
* Created at column name.
|
|
268
|
+
*/
|
|
269
|
+
static createdAtColumn?: string | false;
|
|
270
|
+
/**
|
|
271
|
+
* Updated at column name.
|
|
272
|
+
*/
|
|
273
|
+
static updatedAtColumn?: string | false;
|
|
274
|
+
/**
|
|
275
|
+
* Delete strategy for this model.
|
|
276
|
+
*
|
|
277
|
+
* Controls how models are deleted:
|
|
278
|
+
* - `"trash"` - Moves to trash collection, then deletes
|
|
279
|
+
* - `"permanent"` - Direct deletion (hard delete)
|
|
280
|
+
* - `"soft"` - Sets deletedAt timestamp (soft delete)
|
|
281
|
+
*
|
|
282
|
+
* Can be overridden by destroy() options.
|
|
283
|
+
* Falls back to data source default if not set.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* class User extends Model {
|
|
288
|
+
* public static deleteStrategy: DeleteStrategy = "soft";
|
|
289
|
+
* }
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
static deleteStrategy?: DeleteStrategy;
|
|
293
|
+
/**
|
|
294
|
+
* Column name for soft delete timestamp.
|
|
295
|
+
*
|
|
296
|
+
* Used when delete strategy is "soft".
|
|
297
|
+
*
|
|
298
|
+
* @default "deletedAt"
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```typescript
|
|
302
|
+
* class User extends Model {
|
|
303
|
+
* public static deletedAtColumn = "archivedAt";
|
|
304
|
+
* }
|
|
305
|
+
* ```
|
|
306
|
+
*/
|
|
307
|
+
static deletedAtColumn: string | false;
|
|
308
|
+
/**
|
|
309
|
+
* Trash table/collection name override.
|
|
310
|
+
*
|
|
311
|
+
* If not set, defaults to `{table}Trash` or data source default.
|
|
312
|
+
* Used when delete strategy is "trash".
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* class User extends Model {
|
|
317
|
+
* public static trashTable = "userRecycleBin";
|
|
318
|
+
* }
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
static trashTable?: string;
|
|
322
|
+
/**
|
|
323
|
+
* Global scopes that are automatically applied to all queries.
|
|
324
|
+
* These scopes are inherited by child models.
|
|
325
|
+
*/
|
|
326
|
+
static globalScopes: Map<string, GlobalScopeDefinition>;
|
|
327
|
+
/**
|
|
328
|
+
* Local scopes that can be manually applied to queries.
|
|
329
|
+
* These are reusable query snippets that developers opt into.
|
|
330
|
+
*/
|
|
331
|
+
static localScopes: Map<string, LocalScopeCallback>;
|
|
332
|
+
/**
|
|
333
|
+
* Relation definitions for this model.
|
|
334
|
+
*
|
|
335
|
+
* Define relationships to other models using helper functions:
|
|
336
|
+
* - `hasMany()` - One-to-many (User has many Posts)
|
|
337
|
+
* - `hasOne()` - One-to-one (User has one Profile)
|
|
338
|
+
* - `belongsTo()` - Inverse of hasMany/hasOne (Post belongs to User)
|
|
339
|
+
* - `belongsToMany()` - Many-to-many with pivot table (User has many Roles)
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```typescript
|
|
343
|
+
* import { hasMany, belongsTo, belongsToMany, hasOne } from "@warlock.js/cascade";
|
|
344
|
+
*
|
|
345
|
+
* class User extends Model {
|
|
346
|
+
* public posts?: Post[]; // Optional: for TypeScript autocomplete
|
|
347
|
+
*
|
|
348
|
+
* static relations = {
|
|
349
|
+
* posts: hasMany("Post"),
|
|
350
|
+
* profile: hasOne("Profile"),
|
|
351
|
+
* organization: belongsTo("Organization"),
|
|
352
|
+
* roles: belongsToMany("Role", { pivot: "user_roles" }),
|
|
353
|
+
* };
|
|
354
|
+
* }
|
|
355
|
+
*
|
|
356
|
+
* // Usage:
|
|
357
|
+
* const users = await User.query().with("posts").get();
|
|
358
|
+
* console.log(users[0].posts); // Post[]
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
static relations: Record<string, any>;
|
|
362
|
+
/**
|
|
363
|
+
* Flag indicating whether this model instance represents a new (unsaved) record.
|
|
364
|
+
*
|
|
365
|
+
* - `true`: The model has not been persisted to the database yet
|
|
366
|
+
* - `false`: The model represents an existing database record
|
|
367
|
+
*
|
|
368
|
+
* This flag is used by the writer to determine whether to perform an insert or update.
|
|
369
|
+
*/
|
|
370
|
+
isNew: boolean;
|
|
371
|
+
/**
|
|
372
|
+
* The raw mutable data backing this model instance.
|
|
373
|
+
*
|
|
374
|
+
* All field accessors (get, set, merge, etc.) operate on this object.
|
|
375
|
+
*/
|
|
376
|
+
data: TSchema;
|
|
377
|
+
/**
|
|
378
|
+
* Dirty tracker that monitors changes to the model's data.
|
|
379
|
+
*
|
|
380
|
+
* Tracks:
|
|
381
|
+
* - Which fields have been modified (dirty columns)
|
|
382
|
+
* - Which fields have been removed
|
|
383
|
+
* - Original vs. current values for each dirty field
|
|
384
|
+
*
|
|
385
|
+
* Used by the writer to generate efficient partial update payloads.
|
|
386
|
+
*/
|
|
387
|
+
readonly dirtyTracker: DatabaseDirtyTracker;
|
|
388
|
+
/**
|
|
389
|
+
* Model instance events.
|
|
390
|
+
* Allows registering listeners for lifecycle events on this specific instance.
|
|
391
|
+
*/
|
|
392
|
+
events: ModelEvents<any>;
|
|
393
|
+
/**
|
|
394
|
+
* Map of loaded relations for this model instance.
|
|
395
|
+
*
|
|
396
|
+
* Populated automatically when using `with()` for eager loading,
|
|
397
|
+
* or when calling `load()` for lazy loading.
|
|
398
|
+
*
|
|
399
|
+
* @example
|
|
400
|
+
* ```typescript
|
|
401
|
+
* const user = await User.query().with("posts").first();
|
|
402
|
+
* console.log(user.loadedRelations.get("posts")); // Post[]
|
|
403
|
+
*
|
|
404
|
+
* // Also accessible as direct properties:
|
|
405
|
+
* console.log(user.posts); // Post[]\n * ```
|
|
406
|
+
*/
|
|
407
|
+
loadedRelations: Map<string, any>;
|
|
408
|
+
/**
|
|
409
|
+
* Column name for active status.
|
|
410
|
+
*/
|
|
411
|
+
protected isActiveColumn: string;
|
|
412
|
+
/**
|
|
413
|
+
* Constructs a new model instance with optional initial data.
|
|
414
|
+
*
|
|
415
|
+
* Initializes the dirty tracker with a snapshot of the provided data.
|
|
416
|
+
*
|
|
417
|
+
* @param initialData - Partial data to populate the model
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const user = new User({ name: "Alice", email: "alice@example.com" });
|
|
422
|
+
* ```
|
|
423
|
+
*/
|
|
424
|
+
constructor(initialData?: Partial<TSchema>);
|
|
425
|
+
/**
|
|
426
|
+
* Lazily load one or more relations for this model instance.
|
|
427
|
+
*
|
|
428
|
+
* This method loads relations on-demand after the model has been fetched.
|
|
429
|
+
* The loaded relations are attached directly to the model instance and
|
|
430
|
+
* also stored in `loadedRelations` map.
|
|
431
|
+
*
|
|
432
|
+
* @param relations - Relation name(s) to load
|
|
433
|
+
* @returns This model instance for chaining
|
|
434
|
+
*
|
|
435
|
+
* @example
|
|
436
|
+
* ```typescript
|
|
437
|
+
* const user = await User.first();
|
|
438
|
+
*
|
|
439
|
+
* // Load single relation
|
|
440
|
+
* await user.load("posts");
|
|
441
|
+
* console.log(user.posts); // Post[]
|
|
442
|
+
*
|
|
443
|
+
* // Load multiple relations
|
|
444
|
+
* await user.load("posts", "organization");
|
|
445
|
+
*
|
|
446
|
+
* // Chain with other operations
|
|
447
|
+
* const posts = await user.load("posts").then(() => user.posts);
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
450
|
+
load(...relations: string[]): Promise<this>;
|
|
451
|
+
/**
|
|
452
|
+
* Check if a relation has been loaded.
|
|
453
|
+
*
|
|
454
|
+
* @param relationName - Name of the relation to check
|
|
455
|
+
* @returns True if the relation has been loaded
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```typescript
|
|
459
|
+
* const user = await User.first();
|
|
460
|
+
*
|
|
461
|
+
* console.log(user.isLoaded("posts")); // false
|
|
462
|
+
* await user.load("posts");
|
|
463
|
+
* console.log(user.isLoaded("posts")); // true
|
|
464
|
+
* ```
|
|
465
|
+
*/
|
|
466
|
+
isLoaded(relationName: string): boolean;
|
|
467
|
+
/**
|
|
468
|
+
* Get a loaded relation by name.
|
|
469
|
+
*
|
|
470
|
+
* Returns undefined if the relation has not been loaded.
|
|
471
|
+
*
|
|
472
|
+
* @param relationName - Name of the relation to get
|
|
473
|
+
* @returns The loaded relation data, or undefined
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```typescript
|
|
477
|
+
* const user = await User.query().with("posts").first();
|
|
478
|
+
*
|
|
479
|
+
* const posts = user.getRelation<Post[]>("posts");
|
|
480
|
+
* console.log(posts?.length);
|
|
481
|
+
* ```
|
|
482
|
+
*/
|
|
483
|
+
getRelation<TRelation = any>(relationName: string): TRelation | undefined;
|
|
484
|
+
/**
|
|
485
|
+
* Get a model class by its name from the global registry.
|
|
486
|
+
*
|
|
487
|
+
* Models must be decorated with @RegisterModel() to be available in the registry.
|
|
488
|
+
*
|
|
489
|
+
* @param name - The model class name
|
|
490
|
+
* @returns The model class or undefined if not found
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* ```typescript
|
|
494
|
+
* const UserModel = Model.getModel("User");
|
|
495
|
+
* if (UserModel) {
|
|
496
|
+
* const user = await UserModel.find(1);
|
|
497
|
+
* }
|
|
498
|
+
* ```
|
|
499
|
+
*/
|
|
500
|
+
static getModel(name: string): ChildModel<Model<ModelSchema>>;
|
|
501
|
+
/**
|
|
502
|
+
* Get all registered models from the global registry.
|
|
503
|
+
*
|
|
504
|
+
* Only models decorated with @RegisterModel() will appear here.
|
|
505
|
+
*
|
|
506
|
+
* @returns A Map of all registered model classes by name
|
|
507
|
+
*
|
|
508
|
+
* @example
|
|
509
|
+
* ```typescript
|
|
510
|
+
* const allModels = Model.getAllModels();
|
|
511
|
+
* for (const [name, ModelClass] of allModels) {
|
|
512
|
+
* console.log(`Found model: ${name} with table: ${ModelClass.table}`);
|
|
513
|
+
* }
|
|
514
|
+
* ```
|
|
515
|
+
*/
|
|
516
|
+
static getAllModels(): Map<string, ChildModel<Model<ModelSchema>>>;
|
|
517
|
+
/**
|
|
518
|
+
* Create a sync operation for a single embedded document.
|
|
519
|
+
*
|
|
520
|
+
* When this model is updated, the target model's field
|
|
521
|
+
* will be updated with the embedded data.
|
|
522
|
+
*
|
|
523
|
+
* @param TargetModel - Target model class that receives data
|
|
524
|
+
* @param targetField - Field path in target model
|
|
525
|
+
* @returns Sync operation for chaining configuration
|
|
526
|
+
*
|
|
527
|
+
* @example
|
|
528
|
+
* ```typescript
|
|
529
|
+
* // When Category updates, update Product.category
|
|
530
|
+
* Category.sync(Product, "category");
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
static sync<TModel extends Model = Model>(this: ChildModel<TModel>, TargetModel: ChildModel<Model>, targetField: string): ModelSyncOperationContract;
|
|
534
|
+
/**
|
|
535
|
+
* Create a sync operation for an array of embedded documents.
|
|
536
|
+
*
|
|
537
|
+
* When this model is updated, the corresponding element
|
|
538
|
+
* in the target model's array field will be updated.
|
|
539
|
+
*
|
|
540
|
+
* @param TargetModel - Target model class that receives data
|
|
541
|
+
* @param targetField - Array field path in target model
|
|
542
|
+
* @returns Sync operation for chaining configuration
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* ```typescript
|
|
546
|
+
* // When Tag updates, update Post.tags[i] where tags[i].id matches
|
|
547
|
+
* Tag.syncMany(Post, "tags").identifyBy("id");
|
|
548
|
+
* ```
|
|
549
|
+
*/
|
|
550
|
+
static syncMany<TModel extends Model = Model>(this: ChildModel<TModel>, TargetModel: ChildModel<Model>, targetField: string): ModelSyncOperationContract;
|
|
551
|
+
/**
|
|
552
|
+
* Get model id
|
|
553
|
+
*/
|
|
554
|
+
get id(): number | string;
|
|
555
|
+
/**
|
|
556
|
+
* Get uuid
|
|
557
|
+
*/
|
|
558
|
+
get uuid(): string;
|
|
559
|
+
/**
|
|
560
|
+
* Retrieves a field value from the model's data.
|
|
561
|
+
*
|
|
562
|
+
* Supports both top-level keys and dot-notation paths for nested access.
|
|
563
|
+
*
|
|
564
|
+
* @param field - The field name or dot-notation path (e.g., "address.city")
|
|
565
|
+
* @param defaultValue - Value to return if the field is missing
|
|
566
|
+
* @returns The field value or the default value if not found
|
|
567
|
+
*
|
|
568
|
+
* @example
|
|
569
|
+
* ```typescript
|
|
570
|
+
* user.get("name"); // "Alice"
|
|
571
|
+
* user.get("address.city", "Unknown"); // "Unknown" if address.city is missing
|
|
572
|
+
* ```
|
|
573
|
+
*/
|
|
574
|
+
get<TKey extends keyof TSchema & string>(field: TKey): TSchema[TKey];
|
|
575
|
+
get<TKey extends keyof TSchema & string>(field: TKey, defaultValue: TSchema[TKey]): TSchema[TKey];
|
|
576
|
+
get<Type extends unknown = any>(field: string): Type;
|
|
577
|
+
get<Type extends unknown = any>(field: string, defaultValue: Type): Type;
|
|
578
|
+
/**
|
|
579
|
+
* Get only the values of the given fields
|
|
580
|
+
*/
|
|
581
|
+
only<TKey extends keyof TSchema & string>(fields: TKey[]): Record<TKey, TSchema[TKey]>;
|
|
582
|
+
only(fields: string[]): Record<string, unknown>;
|
|
583
|
+
/**
|
|
584
|
+
* Get a string value
|
|
585
|
+
*/
|
|
586
|
+
string(key: string, defaultValue?: string): string | undefined;
|
|
587
|
+
/**
|
|
588
|
+
* Get a number value
|
|
589
|
+
*/
|
|
590
|
+
number(key: string, defaultValue?: number): number | undefined;
|
|
591
|
+
/**
|
|
592
|
+
* Get a boolean value
|
|
593
|
+
*/
|
|
594
|
+
boolean(key: string, defaultValue?: boolean): boolean | undefined;
|
|
595
|
+
/**
|
|
596
|
+
* Sets a field value in the model's data and marks it as dirty.
|
|
597
|
+
*
|
|
598
|
+
* Supports both top-level keys and dot-notation paths for nested assignment.
|
|
599
|
+
* Automatically updates the dirty tracker to reflect the change.
|
|
600
|
+
*
|
|
601
|
+
* @param field - The field name or dot-notation path (e.g., "address.city")
|
|
602
|
+
* @param value - The value to assign
|
|
603
|
+
* @returns The model instance for method chaining
|
|
604
|
+
*
|
|
605
|
+
* @example
|
|
606
|
+
* ```typescript
|
|
607
|
+
* user.set("name", "Bob").set("address.city", "NYC");
|
|
608
|
+
* ```
|
|
609
|
+
*/
|
|
610
|
+
set<TKey extends keyof TSchema & string>(field: TKey, value: TSchema[TKey]): this;
|
|
611
|
+
set(field: string, value: unknown): this;
|
|
612
|
+
/**
|
|
613
|
+
* Checks whether a field exists in the model's data.
|
|
614
|
+
*
|
|
615
|
+
* Supports both top-level keys and dot-notation paths.
|
|
616
|
+
*
|
|
617
|
+
* @param field - The field name or dot-notation path
|
|
618
|
+
* @returns `true` if the field exists, `false` otherwise
|
|
619
|
+
*
|
|
620
|
+
* @example
|
|
621
|
+
* ```typescript
|
|
622
|
+
* user.has("name"); // true
|
|
623
|
+
* user.has("address.zipCode"); // false
|
|
624
|
+
* ```
|
|
625
|
+
*/
|
|
626
|
+
has<TKey extends keyof TSchema & string>(field: TKey): boolean;
|
|
627
|
+
has(field: string): boolean;
|
|
628
|
+
/**
|
|
629
|
+
* Increment the given field by the given amount
|
|
630
|
+
*/
|
|
631
|
+
increment<TKey extends keyof TSchema & string>(field: TKey, amount: number): this;
|
|
632
|
+
increment(field: string, amount?: number): this;
|
|
633
|
+
/**
|
|
634
|
+
* Decrement the given field by the given amount
|
|
635
|
+
*/
|
|
636
|
+
decrement<TKey extends keyof TSchema & string>(field: TKey, amount: number): this;
|
|
637
|
+
decrement(field: string, amount?: number): this;
|
|
638
|
+
/**
|
|
639
|
+
* Removes one or more fields from the model's data and marks them as removed.
|
|
640
|
+
*
|
|
641
|
+
* Supports both top-level keys and dot-notation paths.
|
|
642
|
+
* Automatically updates the dirty tracker to reflect the removal.
|
|
643
|
+
*
|
|
644
|
+
* @param fields - One or more field names or dot-notation paths to remove
|
|
645
|
+
* @returns The model instance for method chaining
|
|
646
|
+
*
|
|
647
|
+
* @example
|
|
648
|
+
* ```typescript
|
|
649
|
+
* user.unset("tempField", "address.oldZip");
|
|
650
|
+
* ```
|
|
651
|
+
*/
|
|
652
|
+
unset(...fields: (keyof TSchema & string)[]): this;
|
|
653
|
+
unset(...fields: string[]): this;
|
|
654
|
+
/**
|
|
655
|
+
* Merges new values into the model's data and marks changed fields as dirty.
|
|
656
|
+
*
|
|
657
|
+
* Performs a deep merge, preserving existing nested structures.
|
|
658
|
+
* Automatically updates the dirty tracker to reflect all changes.
|
|
659
|
+
*
|
|
660
|
+
* @param values - Partial data to merge into the model
|
|
661
|
+
* @returns The model instance for method chaining
|
|
662
|
+
*
|
|
663
|
+
* @example
|
|
664
|
+
* ```typescript
|
|
665
|
+
* user.merge({ name: "Charlie", address: { city: "LA" } });
|
|
666
|
+
* ```
|
|
667
|
+
*/
|
|
668
|
+
merge(values: Partial<TSchema>): this;
|
|
669
|
+
merge(values: Record<string, unknown>): this;
|
|
670
|
+
/**
|
|
671
|
+
* Perform atomoic update from current model instance
|
|
672
|
+
* Please note that it would require the id to be existing in the current
|
|
673
|
+
* model instance
|
|
674
|
+
* @returns number of affected records
|
|
675
|
+
*/
|
|
676
|
+
atomicUpdate(operations: Record<string, unknown>): Promise<number>;
|
|
677
|
+
/**
|
|
678
|
+
* Perform atomic increment
|
|
679
|
+
* This would issue a query update and update the given field without
|
|
680
|
+
* saving the model
|
|
681
|
+
*/
|
|
682
|
+
atomicIncrement<T extends keyof TSchema & string>(field: T, amount?: number): Promise<number>;
|
|
683
|
+
/**
|
|
684
|
+
* Perform atomic decrement
|
|
685
|
+
* This would issue a query update and update the given field without
|
|
686
|
+
* saving the model
|
|
687
|
+
*/
|
|
688
|
+
atomicDecrement<T extends keyof TSchema & string>(field: T, amount?: number): Promise<number>;
|
|
689
|
+
/**
|
|
690
|
+
* Determine if current model is active
|
|
691
|
+
*/
|
|
692
|
+
get isActive(): boolean;
|
|
693
|
+
/**
|
|
694
|
+
* Get created at date
|
|
695
|
+
*/
|
|
696
|
+
get createdAt(): Date | undefined;
|
|
697
|
+
/**
|
|
698
|
+
* Get updated at date
|
|
699
|
+
*/
|
|
700
|
+
get updatedAt(): Date | undefined;
|
|
701
|
+
/**
|
|
702
|
+
* Check if current model record is created by the given user model
|
|
703
|
+
*/
|
|
704
|
+
isCreatedBy(user: Model | GenericObject): boolean;
|
|
705
|
+
/**
|
|
706
|
+
* Checks whether the model's data has changed since instantiation or last reset.
|
|
707
|
+
*
|
|
708
|
+
* @returns `true` if any fields have been modified or removed, `false` otherwise
|
|
709
|
+
*
|
|
710
|
+
* @example
|
|
711
|
+
* ```typescript
|
|
712
|
+
* const user = new User({ name: "Alice" });
|
|
713
|
+
* user.hasChanges(); // false
|
|
714
|
+
* user.set("name", "Bob");
|
|
715
|
+
* user.hasChanges(); // true
|
|
716
|
+
* ```
|
|
717
|
+
*/
|
|
718
|
+
hasChanges(): boolean;
|
|
719
|
+
/**
|
|
720
|
+
* Check if the given column has been modified.
|
|
721
|
+
*
|
|
722
|
+
* @param column - The column name to check
|
|
723
|
+
* @returns `true` if the column has been modified, `false` otherwise
|
|
724
|
+
*
|
|
725
|
+
* @example
|
|
726
|
+
* ```typescript
|
|
727
|
+
* user.set("name", "Bob");
|
|
728
|
+
* user.isDirty("name"); // true
|
|
729
|
+
* ```
|
|
730
|
+
*/
|
|
731
|
+
isDirty(column: string): boolean;
|
|
732
|
+
/**
|
|
733
|
+
* Retrieves all dirty columns with their old and new values.
|
|
734
|
+
*
|
|
735
|
+
* @returns A record mapping each dirty column to its previous and current value
|
|
736
|
+
*
|
|
737
|
+
* @example
|
|
738
|
+
* ```typescript
|
|
739
|
+
* user.set("name", "Bob");
|
|
740
|
+
* user.getDirtyColumnsWithValues();
|
|
741
|
+
* // { name: { oldValue: "Alice", newValue: "Bob" } }
|
|
742
|
+
* ```
|
|
743
|
+
*/
|
|
744
|
+
getDirtyColumnsWithValues(): Record<string, {
|
|
745
|
+
oldValue: unknown;
|
|
746
|
+
newValue: unknown;
|
|
747
|
+
}>;
|
|
748
|
+
/**
|
|
749
|
+
* Lists all columns that have been removed from the model's data.
|
|
750
|
+
*
|
|
751
|
+
* @returns An array of field names that were present initially but have been unset
|
|
752
|
+
*
|
|
753
|
+
* @example
|
|
754
|
+
* ```typescript
|
|
755
|
+
* user.unset("tempField");
|
|
756
|
+
* user.getRemovedColumns(); // ["tempField"]
|
|
757
|
+
* ```
|
|
758
|
+
*/
|
|
759
|
+
getRemovedColumns(): string[];
|
|
760
|
+
/**
|
|
761
|
+
* Lists all columns that have been modified since instantiation or last reset.
|
|
762
|
+
*
|
|
763
|
+
* @returns An array of field names that have changed
|
|
764
|
+
*
|
|
765
|
+
* @example
|
|
766
|
+
* ```typescript
|
|
767
|
+
* user.set("name", "Bob");
|
|
768
|
+
* user.getDirtyColumns(); // ["name"]
|
|
769
|
+
* ```
|
|
770
|
+
*/
|
|
771
|
+
getDirtyColumns(): string[];
|
|
772
|
+
/**
|
|
773
|
+
* Emits a lifecycle event to both per-model and global listeners.
|
|
774
|
+
*
|
|
775
|
+
* This method is public so that external services (like the writer) can trigger
|
|
776
|
+
* lifecycle events when appropriate.
|
|
777
|
+
*
|
|
778
|
+
* @param event - The event name (e.g., "saving", "created", "deleting")
|
|
779
|
+
* @param context - Optional context data to pass to listeners
|
|
780
|
+
*
|
|
781
|
+
* @example
|
|
782
|
+
* ```typescript
|
|
783
|
+
* await user.emitEvent("saving");
|
|
784
|
+
* await user.emitEvent("validated", { errors: [] });
|
|
785
|
+
* ```
|
|
786
|
+
*/
|
|
787
|
+
emitEvent<TContext = unknown>(event: ModelEventName, context?: TContext): Promise<void>;
|
|
788
|
+
/**
|
|
789
|
+
* Register a listener for a model lifecycle event on this instance.
|
|
790
|
+
*
|
|
791
|
+
* @param event - Event name (e.g., "saving", "updated")
|
|
792
|
+
* @param listener - Callback function
|
|
793
|
+
* @returns Unsubscribe function
|
|
794
|
+
*/
|
|
795
|
+
on<TContext = unknown>(event: ModelEventName, listener: ModelEventListener<this, TContext>): () => void;
|
|
796
|
+
/**
|
|
797
|
+
* Register a one-time listener for a model lifecycle event on this instance.
|
|
798
|
+
*
|
|
799
|
+
* @param event - Event name
|
|
800
|
+
* @param listener - Callback function
|
|
801
|
+
* @returns Unsubscribe function
|
|
802
|
+
*/
|
|
803
|
+
once<TContext = unknown>(event: ModelEventName, listener: ModelEventListener<this, TContext>): () => void;
|
|
804
|
+
/**
|
|
805
|
+
* Remove a listener from this instance.
|
|
806
|
+
*
|
|
807
|
+
* @param event - Event name
|
|
808
|
+
* @param listener - Callback function to remove
|
|
809
|
+
*/
|
|
810
|
+
off<TContext = unknown>(event: ModelEventName, listener: ModelEventListener<this, TContext>): void;
|
|
811
|
+
/**
|
|
812
|
+
* Resolves the data source associated with this model.
|
|
813
|
+
*
|
|
814
|
+
* Resolution order:
|
|
815
|
+
* 1. If `dataSource` is a string, looks it up in the data-source registry
|
|
816
|
+
* 2. If `dataSource` is a DataSource instance, returns it directly
|
|
817
|
+
* 3. Otherwise, returns the default data source from the registry
|
|
818
|
+
*
|
|
819
|
+
* @returns The resolved DataSource instance
|
|
820
|
+
* @throws Error if no data source is found
|
|
821
|
+
*
|
|
822
|
+
* @example
|
|
823
|
+
* ```typescript
|
|
824
|
+
* class User extends Model {
|
|
825
|
+
* public static dataSource = "primary";
|
|
826
|
+
* }
|
|
827
|
+
*
|
|
828
|
+
* const ds = User.getDataSource();
|
|
829
|
+
* ```
|
|
830
|
+
*/
|
|
831
|
+
static getDataSource(): DataSource;
|
|
832
|
+
/**
|
|
833
|
+
* Get driver instance
|
|
834
|
+
*/
|
|
835
|
+
static getDriver(): DriverContract;
|
|
836
|
+
/**
|
|
837
|
+
* Generate next id and set it to current model's id
|
|
838
|
+
*/
|
|
839
|
+
generateNextId(): Promise<number | string>;
|
|
840
|
+
/**
|
|
841
|
+
* Apply model defaults from data source configuration.
|
|
842
|
+
*
|
|
843
|
+
* This is called automatically by getDataSource() the first time
|
|
844
|
+
* a model accesses its data source. Defaults are only applied if
|
|
845
|
+
* the model doesn't already have its own value set.
|
|
846
|
+
*
|
|
847
|
+
* The hierarchy is:
|
|
848
|
+
* 1. Model static property (highest priority - skipped here)
|
|
849
|
+
* 2. Database config modelDefaults (passed here)
|
|
850
|
+
* 3. Driver modelDefaults (merged before passing here)
|
|
851
|
+
* 4. Framework defaults (fallback values in the code)
|
|
852
|
+
*
|
|
853
|
+
* @param defaults - Model default configuration from data source
|
|
854
|
+
*/
|
|
855
|
+
static applyModelDefaults(defaults: any): void;
|
|
856
|
+
/**
|
|
857
|
+
* Add a global scope that is automatically applied to all queries.
|
|
858
|
+
*
|
|
859
|
+
* Global scopes are inherited by child models and applied before query execution.
|
|
860
|
+
* Use for security filters, multi-tenancy, soft deletes, etc.
|
|
861
|
+
*
|
|
862
|
+
* @param name - Unique name for the scope
|
|
863
|
+
* @param callback - Function that modifies the query
|
|
864
|
+
* @param options - Scope options (timing: 'before' | 'after')
|
|
865
|
+
*
|
|
866
|
+
* @example
|
|
867
|
+
* ```typescript
|
|
868
|
+
* // Multi-tenancy scope
|
|
869
|
+
* Model.addGlobalScope('tenant', (query) => {
|
|
870
|
+
* query.where('tenantId', getCurrentTenant());
|
|
871
|
+
* }, { timing: 'before' });
|
|
872
|
+
*
|
|
873
|
+
* // Soft delete scope
|
|
874
|
+
* User.addGlobalScope('notDeleted', (query) => {
|
|
875
|
+
* query.whereNull('deletedAt');
|
|
876
|
+
* });
|
|
877
|
+
* ```
|
|
878
|
+
*/
|
|
879
|
+
static addGlobalScope(name: string, callback: (query: QueryBuilderContract) => void, options?: GlobalScopeOptions): void;
|
|
880
|
+
/**
|
|
881
|
+
* Remove a global scope by name.
|
|
882
|
+
*
|
|
883
|
+
* @param name - Name of the scope to remove
|
|
884
|
+
*
|
|
885
|
+
* @example
|
|
886
|
+
* ```typescript
|
|
887
|
+
* Model.removeGlobalScope('tenant');
|
|
888
|
+
* ```
|
|
889
|
+
*/
|
|
890
|
+
static removeGlobalScope(name: string): void;
|
|
891
|
+
/**
|
|
892
|
+
* Add a local scope that can be manually applied to queries.
|
|
893
|
+
*
|
|
894
|
+
* Local scopes are reusable query snippets that developers opt into.
|
|
895
|
+
* They are not automatically applied.
|
|
896
|
+
*
|
|
897
|
+
* @param name - Unique name for the scope
|
|
898
|
+
* @param callback - Function that modifies the query
|
|
899
|
+
*
|
|
900
|
+
* @example
|
|
901
|
+
* ```typescript
|
|
902
|
+
* // Define reusable scopes
|
|
903
|
+
* User.addScope('active', (query) => {
|
|
904
|
+
* query.where('isActive', true);
|
|
905
|
+
* });
|
|
906
|
+
*
|
|
907
|
+
* User.addScope('admins', (query) => {
|
|
908
|
+
* query.where('role', 'admin');
|
|
909
|
+
* });
|
|
910
|
+
*
|
|
911
|
+
* // Use explicitly
|
|
912
|
+
* await User.query().scope('active').get();
|
|
913
|
+
* await User.query().scope('admins').get();
|
|
914
|
+
* ```
|
|
915
|
+
*/
|
|
916
|
+
static addScope(name: string, callback: LocalScopeCallback): void;
|
|
917
|
+
/**
|
|
918
|
+
* Remove a local scope by name.
|
|
919
|
+
*
|
|
920
|
+
* @param name - Name of the scope to remove
|
|
921
|
+
*
|
|
922
|
+
* @example
|
|
923
|
+
* ```typescript
|
|
924
|
+
* User.removeScope('active');
|
|
925
|
+
* ```
|
|
926
|
+
*/
|
|
927
|
+
static removeScope(name: string): void;
|
|
928
|
+
/**
|
|
929
|
+
* Create a new query builder for this model
|
|
930
|
+
*/
|
|
931
|
+
static query<TModel extends Model = Model>(this: ChildModel<TModel>): QueryBuilderContract<TModel>;
|
|
932
|
+
/**
|
|
933
|
+
* Eagerly load one or more relations with the query results.
|
|
934
|
+
*
|
|
935
|
+
* Relations are loaded in separate optimized queries to prevent N+1 problems.
|
|
936
|
+
* The loaded relations are attached to each model instance.
|
|
937
|
+
*
|
|
938
|
+
* @param relation - Single relation name to load
|
|
939
|
+
* @returns Query builder for chaining
|
|
940
|
+
*
|
|
941
|
+
* @example
|
|
942
|
+
* ```typescript
|
|
943
|
+
* // Load single relation
|
|
944
|
+
* const user = await User.query().with("posts").find(1);
|
|
945
|
+
* console.log(user.posts); // Post[]
|
|
946
|
+
*
|
|
947
|
+
* // Load multiple relations
|
|
948
|
+
* const user = await User.query().with("posts", "organization").find(1);
|
|
949
|
+
*
|
|
950
|
+
* // Load nested relations
|
|
951
|
+
* const user = await User.query().with("posts.comments.author").find(1);
|
|
952
|
+
* ```
|
|
953
|
+
*/
|
|
954
|
+
static with<TModel extends Model = Model>(this: ChildModel<TModel>, relation: string): QueryBuilderContract<TModel>;
|
|
955
|
+
/**
|
|
956
|
+
* Eagerly load multiple relations.
|
|
957
|
+
*
|
|
958
|
+
* @param relations - Relation names to load
|
|
959
|
+
* @returns Query builder for chaining
|
|
960
|
+
*/
|
|
961
|
+
static with<TModel extends Model = Model>(this: ChildModel<TModel>, ...relations: string[]): QueryBuilderContract<TModel>;
|
|
962
|
+
/**
|
|
963
|
+
* Eagerly load a relation with a constraint callback.
|
|
964
|
+
*
|
|
965
|
+
* The callback receives the relation query builder, allowing you to
|
|
966
|
+
* add conditions, ordering, or limits to the related query.
|
|
967
|
+
*
|
|
968
|
+
* @param relation - Relation name to load
|
|
969
|
+
* @param constraint - Callback to configure the relation query
|
|
970
|
+
* @returns Query builder for chaining
|
|
971
|
+
*
|
|
972
|
+
* @example
|
|
973
|
+
* ```typescript
|
|
974
|
+
* const user = await User.query()
|
|
975
|
+
* .with("posts", (query) => {
|
|
976
|
+
* query.where("isPublished", true)
|
|
977
|
+
* .orderBy("createdAt", "desc")
|
|
978
|
+
* .limit(5);
|
|
979
|
+
* })
|
|
980
|
+
* .find(1);
|
|
981
|
+
* ```
|
|
982
|
+
*/
|
|
983
|
+
static with<TModel extends Model = Model>(this: ChildModel<TModel>, relation: string, constraint: (query: QueryBuilderContract) => void): QueryBuilderContract<TModel>;
|
|
984
|
+
/**
|
|
985
|
+
* Eagerly load multiple relations with constraints.
|
|
986
|
+
*
|
|
987
|
+
* Pass an object where keys are relation names and values are either:
|
|
988
|
+
* - `true` to load without constraints
|
|
989
|
+
* - A callback function to configure the relation query
|
|
990
|
+
*
|
|
991
|
+
* @param relations - Object mapping relation names to constraints
|
|
992
|
+
* @returns Query builder for chaining
|
|
993
|
+
*
|
|
994
|
+
* @example
|
|
995
|
+
* ```typescript
|
|
996
|
+
* const user = await User.query()
|
|
997
|
+
* .with({
|
|
998
|
+
* posts: (query) => query.where("isPublished", true),
|
|
999
|
+
* organization: true,
|
|
1000
|
+
* roles: (query) => query.orderBy("priority"),
|
|
1001
|
+
* })
|
|
1002
|
+
* .find(1);
|
|
1003
|
+
* ```
|
|
1004
|
+
*/
|
|
1005
|
+
static with<TModel extends Model = Model>(this: ChildModel<TModel>, relations: Record<string, boolean | ((query: QueryBuilderContract) => void)>): QueryBuilderContract<TModel>;
|
|
1006
|
+
/**
|
|
1007
|
+
* Load relations using database JOINs in a single query.
|
|
1008
|
+
*
|
|
1009
|
+
* Unlike `with()` which uses separate queries, `joinWith()` uses
|
|
1010
|
+
* LEFT JOIN (SQL) or $lookup (MongoDB) to fetch related data
|
|
1011
|
+
* in a single query. The related data is hydrated into proper
|
|
1012
|
+
* model instances and attached to the main model.
|
|
1013
|
+
*
|
|
1014
|
+
* Best for: belongsTo and hasOne relations where you need
|
|
1015
|
+
* efficient single-query loading.
|
|
1016
|
+
*
|
|
1017
|
+
* @param relations - Relation names to load via JOIN
|
|
1018
|
+
* @returns Query builder for chaining
|
|
1019
|
+
*
|
|
1020
|
+
* @example
|
|
1021
|
+
* ```typescript
|
|
1022
|
+
* // Single relation
|
|
1023
|
+
* const post = await Post.joinWith("author").first();
|
|
1024
|
+
* console.log(post.author); // User model instance
|
|
1025
|
+
* console.log(post.data); // { id, title, authorId } - no author data
|
|
1026
|
+
*
|
|
1027
|
+
* // Multiple relations
|
|
1028
|
+
* const post = await Post.joinWith("author", "category").first();
|
|
1029
|
+
* ```
|
|
1030
|
+
*/
|
|
1031
|
+
static joinWith<TModel extends Model = Model>(this: ChildModel<TModel>, ...relations: string[]): QueryBuilderContract<TModel>;
|
|
1032
|
+
/**
|
|
1033
|
+
* Create new query builder.
|
|
1034
|
+
*
|
|
1035
|
+
* If the model has a static `builder` property set to a query builder class,
|
|
1036
|
+
* it will be instantiated instead of the default driver query builder.
|
|
1037
|
+
*
|
|
1038
|
+
* @example
|
|
1039
|
+
* ```typescript
|
|
1040
|
+
* class UserQueryBuilder<T = User> extends MongoQueryBuilder<T> {
|
|
1041
|
+
* active() { return this.where("isActive", true); }
|
|
1042
|
+
* }
|
|
1043
|
+
*
|
|
1044
|
+
* class User extends Model {
|
|
1045
|
+
* static builder = UserQueryBuilder; // That's it! ✨
|
|
1046
|
+
* }
|
|
1047
|
+
*
|
|
1048
|
+
* // Now User.query() returns UserQueryBuilder<User> with autocomplete!
|
|
1049
|
+
* ```
|
|
1050
|
+
*/
|
|
1051
|
+
static newQueryBuilder<TModel extends Model = Model>(this: ChildModel<TModel>): QueryBuilderContract<TModel>;
|
|
1052
|
+
/**
|
|
1053
|
+
* Get First matched record for the given filter
|
|
1054
|
+
*/
|
|
1055
|
+
static first<TModel extends Model = Model>(this: ChildModel<TModel>, filter?: Record<string, unknown>): Promise<TModel | null>;
|
|
1056
|
+
/**
|
|
1057
|
+
* Get last matched record for the given filter
|
|
1058
|
+
*/
|
|
1059
|
+
static last<TModel extends Model = Model>(this: ChildModel<TModel>, filter?: Record<string, unknown>): Promise<TModel | null>;
|
|
1060
|
+
/**
|
|
1061
|
+
* Use where clause directly
|
|
1062
|
+
*/
|
|
1063
|
+
static where<TModel extends Model = Model>(this: ChildModel<TModel>, field: string, value: unknown): QueryBuilderContract<TModel>;
|
|
1064
|
+
static where<TModel extends Model = Model>(this: ChildModel<TModel>, field: string, operator: WhereOperator, value: unknown): QueryBuilderContract<TModel>;
|
|
1065
|
+
static where<TModel extends Model = Model>(this: (new (...args: any[]) => TModel) & Pick<typeof Model, "query" | "getDataSource" | "table">, conditions: WhereObject): QueryBuilderContract<TModel>;
|
|
1066
|
+
static where<TModel extends Model = Model>(this: (new (...args: any[]) => TModel) & Pick<typeof Model, "query" | "getDataSource" | "table">, callback: WhereCallback<TModel>): QueryBuilderContract<TModel>;
|
|
1067
|
+
/**
|
|
1068
|
+
* Count the number of records in the table
|
|
1069
|
+
* @param filter - The filter to apply to the query
|
|
1070
|
+
*/
|
|
1071
|
+
static count<TModel extends Model = Model>(this: ChildModel<TModel>, filter?: Record<string, unknown>): Promise<number>;
|
|
1072
|
+
/**
|
|
1073
|
+
* Find record by id
|
|
1074
|
+
*/
|
|
1075
|
+
static find<TModel extends Model = Model>(this: ChildModel<TModel>, id: string | number): Promise<TModel | null>;
|
|
1076
|
+
/**
|
|
1077
|
+
* Get all records from the table
|
|
1078
|
+
*
|
|
1079
|
+
* @param filter - The filter to apply to the query
|
|
1080
|
+
* @returns All records from the table
|
|
1081
|
+
*/
|
|
1082
|
+
static all<TModel extends Model = Model>(this: ChildModel<TModel>, filter?: Record<string, unknown>): Promise<TModel[]>;
|
|
1083
|
+
/**
|
|
1084
|
+
* Perform pagination
|
|
1085
|
+
*/
|
|
1086
|
+
static paginate<TModel extends Model = Model>(this: ChildModel<TModel>, options?: PaginationOptions & {
|
|
1087
|
+
filter?: Record<string, unknown>;
|
|
1088
|
+
}): Promise<PaginationResult<TModel>>;
|
|
1089
|
+
/**
|
|
1090
|
+
* Get latest records from the table
|
|
1091
|
+
*
|
|
1092
|
+
* @param filter - The filter to apply to the query
|
|
1093
|
+
*/
|
|
1094
|
+
static latest<TModel extends Model = Model>(this: ChildModel<TModel>, filter?: Record<string, unknown>): Promise<TModel[]>;
|
|
1095
|
+
/**
|
|
1096
|
+
* Increment the given field by the given amount using atomic update
|
|
1097
|
+
*
|
|
1098
|
+
* @example ```typescript
|
|
1099
|
+
* // Increase age by 1 for user id 1
|
|
1100
|
+
* User.increment({id: 1}, "age", 1);
|
|
1101
|
+
* // Increase age by 1 and views by 2 for user id 1
|
|
1102
|
+
* User.increment({id: 1}, {age: 1, views: 2});
|
|
1103
|
+
* ```
|
|
1104
|
+
*/
|
|
1105
|
+
static increase<TModel extends Model = Model>(this: ChildModel<TModel>, filter: Record<string, unknown>, field: string, amount: number): Promise<number>;
|
|
1106
|
+
/**
|
|
1107
|
+
* Decrement the given field by the given amount using atomic update
|
|
1108
|
+
* @example ```typescript
|
|
1109
|
+
* // Decrease age by 1 for user id 1
|
|
1110
|
+
* User.decrement({id: 1}, "age", 1);
|
|
1111
|
+
* // Decrease age by 1 and views by 2 for user id 1
|
|
1112
|
+
* User.decrement({id: 1}, {age: 1, views: 2});
|
|
1113
|
+
* ```
|
|
1114
|
+
*/
|
|
1115
|
+
static decrease<TModel extends Model = Model>(this: ChildModel<TModel>, filter: Record<string, unknown>, field: string, amount: number): Promise<number>;
|
|
1116
|
+
/**
|
|
1117
|
+
* Perform atomic operation
|
|
1118
|
+
* Example
|
|
1119
|
+
*
|
|
1120
|
+
* ```typescript
|
|
1121
|
+
* const user = await User.atomic({id: 1}, {$inc: {age: 1}})
|
|
1122
|
+
* Returns user model with updated age
|
|
1123
|
+
*/
|
|
1124
|
+
static atomic<TModel extends Model = Model>(this: ChildModel<TModel>, filter: Record<string, unknown>, operations: UpdateOperations): Promise<number>;
|
|
1125
|
+
/**
|
|
1126
|
+
* Perform a direct update $set for the given id
|
|
1127
|
+
*/
|
|
1128
|
+
static update<TModel extends Model = Model>(this: ChildModel<TModel>, id: string | number, data: Record<string, unknown>): Promise<number>;
|
|
1129
|
+
/**
|
|
1130
|
+
* Find one and update multiple records that matches the provided filter and return the updated record
|
|
1131
|
+
* @param filter - Filter conditions
|
|
1132
|
+
* @param update - Update operations ($set, $unset, $inc)
|
|
1133
|
+
* @returns The updated records
|
|
1134
|
+
*/
|
|
1135
|
+
static findAndUpdate<TModel extends Model = Model>(this: ChildModel<TModel>, filter: Record<string, unknown>, update: UpdateOperations): Promise<TModel[]>;
|
|
1136
|
+
/**
|
|
1137
|
+
* Find one and update a single record that matches the provided filter and return the updated record
|
|
1138
|
+
* @param filter - Filter conditions
|
|
1139
|
+
* @param update - Update operations ($set, $unset, $inc)
|
|
1140
|
+
* @returns The updated record or null
|
|
1141
|
+
*/
|
|
1142
|
+
static findOneAndUpdate<TModel extends Model = Model>(this: ChildModel<TModel>, filter: Record<string, unknown>, update: UpdateOperations): Promise<TModel | null>;
|
|
1143
|
+
/**
|
|
1144
|
+
* Find and replace the entire document that matches the provided filter and return the replaced document
|
|
1145
|
+
*/
|
|
1146
|
+
static findAndReplace<TModel extends Model = Model>(this: ChildModel<TModel>, filter: Record<string, unknown>, document: Record<string, unknown>): Promise<TModel | null>;
|
|
1147
|
+
/**
|
|
1148
|
+
* Destroy (delete) the current model instance from the database.
|
|
1149
|
+
*
|
|
1150
|
+
* Emits lifecycle events:
|
|
1151
|
+
* - `deleting` - Before deletion
|
|
1152
|
+
* - `deleted` - After successful deletion
|
|
1153
|
+
*
|
|
1154
|
+
* @param options - Destroy options (strategy override, skipEvents)
|
|
1155
|
+
* @throws {Error} If the model is new (not saved) or if deletion fails
|
|
1156
|
+
*
|
|
1157
|
+
* @example
|
|
1158
|
+
* ```typescript
|
|
1159
|
+
* const user = await User.find(1);
|
|
1160
|
+
* await user.destroy(); // Uses default strategy
|
|
1161
|
+
* await user.destroy({ strategy: "permanent" }); // Override strategy
|
|
1162
|
+
* await user.destroy({ skipEvents: true }); // Silent delete
|
|
1163
|
+
* ```
|
|
1164
|
+
*/
|
|
1165
|
+
destroy(options?: {
|
|
1166
|
+
strategy?: DeleteStrategy;
|
|
1167
|
+
skipEvents?: boolean;
|
|
1168
|
+
}): Promise<RemoverResult>;
|
|
1169
|
+
/**
|
|
1170
|
+
* Get the class constructor from an instance.
|
|
1171
|
+
*
|
|
1172
|
+
* This helper method allows instance methods to access static properties
|
|
1173
|
+
* and methods of the model class in a type-safe way.
|
|
1174
|
+
*
|
|
1175
|
+
* @returns The model class constructor
|
|
1176
|
+
*
|
|
1177
|
+
* @example
|
|
1178
|
+
* ```typescript
|
|
1179
|
+
* const constructor = this.self();
|
|
1180
|
+
* const table = constructor.table;
|
|
1181
|
+
* await constructor.deleteOne({ id: 1 });
|
|
1182
|
+
* ```
|
|
1183
|
+
*/
|
|
1184
|
+
self<TModel extends Model = this>(): ChildModel<TModel>;
|
|
1185
|
+
/**
|
|
1186
|
+
* Creates an immutable clone of the model with its current state.
|
|
1187
|
+
*
|
|
1188
|
+
* The cloned model:
|
|
1189
|
+
* - Contains a deep copy of all current data
|
|
1190
|
+
* - Has frozen (immutable) data that cannot be modified
|
|
1191
|
+
* - Preserves the `isNew` flag from the original
|
|
1192
|
+
* - Has no dirty changes (clean state)
|
|
1193
|
+
* - Cannot be saved or modified
|
|
1194
|
+
*
|
|
1195
|
+
* This is useful for:
|
|
1196
|
+
* - Creating snapshots of model state
|
|
1197
|
+
* - Passing read-only model data to other parts of the application
|
|
1198
|
+
* - Preventing accidental mutations
|
|
1199
|
+
* - Maintaining historical records
|
|
1200
|
+
*
|
|
1201
|
+
* @returns A new immutable model instance with the current state
|
|
1202
|
+
*
|
|
1203
|
+
* @example
|
|
1204
|
+
* ```typescript
|
|
1205
|
+
* const user = new User({ name: "Alice", email: "alice@example.com" });
|
|
1206
|
+
* await user.save();
|
|
1207
|
+
*
|
|
1208
|
+
* // Create an immutable snapshot
|
|
1209
|
+
* const snapshot = user.clone();
|
|
1210
|
+
*
|
|
1211
|
+
* // This will throw an error because the clone is immutable
|
|
1212
|
+
* snapshot.set("name", "Bob"); // TypeError: Cannot assign to read only property
|
|
1213
|
+
*
|
|
1214
|
+
* // Original can still be modified
|
|
1215
|
+
* user.set("name", "Bob");
|
|
1216
|
+
* await user.save();
|
|
1217
|
+
* ```
|
|
1218
|
+
*/
|
|
1219
|
+
clone(): this;
|
|
1220
|
+
/**
|
|
1221
|
+
* Recursively freezes an object and all its nested properties.
|
|
1222
|
+
*
|
|
1223
|
+
* @param obj - The object to freeze
|
|
1224
|
+
* @returns The frozen object
|
|
1225
|
+
*/
|
|
1226
|
+
deepFreeze<T>(obj: T): T;
|
|
1227
|
+
/**
|
|
1228
|
+
* Get table name
|
|
1229
|
+
*/
|
|
1230
|
+
getTableName(): string;
|
|
1231
|
+
/**
|
|
1232
|
+
* Get primary key name
|
|
1233
|
+
*/
|
|
1234
|
+
getPrimaryKey(): string;
|
|
1235
|
+
/**
|
|
1236
|
+
* Get model schema
|
|
1237
|
+
*/
|
|
1238
|
+
getSchema(): ObjectValidator;
|
|
1239
|
+
/**
|
|
1240
|
+
* Check if schema has the given key
|
|
1241
|
+
*/
|
|
1242
|
+
schemaHas(key: string): boolean;
|
|
1243
|
+
/**
|
|
1244
|
+
* Get strict mode
|
|
1245
|
+
*/
|
|
1246
|
+
getStrictMode(): StrictMode;
|
|
1247
|
+
/**
|
|
1248
|
+
* Get data source (Connection)
|
|
1249
|
+
*/
|
|
1250
|
+
getConnection(): DataSource;
|
|
1251
|
+
/**
|
|
1252
|
+
* Delete all matching documents from the table.
|
|
1253
|
+
*/
|
|
1254
|
+
static delete<TModel extends Model = Model>(this: ChildModel<TModel>, filter?: Record<string, unknown>): Promise<number>;
|
|
1255
|
+
/**
|
|
1256
|
+
* Delete a single matching document from the table.
|
|
1257
|
+
*/
|
|
1258
|
+
static deleteOne<TModel extends Model = Model>(this: ChildModel<TModel>, filter?: Record<string, unknown>): Promise<number>;
|
|
1259
|
+
/**
|
|
1260
|
+
* Restore a single deleted record by its ID.
|
|
1261
|
+
*
|
|
1262
|
+
* Automatically detects whether the record was deleted via "trash" or "soft" strategy.
|
|
1263
|
+
* Handles ID conflicts based on options.
|
|
1264
|
+
*
|
|
1265
|
+
* @param id - The primary key value of the record to restore
|
|
1266
|
+
* @param options - Restorer options (onIdConflict, skipEvents)
|
|
1267
|
+
* @returns The restored model instance
|
|
1268
|
+
*
|
|
1269
|
+
* @throws {Error} If record not found in trash or soft-deleted records
|
|
1270
|
+
* @throws {Error} If ID conflict and onIdConflict is "fail"
|
|
1271
|
+
*
|
|
1272
|
+
* @example
|
|
1273
|
+
* ```typescript
|
|
1274
|
+
* // Restore with default options (assign new ID if conflict)
|
|
1275
|
+
* const user = await User.restore(123);
|
|
1276
|
+
*
|
|
1277
|
+
* // Restore and fail if ID conflict
|
|
1278
|
+
* const user = await User.restore(123, { onIdConflict: "fail" });
|
|
1279
|
+
*
|
|
1280
|
+
* // Silent restore (skip events)
|
|
1281
|
+
* const user = await User.restore(123, { skipEvents: true });
|
|
1282
|
+
* ```
|
|
1283
|
+
*/
|
|
1284
|
+
static restore<TModel extends Model = Model>(this: ChildModel<TModel>, id: string | number, options?: {
|
|
1285
|
+
onIdConflict?: "fail" | "assignNew";
|
|
1286
|
+
skipEvents?: boolean;
|
|
1287
|
+
}): Promise<TModel>;
|
|
1288
|
+
/**
|
|
1289
|
+
* Restore all deleted records for the model's table.
|
|
1290
|
+
*
|
|
1291
|
+
* Restores all records from the trash table (if using trash strategy)
|
|
1292
|
+
* or all soft-deleted records (if using soft strategy).
|
|
1293
|
+
*
|
|
1294
|
+
* @param options - Restorer options (onIdConflict, skipEvents)
|
|
1295
|
+
* @returns Array of restored model instances
|
|
1296
|
+
*
|
|
1297
|
+
* @example
|
|
1298
|
+
* ```typescript
|
|
1299
|
+
* // Restore all with default options
|
|
1300
|
+
* const users = await User.restoreAll();
|
|
1301
|
+
*
|
|
1302
|
+
* // Restore all and fail on any ID conflict
|
|
1303
|
+
* const users = await User.restoreAll({ onIdConflict: "fail" });
|
|
1304
|
+
* ```
|
|
1305
|
+
*/
|
|
1306
|
+
static restoreAll<TModel extends Model = Model>(this: ChildModel<TModel>, options?: {
|
|
1307
|
+
onIdConflict?: "fail" | "assignNew";
|
|
1308
|
+
skipEvents?: boolean;
|
|
1309
|
+
}): Promise<TModel[]>;
|
|
1310
|
+
/**
|
|
1311
|
+
* Create a new record in database and return the model instance.
|
|
1312
|
+
*
|
|
1313
|
+
* The data type is automatically inferred from the model's schema type.
|
|
1314
|
+
*
|
|
1315
|
+
* @param data - Partial data matching the model's schema type
|
|
1316
|
+
* @returns The created model instance
|
|
1317
|
+
*
|
|
1318
|
+
* @example
|
|
1319
|
+
* ```typescript
|
|
1320
|
+
* // TypeScript automatically infers UserSchema from User model
|
|
1321
|
+
* const user = await User.create({
|
|
1322
|
+
* name: "Alice",
|
|
1323
|
+
* email: "alice@example.com",
|
|
1324
|
+
* age: 30
|
|
1325
|
+
* });
|
|
1326
|
+
* // Type: User (with UserSchema inferred)
|
|
1327
|
+
* ```
|
|
1328
|
+
*/
|
|
1329
|
+
static create<TModel extends Model = Model, TSchema extends ModelSchema = TModel extends Model<infer S> ? S : ModelSchema>(this: ChildModel<TModel>, data: Partial<TSchema>): Promise<TModel>;
|
|
1330
|
+
/**
|
|
1331
|
+
* Create many documents and return an array of created models
|
|
1332
|
+
*/
|
|
1333
|
+
static createMany<TModel extends Model = Model, TSchema extends ModelSchema = TModel extends Model<infer S> ? S : ModelSchema>(this: ChildModel<TModel>, data: Partial<TSchema>[]): Promise<TModel[]>;
|
|
1334
|
+
/**
|
|
1335
|
+
* Find a record or create it if not found.
|
|
1336
|
+
*
|
|
1337
|
+
* Does NOT update existing records - returns them as-is.
|
|
1338
|
+
* Useful when you want to ensure a record exists without modifying it.
|
|
1339
|
+
*
|
|
1340
|
+
* @param filter - Conditions to find by
|
|
1341
|
+
* @param data - Data to create if not found (merged with filter)
|
|
1342
|
+
* @returns Promise resolving to found or created model
|
|
1343
|
+
*
|
|
1344
|
+
* @example
|
|
1345
|
+
* ```typescript
|
|
1346
|
+
* // Ensure default admin exists (don't modify if exists)
|
|
1347
|
+
* const admin = await User.findOrCreate(
|
|
1348
|
+
* { email: "admin@example.com" },
|
|
1349
|
+
* { email: "admin@example.com", name: "Admin", role: "admin" }
|
|
1350
|
+
* );
|
|
1351
|
+
* // If admin exists, returns existing (password unchanged)
|
|
1352
|
+
* // If not found, creates new admin
|
|
1353
|
+
* ```
|
|
1354
|
+
*/
|
|
1355
|
+
static findOrCreate<TModel extends Model = Model, TSchema extends ModelSchema = TModel extends Model<infer S> ? S : ModelSchema>(this: ChildModel<TModel>, filter: Partial<TSchema>, data: Partial<TSchema>): Promise<TModel>;
|
|
1356
|
+
/**
|
|
1357
|
+
* Upsert (insert or update) a record atomically.
|
|
1358
|
+
*
|
|
1359
|
+
* Uses the driver's native upsert operation for atomic insert-or-update.
|
|
1360
|
+
* More efficient than updateOrCreate as it's a single database operation.
|
|
1361
|
+
*
|
|
1362
|
+
* Includes full Model features:
|
|
1363
|
+
* - ID generation (if creating)
|
|
1364
|
+
* - createdAt timestamp (if creating)
|
|
1365
|
+
* - updatedAt timestamp (always)
|
|
1366
|
+
* - Validation & casting
|
|
1367
|
+
* - Lifecycle events
|
|
1368
|
+
*
|
|
1369
|
+
* @param filter - Conditions to find by (used for conflict detection)
|
|
1370
|
+
* @param data - Data to update or create (merged with filter)
|
|
1371
|
+
* @param options - Upsert options (conflictColumns for PostgreSQL, etc.)
|
|
1372
|
+
* @returns Promise resolving to upserted model
|
|
1373
|
+
*
|
|
1374
|
+
* @example
|
|
1375
|
+
* ```typescript
|
|
1376
|
+
* // PostgreSQL: upsert on unique email
|
|
1377
|
+
* const user = await User.upsert(
|
|
1378
|
+
* { email: "user@example.com" },
|
|
1379
|
+
* {
|
|
1380
|
+
* email: "user@example.com",
|
|
1381
|
+
* name: "John Updated",
|
|
1382
|
+
* lastSyncedAt: new Date()
|
|
1383
|
+
* },
|
|
1384
|
+
* { conflictColumns: ["email"] }
|
|
1385
|
+
* );
|
|
1386
|
+
*
|
|
1387
|
+
* // MongoDB: upsert by filter
|
|
1388
|
+
* const user = await User.upsert(
|
|
1389
|
+
* { externalId: "ext-123" },
|
|
1390
|
+
* {
|
|
1391
|
+
* externalId: "ext-123",
|
|
1392
|
+
* name: "John Updated",
|
|
1393
|
+
* email: "john.new@example.com"
|
|
1394
|
+
* }
|
|
1395
|
+
* );
|
|
1396
|
+
* ```
|
|
1397
|
+
*/
|
|
1398
|
+
static upsert<TModel extends Model = Model, TSchema extends ModelSchema = TModel extends Model<infer S> ? S : ModelSchema>(this: ChildModel<TModel>, filter: Partial<TSchema>, data: Partial<TSchema>, options?: Record<string, unknown>): Promise<TModel>;
|
|
1399
|
+
/**
|
|
1400
|
+
* Update a record or create it if not found (upsert).
|
|
1401
|
+
*
|
|
1402
|
+
* @deprecated Use `upsert()` instead for better performance and atomicity.
|
|
1403
|
+
* This method is kept for backward compatibility but uses upsert internally.
|
|
1404
|
+
*
|
|
1405
|
+
* @param filter - Conditions to find by
|
|
1406
|
+
* @param data - Data to update or create (merged with filter)
|
|
1407
|
+
* @returns Promise resolving to updated or created model
|
|
1408
|
+
*/
|
|
1409
|
+
static updateOrCreate<TModel extends Model = Model, TSchema extends ModelSchema = TModel extends Model<infer S> ? S : ModelSchema>(this: ChildModel<TModel>, filter: Partial<TSchema>, data: Partial<TSchema>, options?: Record<string, unknown>): Promise<TModel>;
|
|
1410
|
+
/**
|
|
1411
|
+
* Find one and delete a record that matches the filter and return the deleted record.
|
|
1412
|
+
*
|
|
1413
|
+
* @param filter - Filter conditions
|
|
1414
|
+
* @param options - Optional delete options
|
|
1415
|
+
* @returns The deleted model instance or null if not found
|
|
1416
|
+
*
|
|
1417
|
+
* @example
|
|
1418
|
+
* ```typescript
|
|
1419
|
+
* const deleted = await User.findOneAndDelete({ id: 1 });
|
|
1420
|
+
* if (deleted) {
|
|
1421
|
+
* console.log('Deleted user:', deleted.get('name'));
|
|
1422
|
+
* }
|
|
1423
|
+
* ```
|
|
1424
|
+
*/
|
|
1425
|
+
static findOneAndDelete<TModel extends Model = Model>(this: ChildModel<TModel>, filter: Record<string, unknown>, options?: Record<string, unknown>): Promise<TModel | null>;
|
|
1426
|
+
/**
|
|
1427
|
+
* Returns embedded data for sync operations.
|
|
1428
|
+
* Excludes internal MongoDB fields and ensures proper date serialization.
|
|
1429
|
+
*
|
|
1430
|
+
* @returns Embedded data object suitable for syncing
|
|
1431
|
+
*
|
|
1432
|
+
* @example
|
|
1433
|
+
* ```typescript
|
|
1434
|
+
* const user = await User.find(1);
|
|
1435
|
+
* const embedData = user.embedData;
|
|
1436
|
+
* // Returns: { id: 1, name: "Alice", email: "alice@example.com", ... }
|
|
1437
|
+
* // Excludes: _id
|
|
1438
|
+
* ```
|
|
1439
|
+
*/
|
|
1440
|
+
get embedData(): Record<string, unknown>;
|
|
1441
|
+
/**
|
|
1442
|
+
* Cleanup model events
|
|
1443
|
+
*/
|
|
1444
|
+
static $cleanup(): void;
|
|
1445
|
+
/**
|
|
1446
|
+
* Accesses the event emitter dedicated to this model constructor.
|
|
1447
|
+
*
|
|
1448
|
+
* Each model subclass gets its own isolated event emitter, allowing you to
|
|
1449
|
+
* register lifecycle hooks that only apply to that specific model.
|
|
1450
|
+
*
|
|
1451
|
+
* @returns The ModelEvents instance for this model constructor
|
|
1452
|
+
*
|
|
1453
|
+
* @example
|
|
1454
|
+
* ```typescript
|
|
1455
|
+
* User.events().onSaving((user) => {
|
|
1456
|
+
* console.log("User is being saved:", user);
|
|
1457
|
+
* });
|
|
1458
|
+
* ```
|
|
1459
|
+
*/
|
|
1460
|
+
static events<TModel extends Model = Model>(this: ChildModel<TModel>): ModelEvents<TModel>;
|
|
1461
|
+
/**
|
|
1462
|
+
* Registers an event listener for this model constructor.
|
|
1463
|
+
*
|
|
1464
|
+
* Convenience shorthand for `Model.events().on(...)`.
|
|
1465
|
+
*
|
|
1466
|
+
* @param event - The event name (e.g., "saving", "created")
|
|
1467
|
+
* @param listener - The callback to invoke when the event fires
|
|
1468
|
+
* @returns An unsubscribe function
|
|
1469
|
+
*
|
|
1470
|
+
* @example
|
|
1471
|
+
* ```typescript
|
|
1472
|
+
* const unsubscribe = User.on("saving", (user) => {
|
|
1473
|
+
* console.log("Saving user:", user);
|
|
1474
|
+
* });
|
|
1475
|
+
* ```
|
|
1476
|
+
*/
|
|
1477
|
+
static on<TModel extends Model = Model, TContext = unknown>(this: ChildModel<TModel>, event: ModelEventName, listener: ModelEventListener<TModel, TContext>): () => void;
|
|
1478
|
+
/**
|
|
1479
|
+
* Registers a one-time event listener for this model constructor.
|
|
1480
|
+
*
|
|
1481
|
+
* The listener will automatically unsubscribe after its first invocation.
|
|
1482
|
+
* Convenience shorthand for `Model.events().once(...)`.
|
|
1483
|
+
*
|
|
1484
|
+
* @param event - The event name (e.g., "saving", "created")
|
|
1485
|
+
* @param listener - The callback to invoke when the event fires
|
|
1486
|
+
* @returns An unsubscribe function
|
|
1487
|
+
*
|
|
1488
|
+
* @example
|
|
1489
|
+
* ```typescript
|
|
1490
|
+
* User.once("created", (user) => {
|
|
1491
|
+
* console.log("First user created:", user);
|
|
1492
|
+
* });
|
|
1493
|
+
* ```
|
|
1494
|
+
*/
|
|
1495
|
+
static once<TModel extends Model = Model, TContext = unknown>(this: ChildModel<TModel>, event: ModelEventName, listener: ModelEventListener<TModel, TContext>): () => void;
|
|
1496
|
+
/**
|
|
1497
|
+
* Removes an event listener from this model constructor.
|
|
1498
|
+
*
|
|
1499
|
+
* Convenience shorthand for `Model.events().off(...)`.
|
|
1500
|
+
*
|
|
1501
|
+
* @param event - The event name
|
|
1502
|
+
* @param listener - The callback to remove
|
|
1503
|
+
*
|
|
1504
|
+
* @example
|
|
1505
|
+
* ```typescript
|
|
1506
|
+
* const listener = (user) => console.log(user);
|
|
1507
|
+
* User.on("saving", listener);
|
|
1508
|
+
* User.off("saving", listener);
|
|
1509
|
+
* ```
|
|
1510
|
+
*/
|
|
1511
|
+
static off<TModel extends Model = Model, TContext = unknown>(this: ChildModel<TModel>, event: ModelEventName, listener: ModelEventListener<TModel, TContext>): void;
|
|
1512
|
+
/**
|
|
1513
|
+
* Accesses the global event emitter shared by all model instances.
|
|
1514
|
+
*
|
|
1515
|
+
* Use this for cross-cutting concerns like auditing, logging, or injecting
|
|
1516
|
+
* common fields (e.g., `createdBy`, `updatedBy`) across all models.
|
|
1517
|
+
*
|
|
1518
|
+
* @returns The global ModelEvents instance
|
|
1519
|
+
*
|
|
1520
|
+
* @example
|
|
1521
|
+
* ```typescript
|
|
1522
|
+
* Model.globalEvents().onSaving((model) => {
|
|
1523
|
+
* model.set("updatedAt", new Date());
|
|
1524
|
+
* });
|
|
1525
|
+
* ```
|
|
1526
|
+
*/
|
|
1527
|
+
static globalEvents(): ModelEvents<Model>;
|
|
1528
|
+
/**
|
|
1529
|
+
* Replace the model's data entirely.
|
|
1530
|
+
*
|
|
1531
|
+
* Used internally by the writer after validation to update the model
|
|
1532
|
+
* with validated/casted data.
|
|
1533
|
+
*
|
|
1534
|
+
* **Warning:** This replaces all data and updates the dirty tracker.
|
|
1535
|
+
* Use with caution in application code.
|
|
1536
|
+
*
|
|
1537
|
+
* @param data - New data to replace current data
|
|
1538
|
+
*
|
|
1539
|
+
* @example
|
|
1540
|
+
* ```typescript
|
|
1541
|
+
* // Internal usage by writer
|
|
1542
|
+
* model.replaceData(validatedData);
|
|
1543
|
+
* ```
|
|
1544
|
+
*/
|
|
1545
|
+
replaceData(data: Record<string, unknown>): void;
|
|
1546
|
+
/**
|
|
1547
|
+
* Save the model to the database.
|
|
1548
|
+
*
|
|
1549
|
+
* Performs insert if `isNew === true`, otherwise performs update.
|
|
1550
|
+
* Automatically validates, casts, generates IDs, and emits lifecycle events.
|
|
1551
|
+
*
|
|
1552
|
+
* **Features:**
|
|
1553
|
+
* - Validation via @warlock.js/seal schema
|
|
1554
|
+
* - Data casting (string → number, etc.)
|
|
1555
|
+
* - ID generation (NoSQL only)
|
|
1556
|
+
* - Partial updates (only changed fields)
|
|
1557
|
+
* - Lifecycle events (validating, saving, created/updated, saved)
|
|
1558
|
+
*
|
|
1559
|
+
* @param data - Optional data to merge before saving
|
|
1560
|
+
* @param options - Save options
|
|
1561
|
+
* @returns The model instance for method chaining
|
|
1562
|
+
*
|
|
1563
|
+
* @throws {ValidationError} If validation fails
|
|
1564
|
+
* @throws {Error} If database operation fails
|
|
1565
|
+
*
|
|
1566
|
+
* @example
|
|
1567
|
+
* ```typescript
|
|
1568
|
+
* // Simple save
|
|
1569
|
+
* const user = new User({ name: "Alice" });
|
|
1570
|
+
* await user.save();
|
|
1571
|
+
*
|
|
1572
|
+
* // Merge data before saving
|
|
1573
|
+
* await user.save({ age: 31, email: "alice@example.com" });
|
|
1574
|
+
*
|
|
1575
|
+
* // Silent save (no events)
|
|
1576
|
+
* await user.save(null, { skipEvents: true });
|
|
1577
|
+
*
|
|
1578
|
+
* // Skip validation
|
|
1579
|
+
* await user.save(null, { skipValidation: true });
|
|
1580
|
+
*
|
|
1581
|
+
* // Method chaining
|
|
1582
|
+
* await user.set("name", "Bob").save();
|
|
1583
|
+
* ```
|
|
1584
|
+
*/
|
|
1585
|
+
save(options?: WriterOptions & {
|
|
1586
|
+
merge?: Partial<TSchema>;
|
|
1587
|
+
}): Promise<this>;
|
|
1588
|
+
/**
|
|
1589
|
+
* Serialize the model data for storage in the database.
|
|
1590
|
+
*
|
|
1591
|
+
* Uses the driver's `serialize` to apply driver-specific type transformations
|
|
1592
|
+
* (e.g. Date → ISO string, BigInt → string for Postgres).
|
|
1593
|
+
*
|
|
1594
|
+
* **Not** the same as `toSnapshot` — this is a DB write concern, not a cache concern.
|
|
1595
|
+
*/
|
|
1596
|
+
serialize(): Record<string, unknown>;
|
|
1597
|
+
/**
|
|
1598
|
+
* Produce a plain-object snapshot of this model suitable for cache storage.
|
|
1599
|
+
*
|
|
1600
|
+
* - `data`: The model's own fields, serialized via the driver (handles Dates, BigInt, ObjectId).
|
|
1601
|
+
* - `relations`: Each entry in `loadedRelations` recursively snapshotted via `toSnapshot`.
|
|
1602
|
+
* A relation that was loaded but resolved to `null` is stored as `null` (not omitted),
|
|
1603
|
+
* so that `fromSnapshot` can distinguish "loaded + null" from "never loaded".
|
|
1604
|
+
*
|
|
1605
|
+
* Use `Model.fromSnapshot(snapshot)` to reconstruct.
|
|
1606
|
+
*
|
|
1607
|
+
* @example
|
|
1608
|
+
* ```typescript
|
|
1609
|
+
* await cache.set(key, chat.toSnapshot());
|
|
1610
|
+
* ```
|
|
1611
|
+
*/
|
|
1612
|
+
toSnapshot(): ModelSnapshot;
|
|
1613
|
+
/**
|
|
1614
|
+
* Reconstruct a model instance (with relations) from a cache snapshot.
|
|
1615
|
+
*
|
|
1616
|
+
* Counterpart to `toSnapshot`. Applies driver deserialization (e.g. ISO string → Date)
|
|
1617
|
+
* and recursively hydrates any nested relation snapshots via `RelationHydrator`.
|
|
1618
|
+
*
|
|
1619
|
+
* @example
|
|
1620
|
+
* ```typescript
|
|
1621
|
+
* const snapshot = await cache.get(key);
|
|
1622
|
+
* const chat = Chat.fromSnapshot(snapshot);
|
|
1623
|
+
* chat.unit; // Unit model instance, fully hydrated
|
|
1624
|
+
* ```
|
|
1625
|
+
*/
|
|
1626
|
+
static fromSnapshot<TModel extends Model>(this: ChildModel<TModel>, snapshot: ModelSnapshot): TModel;
|
|
1627
|
+
/**
|
|
1628
|
+
* Create a model instance from raw data (no relations).
|
|
1629
|
+
*
|
|
1630
|
+
* This is the data-only hydration path, used by the query builder when
|
|
1631
|
+
* converting a raw DB row into a model instance. Relations are NOT restored
|
|
1632
|
+
* here — use `fromSnapshot` when restoring from a cache snapshot that
|
|
1633
|
+
* includes relation data.
|
|
1634
|
+
*
|
|
1635
|
+
* @example
|
|
1636
|
+
* ```typescript
|
|
1637
|
+
* // Query builder internals:
|
|
1638
|
+
* const user = User.hydrate(rawRow);
|
|
1639
|
+
* ```
|
|
1640
|
+
*/
|
|
1641
|
+
static hydrate<TModel extends Model = Model>(this: ChildModel<TModel>, data: Record<string, unknown>): TModel;
|
|
1642
|
+
/**
|
|
1643
|
+
* Convert the model into JSON
|
|
1644
|
+
*/
|
|
1645
|
+
toJSON(): Record<string, unknown>;
|
|
1646
|
+
}
|
|
1647
|
+
//# sourceMappingURL=model.d.ts.map
|