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
package/package.json
CHANGED
|
@@ -1,6 +1,105 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "postgrelize",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "PostgreSQL-only ORM, drop-in compatible with the Sequelize v6 API. Ships llm/USAGE.md and an Agent Skill for LLMs.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"jsonb",
|
|
7
|
+
"migrations",
|
|
8
|
+
"orm",
|
|
9
|
+
"pg",
|
|
10
|
+
"postgres",
|
|
11
|
+
"postgresql",
|
|
12
|
+
"query-builder",
|
|
13
|
+
"sequelize",
|
|
14
|
+
"sequelize-v6",
|
|
15
|
+
"typescript",
|
|
16
|
+
"upsert"
|
|
17
|
+
],
|
|
18
|
+
"homepage": "https://tormozz48.github.io/postgrelize/",
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/tormozz48/postgrelize/issues"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/tormozz48/postgrelize.git"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist",
|
|
29
|
+
"llm",
|
|
30
|
+
"skills",
|
|
31
|
+
"CHANGELOG.md"
|
|
32
|
+
],
|
|
33
|
+
"type": "module",
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"main": "./dist/index.cjs",
|
|
36
|
+
"module": "./dist/index.js",
|
|
37
|
+
"types": "./dist/index.d.ts",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"import": "./dist/index.js",
|
|
42
|
+
"require": "./dist/index.cjs"
|
|
43
|
+
},
|
|
44
|
+
"./package.json": "./package.json"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"registry": "https://registry.npmjs.org/"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "vite build",
|
|
52
|
+
"lint": "oxlint",
|
|
53
|
+
"format": "oxfmt",
|
|
54
|
+
"format:check": "oxfmt --check",
|
|
55
|
+
"typecheck": "tsc --noEmit",
|
|
56
|
+
"test": "vitest run --project unit --project decorators-legacy --project integration --project compat --project docs",
|
|
57
|
+
"test:unit": "vitest run --project unit --project decorators-legacy",
|
|
58
|
+
"test:build": "vitest run --project build",
|
|
59
|
+
"test:integration": "vitest run --project integration",
|
|
60
|
+
"test:compat": "vitest run --project compat",
|
|
61
|
+
"test:db": "vitest run --project integration --project compat --project docs",
|
|
62
|
+
"test:coverage": "vitest run --project unit --project integration --project compat --project docs --coverage && node scripts/coverage-badge.mjs",
|
|
63
|
+
"db:up": "docker compose up -d --wait",
|
|
64
|
+
"db:down": "docker compose down -v",
|
|
65
|
+
"prepublishOnly": "npm run llm:build && npm run build",
|
|
66
|
+
"api:differences": "node --experimental-strip-types scripts/generate-api-differences.ts",
|
|
67
|
+
"llm:build": "node --experimental-strip-types scripts/generate-llm.ts",
|
|
68
|
+
"release:preflight": "node --experimental-strip-types scripts/release-preflight.ts",
|
|
69
|
+
"docs": "typedoc && vitepress build docs",
|
|
70
|
+
"docs:dev": "typedoc && vitepress dev docs",
|
|
71
|
+
"docs:examples": "vitest run --project docs",
|
|
72
|
+
"bench": "vitest run --project bench"
|
|
73
|
+
},
|
|
74
|
+
"dependencies": {
|
|
75
|
+
"inflection": "~1.13.4",
|
|
76
|
+
"validator": "^13.15.35"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@types/inflection": "^1.13.2",
|
|
80
|
+
"@types/node": "^22.20.5",
|
|
81
|
+
"@types/pg": "^8.23.1",
|
|
82
|
+
"@types/validator": "^13.15.10",
|
|
83
|
+
"@vitest/coverage-v8": "^5.1.0",
|
|
84
|
+
"fast-check": "^4.10.2",
|
|
85
|
+
"oxfmt": "0.78.0",
|
|
86
|
+
"oxlint": "^1.93.0",
|
|
87
|
+
"pg": "^8.23.1",
|
|
88
|
+
"sequelize": "~6.37.8",
|
|
89
|
+
"typedoc": "^0.28.20",
|
|
90
|
+
"typedoc-plugin-markdown": "^4.14.1",
|
|
91
|
+
"typedoc-vitepress-theme": "^1.1.4",
|
|
92
|
+
"typescript": "~6.0.3",
|
|
93
|
+
"vite": "^8.3.8",
|
|
94
|
+
"vite-plugin-dts": "^5.1.2",
|
|
95
|
+
"vitepress": "^1.6.4",
|
|
96
|
+
"vitest": "^5.1.0",
|
|
97
|
+
"zod": "^4.6.7"
|
|
98
|
+
},
|
|
99
|
+
"peerDependencies": {
|
|
100
|
+
"pg": "^8.0.0"
|
|
101
|
+
},
|
|
102
|
+
"engines": {
|
|
103
|
+
"node": ">=20"
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: postgrelize
|
|
3
|
+
description: Use when writing, reviewing or migrating TypeScript/JavaScript code that uses postgrelize or Sequelize v6 on PostgreSQL — models, queries, associations, transactions, upserts (ON CONFLICT, MERGE), COPY, LISTEN/NOTIFY, sync and migrations, error handling, or moving a Sequelize v6 project to postgrelize.
|
|
4
|
+
---
|
|
5
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
6
|
+
# postgrelize
|
|
7
|
+
|
|
8
|
+
PostgreSQL-only ORM with the **Sequelize v6 public API** (drop-in) plus PostgreSQL features (`onConflict`, `Model.insert`, `merge`, CTEs, `copyFrom`/`copyTo`, `listen`/`notify`, advisory locks, `returningOld`). Sequelize v6 knowledge applies; PostgreSQL extras are additive options and methods.
|
|
9
|
+
|
|
10
|
+
Install: `npm install postgrelize pg`; import path `from 'postgrelize'` (`@tormozz48/postgrelize` is the deprecated former name). Node 20+, PostgreSQL 16–18, `pg` peer dependency.
|
|
11
|
+
|
|
12
|
+
## How to use this skill
|
|
13
|
+
|
|
14
|
+
1. Read the reference file for the task (progressive disclosure, load only what you need):
|
|
15
|
+
- `reference/querying.md` — finders, where/operators, pagination, aggregates, raw queries
|
|
16
|
+
- `reference/writing.md` — create/update/destroy, hooks, validation, upserts, bulk writes, RETURNING
|
|
17
|
+
- `reference/schema.md` — define models (init / define / decorators), data types, sync, QueryInterface migrations
|
|
18
|
+
- `reference/associations.md` — hasMany/belongsTo/belongsToMany, include, accessors
|
|
19
|
+
- `reference/transactions-and-sessions.md` — transactions, CLS, locks, LISTEN/NOTIFY, advisory locks
|
|
20
|
+
- `reference/errors.md` — error classes and how to catch them
|
|
21
|
+
- `reference/security.md` — what is bound, quoted, allowlisted or raw
|
|
22
|
+
- `reference/migrating-from-sequelize.md` — moving a v6 project, differences, unsupported features
|
|
23
|
+
- `reference/postgresql-features.md` — MERGE, COPY, CTE, windows, subqueries, what is not supported
|
|
24
|
+
2. Types are authoritative: read `node_modules/postgrelize/dist/*.d.ts` (TSDoc on every public item) before guessing an option. Compact guide: `node_modules/postgrelize/llm/USAGE.md`. Full docs: <https://tormozz48.github.io/postgrelize/llms-full.txt>.
|
|
25
|
+
3. **Verify by running.** Execute the code against a real PostgreSQL (`docker run -e POSTGRES_PASSWORD=pw -p 5432:5432 postgres:17`) and read the output or the thrown error. Enable SQL logging with `new Sequelize(url, { logging: console.log })` to see the generated SQL. Run `tsc --noEmit` — option objects are strictly typed.
|
|
26
|
+
|
|
27
|
+
## Checklists
|
|
28
|
+
|
|
29
|
+
**New model**
|
|
30
|
+
- [ ] Pick a style: class + `init` (typed with `InferAttributes`), `sequelize.define`, or decorators + `sequelize.addModels`.
|
|
31
|
+
- [ ] Set `tableName`, `timestamps`, `paranoid` explicitly when the table already exists.
|
|
32
|
+
- [ ] `BIGINT` and `DECIMAL` come back as strings.
|
|
33
|
+
- [ ] Create tables with `sync()` only in tests/prototypes; production uses `QueryInterface` migrations.
|
|
34
|
+
|
|
35
|
+
**New query**
|
|
36
|
+
- [ ] Values go in `where` / `replacements` / `bind`, never in SQL strings.
|
|
37
|
+
- [ ] Inside a transaction, pass `{ transaction }` on every call (or enable CLS).
|
|
38
|
+
- [ ] Prefer `include` over manual joins; add `required: true` for inner joins.
|
|
39
|
+
- [ ] Large result sets: `findEach` (cursor) or `copyTo`.
|
|
40
|
+
|
|
41
|
+
**Write path**
|
|
42
|
+
- [ ] Insert-or-update: `upsert` (v6 shape `[instance, null]`) or `onConflict` for a real inserted flag and expressions.
|
|
43
|
+
- [ ] Bulk: `bulkCreate`, `insertFrom`, `merge`, `copyFrom` for very large imports.
|
|
44
|
+
- [ ] Catch `UniqueConstraintError` / `ValidationError` / `DatabaseError`.
|
|
45
|
+
|
|
46
|
+
**Migrating a Sequelize v6 project**
|
|
47
|
+
- [ ] Replace the import, remove other dialect packages, keep `pg`.
|
|
48
|
+
- [ ] Read `reference/migrating-from-sequelize.md` for the differences and unsupported features.
|
|
49
|
+
- [ ] Run the existing tests; fix what the list of intentional differences explains.
|
|
50
|
+
|
|
51
|
+
## Top mistakes
|
|
52
|
+
|
|
53
|
+
1. Missing `{ transaction }` on a query inside a transaction.
|
|
54
|
+
2. Expecting `upsert` to tell insert from update (it returns `[instance, null]`; use `onConflict`).
|
|
55
|
+
3. Building SQL with string concatenation; `literal()` and raw query text are not escaped.
|
|
56
|
+
4. Using `sync()` in production, or `Sequelize.useCLS()` per instance (it is process-wide).
|
|
57
|
+
5. Using v7 APIs or other dialects: not supported.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Associations
|
|
3
|
+
|
|
4
|
+
```ts
|
|
5
|
+
class Author extends Model<InferAttributes<Author>, InferCreationAttributes<Author>> {
|
|
6
|
+
declare id: CreationOptional<number>;
|
|
7
|
+
declare name: string;
|
|
8
|
+
declare posts?: NonAttribute<Post[]>;
|
|
9
|
+
declare getPosts: (options?: FindOptions) => Promise<Post[]>;
|
|
10
|
+
declare countPosts: (options?: FindOptions) => Promise<number>;
|
|
11
|
+
declare createPost: (values: Partial<InferCreationAttributes<Post>>) => Promise<Post>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class Post extends Model<InferAttributes<Post>, InferCreationAttributes<Post>> {
|
|
15
|
+
declare id: CreationOptional<number>;
|
|
16
|
+
declare title: string;
|
|
17
|
+
declare authorId: ForeignKey<number>;
|
|
18
|
+
declare author?: NonAttribute<Author>;
|
|
19
|
+
declare tags?: NonAttribute<Tag[]>;
|
|
20
|
+
declare addTags: (tags: Tag[]) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
class Tag extends Model<InferAttributes<Tag>, InferCreationAttributes<Tag>> {
|
|
24
|
+
declare id: CreationOptional<number>;
|
|
25
|
+
declare label: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
Author.init(
|
|
29
|
+
{
|
|
30
|
+
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
|
|
31
|
+
name: { type: DataTypes.TEXT, allowNull: false },
|
|
32
|
+
},
|
|
33
|
+
{ sequelize },
|
|
34
|
+
);
|
|
35
|
+
Post.init(
|
|
36
|
+
{
|
|
37
|
+
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
|
|
38
|
+
title: { type: DataTypes.TEXT, allowNull: false },
|
|
39
|
+
},
|
|
40
|
+
{ sequelize },
|
|
41
|
+
);
|
|
42
|
+
Tag.init(
|
|
43
|
+
{
|
|
44
|
+
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
|
|
45
|
+
label: { type: DataTypes.TEXT, allowNull: false },
|
|
46
|
+
},
|
|
47
|
+
{ sequelize },
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
Author.hasMany(Post, { as: 'posts', foreignKey: 'authorId' });
|
|
51
|
+
Post.belongsTo(Author, { as: 'author', foreignKey: 'authorId' });
|
|
52
|
+
Post.belongsToMany(Tag, { as: 'tags', through: 'PostTags' });
|
|
53
|
+
Tag.belongsToMany(Post, { as: 'posts', through: 'PostTags' });
|
|
54
|
+
|
|
55
|
+
await sequelize.sync();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Nested creation
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
// Nested values are not creation attributes: pass them as a variable, not an object literal.
|
|
62
|
+
const values = { name: 'Ann', posts: [{ title: 'Hello' }, { title: 'PostgreSQL tips' }] };
|
|
63
|
+
const ann = await Author.create(values, { include: [{ association: 'posts' }] });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Accessors
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const sql = await Tag.create({ label: 'sql' });
|
|
70
|
+
const [tips] = await ann.getPosts({ where: { title: { [Op.like]: '%tips' } } });
|
|
71
|
+
await tips?.addTags([sql]);
|
|
72
|
+
await ann.createPost({ title: 'Drafts' });
|
|
73
|
+
const postCount = await ann.countPosts();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Include (eager loading)
|
|
77
|
+
|
|
78
|
+
```ts
|
|
79
|
+
const authors = await Author.findAll({
|
|
80
|
+
include: [
|
|
81
|
+
{
|
|
82
|
+
association: 'posts',
|
|
83
|
+
required: true,
|
|
84
|
+
include: [{ association: 'tags', where: { label: 'sql' }, through: { attributes: [] } }],
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
});
|
|
88
|
+
const withAuthor = await Post.findAll({
|
|
89
|
+
include: [{ association: 'author', attributes: ['name'] }],
|
|
90
|
+
order: [['id', 'ASC']],
|
|
91
|
+
});
|
|
92
|
+
const separate = await Author.findAll({
|
|
93
|
+
include: [{ association: 'posts', separate: true, order: [['title', 'ASC']] }],
|
|
94
|
+
});
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Include options: `association` (or `model` + `as`), `where`, `required`, `right`, `attributes`, nested `include`, `separate`, `through`, `paranoid`, `all: true`.
|
|
98
|
+
|
|
99
|
+
## With decorators
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const team = await Team.create({ name: 'blue' });
|
|
103
|
+
await Player.create({ nick: 'ace', teamId: team.id });
|
|
104
|
+
const teams = await Team.findAll({
|
|
105
|
+
include: [{ association: 'players', required: true, attributes: ['nick'] }],
|
|
106
|
+
});
|
|
107
|
+
// teams[0].players?.[0].nick === 'ace'; accessors such as team.getPlayers() are generated too
|
|
108
|
+
```
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Errors
|
|
3
|
+
|
|
4
|
+
```ts
|
|
5
|
+
const duplicate = await User.create({ email: 'new@example.com' }).catch(
|
|
6
|
+
(error: unknown) => error,
|
|
7
|
+
);
|
|
8
|
+
const invalid = await User.create({ email: null as unknown as string }).catch(
|
|
9
|
+
(error: unknown) => error,
|
|
10
|
+
);
|
|
11
|
+
const bad = await sequelize
|
|
12
|
+
.query('SELECT * FROM "no_such_table"')
|
|
13
|
+
.catch((error: unknown) => error);
|
|
14
|
+
// UniqueConstraintError and ValidationError extend ValidationError/BaseError; DatabaseError wraps
|
|
15
|
+
// any other server error: `error.parent` holds the pg error with `code`, `detail`, `constraint`
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
| Class | When |
|
|
19
|
+
| --- | --- |
|
|
20
|
+
| `ValidationError` (`.errors[]`) | validators, `allowNull`, schema validation |
|
|
21
|
+
| `UniqueConstraintError`, `ForeignKeyConstraintError`, `ExclusionConstraintError` | constraint violation |
|
|
22
|
+
| `DatabaseError` (`.parent` holds the `pg` error with `code`, `detail`, `constraint`) | any other server error |
|
|
23
|
+
| `TimeoutError` | cancelled statement (SQLSTATE 57014) |
|
|
24
|
+
| `OptimisticLockError` | stale `version` on save |
|
|
25
|
+
| `EmptyResultError` | `rejectOnEmpty` found nothing |
|
|
26
|
+
| `ConnectionError` and subclasses | cannot connect or authenticate |
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
const error = await Signup.create({ email: 'nope', age: 12, role: 'admin' }).catch(
|
|
30
|
+
(caught: unknown) => caught,
|
|
31
|
+
);
|
|
32
|
+
if (error instanceof ValidationError) {
|
|
33
|
+
const paths = error.errors.map((item) => `${String(item.path)}:${String(item.validatorKey)}`);
|
|
34
|
+
// paths: ['email:schema', 'role:schema']
|
|
35
|
+
audit.push(...paths.toSorted());
|
|
36
|
+
}
|
|
37
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Migrating from Sequelize v6
|
|
3
|
+
|
|
4
|
+
1. Install `postgrelize` and `pg`; remove `sequelize` and other dialect drivers.
|
|
5
|
+
2. Change imports to `from 'postgrelize'` (type helpers keep their v6 names: `InferAttributes`, `CreationOptional`, `ModelStatic`, `WhereOptions`, ...).
|
|
6
|
+
3. Connection: omit `dialect` or use `'postgres'`; `dialectOptions` goes to the `pg` pool; `pool.min` and `pool.evict` are accepted but have no effect.
|
|
7
|
+
4. Run your tests.
|
|
8
|
+
|
|
9
|
+
Behaves like v6: `init`/`define`, finders, `create`/`bulkCreate`/`upsert`/`update`/`destroy`, instances and change tracking, hooks, validators, scopes, paranoid, optimistic locking, associations, nested creation, managed and unmanaged transactions, CLS, `QueryInterface`, `sync`. `BIGINT` and `DECIMAL` are strings; `upsert` returns `[instance, null]`.
|
|
10
|
+
|
|
11
|
+
Intentional differences (v6 bugs fixed on PostgreSQL): JSON `where` keys with special characters, `upsert` on partial unique indexes, `sync({ alter })` sequences, `describeTable`/`showAllTables`/`tableExists` use `current_schema()`, `Op.any` shorthand, validated function names and isolation levels, and more. Full list with issue numbers: <https://tormozz48.github.io/postgrelize/sequelize-issues>.
|
|
12
|
+
|
|
13
|
+
Not supported: other dialects, `Model.schema('tenant')` (use the `schema` option), the `searchPath` option (use `dialectOptions: { options: '-c search_path=tenant' }`), named/shared `ENUM` types, `create({ ignoreDuplicates })`, Sequelize v7 API. Connection, sync and define hooks are accepted but never called.
|
|
14
|
+
|
|
15
|
+
After migrating, replace workarounds with native features: `literal()` upserts and `updateOnDuplicate` → `onConflict`; loops of `update` → `UPDATE ... FROM` / `merge`; large `bulkCreate` → `copyFrom`; hand-written CTEs → `with`.
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# PostgreSQL features
|
|
3
|
+
|
|
4
|
+
## MERGE (PostgreSQL 17+ for `returning`)
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
const { rows } = await Stock.merge({
|
|
8
|
+
source: [
|
|
9
|
+
{ sku: 'a', qty: 3 },
|
|
10
|
+
{ sku: 'n', qty: 9 },
|
|
11
|
+
],
|
|
12
|
+
on: { sku: col('source.sku') },
|
|
13
|
+
whenMatched: { update: { qty: col('source.qty') } },
|
|
14
|
+
whenNotMatched: { insert: true },
|
|
15
|
+
whenNotMatchedBySource: { update: { status: 'retired' } },
|
|
16
|
+
returning: true,
|
|
17
|
+
});
|
|
18
|
+
const actions = rows.map(({ action, instance }) => `${action} ${String(instance.get('sku'))}`);
|
|
19
|
+
// actions (in any order): 'UPDATE a', 'INSERT n', 'UPDATE b' (b is retired)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## RETURNING old values (PostgreSQL 18+)
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
const [, changed = []] = await Stock.update(
|
|
26
|
+
{ qty: literal('qty + 10') },
|
|
27
|
+
{ where: { sku: 'a' }, returning: true, returningOld: true },
|
|
28
|
+
);
|
|
29
|
+
const [stock] = changed;
|
|
30
|
+
// stock.get('qty') === 11, stock.oldValues === { id: 1, sku: 'a', qty: 1 }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Subqueries, EXISTS, DISTINCT ON, window functions
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const authors = await User.findAll({
|
|
37
|
+
where: {
|
|
38
|
+
id: { [Op.in]: Post.subquery({ attributes: ['UserId'], where: { published: true } }) },
|
|
39
|
+
},
|
|
40
|
+
order: [['id', 'ASC']],
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
const counted = await User.findAll({
|
|
44
|
+
attributes: [
|
|
45
|
+
'name',
|
|
46
|
+
[
|
|
47
|
+
Post.subquery({
|
|
48
|
+
attributes: [[fn('count', col('Post.id')), 'n']],
|
|
49
|
+
where: { UserId: col('User.id') },
|
|
50
|
+
}),
|
|
51
|
+
'postCount',
|
|
52
|
+
],
|
|
53
|
+
],
|
|
54
|
+
order: [['id', 'ASC']],
|
|
55
|
+
raw: true,
|
|
56
|
+
});
|
|
57
|
+
// counted: [{ name: 'ann', postCount: '2' }, { name: 'bob', postCount: '2' }, { name: 'cid', postCount: '0' }]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
```ts
|
|
61
|
+
const postsOf = (extra: object) =>
|
|
62
|
+
Post.subquery({ attributes: ['id'], where: { UserId: col('User.id'), ...extra } });
|
|
63
|
+
|
|
64
|
+
const highRanked = await User.findAll({ where: exists(postsOf({ rank: { [Op.gte]: 5 } })) });
|
|
65
|
+
const silent = await User.findAll({ where: notExists(postsOf({})) });
|
|
66
|
+
// highRanked: bob; silent: cid
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
const topPerUser = await Post.findAll({
|
|
71
|
+
distinctOn: ['UserId'],
|
|
72
|
+
order: [
|
|
73
|
+
['UserId', 'ASC'],
|
|
74
|
+
['rank', 'DESC'],
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
// topPerUser: a1, b2
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const ranked = await Post.findAll({
|
|
82
|
+
attributes: [
|
|
83
|
+
'title',
|
|
84
|
+
[
|
|
85
|
+
over(fn('row_number'), { partitionBy: ['UserId'], orderBy: [['rank', 'DESC']] }),
|
|
86
|
+
'position',
|
|
87
|
+
],
|
|
88
|
+
],
|
|
89
|
+
order: [['title', 'ASC']],
|
|
90
|
+
raw: true,
|
|
91
|
+
});
|
|
92
|
+
// ranked: a1 → '1', a2 → '2', b1 → '2', b2 → '1'
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## CTEs and keyset pagination
|
|
96
|
+
|
|
97
|
+
```ts
|
|
98
|
+
const rootChildren = await Category.count({
|
|
99
|
+
with: {
|
|
100
|
+
roots: {
|
|
101
|
+
query: Category.subquery({ attributes: ['id'], where: { parentId: null } }),
|
|
102
|
+
materialized: true,
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
where: {
|
|
106
|
+
parentId: { [Op.in]: Category.subquery({ attributes: ['id'], source: 'roots' }) },
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
// rootChildren === 2
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const subtree = await Category.findAll({
|
|
114
|
+
with: {
|
|
115
|
+
tree: {
|
|
116
|
+
query: Category.subquery({ where: { name: 'a' } }),
|
|
117
|
+
recursive: Category.subquery({
|
|
118
|
+
where: { parentId: col('tree.id') },
|
|
119
|
+
from: { tree: 'tree' },
|
|
120
|
+
}),
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
source: 'tree',
|
|
124
|
+
order: [['id', 'ASC']],
|
|
125
|
+
});
|
|
126
|
+
// subtree: a, a1, a1x — Category instances read from the CTE
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
const nextPage = await Post.findAll({
|
|
131
|
+
where: where(tuple(col('rank'), col('id')), Op.lt, tuple(3, 1)),
|
|
132
|
+
order: [
|
|
133
|
+
['rank', 'DESC'],
|
|
134
|
+
['id', 'DESC'],
|
|
135
|
+
],
|
|
136
|
+
limit: 10,
|
|
137
|
+
});
|
|
138
|
+
// nextPage: b1, a2 — rows after (rank 3, id 1) in (rank DESC, id DESC) order
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
const seen: string[] = [];
|
|
143
|
+
for await (const post of Post.findEach({ order: [['id', 'ASC']] }, { batchSize: 2 })) {
|
|
144
|
+
seen.push(String(post.get('title')));
|
|
145
|
+
}
|
|
146
|
+
// seen: a1, a2, b1, b2 — fetched two rows at a time through a server-side cursor
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Not supported
|
|
150
|
+
|
|
151
|
+
Source of truth: <https://tormozz48.github.io/postgrelize/postgresql-coverage>. Missing features (use `sequelize.query()` raw SQL or `literal()`):
|
|
152
|
+
|
|
153
|
+
- **SELECT clauses**: ONLY (table inheritance)
|
|
154
|
+
- **Data types**: pg_lsn, txid_snapshot and other system types
|
|
155
|
+
- **DDL (sync and QueryInterface)**: Rules (CREATE RULE)
|
|
156
|
+
- **Session features**: Two-phase commit (PREPARE TRANSACTION)
|
|
157
|
+
- **Out of scope**: Roles, GRANT / REVOKE, default privileges; Replication setup (publications, subscriptions, slots, logical decoding); Tablespaces; VACUUM, ANALYZE, REINDEX, CLUSTER, pg_stat views; Procedural languages (PL/pgSQL bodies, DO blocks, procedures); Extension internals (writing or configuring extensions); Foreign data wrappers and foreign tables; Server configuration (ALTER SYSTEM, pg_hba.conf, event triggers)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
<!-- Generated by `npm run llm:build` from docs/llm and docs/examples/llm-usage.ts. Do not edit. -->
|
|
2
|
+
# Querying
|
|
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
|
+
## Operators and where
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
const matches = await Note.findAll({
|
|
32
|
+
where: {
|
|
33
|
+
[Op.and]: [
|
|
34
|
+
{ text: { [Op.iLike]: '%pg%' } },
|
|
35
|
+
{ [Op.or]: [{ 'meta.level': 'info' }, { tags: { [Op.contains]: ['sql'] } }] },
|
|
36
|
+
{ id: { [Op.notIn]: [0, -1] } },
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
// other operators: Op.eq ne gt gte lt lte between in is not like notLike regexp overlap
|
|
41
|
+
// contains contained hasKey any all isDistinctFrom; compare columns with col('other')
|
|
42
|
+
// PostgreSQL: Op.similarTo jsonPathExists containedOrEqual trigramSimilar; full text:
|
|
43
|
+
// where(toTsVector(col('body')), Op.match, websearchToTsQuery(text)), order by tsRank()
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
More patterns (JSON paths, `Op.hasKey`, `Op.any`, scopes, aggregates, pagination, raw queries):
|
|
47
|
+
|
|
48
|
+
```ts
|
|
49
|
+
const red = await Item.findAll({ where: { 'attrs.color': 'red' } });
|
|
50
|
+
const big = await Item.findAll({
|
|
51
|
+
where: { [Op.or]: [{ qty: { [Op.gte]: 2 } }, { code: { [Op.iLike]: 'A%' } }] },
|
|
52
|
+
order: [['qty', 'DESC']],
|
|
53
|
+
});
|
|
54
|
+
const withSize = await Item.count({ where: { attrs: { [Op.hasKey]: 'size' } } });
|
|
55
|
+
const notA = await Item.count({ where: { code: { [Op.isDistinctFrom]: 'a' } } });
|
|
56
|
+
const listed = await Item.count({ where: { id: { [Op.any]: [1, 3] } } });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
const total = await Item.sum('qty');
|
|
61
|
+
const maxQty = await Item.max('qty');
|
|
62
|
+
const perTag = await Item.findAll({
|
|
63
|
+
attributes: [
|
|
64
|
+
[fn('unnest', col('tags')), 'tag'],
|
|
65
|
+
[fn('count', col('id')), 'n'],
|
|
66
|
+
],
|
|
67
|
+
group: ['tag'],
|
|
68
|
+
order: [['tag', 'ASC']],
|
|
69
|
+
raw: true,
|
|
70
|
+
});
|
|
71
|
+
await Item.increment('qty', { by: 5, where: { code: 'c' } });
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const onSale = await Item.scope('inStock', { method: ['tagged', 'sale'] }).findAll();
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Pagination, aggregates, soft deletes
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
const { count, rows } = await Note.findAndCountAll({
|
|
82
|
+
attributes: ['id', 'text'],
|
|
83
|
+
order: [['id', 'DESC']],
|
|
84
|
+
limit: 1,
|
|
85
|
+
offset: 0,
|
|
86
|
+
raw: true,
|
|
87
|
+
});
|
|
88
|
+
const perTag = await Note.findAll({
|
|
89
|
+
attributes: [
|
|
90
|
+
[fn('unnest', col('tags')), 'tag'],
|
|
91
|
+
[fn('count', col('id')), 'n'],
|
|
92
|
+
],
|
|
93
|
+
group: ['tag'],
|
|
94
|
+
raw: true,
|
|
95
|
+
});
|
|
96
|
+
const sum = await Note.sum('id');
|
|
97
|
+
await Note.destroy({ where: { text: 'misc' } }); // soft delete, the row stays with deletedAt set
|
|
98
|
+
const withDeleted = await Note.findAll({ paranoid: false });
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Raw queries
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
const found = (await sequelize.query('SELECT id FROM llm_users WHERE email = :email', {
|
|
105
|
+
replacements: { email: 'ann@corp.example.com' },
|
|
106
|
+
type: QueryTypes.SELECT,
|
|
107
|
+
})) as { id: number }[];
|
|
108
|
+
const ordered = await User.findAll({ order: [literal('"email" DESC NULLS LAST')] });
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Use `replacements` (`:name`, `?`) or `bind` (`$1`). Never interpolate user values into the SQL text.
|