postgrelize 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +289 -0
- package/LICENSE +21 -0
- package/README.md +131 -2
- package/dist/associations/alias-minifier.d.ts +43 -0
- package/dist/associations/association-accessors.d.ts +21 -0
- package/dist/associations/association-key-types.d.ts +44 -0
- package/dist/associations/association-keys.d.ts +98 -0
- package/dist/associations/association-lookup.d.ts +26 -0
- package/dist/associations/association-shape.d.ts +25 -0
- package/dist/associations/association.d.ts +95 -0
- package/dist/associations/belongs-to-many.d.ts +86 -0
- package/dist/associations/belongs-to.d.ts +45 -0
- package/dist/associations/has-many.d.ts +79 -0
- package/dist/associations/has-one.d.ts +44 -0
- package/dist/associations/include-assembler.d.ts +21 -0
- package/dist/associations/include-columns.d.ts +24 -0
- package/dist/associations/include-joins.d.ts +17 -0
- package/dist/associations/include-loader.d.ts +48 -0
- package/dist/associations/include-order.d.ts +22 -0
- package/dist/associations/include-query-builder.d.ts +54 -0
- package/dist/associations/include-resolver.d.ts +29 -0
- package/dist/associations/include-rows.d.ts +47 -0
- package/dist/associations/include-sub-query.d.ts +35 -0
- package/dist/associations/include.d.ts +112 -0
- package/dist/associations/junction-keys.d.ts +23 -0
- package/dist/associations/nested-creator.d.ts +44 -0
- package/dist/associations/nested-saver.d.ts +30 -0
- package/dist/associations/owner-keys.d.ts +34 -0
- package/dist/associations/separate-include-loader.d.ts +24 -0
- package/dist/associations/types.d.ts +139 -0
- package/dist/connection/after-connect-hook.d.ts +24 -0
- package/dist/connection/connect-hooks.d.ts +26 -0
- package/dist/connection/connection-config.d.ts +33 -0
- package/dist/connection/connection-manager.d.ts +65 -0
- package/dist/connection/connection-types.d.ts +21 -0
- package/dist/connection/copy-from-stdin.d.ts +52 -0
- package/dist/connection/copy-runner.d.ts +30 -0
- package/dist/connection/copy-to-stdout.d.ts +49 -0
- package/dist/connection/copy.d.ts +22 -0
- package/dist/connection/notifications.d.ts +28 -0
- package/dist/connection/options.d.ts +165 -0
- package/dist/connection/query-logger.d.ts +41 -0
- package/dist/connection/replica-router.d.ts +20 -0
- package/dist/connection/statement-cache.d.ts +36 -0
- package/dist/connection/statement-timeout.d.ts +22 -0
- package/dist/data-types/abstract-data-type.d.ts +58 -0
- package/dist/data-types/array-literal-parser.d.ts +17 -0
- package/dist/data-types/array-type.d.ts +18 -0
- package/dist/data-types/bit-type.d.ts +14 -0
- package/dist/data-types/blob-type.d.ts +12 -0
- package/dist/data-types/char-type.d.ts +12 -0
- package/dist/data-types/composite-fields.d.ts +26 -0
- package/dist/data-types/composite-type.d.ts +32 -0
- package/dist/data-types/data-types.d.ts +176 -0
- package/dist/data-types/date-type.d.ts +14 -0
- package/dist/data-types/decimal-type.d.ts +13 -0
- package/dist/data-types/default-value-generator.d.ts +15 -0
- package/dist/data-types/enum-type.d.ts +19 -0
- package/dist/data-types/ewkb-reader.d.ts +18 -0
- package/dist/data-types/ewkb-writer.d.ts +17 -0
- package/dist/data-types/float-type.d.ts +12 -0
- package/dist/data-types/geometric-text.d.ts +65 -0
- package/dist/data-types/geometric-type.d.ts +46 -0
- package/dist/data-types/hstore-type.d.ts +16 -0
- package/dist/data-types/integer-type.d.ts +24 -0
- package/dist/data-types/json-type.d.ts +16 -0
- package/dist/data-types/multirange-type.d.ts +19 -0
- package/dist/data-types/range-type.d.ts +28 -0
- package/dist/data-types/record-text.d.ts +29 -0
- package/dist/data-types/scalar-type.d.ts +13 -0
- package/dist/data-types/spatial-type.d.ts +20 -0
- package/dist/data-types/string-type.d.ts +12 -0
- package/dist/data-types/text-type.d.ts +12 -0
- package/dist/data-types/timestamp-type.d.ts +15 -0
- package/dist/data-types/virtual-type.d.ts +15 -0
- package/dist/data-types/wall-clock.d.ts +14 -0
- package/dist/ddl/column-ddl.d.ts +73 -0
- package/dist/ddl/constraint-ddl.d.ts +30 -0
- package/dist/ddl/ddl-generator.d.ts +181 -0
- package/dist/ddl/ddl-guards.d.ts +60 -0
- package/dist/ddl/ddl-types.d.ts +204 -0
- package/dist/ddl/enum-ddl.d.ts +42 -0
- package/dist/ddl/extension-ddl.d.ts +57 -0
- package/dist/ddl/function-ddl.d.ts +98 -0
- package/dist/ddl/index-ddl.d.ts +29 -0
- package/dist/ddl/index-element-ddl.d.ts +19 -0
- package/dist/ddl/partition-ddl.d.ts +88 -0
- package/dist/ddl/partition-types.d.ts +94 -0
- package/dist/ddl/policy-ddl.d.ts +137 -0
- package/dist/ddl/routine-guards.d.ts +63 -0
- package/dist/ddl/sequence-ddl.d.ts +103 -0
- package/dist/ddl/sql-literal.d.ts +61 -0
- package/dist/ddl/table-ddl.d.ts +35 -0
- package/dist/ddl/trigger-ddl.d.ts +81 -0
- package/dist/ddl/view-ddl.d.ts +122 -0
- package/dist/decorators/association-applier.d.ts +18 -0
- package/dist/decorators/association-decorators.d.ts +72 -0
- package/dist/decorators/column-decorators.d.ts +147 -0
- package/dist/decorators/decorator-factory.d.ts +45 -0
- package/dist/decorators/decorator-signature.d.ts +26 -0
- package/dist/decorators/decorator-types.d.ts +24 -0
- package/dist/decorators/foreign-key-decorator.d.ts +21 -0
- package/dist/decorators/hook-decorators.d.ts +393 -0
- package/dist/decorators/init-arguments.d.ts +29 -0
- package/dist/decorators/model-decorators.d.ts +18 -0
- package/dist/decorators/model-metadata.d.ts +70 -0
- package/dist/decorators/table-decorators.d.ts +43 -0
- package/dist/decorators/timestamp-decorators.d.ts +38 -0
- package/dist/errors/base-error.d.ts +40 -0
- package/dist/errors/connection-errors.d.ts +152 -0
- package/dist/errors/database-errors.d.ts +186 -0
- package/dist/errors/errors.d.ts +5 -0
- package/dist/errors/map-pg-error.d.ts +27 -0
- package/dist/errors/misc-errors.d.ts +232 -0
- package/dist/errors/validation-errors.d.ts +141 -0
- package/dist/exports-associations.d.ts +6 -0
- package/dist/exports-core.d.ts +95 -0
- package/dist/exports-decorators.d.ts +8 -0
- package/dist/exports-errors.d.ts +2 -0
- package/dist/exports-model.d.ts +21 -0
- package/dist/exports-schema.d.ts +19 -0
- package/dist/expressions/call-expressions.d.ts +54 -0
- package/dist/expressions/condition-expressions.d.ts +51 -0
- package/dist/expressions/expression.d.ts +52 -0
- package/dist/expressions/jsonpath-helpers.d.ts +37 -0
- package/dist/expressions/logic.d.ts +25 -0
- package/dist/expressions/methods.d.ts +188 -0
- package/dist/expressions/op.d.ts +182 -0
- package/dist/expressions/query-expressions.d.ts +110 -0
- package/dist/expressions/row-expressions.d.ts +35 -0
- package/dist/expressions/search-helpers.d.ts +138 -0
- package/dist/expressions/trigram-helpers.d.ts +40 -0
- package/dist/hooks/hooks.d.ts +125 -0
- package/dist/index.cjs +21864 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +21688 -0
- package/dist/index.js.map +1 -0
- package/dist/model/attribute-columns.d.ts +85 -0
- package/dist/model/attribute-normalizer.d.ts +42 -0
- package/dist/model/attribute-options.d.ts +121 -0
- package/dist/model/bulk-insert-builder.d.ts +24 -0
- package/dist/model/bulk-options.d.ts +173 -0
- package/dist/model/copy-options.d.ts +51 -0
- package/dist/model/cte-builder.d.ts +134 -0
- package/dist/model/definition-normalizer.d.ts +26 -0
- package/dist/model/delete-query-builder.d.ts +42 -0
- package/dist/model/find-options.d.ts +144 -0
- package/dist/model/find-query-builder.d.ts +38 -0
- package/dist/model/inflector.d.ts +28 -0
- package/dist/model/insert-query-builder.d.ts +41 -0
- package/dist/model/instance-destroyer.d.ts +44 -0
- package/dist/model/instance-lifecycle-services.d.ts +61 -0
- package/dist/model/instance-lifecycle.d.ts +128 -0
- package/dist/model/instance-saver.d.ts +25 -0
- package/dist/model/instance-state.d.ts +48 -0
- package/dist/model/instance-validation.d.ts +35 -0
- package/dist/model/instance-values.d.ts +88 -0
- package/dist/model/instance-writer.d.ts +59 -0
- package/dist/model/join-source-builder.d.ts +25 -0
- package/dist/model/lateral-keys-builder.d.ts +25 -0
- package/dist/model/merge-action-builder.d.ts +31 -0
- package/dist/model/merge-options.d.ts +72 -0
- package/dist/model/merge-query-builder.d.ts +29 -0
- package/dist/model/merge-source-builder.d.ts +22 -0
- package/dist/model/mixins/model-aggregates.d.ts +69 -0
- package/dist/model/mixins/model-associations.d.ts +113 -0
- package/dist/model/mixins/model-constructor.d.ts +7 -0
- package/dist/model/mixins/model-finders.d.ts +140 -0
- package/dist/model/mixins/model-hooks.d.ts +142 -0
- package/dist/model/mixins/model-postgres.d.ts +198 -0
- package/dist/model/mixins/model-schema-binding.d.ts +36 -0
- package/dist/model/mixins/model-schema.d.ts +100 -0
- package/dist/model/mixins/model-scopes.d.ts +57 -0
- package/dist/model/mixins/model-writers.d.ts +175 -0
- package/dist/model/mixins/named-hooks.d.ts +28 -0
- package/dist/model/model-accessors.d.ts +22 -0
- package/dist/model/model-base.d.ts +83 -0
- package/dist/model/model-definition.d.ts +130 -0
- package/dist/model/model-instance-persistence.d.ts +132 -0
- package/dist/model/model-instance-values.d.ts +197 -0
- package/dist/model/model-operations.d.ts +22 -0
- package/dist/model/model-registry.d.ts +152 -0
- package/dist/model/model-services.d.ts +46 -0
- package/dist/model/model-state.d.ts +68 -0
- package/dist/model/model-values.d.ts +86 -0
- package/dist/model/model.d.ts +629 -0
- package/dist/model/on-conflict-builder.d.ts +111 -0
- package/dist/model/operations/bulk-create-operation.d.ts +40 -0
- package/dist/model/operations/copy-from-source.d.ts +51 -0
- package/dist/model/operations/copy-operation.d.ts +54 -0
- package/dist/model/operations/count-operation.d.ts +52 -0
- package/dist/model/operations/create-operation.d.ts +31 -0
- package/dist/model/operations/destroy-operation.d.ts +65 -0
- package/dist/model/operations/find-cursor.d.ts +23 -0
- package/dist/model/operations/find-lookups.d.ts +99 -0
- package/dist/model/operations/find-operation.d.ts +47 -0
- package/dist/model/operations/increment-operation.d.ts +40 -0
- package/dist/model/operations/insert-operation.d.ts +71 -0
- package/dist/model/operations/merge-operation.d.ts +56 -0
- package/dist/model/operations/model-operation.d.ts +33 -0
- package/dist/model/operations/update-operation.d.ts +45 -0
- package/dist/model/operations/upsert-operation.d.ts +39 -0
- package/dist/model/options.d.ts +103 -0
- package/dist/model/returning-old.d.ts +52 -0
- package/dist/model/row-mapper.d.ts +78 -0
- package/dist/model/row-plan.d.ts +31 -0
- package/dist/model/runtime.d.ts +56 -0
- package/dist/model/scope-registry.d.ts +81 -0
- package/dist/model/select-aggregates.d.ts +37 -0
- package/dist/model/select-lock.d.ts +13 -0
- package/dist/model/select-options.d.ts +92 -0
- package/dist/model/select-query-builder.d.ts +73 -0
- package/dist/model/select-targets.d.ts +77 -0
- package/dist/model/source-option-builder.d.ts +23 -0
- package/dist/model/source-options.d.ts +108 -0
- package/dist/model/statement-builder.d.ts +77 -0
- package/dist/model/statement-services.d.ts +2 -0
- package/dist/model/sync-hooks.d.ts +18 -0
- package/dist/model/table-sample-builder.d.ts +13 -0
- package/dist/model/types.d.ts +85 -0
- package/dist/model/unnest-insert-builder.d.ts +24 -0
- package/dist/model/update-query-builder.d.ts +39 -0
- package/dist/model/v6-conflict-builder.d.ts +37 -0
- package/dist/model/value-cloner.d.ts +17 -0
- package/dist/model/values-writer.d.ts +12 -0
- package/dist/model/where-options.d.ts +164 -0
- package/dist/model/write-options.d.ts +160 -0
- package/dist/model/written-rows.d.ts +56 -0
- package/dist/package-version.d.ts +8 -0
- package/dist/query/query-result-shaper.d.ts +93 -0
- package/dist/query/raw-query.d.ts +27 -0
- package/dist/query-interface/alter-planner.d.ts +40 -0
- package/dist/query-interface/bulk-data.d.ts +44 -0
- package/dist/query-interface/column-commands.d.ts +52 -0
- package/dist/query-interface/column-describer.d.ts +17 -0
- package/dist/query-interface/enum-commands.d.ts +36 -0
- package/dist/query-interface/index-commands.d.ts +57 -0
- package/dist/query-interface/model-tables.d.ts +58 -0
- package/dist/query-interface/object-commands.d.ts +71 -0
- package/dist/query-interface/partition-commands.d.ts +43 -0
- package/dist/query-interface/policy-commands.d.ts +54 -0
- package/dist/query-interface/query-interface-catalog.d.ts +180 -0
- package/dist/query-interface/query-interface-objects.d.ts +108 -0
- package/dist/query-interface/query-interface-partitions.d.ts +66 -0
- package/dist/query-interface/query-interface-policies.d.ts +86 -0
- package/dist/query-interface/query-interface-routines.d.ts +120 -0
- package/dist/query-interface/query-interface-tables.d.ts +179 -0
- package/dist/query-interface/query-interface-types.d.ts +62 -0
- package/dist/query-interface/query-interface-views.d.ts +84 -0
- package/dist/query-interface/query-interface.d.ts +256 -0
- package/dist/query-interface/routine-commands.d.ts +71 -0
- package/dist/query-interface/schema-synchronizer.d.ts +110 -0
- package/dist/query-interface/statement-runner.d.ts +40 -0
- package/dist/query-interface/sync-planner.d.ts +26 -0
- package/dist/query-interface/table-commands.d.ts +63 -0
- package/dist/query-interface/table-introspector.d.ts +58 -0
- package/dist/query-interface/view-commands.d.ts +47 -0
- package/dist/sequelize/instance-hook-adders.d.ts +72 -0
- package/dist/sequelize/sequelize-base.d.ts +196 -0
- package/dist/sequelize/sequelize-constructor.d.ts +7 -0
- package/dist/sequelize/sequelize-helpers.d.ts +133 -0
- package/dist/sequelize/sequelize-hooks.d.ts +99 -0
- package/dist/sequelize/sequelize-internals.d.ts +87 -0
- package/dist/sequelize/sequelize-notifications.d.ts +66 -0
- package/dist/sequelize/sequelize-schema.d.ts +125 -0
- package/dist/sequelize/sequelize-transactions.d.ts +96 -0
- package/dist/sequelize.d.ts +1175 -0
- package/dist/sql/ast-clauses.d.ts +137 -0
- package/dist/sql/ast-expressions.d.ts +88 -0
- package/dist/sql/ast-statements.d.ts +80 -0
- package/dist/sql/ast.d.ts +21 -0
- package/dist/sql/compile.d.ts +21 -0
- package/dist/sql/dml-compiler.d.ts +54 -0
- package/dist/sql/expression-compiler.d.ts +56 -0
- package/dist/sql/from-compiler.d.ts +39 -0
- package/dist/sql/identifier-quoter.d.ts +15 -0
- package/dist/sql/merge-compiler.d.ts +20 -0
- package/dist/sql/order.d.ts +15 -0
- package/dist/sql/query-compiler.d.ts +45 -0
- package/dist/sql/sql-chunks.d.ts +50 -0
- package/dist/sql/with-compiler.d.ts +19 -0
- package/dist/transaction/advisory-locks.d.ts +28 -0
- package/dist/transaction/cls-namespace.d.ts +44 -0
- package/dist/transaction/transaction-constants.d.ts +35 -0
- package/dist/transaction/transaction-runner.d.ts +78 -0
- package/dist/transaction/transaction-starter.d.ts +25 -0
- package/dist/transaction/transaction-statements.d.ts +29 -0
- package/dist/transaction/transaction.d.ts +201 -0
- package/dist/validation/attribute-validators.d.ts +23 -0
- package/dist/validation/builtin-attribute-validator.d.ts +19 -0
- package/dist/validation/builtin-validators.d.ts +16 -0
- package/dist/validation/core-validator.d.ts +16 -0
- package/dist/validation/custom-validators.d.ts +13 -0
- package/dist/validation/instance-validator.d.ts +25 -0
- package/dist/validation/schema-items.d.ts +22 -0
- package/dist/validation/standard-schema-validator.d.ts +30 -0
- package/dist/validation/standard-schema.d.ts +39 -0
- package/dist/validation/types.d.ts +36 -0
- package/dist/validation/validation-target.d.ts +13 -0
- package/dist/where/comparison-compiler.d.ts +27 -0
- package/dist/where/condition-compiler.d.ts +18 -0
- package/dist/where/operand-resolver.d.ts +10 -0
- package/dist/where/operator-allowlist.d.ts +14 -0
- package/dist/where/where-support.d.ts +52 -0
- package/dist/where/where.d.ts +46 -0
- package/dist/where/window-compiler.d.ts +14 -0
- package/llm/USAGE.md +423 -0
- package/package.json +103 -4
- package/skills/postgrelize/SKILL.md +57 -0
- package/skills/postgrelize/reference/associations.md +108 -0
- package/skills/postgrelize/reference/errors.md +37 -0
- package/skills/postgrelize/reference/migrating-from-sequelize.md +15 -0
- package/skills/postgrelize/reference/postgresql-features.md +157 -0
- package/skills/postgrelize/reference/querying.md +111 -0
- package/skills/postgrelize/reference/schema.md +135 -0
- package/skills/postgrelize/reference/security.md +34 -0
- package/skills/postgrelize/reference/transactions-and-sessions.md +122 -0
- package/skills/postgrelize/reference/writing.md +198 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { DeleteNode, FromItem, TruncateNode } from '../sql/ast.ts';
|
|
2
|
+
import { AttributeColumns } from './attribute-columns.ts';
|
|
3
|
+
import { ModelDefinition } from './model-definition.ts';
|
|
4
|
+
/** Options of {@link DeleteQueryBuilder.build}. */
|
|
5
|
+
export type DeleteBuildOptions = Readonly<{
|
|
6
|
+
/** Return all stored columns. */
|
|
7
|
+
returning?: boolean;
|
|
8
|
+
/** Lists at least this long compile to `= ANY($n)`. */
|
|
9
|
+
inAsAnyThreshold?: number;
|
|
10
|
+
/** Delete at most this many rows (`ctid IN (SELECT ... LIMIT n)`). */
|
|
11
|
+
limit?: number;
|
|
12
|
+
/** Extra relations (`DELETE ... USING`); the table is then aliased with the model name. */
|
|
13
|
+
using?: readonly FromItem[];
|
|
14
|
+
}>;
|
|
15
|
+
/** Builds DELETE and TRUNCATE statements of a model table. */
|
|
16
|
+
export declare class DeleteQueryBuilder {
|
|
17
|
+
#private;
|
|
18
|
+
/**
|
|
19
|
+
* @param columns - Attribute to column mapping.
|
|
20
|
+
*/
|
|
21
|
+
constructor(columns: AttributeColumns);
|
|
22
|
+
/**
|
|
23
|
+
* Builds a DELETE.
|
|
24
|
+
*
|
|
25
|
+
* @param definition - Model definition.
|
|
26
|
+
* @param where - Condition.
|
|
27
|
+
* @param options - RETURNING, `= ANY` threshold, LIMIT, USING relations.
|
|
28
|
+
* @returns DELETE node.
|
|
29
|
+
*/
|
|
30
|
+
build(definition: ModelDefinition, where: unknown, options?: DeleteBuildOptions): DeleteNode;
|
|
31
|
+
/**
|
|
32
|
+
* Builds `TRUNCATE`.
|
|
33
|
+
*
|
|
34
|
+
* @param definition - Model definition.
|
|
35
|
+
* @param options - `cascade`, `restartIdentity`.
|
|
36
|
+
* @returns TRUNCATE node.
|
|
37
|
+
*/
|
|
38
|
+
truncate(definition: ModelDefinition, options: Readonly<{
|
|
39
|
+
cascade?: boolean;
|
|
40
|
+
restartIdentity?: boolean;
|
|
41
|
+
}>): TruncateNode;
|
|
42
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { IncludeOption } from '../associations/include.ts';
|
|
2
|
+
import { DataTypeInput } from '../data-types/data-types.ts';
|
|
3
|
+
import { CteFindOptions } from './cte-builder.ts';
|
|
4
|
+
import { Values } from './options.ts';
|
|
5
|
+
import { QueryOptionsBase } from './runtime.ts';
|
|
6
|
+
import { TableSampleOptions } from './source-options.ts';
|
|
7
|
+
import { SelectOptions } from './select-query-builder.ts';
|
|
8
|
+
import { WhereOptions } from './types.ts';
|
|
9
|
+
/** Result of `findAndCountAll`: one page of rows plus the total count. */
|
|
10
|
+
export type FindAndCountResult<M> = {
|
|
11
|
+
/** Total number of matching rows, ignoring `limit` / `offset`. */
|
|
12
|
+
count: number;
|
|
13
|
+
/** Rows of the current page. */
|
|
14
|
+
rows: M[];
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Options of the finders (`findAll`, `findOne`, `findByPk`, ...), without association loading details.
|
|
18
|
+
*
|
|
19
|
+
* Extends the query options (`transaction`, `logging`, ...) and the SELECT options with finder flags and the PostgreSQL CTE extension. `include` is typed through {@link IncludeOption}.
|
|
20
|
+
* A `beforeFind` hook may change these options before the query runs.
|
|
21
|
+
*
|
|
22
|
+
* @see {@link Model.findAll}
|
|
23
|
+
* @see {@link WhereOptions}
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* const opts: FindOptions<User> = {
|
|
28
|
+
* where: { active: true, age: { [Op.gte]: 18 } },
|
|
29
|
+
* attributes: ['id', 'name'],
|
|
30
|
+
* order: [['name', 'ASC']],
|
|
31
|
+
* limit: 10,
|
|
32
|
+
* include: [Post],
|
|
33
|
+
* };
|
|
34
|
+
* const rows = await User.findAll(opts);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export type FindOptions<T = Values> = QueryOptionsBase & Omit<SelectOptions, 'where'> & Readonly<{
|
|
38
|
+
/** Filter rows; see {@link WhereOptions}. All rows when omitted. */
|
|
39
|
+
where?: WhereOptions<T>;
|
|
40
|
+
/** Return plain row objects instead of model instances.
|
|
41
|
+
* @defaultValue false */
|
|
42
|
+
raw?: boolean;
|
|
43
|
+
/** With `raw`, build nested objects from dotted keys such as `'posts.title'`.
|
|
44
|
+
* @defaultValue false */
|
|
45
|
+
nest?: boolean;
|
|
46
|
+
/** Return a single row instead of an array (set internally by `findOne`).
|
|
47
|
+
* @defaultValue false */
|
|
48
|
+
plain?: boolean;
|
|
49
|
+
/** Throw `EmptyResultError` (or the given `Error`) when no row is found.
|
|
50
|
+
* @defaultValue false */
|
|
51
|
+
rejectOnEmpty?: boolean | Error;
|
|
52
|
+
/** For paranoid models: `false` includes soft-deleted rows.
|
|
53
|
+
* @defaultValue true */
|
|
54
|
+
paranoid?: boolean;
|
|
55
|
+
/** Run `beforeFind` / `afterFind` hooks.
|
|
56
|
+
* @defaultValue true */
|
|
57
|
+
hooks?: boolean;
|
|
58
|
+
/** Eager-load associations through JOINs (or separate queries for `separate` includes). */
|
|
59
|
+
include?: IncludeOption;
|
|
60
|
+
/** `false` disables the sub-query used for `limit` combined with multi-row includes. */
|
|
61
|
+
subQuery?: boolean;
|
|
62
|
+
}> & CteFindOptions;
|
|
63
|
+
/**
|
|
64
|
+
* Options of `count`.
|
|
65
|
+
*
|
|
66
|
+
* Includes joins and CTE options; `count` fires `beforeCount` hooks.
|
|
67
|
+
*
|
|
68
|
+
* @see {@link Model.count}
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* ```ts
|
|
72
|
+
* const total = await Post.count({ where: { published: true }, distinct: true, col: 'authorId' });
|
|
73
|
+
* const opts: CountOptions<Post> = { group: 'authorId' };
|
|
74
|
+
* const perAuthor = await Post.count(opts); // [{ authorId, count }, ...]
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export type CountOptions<T = Values> = QueryOptionsBase & Readonly<{
|
|
78
|
+
/** Filter rows; see {@link WhereOptions}. */
|
|
79
|
+
where?: WhereOptions<T>;
|
|
80
|
+
/** Column to count. Default: `*` (primary key with `distinct`). */
|
|
81
|
+
col?: string;
|
|
82
|
+
/** Use `count(DISTINCT ...)`.
|
|
83
|
+
* @defaultValue false */
|
|
84
|
+
distinct?: boolean;
|
|
85
|
+
/** Group by attributes; the result becomes an array of `{ count, ...groups }`. */
|
|
86
|
+
group?: string | readonly string[];
|
|
87
|
+
/** For paranoid models: `false` includes soft-deleted rows.
|
|
88
|
+
* @defaultValue true */
|
|
89
|
+
paranoid?: boolean;
|
|
90
|
+
/** Run `beforeCount` hooks.
|
|
91
|
+
* @defaultValue true */
|
|
92
|
+
hooks?: boolean;
|
|
93
|
+
/** Join associations (rows are counted per joined row unless `distinct`). */
|
|
94
|
+
include?: IncludeOption;
|
|
95
|
+
/** Count only a random sample of the table (`TABLESAMPLE`). PostgreSQL extension. */
|
|
96
|
+
sample?: TableSampleOptions;
|
|
97
|
+
}> & CteFindOptions;
|
|
98
|
+
/**
|
|
99
|
+
* Options of `aggregate`, `max`, `min` and `sum`.
|
|
100
|
+
*
|
|
101
|
+
* Same as {@link CountOptions} plus `plain`.
|
|
102
|
+
*
|
|
103
|
+
* @see {@link Model.aggregate}
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* const opts: AggregateOptions<Product> = { where: { active: true }, paranoid: false };
|
|
108
|
+
* const highest = await Product.max('price', opts);
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export type AggregateOptions<T = Values> = CountOptions<T> & Readonly<{
|
|
112
|
+
/**
|
|
113
|
+
* Return all rows exactly as the driver delivers them (numeric results such as `sum` are
|
|
114
|
+
* strings, like in Sequelize v6) instead of one converted value.
|
|
115
|
+
*
|
|
116
|
+
* @defaultValue `true`
|
|
117
|
+
*/
|
|
118
|
+
plain?: boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Data type that the single result is converted to: `INTEGER` / `BIGINT` give a number,
|
|
121
|
+
* `FLOAT` / `DECIMAL` a float, `DATE` a `Date`. Defaults to the type of the aggregated
|
|
122
|
+
* attribute, or `FLOAT` for `'*'`. Ignored with `plain: false`.
|
|
123
|
+
*/
|
|
124
|
+
dataType?: DataTypeInput;
|
|
125
|
+
}>;
|
|
126
|
+
/**
|
|
127
|
+
* Options of `findOrCreate` and `findOrBuild`.
|
|
128
|
+
*
|
|
129
|
+
* Extends {@link FindOptions}; the `where` values are also used when creating the row.
|
|
130
|
+
*
|
|
131
|
+
* @see {@link Model.findOrCreate}
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* const [user, created] = await User.findOrCreate({
|
|
136
|
+
* where: { email: 'a@example.com' },
|
|
137
|
+
* defaults: { name: 'Ann' },
|
|
138
|
+
* });
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export type FindOrCreateOptions<T = Values> = FindOptions<T> & Readonly<{
|
|
142
|
+
/** Values used in addition to the plain equality values of `where` when the row is created. */
|
|
143
|
+
defaults?: Partial<T>;
|
|
144
|
+
}>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { IncludeNode } from '../associations/include.ts';
|
|
2
|
+
import { IncludeQueryBuilder } from '../associations/include-query-builder.ts';
|
|
3
|
+
import { SelectNode } from '../sql/ast.ts';
|
|
4
|
+
import { CteBuilder } from './cte-builder.ts';
|
|
5
|
+
import { ModelDefinition } from './model-definition.ts';
|
|
6
|
+
import { FindOptions } from './options.ts';
|
|
7
|
+
import { SelectQueryBuilder } from './select-query-builder.ts';
|
|
8
|
+
/**
|
|
9
|
+
* Builds the SELECT of a finder from resolved find options: the `source` / `with` / `from` CTE
|
|
10
|
+
* options and the joins of includes (whose ordering is applied by the include builder).
|
|
11
|
+
*/
|
|
12
|
+
export declare class FindQueryBuilder {
|
|
13
|
+
#private;
|
|
14
|
+
/**
|
|
15
|
+
* @param selects - SELECT builder.
|
|
16
|
+
* @param cte - CTE options.
|
|
17
|
+
* @param includes - Include joins.
|
|
18
|
+
*/
|
|
19
|
+
constructor(selects: SelectQueryBuilder, cte: CteBuilder, includes: IncludeQueryBuilder);
|
|
20
|
+
/**
|
|
21
|
+
* Builds the SELECT of resolved find options (scope and paranoid filter already applied).
|
|
22
|
+
*
|
|
23
|
+
* @param definition - Model definition.
|
|
24
|
+
* @param query - Find options.
|
|
25
|
+
* @param includes - Normalized include tree.
|
|
26
|
+
* @param inAsAnyThreshold - Lists at least this long compile to `= ANY($n)`.
|
|
27
|
+
* @param alias - Table alias instead of the model name.
|
|
28
|
+
* @returns SELECT node.
|
|
29
|
+
*/
|
|
30
|
+
build(definition: ModelDefinition, query: FindOptions, includes: readonly IncludeNode[], inAsAnyThreshold: number | undefined, alias?: string): SelectNode;
|
|
31
|
+
/**
|
|
32
|
+
* Route of the built SELECT: the writer when a CTE modifies data.
|
|
33
|
+
*
|
|
34
|
+
* @param select - Built SELECT.
|
|
35
|
+
* @returns `'read'` or `'write'`.
|
|
36
|
+
*/
|
|
37
|
+
route(select: SelectNode): 'read' | 'write';
|
|
38
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** The inflection functions postgrelize uses; a library given to `useInflection` must provide them. */
|
|
2
|
+
export interface Inflection {
|
|
3
|
+
/** Singular form of a word. */
|
|
4
|
+
singularize: (word: string) => string;
|
|
5
|
+
/** Plural form of a word. */
|
|
6
|
+
pluralize: (word: string) => string;
|
|
7
|
+
/** `snake_case` form of a name. */
|
|
8
|
+
underscore: (word: string) => string;
|
|
9
|
+
}
|
|
10
|
+
/** Inflector used for association names and `underscored` columns. */
|
|
11
|
+
export declare const inflector: Inflection;
|
|
12
|
+
/**
|
|
13
|
+
* Replaces the inflection library used to singularize and pluralize association names and to
|
|
14
|
+
* underscore column names (Sequelize v6 `useInflection`). Call it before defining models.
|
|
15
|
+
*
|
|
16
|
+
* @param custom - Library with `singularize`, `pluralize` and `underscore`.
|
|
17
|
+
* @see {@link Sequelize.useInflection}
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* useInflection({
|
|
22
|
+
* singularize: (word) => word.replace(/s$/, ''),
|
|
23
|
+
* pluralize: (word) => `${word}s`,
|
|
24
|
+
* underscore: (word) => word.replace(/([A-Z])/g, '_$1').toLowerCase(),
|
|
25
|
+
* });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare const useInflection: (custom: Inflection) => void;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ExprNode, InsertNode, OnConflictNode, QueryNode } from '../sql/ast.ts';
|
|
2
|
+
import { AttributeColumns } from './attribute-columns.ts';
|
|
3
|
+
import { ModelDefinition } from './model-definition.ts';
|
|
4
|
+
/** Options of {@link InsertQueryBuilder.build}. */
|
|
5
|
+
export type InsertBuildOptions = Readonly<{
|
|
6
|
+
/** `true`: all stored columns; a list: these expressions. */
|
|
7
|
+
returning?: boolean | readonly ExprNode[];
|
|
8
|
+
/** `ON CONFLICT` clause. */
|
|
9
|
+
onConflict?: OnConflictNode;
|
|
10
|
+
/** `OVERRIDING { SYSTEM | USER } VALUE`. */
|
|
11
|
+
overriding?: 'system' | 'user';
|
|
12
|
+
}>;
|
|
13
|
+
/** Builds INSERT statements: multi-row `VALUES` from attribute-keyed rows, or `INSERT ... SELECT`. */
|
|
14
|
+
export declare class InsertQueryBuilder {
|
|
15
|
+
#private;
|
|
16
|
+
/**
|
|
17
|
+
* @param columns - Attribute to column mapping.
|
|
18
|
+
*/
|
|
19
|
+
constructor(columns: AttributeColumns);
|
|
20
|
+
/**
|
|
21
|
+
* Builds a multi-row INSERT from attribute-keyed rows. Columns no row provides are left out;
|
|
22
|
+
* missing values and empty auto-increment keys are written as `DEFAULT`.
|
|
23
|
+
*
|
|
24
|
+
* @param definition - Model definition.
|
|
25
|
+
* @param rows - Values by attribute name.
|
|
26
|
+
* @param options - RETURNING, ON CONFLICT, OVERRIDING.
|
|
27
|
+
* @returns INSERT node.
|
|
28
|
+
*/
|
|
29
|
+
build(definition: ModelDefinition, rows: readonly Readonly<Record<string, unknown>>[], options?: InsertBuildOptions): InsertNode;
|
|
30
|
+
/**
|
|
31
|
+
* Builds `INSERT INTO ... (columns) SELECT ...`.
|
|
32
|
+
*
|
|
33
|
+
* @param definition - Model definition.
|
|
34
|
+
* @param columns - Target attribute names.
|
|
35
|
+
* @param select - Source query.
|
|
36
|
+
* @param onConflict - `ON CONFLICT` clause.
|
|
37
|
+
* @param returning - RETURNING list.
|
|
38
|
+
* @returns INSERT node.
|
|
39
|
+
*/
|
|
40
|
+
fromSelect(definition: ModelDefinition, columns: readonly string[], select: QueryNode, onConflict: OnConflictNode | undefined, returning: readonly ExprNode[]): InsertNode;
|
|
41
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { InstanceLifecycleServices } from './instance-lifecycle-services.ts';
|
|
2
|
+
import { InstanceLifecycle } from './instance-lifecycle.ts';
|
|
3
|
+
import { AnyModel, InstanceDestroyOptions } from './options.ts';
|
|
4
|
+
import { QueryOptionsBase } from './runtime.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Instance `destroy` (DELETE, or soft delete of paranoid models), `restore` and `isSoftDeleted`.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare class InstanceDestroyer {
|
|
11
|
+
#private;
|
|
12
|
+
/**
|
|
13
|
+
* @param services - Collaborators.
|
|
14
|
+
* @param lifecycle - Hook runner.
|
|
15
|
+
*/
|
|
16
|
+
constructor(services: InstanceLifecycleServices, lifecycle: InstanceLifecycle);
|
|
17
|
+
/**
|
|
18
|
+
* Deletes the row of an instance (sets `deletedAt` for paranoid models unless `force`).
|
|
19
|
+
*
|
|
20
|
+
* @param instance - Instance.
|
|
21
|
+
* @param options - Destroy options.
|
|
22
|
+
* @returns Resolves when deleted.
|
|
23
|
+
*/
|
|
24
|
+
destroy(instance: AnyModel, options: InstanceDestroyOptions): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Restores a soft-deleted instance of a paranoid model.
|
|
27
|
+
*
|
|
28
|
+
* @param instance - Instance.
|
|
29
|
+
* @param options - Query options; `hooks: false` skips restore hooks.
|
|
30
|
+
* @returns Resolves when restored.
|
|
31
|
+
* @throws Error when the model is not paranoid.
|
|
32
|
+
*/
|
|
33
|
+
restore(instance: AnyModel, options: QueryOptionsBase & Readonly<{
|
|
34
|
+
hooks?: boolean;
|
|
35
|
+
}>): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Whether an instance of a paranoid model is soft-deleted.
|
|
38
|
+
*
|
|
39
|
+
* @param instance - Instance.
|
|
40
|
+
* @returns True when `deletedAt` is set.
|
|
41
|
+
* @throws Error when the model is not paranoid.
|
|
42
|
+
*/
|
|
43
|
+
isSoftDeleted(instance: AnyModel): boolean;
|
|
44
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { IncludeResolver } from '../associations/include-resolver.ts';
|
|
2
|
+
import { NestedCreator } from '../associations/nested-creator.ts';
|
|
3
|
+
import { SqlCompiler } from '../sql/compile.ts';
|
|
4
|
+
import { InstanceValidator } from '../validation/instance-validator.ts';
|
|
5
|
+
import { AttributeColumns } from './attribute-columns.ts';
|
|
6
|
+
import { DeleteQueryBuilder } from './delete-query-builder.ts';
|
|
7
|
+
import { InsertQueryBuilder } from './insert-query-builder.ts';
|
|
8
|
+
import { ModelRegistry } from './model-registry.ts';
|
|
9
|
+
import { ModelValues } from './model-values.ts';
|
|
10
|
+
import { RowMapper } from './row-mapper.ts';
|
|
11
|
+
import { SelectQueryBuilder } from './select-query-builder.ts';
|
|
12
|
+
import { UpdateQueryBuilder } from './update-query-builder.ts';
|
|
13
|
+
/**
|
|
14
|
+
* Options of `instance.validate()`.
|
|
15
|
+
*
|
|
16
|
+
* Validation throws `ValidationError` when an attribute is invalid.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link Model.validate}
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* const opts: ValidateOptions = { skip: ['password'], hooks: false };
|
|
23
|
+
* await user.validate(opts);
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export type ValidateOptions = Readonly<{
|
|
27
|
+
/** Run `beforeValidate` / `afterValidate` / `validationFailed` hooks.
|
|
28
|
+
* @defaultValue true */
|
|
29
|
+
hooks?: boolean;
|
|
30
|
+
/** Attributes to skip. */
|
|
31
|
+
skip?: readonly string[];
|
|
32
|
+
/** Only validate these attributes. */
|
|
33
|
+
fields?: readonly string[];
|
|
34
|
+
}>;
|
|
35
|
+
/** Collaborators of {@link InstanceLifecycle}. */
|
|
36
|
+
export type InstanceLifecycleServices = Readonly<{
|
|
37
|
+
/** Model state. */
|
|
38
|
+
registry: ModelRegistry;
|
|
39
|
+
/** Value rules. */
|
|
40
|
+
values: ModelValues;
|
|
41
|
+
/** Row parsing. */
|
|
42
|
+
rows: RowMapper;
|
|
43
|
+
/** Shared column helpers. */
|
|
44
|
+
columns: AttributeColumns;
|
|
45
|
+
/** SELECT builder (`reload`). */
|
|
46
|
+
selects: SelectQueryBuilder;
|
|
47
|
+
/** INSERT builder (`save` of new instances). */
|
|
48
|
+
inserts: InsertQueryBuilder;
|
|
49
|
+
/** UPDATE builder (`save` of loaded instances). */
|
|
50
|
+
updates: UpdateQueryBuilder;
|
|
51
|
+
/** DELETE builder (`destroy`). */
|
|
52
|
+
deletes: DeleteQueryBuilder;
|
|
53
|
+
/** Include tree of nested builds. */
|
|
54
|
+
includes: IncludeResolver;
|
|
55
|
+
/** Nested creation. */
|
|
56
|
+
nested: NestedCreator;
|
|
57
|
+
/** Attribute / model validators. */
|
|
58
|
+
validator: InstanceValidator;
|
|
59
|
+
/** SQL compiler. */
|
|
60
|
+
compiler: SqlCompiler;
|
|
61
|
+
}>;
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { HookType } from '../hooks/hooks.ts';
|
|
2
|
+
import { InstanceLifecycleServices, ValidateOptions } from './instance-lifecycle-services.ts';
|
|
3
|
+
import { ModelDefinition } from './model-definition.ts';
|
|
4
|
+
import { AnyModel, AttributeAmounts, BuildOptions, IncrementOptions, InstanceDestroyOptions, SaveOptions, Values } from './options.ts';
|
|
5
|
+
import { QueryOptionsBase } from './runtime.ts';
|
|
6
|
+
export type { InstanceLifecycleServices, ValidateOptions } from './instance-lifecycle-services.ts';
|
|
7
|
+
/**
|
|
8
|
+
* Lifecycle of model instances: construction from values (with nested includes), validation with
|
|
9
|
+
* hooks, `save` (INSERT or UPDATE of changed attributes with optimistic locking), `reload`,
|
|
10
|
+
* `destroy` / `restore` (soft delete for paranoid models), instance `increment` / `decrement`,
|
|
11
|
+
* and the model hook runner used by every operation.
|
|
12
|
+
*/
|
|
13
|
+
export declare class InstanceLifecycle {
|
|
14
|
+
#private;
|
|
15
|
+
/**
|
|
16
|
+
* @param services - Collaborators.
|
|
17
|
+
*/
|
|
18
|
+
constructor(services: InstanceLifecycleServices);
|
|
19
|
+
/**
|
|
20
|
+
* Runs model hooks, then Sequelize-level hooks, with the model class as `this`.
|
|
21
|
+
*
|
|
22
|
+
* @param model - Model class.
|
|
23
|
+
* @param type - Hook type.
|
|
24
|
+
* @param args - Hook arguments.
|
|
25
|
+
* @returns Resolves when all hooks finished.
|
|
26
|
+
*/
|
|
27
|
+
runHooks(model: object, type: HookType, ...args: readonly unknown[]): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Runs a hook for each instance, one after another (individual hooks of bulk operations).
|
|
30
|
+
*
|
|
31
|
+
* @param model - Model class.
|
|
32
|
+
* @param type - Hook type.
|
|
33
|
+
* @param instances - Instances, passed as the first hook argument.
|
|
34
|
+
* @param args - Further hook arguments.
|
|
35
|
+
* @returns Resolves when all hooks finished.
|
|
36
|
+
*/
|
|
37
|
+
runEach(model: object, type: HookType, instances: readonly object[], ...args: readonly unknown[]): Promise<void>;
|
|
38
|
+
/**
|
|
39
|
+
* Initializes a new instance (`new Model(values, options)`): defaults for new records, values
|
|
40
|
+
* (raw or through setters), nested include instances and change tracking.
|
|
41
|
+
*
|
|
42
|
+
* @param instance - Instance being constructed.
|
|
43
|
+
* @param values - Attribute values (and nested include values).
|
|
44
|
+
* @param options - Build options.
|
|
45
|
+
*/
|
|
46
|
+
initialize(instance: AnyModel, values: Values, options: BuildOptions): void;
|
|
47
|
+
/**
|
|
48
|
+
* Validates an instance like Sequelize v6 `instance.validate()`.
|
|
49
|
+
*
|
|
50
|
+
* @param instance - Instance.
|
|
51
|
+
* @param options - `hooks`, `skip`, `fields`.
|
|
52
|
+
* @returns Resolves when valid.
|
|
53
|
+
* @throws ValidationError with all failures.
|
|
54
|
+
*/
|
|
55
|
+
validate(instance: AnyModel, options?: ValidateOptions): Promise<void>;
|
|
56
|
+
/**
|
|
57
|
+
* Validates instances of a bulk write; failures are collected into one error.
|
|
58
|
+
*
|
|
59
|
+
* @param instances - Instances.
|
|
60
|
+
* @param hooks - Run validation hooks.
|
|
61
|
+
* @returns Resolves when all are valid.
|
|
62
|
+
* @throws AggregateError of BulkRecordError.
|
|
63
|
+
*/
|
|
64
|
+
validateAll(instances: readonly AnyModel[], hooks: boolean): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Inserts the instance if new, otherwise updates its changed attributes.
|
|
67
|
+
*
|
|
68
|
+
* @param instance - Instance.
|
|
69
|
+
* @param options - Save options.
|
|
70
|
+
* @returns The instance with values returned by the database.
|
|
71
|
+
*/
|
|
72
|
+
save<I extends AnyModel>(instance: I, options: SaveOptions): Promise<I>;
|
|
73
|
+
/**
|
|
74
|
+
* Reloads values from the database.
|
|
75
|
+
*
|
|
76
|
+
* @param instance - Instance.
|
|
77
|
+
* @param options - Query options.
|
|
78
|
+
* @returns The instance.
|
|
79
|
+
* @throws InstanceError when the row no longer exists.
|
|
80
|
+
*/
|
|
81
|
+
reload<I extends AnyModel>(instance: I, options: QueryOptionsBase): Promise<I>;
|
|
82
|
+
/**
|
|
83
|
+
* Deletes the row of an instance (sets `deletedAt` for paranoid models unless `force`).
|
|
84
|
+
*
|
|
85
|
+
* @param instance - Instance.
|
|
86
|
+
* @param options - Destroy options.
|
|
87
|
+
* @returns Resolves when deleted.
|
|
88
|
+
*/
|
|
89
|
+
destroy(instance: AnyModel, options: InstanceDestroyOptions): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Restores a soft-deleted instance of a paranoid model.
|
|
92
|
+
*
|
|
93
|
+
* @param instance - Instance.
|
|
94
|
+
* @param options - Query options; `hooks: false` skips restore hooks.
|
|
95
|
+
* @returns Resolves when restored.
|
|
96
|
+
* @throws Error when the model is not paranoid.
|
|
97
|
+
*/
|
|
98
|
+
restore(instance: AnyModel, options: QueryOptionsBase & Readonly<{
|
|
99
|
+
hooks?: boolean;
|
|
100
|
+
}>): Promise<void>;
|
|
101
|
+
/**
|
|
102
|
+
* Whether an instance of a paranoid model is soft-deleted.
|
|
103
|
+
*
|
|
104
|
+
* @param instance - Instance.
|
|
105
|
+
* @returns True when `deletedAt` is set.
|
|
106
|
+
* @throws Error when the model is not paranoid.
|
|
107
|
+
*/
|
|
108
|
+
isSoftDeleted(instance: AnyModel): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Increments / decrements attributes of the instance's row through the model static.
|
|
111
|
+
*
|
|
112
|
+
* @param instance - Instance.
|
|
113
|
+
* @param method - `'increment'` or `'decrement'`.
|
|
114
|
+
* @param fields - Attribute name(s) or map of amounts.
|
|
115
|
+
* @param options - Options.
|
|
116
|
+
* @returns Result of the model static.
|
|
117
|
+
*/
|
|
118
|
+
changeBy(instance: AnyModel, method: 'increment' | 'decrement', fields: AttributeAmounts<Values>, options: Omit<IncrementOptions, 'where'>): Promise<unknown>;
|
|
119
|
+
/**
|
|
120
|
+
* Applies a returned row to an instance and marks it as persisted and unchanged.
|
|
121
|
+
*
|
|
122
|
+
* @param instance - Instance.
|
|
123
|
+
* @param definition - Model definition.
|
|
124
|
+
* @param row - Returned row (`undefined` keeps the values).
|
|
125
|
+
* @returns The instance.
|
|
126
|
+
*/
|
|
127
|
+
refresh<I extends AnyModel>(instance: I, definition: ModelDefinition, row: Readonly<Record<string, unknown>> | undefined): I;
|
|
128
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { InstanceLifecycleServices } from './instance-lifecycle-services.ts';
|
|
2
|
+
import { InstanceLifecycle } from './instance-lifecycle.ts';
|
|
3
|
+
import { AnyModel, SaveOptions } from './options.ts';
|
|
4
|
+
/**
|
|
5
|
+
* `instance.save()`: validation, `beforeCreate` / `beforeUpdate` hooks (with re-validation of
|
|
6
|
+
* attributes the hooks changed), the INSERT or UPDATE, then the `after` hooks.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare class InstanceSaver {
|
|
11
|
+
#private;
|
|
12
|
+
/**
|
|
13
|
+
* @param services - Collaborators.
|
|
14
|
+
* @param lifecycle - Hook runner and row refresh.
|
|
15
|
+
*/
|
|
16
|
+
constructor(services: InstanceLifecycleServices, lifecycle: InstanceLifecycle);
|
|
17
|
+
/**
|
|
18
|
+
* Inserts the instance if new, otherwise updates its changed attributes.
|
|
19
|
+
*
|
|
20
|
+
* @param instance - Instance.
|
|
21
|
+
* @param options - Save options.
|
|
22
|
+
* @returns The instance with values returned by the database.
|
|
23
|
+
*/
|
|
24
|
+
save<I extends AnyModel>(instance: I, options: SaveOptions): Promise<I>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { IncludeNode } from '../associations/include.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Internal state of one model instance: changed attribute names, the include tree it was built
|
|
4
|
+
* with, and the write metadata exposed as `wasInserted` / `oldValues`. Stored under a private symbol
|
|
5
|
+
* (no attribute-like member, no private field that would break mixin declaration types).
|
|
6
|
+
*/
|
|
7
|
+
export declare class InstanceState {
|
|
8
|
+
/**
|
|
9
|
+
* Creates the state of a new instance.
|
|
10
|
+
*
|
|
11
|
+
* @param instance - Model instance.
|
|
12
|
+
*/
|
|
13
|
+
static attach(instance: object): void;
|
|
14
|
+
/**
|
|
15
|
+
* State of an instance.
|
|
16
|
+
*
|
|
17
|
+
* @param instance - Model instance.
|
|
18
|
+
* @returns Its state.
|
|
19
|
+
*/
|
|
20
|
+
static of(instance: object): InstanceState;
|
|
21
|
+
/**
|
|
22
|
+
* Whether a value is a model instance (has a state).
|
|
23
|
+
*
|
|
24
|
+
* @param value - Any value.
|
|
25
|
+
* @returns True for model instances.
|
|
26
|
+
*/
|
|
27
|
+
static has(value: unknown): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Changed attribute names. The set belongs to this instance and grows in place; readers copy it
|
|
30
|
+
* (`[...changed]`) when they need a snapshot.
|
|
31
|
+
*/
|
|
32
|
+
changed: ReadonlySet<string>;
|
|
33
|
+
/** Include tree of a nested build. */
|
|
34
|
+
includes: readonly IncludeNode[];
|
|
35
|
+
/** Whether the row was inserted (`true`) or updated (`false`) by an `ON CONFLICT` write. */
|
|
36
|
+
wasInserted: boolean | undefined;
|
|
37
|
+
/** Pre-statement values from `returningOld`, `null` for inserted rows. */
|
|
38
|
+
oldValues: Readonly<Record<string, unknown>> | null | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* Marks an attribute as changed or unchanged.
|
|
41
|
+
*
|
|
42
|
+
* @param name - Attribute name.
|
|
43
|
+
* @param changed - Changed flag.
|
|
44
|
+
*/
|
|
45
|
+
mark(name: string, changed: boolean): void;
|
|
46
|
+
/** Forgets all changes (after a load or save). */
|
|
47
|
+
reset(): void;
|
|
48
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { InstanceLifecycleServices, ValidateOptions } from './instance-lifecycle-services.ts';
|
|
2
|
+
import { InstanceLifecycle } from './instance-lifecycle.ts';
|
|
3
|
+
import { AnyModel } from './options.ts';
|
|
4
|
+
/**
|
|
5
|
+
* Instance validation with `beforeValidate` / `afterValidate` / `validationFailed` hooks, and the
|
|
6
|
+
* validation of bulk writes.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export declare class InstanceValidation {
|
|
11
|
+
#private;
|
|
12
|
+
/**
|
|
13
|
+
* @param services - Collaborators.
|
|
14
|
+
* @param lifecycle - Hook runner.
|
|
15
|
+
*/
|
|
16
|
+
constructor(services: InstanceLifecycleServices, lifecycle: InstanceLifecycle);
|
|
17
|
+
/**
|
|
18
|
+
* Validates an instance like Sequelize v6 `instance.validate()`.
|
|
19
|
+
*
|
|
20
|
+
* @param instance - Instance.
|
|
21
|
+
* @param options - `hooks`, `skip`, `fields`.
|
|
22
|
+
* @returns Resolves when valid.
|
|
23
|
+
* @throws ValidationError with all failures.
|
|
24
|
+
*/
|
|
25
|
+
validate(instance: AnyModel, options: ValidateOptions): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Validates instances of a bulk write; failures are collected into one error.
|
|
28
|
+
*
|
|
29
|
+
* @param instances - Instances.
|
|
30
|
+
* @param hooks - Run validation hooks.
|
|
31
|
+
* @returns Resolves when all are valid.
|
|
32
|
+
* @throws AggregateError of BulkRecordError.
|
|
33
|
+
*/
|
|
34
|
+
validateAll(instances: readonly AnyModel[], hooks: boolean): Promise<void>;
|
|
35
|
+
}
|