postgrelize 0.0.0-stage → 1.0.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 +269 -0
- package/LICENSE +21 -0
- package/README.md +131 -2
- 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/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 +158 -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 +172 -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 +193 -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 +152 -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 +188 -0
- package/dist/errors/validation-errors.d.ts +117 -0
- package/dist/exports-core.d.ts +15 -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/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 +20461 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +20332 -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/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 +80 -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 +73 -0
- package/dist/model/mixins/model-constructor.d.ts +7 -0
- package/dist/model/mixins/model-finders.d.ts +124 -0
- package/dist/model/mixins/model-hooks.d.ts +138 -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 +55 -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 +154 -0
- package/dist/model/model-operations.d.ts +22 -0
- package/dist/model/model-registry.d.ts +139 -0
- package/dist/model/model-services.d.ts +46 -0
- package/dist/model/model-state.d.ts +60 -0
- package/dist/model/model-values.d.ts +86 -0
- package/dist/model/model.d.ts +560 -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 +89 -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 +54 -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/query/query-result-shaper.d.ts +91 -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-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 +99 -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/sequelize-base.d.ts +196 -0
- package/dist/sequelize/sequelize-constructor.d.ts +7 -0
- package/dist/sequelize/sequelize-hooks.d.ts +86 -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 +106 -0
- package/dist/sequelize/sequelize-transactions.d.ts +96 -0
- package/dist/sequelize.d.ts +790 -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/transaction-constants.d.ts +35 -0
- package/dist/transaction/transaction-runner.d.ts +60 -0
- package/dist/transaction/transaction-starter.d.ts +25 -0
- package/dist/transaction/transaction-statements.d.ts +28 -0
- package/dist/transaction/transaction.d.ts +198 -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 +102 -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,135 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Models and schema
|
|
3
|
+
|
|
4
|
+
## Class and init
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
|
|
8
|
+
declare id: CreationOptional<number>;
|
|
9
|
+
declare email: string;
|
|
10
|
+
declare active: CreationOptional<boolean>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
User.init(
|
|
14
|
+
{
|
|
15
|
+
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
|
|
16
|
+
email: { type: DataTypes.STRING, allowNull: false, unique: true },
|
|
17
|
+
active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true },
|
|
18
|
+
},
|
|
19
|
+
{ sequelize, tableName: 'llm_users' },
|
|
20
|
+
);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## define
|
|
24
|
+
|
|
25
|
+
```ts
|
|
26
|
+
const Note = sequelize.define(
|
|
27
|
+
'Note',
|
|
28
|
+
{ text: DataTypes.TEXT, meta: DataTypes.JSONB, tags: DataTypes.ARRAY(DataTypes.TEXT) },
|
|
29
|
+
{ tableName: 'llm_notes', paranoid: true },
|
|
30
|
+
);
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Decorators
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
@Table({ tableName: 'llm_teams' })
|
|
37
|
+
class Team extends Model {
|
|
38
|
+
@PrimaryKey
|
|
39
|
+
@AutoIncrement
|
|
40
|
+
@Column(DataTypes.INTEGER)
|
|
41
|
+
id!: number;
|
|
42
|
+
|
|
43
|
+
@AllowNull(false)
|
|
44
|
+
@Column(DataTypes.TEXT)
|
|
45
|
+
name!: string;
|
|
46
|
+
|
|
47
|
+
@HasMany(() => Player)
|
|
48
|
+
players?: Player[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@Table({ tableName: 'llm_players' })
|
|
52
|
+
class Player extends Model {
|
|
53
|
+
@PrimaryKey
|
|
54
|
+
@AutoIncrement
|
|
55
|
+
@Column(DataTypes.INTEGER)
|
|
56
|
+
id!: number;
|
|
57
|
+
|
|
58
|
+
@Column(DataTypes.TEXT)
|
|
59
|
+
nick!: string;
|
|
60
|
+
|
|
61
|
+
@ForeignKey(() => Team)
|
|
62
|
+
@Column(DataTypes.INTEGER)
|
|
63
|
+
teamId!: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
sequelize.addModels([Team, Player]);
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## PostgreSQL column types
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
const Line = sequelize.define(
|
|
73
|
+
'Line',
|
|
74
|
+
{
|
|
75
|
+
id: { type: DataTypes.BIGINT, primaryKey: true, identity: 'always' },
|
|
76
|
+
price: { type: DataTypes.DECIMAL(10, 2), allowNull: false },
|
|
77
|
+
qty: { type: DataTypes.INTEGER, allowNull: false },
|
|
78
|
+
total: { type: DataTypes.DECIMAL(12, 2), generated: { as: literal('"price" * "qty"') } },
|
|
79
|
+
period: DataTypes.INTERVAL,
|
|
80
|
+
},
|
|
81
|
+
{ timestamps: false },
|
|
82
|
+
);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## sync
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const Device = sequelize.define(
|
|
89
|
+
'Device',
|
|
90
|
+
{
|
|
91
|
+
serial: { type: DataTypes.TEXT, unique: { name: 'devices_serial', nullsNotDistinct: true } },
|
|
92
|
+
vendor: DataTypes.TEXT,
|
|
93
|
+
model: DataTypes.TEXT,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
indexes: [
|
|
97
|
+
{ fields: ['vendor', 'model'], unique: true, nullsNotDistinct: true },
|
|
98
|
+
{ fields: ['model'], where: { vendor: null } },
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
);
|
|
102
|
+
await sequelize.sync();
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`sync()` creates missing tables, `{ alter: true }` adjusts columns, `{ force: true }` drops first. Use it for tests and prototypes.
|
|
106
|
+
|
|
107
|
+
## Migrations with QueryInterface
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
export const up = async (queryInterface: QueryInterface): Promise<void> => {
|
|
111
|
+
await queryInterface.createTable('accounts', {
|
|
112
|
+
id: { type: DataTypes.BIGINT, primaryKey: true, autoIncrement: true },
|
|
113
|
+
email: { type: DataTypes.TEXT, allowNull: false, unique: true },
|
|
114
|
+
plan: { type: DataTypes.ENUM('free', 'pro'), allowNull: false, defaultValue: 'free' },
|
|
115
|
+
created_at: { type: DataTypes.DATE, allowNull: false, defaultValue: DataTypes.NOW },
|
|
116
|
+
});
|
|
117
|
+
await queryInterface.createTable('sessions', {
|
|
118
|
+
id: { type: DataTypes.UUID, primaryKey: true },
|
|
119
|
+
account_id: {
|
|
120
|
+
type: DataTypes.BIGINT,
|
|
121
|
+
allowNull: false,
|
|
122
|
+
references: { model: 'accounts', key: 'id', deferrable: Deferrable.INITIALLY_DEFERRED },
|
|
123
|
+
onDelete: 'CASCADE',
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
await queryInterface.addIndex('sessions', ['account_id']);
|
|
127
|
+
await queryInterface.addColumn('accounts', 'note', { type: DataTypes.TEXT });
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
export const down = async (queryInterface: QueryInterface): Promise<void> => {
|
|
131
|
+
await queryInterface.dropTable('sessions');
|
|
132
|
+
await queryInterface.dropTable('accounts');
|
|
133
|
+
await queryInterface.dropEnum('enum_accounts_plan');
|
|
134
|
+
};
|
|
135
|
+
```
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Security
|
|
3
|
+
|
|
4
|
+
- Values are always bound (`$1..$n`): `where`, `create`/`update`/`upsert`/`insert`/`merge`, `replacements`, `bind`, `NOTIFY` payloads, advisory keys, transaction `settings`.
|
|
5
|
+
- Identifiers (columns, tables, aliases, schemas, channels) are quoted; operators, function names, cast types, lock strengths, isolation levels and order directions are allowlisted and throw otherwise.
|
|
6
|
+
- `literal()`, raw `sequelize.query` text and string `where` are raw by design: only constants you wrote.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
// Values from users are always sent as bound parameters.
|
|
10
|
+
const byTitle = await Post.findAll({ where: { title: input['title'] } });
|
|
11
|
+
const searched = await Post.count({ where: { title: { [Op.iLike]: `%${input['search']}%` } } });
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
// Raw SQL: put user values in replacements or bind, never in the SQL text.
|
|
16
|
+
const rows = (await sequelize.query('SELECT title FROM "Posts" WHERE title = :title', {
|
|
17
|
+
type: QueryTypes.SELECT,
|
|
18
|
+
replacements: { title: input['title'] },
|
|
19
|
+
})) as { title: string }[];
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
// Column names from users: map them through your own allowlist.
|
|
24
|
+
const sortable = { rank: 'rank', title: 'title' } as const;
|
|
25
|
+
const column = sortable[input['sort'] as keyof typeof sortable] ?? 'rank';
|
|
26
|
+
const sorted = await Post.findAll({ order: [[column, 'DESC']] });
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// literal() is inserted as is: only constant SQL written by you.
|
|
31
|
+
const nullsLast = await Post.findAll({ order: [literal('"rank" DESC NULLS LAST')] });
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Full table: <https://tormozz48.github.io/postgrelize/security>.
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Transactions and sessions
|
|
3
|
+
|
|
4
|
+
## Managed and unmanaged
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
await sequelize.transaction(async (transaction) => {
|
|
8
|
+
await Account.decrement('balance', { by: 30, where: { owner: 'ann' }, transaction });
|
|
9
|
+
await Account.increment('balance', { by: 30, where: { owner: 'bob' }, transaction });
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const failed = await sequelize
|
|
13
|
+
.transaction(async (transaction) => {
|
|
14
|
+
await Account.update({ balance: 0 }, { where: {}, transaction });
|
|
15
|
+
throw new Error('abort');
|
|
16
|
+
})
|
|
17
|
+
.catch((error: Error) => error.message);
|
|
18
|
+
// failed === 'abort'; the update was rolled back
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
const transaction = await sequelize.transaction({
|
|
23
|
+
isolationLevel: Transaction.ISOLATION_LEVELS.REPEATABLE_READ,
|
|
24
|
+
});
|
|
25
|
+
const committed: string[] = [];
|
|
26
|
+
transaction.afterCommit(() => {
|
|
27
|
+
committed.push(transaction.id);
|
|
28
|
+
});
|
|
29
|
+
try {
|
|
30
|
+
await Account.create({ owner: 'cid', balance: 5 }, { transaction });
|
|
31
|
+
await transaction.commit();
|
|
32
|
+
} catch (error) {
|
|
33
|
+
await transaction.rollback();
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Savepoints
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
await sequelize.transaction(async (outer) => {
|
|
42
|
+
await Account.create({ owner: 'dan', balance: 1 }, { transaction: outer });
|
|
43
|
+
await sequelize
|
|
44
|
+
.transaction({ transaction: outer }, async (inner) => {
|
|
45
|
+
await Account.create({ owner: 'eve', balance: 1 }, { transaction: inner });
|
|
46
|
+
throw new Error('rolls back to the savepoint only');
|
|
47
|
+
})
|
|
48
|
+
.catch(() => undefined);
|
|
49
|
+
});
|
|
50
|
+
// dan is saved, eve is not
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Row locks
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
const holder = await sequelize.transaction();
|
|
57
|
+
await Account.findAll({ where: { owner: 'ann' }, lock: true, transaction: holder });
|
|
58
|
+
|
|
59
|
+
const free = await sequelize.transaction((worker) =>
|
|
60
|
+
Account.findAll({ lock: Transaction.LOCK.UPDATE, skipLocked: true, transaction: worker }),
|
|
61
|
+
);
|
|
62
|
+
const noWait = await sequelize
|
|
63
|
+
.transaction((worker) =>
|
|
64
|
+
Account.findAll({ where: { owner: 'ann' }, lock: true, noWait: true, transaction: worker }),
|
|
65
|
+
)
|
|
66
|
+
.catch((error: unknown) => error);
|
|
67
|
+
await holder.rollback();
|
|
68
|
+
// free: every row except ann's; noWait: DatabaseError (could not obtain lock)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## CLS
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
Sequelize.useCLS(); // global switch: managed transactions now apply to queries inside the callback
|
|
75
|
+
await sequelize.transaction(async () => {
|
|
76
|
+
await User.update({ active: true }, { where: {} }); // runs in the transaction, no option needed
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Settings, LISTEN/NOTIFY, advisory locks
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
const user = await sequelize.transaction(
|
|
84
|
+
{ settings: { 'app.user_id': 42, statement_timeout: '5s' } },
|
|
85
|
+
(transaction) =>
|
|
86
|
+
sequelize.query("SELECT current_setting('app.user_id') AS id", {
|
|
87
|
+
type: QueryTypes.SELECT,
|
|
88
|
+
plain: true,
|
|
89
|
+
transaction,
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
// user: { id: '42' }; the settings end with the transaction (SET LOCAL semantics)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const received: string[] = [];
|
|
97
|
+
const unlisten = await sequelize.listen('jobs', (payload, channel) => {
|
|
98
|
+
received.push(`${channel}: ${payload ?? ''}`);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
await sequelize.notify('jobs', { id: 1 });
|
|
102
|
+
await sequelize.transaction(async (transaction) => {
|
|
103
|
+
await Job.create({ name: 'export', state: 'queued' }, { transaction });
|
|
104
|
+
await sequelize.notify('jobs', 'export queued', { transaction }); // delivered on commit
|
|
105
|
+
});
|
|
106
|
+
await pause(200);
|
|
107
|
+
await unlisten();
|
|
108
|
+
// received: ['jobs: {"id":1}', 'jobs: export queued']
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
const holder = await sequelize.transaction();
|
|
113
|
+
await holder.advisoryLock('jobs:42');
|
|
114
|
+
|
|
115
|
+
const other = await sequelize.transaction();
|
|
116
|
+
const busy = await other.tryAdvisoryLock('jobs:42'); // false: held by `holder`
|
|
117
|
+
const free = await other.tryAdvisoryLock(7); // true: integer keys work too
|
|
118
|
+
await holder.commit(); // transaction-level locks are released on commit / rollback
|
|
119
|
+
await other.rollback();
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Read replicas: `new Sequelize({ replication: { read: [...], write } })` routes reads to replicas; see <https://tormozz48.github.io/postgrelize/guide/sessions>.
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Writing data
|
|
3
|
+
|
|
4
|
+
```ts
|
|
5
|
+
const ann = await User.create({ email: 'ann@example.com' });
|
|
6
|
+
await User.bulkCreate([
|
|
7
|
+
{ email: 'bob@example.com' },
|
|
8
|
+
{ email: 'eve@example.com', active: false },
|
|
9
|
+
]);
|
|
10
|
+
|
|
11
|
+
const byKey = await User.findByPk(ann.id);
|
|
12
|
+
const one = await User.findOne({ where: { email: 'bob@example.com' } });
|
|
13
|
+
const [cid, created] = await User.findOrCreate({
|
|
14
|
+
where: { email: 'cid@example.com' },
|
|
15
|
+
defaults: { active: true },
|
|
16
|
+
});
|
|
17
|
+
const all = await User.findAll({ where: { active: true }, order: [['email', 'ASC']], limit: 10 });
|
|
18
|
+
const total = await User.count();
|
|
19
|
+
|
|
20
|
+
ann.active = false; // change tracking: save() writes only changed attributes
|
|
21
|
+
await ann.save();
|
|
22
|
+
await ann.update({ email: 'ann@corp.example.com' });
|
|
23
|
+
await ann.reload();
|
|
24
|
+
await User.update({ active: true }, { where: { email: { [Op.like]: '%@example.com' } } });
|
|
25
|
+
await User.destroy({ where: { id: cid.id } }); // paranoid models soft-delete; { force: true } removes
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Hooks
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const log: string[] = [];
|
|
32
|
+
User.beforeCreate((user: User) => {
|
|
33
|
+
user.email = user.email.toLowerCase();
|
|
34
|
+
});
|
|
35
|
+
User.addHook('afterUpdate', 'log-update', (user: User) => {
|
|
36
|
+
log.push(`updated ${user.id}`);
|
|
37
|
+
});
|
|
38
|
+
const mixed = await User.create({ email: 'MiXed@Example.com' });
|
|
39
|
+
await mixed.update({ active: false });
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Order for `create`: `beforeValidate`, `afterValidate` (or `validationFailed`), `beforeCreate`, `afterCreate`. Bulk operations run row hooks only with `individualHooks: true`; `hooks: false` skips hooks for one call.
|
|
43
|
+
|
|
44
|
+
## Validation
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
const Person = sequelize.define(
|
|
48
|
+
'Person',
|
|
49
|
+
{
|
|
50
|
+
email: { type: DataTypes.TEXT, allowNull: false, validate: { isEmail: true } },
|
|
51
|
+
name: {
|
|
52
|
+
type: DataTypes.TEXT,
|
|
53
|
+
validate: { len: { args: [2, 50], msg: 'name must be 2-50 characters' } },
|
|
54
|
+
},
|
|
55
|
+
age: {
|
|
56
|
+
type: DataTypes.INTEGER,
|
|
57
|
+
validate: {
|
|
58
|
+
isAdult(value: unknown) {
|
|
59
|
+
if (typeof value === 'number' && value < 18) {
|
|
60
|
+
throw new Error('must be an adult');
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
validate: {
|
|
68
|
+
nameOrEmail(this: { name: string | null; email: string | null }) {
|
|
69
|
+
if (this.name === null && this.email === null) {
|
|
70
|
+
throw new Error('name or email is required');
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
);
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Standard Schema (zod, valibot, arktype) is supported through `validate: { schema }` and the `validationSchema` model option.
|
|
79
|
+
|
|
80
|
+
## Upserts and RETURNING
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
const [row] = await User.upsert({ id: ann.id, email: 'ann@corp.example.com', active: true });
|
|
84
|
+
// v6 shape: [instance, null]; pass onConflict to get a real inserted/updated boolean
|
|
85
|
+
const [visit, wasInserted] = await User.upsert(
|
|
86
|
+
{ email: 'new@example.com' },
|
|
87
|
+
{ onConflict: { target: ['email'], action: { update: { active: true } } } },
|
|
88
|
+
);
|
|
89
|
+
const written = await User.insert(
|
|
90
|
+
[{ email: 'new@example.com' }, { email: 'other@example.com' }],
|
|
91
|
+
{ onConflict: { target: ['email'], action: 'nothing' } },
|
|
92
|
+
); // returns the rows that were actually inserted
|
|
93
|
+
const [changed, updated = []] = await User.update(
|
|
94
|
+
{ active: false },
|
|
95
|
+
{ where: { email: 'new@example.com' }, returning: true },
|
|
96
|
+
);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
const newer = await Counter.insert(
|
|
101
|
+
[
|
|
102
|
+
{ tenant: 't1', key: 'a', hits: 100, version: 2 },
|
|
103
|
+
{ tenant: 't1', key: 'b', hits: 100, version: 3 },
|
|
104
|
+
],
|
|
105
|
+
{
|
|
106
|
+
onConflict: {
|
|
107
|
+
target: ['tenant', 'key'],
|
|
108
|
+
action: {
|
|
109
|
+
update: ['hits', 'version'],
|
|
110
|
+
where: { version: { [Op.lt]: excluded('version') } },
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
);
|
|
115
|
+
// only 'a' is updated: 'b' already has version 5; rows that were skipped are not returned
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```ts
|
|
119
|
+
await Counter.bulkCreate([{ tenant: 't1', key: 'a', tags: ['y'] }], {
|
|
120
|
+
onConflict: {
|
|
121
|
+
target: { constraint: 'counters_tenant_key' },
|
|
122
|
+
action: { update: { tags: fn('array_cat', col('Counter.tags'), excluded('tags')) } },
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const [partial] = await Counter.insert(
|
|
127
|
+
{ tenant: 't9', key: 'c', hits: 50 },
|
|
128
|
+
{
|
|
129
|
+
onConflict: {
|
|
130
|
+
target: { columns: ['key'], where: { archived: false } },
|
|
131
|
+
action: { update: { hits: excluded('hits') } },
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
);
|
|
135
|
+
// partial.tenant === 't1': the existing active row with key 'c' was updated
|
|
136
|
+
|
|
137
|
+
const skipped = await Counter.bulkCreate(
|
|
138
|
+
[
|
|
139
|
+
{ tenant: 't1', key: 'b' },
|
|
140
|
+
{ tenant: 't1', key: 'n' },
|
|
141
|
+
],
|
|
142
|
+
{ onConflict: { action: 'nothing' } },
|
|
143
|
+
);
|
|
144
|
+
// skipped: only the inserted row 'n'
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
## UPDATE FROM, DELETE USING, INSERT FROM
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
const [updated] = await Stock.update(
|
|
151
|
+
{ qty: literal('"Stock"."qty" + "d"."amount"') },
|
|
152
|
+
{ where: { sku: col('d.sku') }, from: { d: Delivery } },
|
|
153
|
+
);
|
|
154
|
+
// UPDATE "Stocks" AS "Stock" SET ... FROM "Deliveries" AS "d" WHERE "Stock"."sku" = "d"."sku"
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
```ts
|
|
158
|
+
const archived = await Archive.insertFrom(
|
|
159
|
+
Stock.subquery({ attributes: ['sku', 'qty'], where: { qty: { [Op.gt]: 20 } } }),
|
|
160
|
+
{ columns: ['sku', 'qty'] },
|
|
161
|
+
);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
```ts
|
|
165
|
+
const removed = await Stock.destroy({
|
|
166
|
+
where: { sku: col('a.sku') },
|
|
167
|
+
using: { a: Archive },
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## COPY (large imports and exports)
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
const fromArray = await Event.copyFrom([
|
|
175
|
+
{ kind: 'signup', payload: { plan: 'pro' }, tags: ['web'] },
|
|
176
|
+
{ kind: 'login', payload: null, tags: null },
|
|
177
|
+
]);
|
|
178
|
+
|
|
179
|
+
const fromStream = await Event.copyFrom(readEvents(), { columns: ['kind', 'payload'] });
|
|
180
|
+
// fromArray === 2, fromStream === 1000; createdAt / updatedAt are filled in
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
```ts
|
|
184
|
+
const chunks: Buffer[] = [];
|
|
185
|
+
for await (const chunk of Event.copyTo({
|
|
186
|
+
attributes: ['id', 'kind', 'tags'],
|
|
187
|
+
where: { kind: { [Op.in]: ['signup', 'login'] } },
|
|
188
|
+
order: [['id', 'ASC']],
|
|
189
|
+
format: 'csv',
|
|
190
|
+
header: true,
|
|
191
|
+
})) {
|
|
192
|
+
chunks.push(chunk);
|
|
193
|
+
}
|
|
194
|
+
const csv = Buffer.concat(chunks).toString('utf8');
|
|
195
|
+
// id,kind,tags
|
|
196
|
+
// 1,signup,{web}
|
|
197
|
+
// 2,login,
|
|
198
|
+
```
|