@opensaas/stack-core 0.43.0 → 0.44.0-alpha.2
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 +2753 -0
- package/README.md +182 -115
- package/dist/.build-hash +1 -0
- package/dist/access/declared-dependencies.d.ts +102 -88
- package/dist/access/declared-dependencies.d.ts.map +1 -1
- package/dist/access/declared-dependencies.js +83 -71
- package/dist/access/declared-dependencies.js.map +1 -1
- package/dist/access/depth-limits.d.ts +4 -0
- package/dist/access/depth-limits.d.ts.map +1 -1
- package/dist/access/depth-limits.js +4 -0
- package/dist/access/depth-limits.js.map +1 -1
- package/dist/access/engine.d.ts +68 -20
- package/dist/access/engine.d.ts.map +1 -1
- package/dist/access/engine.js +151 -37
- package/dist/access/engine.js.map +1 -1
- package/dist/access/errors.d.ts +33 -48
- package/dist/access/errors.d.ts.map +1 -1
- package/dist/access/errors.js +73 -63
- package/dist/access/errors.js.map +1 -1
- package/dist/access/field-access.d.ts +76 -5
- package/dist/access/field-access.d.ts.map +1 -1
- package/dist/access/field-access.js +235 -61
- package/dist/access/field-access.js.map +1 -1
- package/dist/access/field-visibility.d.ts +50 -4
- package/dist/access/field-visibility.d.ts.map +1 -1
- package/dist/access/field-visibility.js +143 -160
- package/dist/access/field-visibility.js.map +1 -1
- package/dist/access/index.d.ts +5 -12
- package/dist/access/index.d.ts.map +1 -1
- package/dist/access/index.js +6 -19
- package/dist/access/index.js.map +1 -1
- package/dist/access/orm-client.d.ts +19 -0
- package/dist/access/orm-client.d.ts.map +1 -0
- package/dist/access/orm-client.js +37 -0
- package/dist/access/orm-client.js.map +1 -0
- package/dist/access/query-validation.d.ts +7 -129
- package/dist/access/query-validation.d.ts.map +1 -1
- package/dist/access/query-validation.js +12 -314
- package/dist/access/query-validation.js.map +1 -1
- package/dist/access/system-fields.d.ts +2 -0
- package/dist/access/system-fields.d.ts.map +1 -0
- package/dist/access/system-fields.js +7 -0
- package/dist/access/system-fields.js.map +1 -0
- package/dist/access/transaction-registry.d.ts +25 -2
- package/dist/access/transaction-registry.d.ts.map +1 -1
- package/dist/access/transaction-registry.js +34 -17
- package/dist/access/transaction-registry.js.map +1 -1
- package/dist/access/types.d.ts +217 -131
- package/dist/access/types.d.ts.map +1 -1
- package/dist/config/field-kind.d.ts +66 -0
- package/dist/config/field-kind.d.ts.map +1 -0
- package/dist/config/field-kind.js +63 -0
- package/dist/config/field-kind.js.map +1 -0
- package/dist/config/hook-only-fields.d.ts +3 -0
- package/dist/config/hook-only-fields.d.ts.map +1 -0
- package/dist/config/hook-only-fields.js +17 -0
- package/dist/config/hook-only-fields.js.map +1 -0
- package/dist/config/index.d.ts +3 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +6 -3
- package/dist/config/index.js.map +1 -1
- package/dist/config/mcp-tool-names.d.ts +3 -0
- package/dist/config/mcp-tool-names.d.ts.map +1 -0
- package/dist/config/mcp-tool-names.js +25 -0
- package/dist/config/mcp-tool-names.js.map +1 -0
- package/dist/config/nav-count.d.ts.map +1 -1
- package/dist/config/nav-count.js +4 -4
- package/dist/config/nav-count.js.map +1 -1
- package/dist/config/plugin-engine.d.ts +8 -2
- package/dist/config/plugin-engine.d.ts.map +1 -1
- package/dist/config/plugin-engine.js +102 -38
- package/dist/config/plugin-engine.js.map +1 -1
- package/dist/config/types.d.ts +1035 -611
- package/dist/config/types.d.ts.map +1 -1
- package/dist/context/apply-defaults.d.ts +18 -1
- package/dist/context/apply-defaults.d.ts.map +1 -1
- package/dist/context/apply-defaults.js +30 -4
- package/dist/context/apply-defaults.js.map +1 -1
- package/dist/context/engine-context.d.ts +73 -0
- package/dist/context/engine-context.d.ts.map +1 -0
- package/dist/context/engine-context.js +67 -0
- package/dist/context/engine-context.js.map +1 -0
- package/dist/context/hook-pipeline.d.ts +9 -2
- package/dist/context/hook-pipeline.d.ts.map +1 -1
- package/dist/context/hook-pipeline.js +5 -4
- package/dist/context/hook-pipeline.js.map +1 -1
- package/dist/context/index.d.ts +98 -89
- package/dist/context/index.d.ts.map +1 -1
- package/dist/context/index.js +917 -722
- package/dist/context/index.js.map +1 -1
- package/dist/context/junction.d.ts +59 -0
- package/dist/context/junction.d.ts.map +1 -0
- package/dist/context/junction.js +126 -0
- package/dist/context/junction.js.map +1 -0
- package/dist/context/plugin-field-read.d.ts +33 -0
- package/dist/context/plugin-field-read.d.ts.map +1 -0
- package/dist/context/plugin-field-read.js +41 -0
- package/dist/context/plugin-field-read.js.map +1 -0
- package/dist/context/plugin-field-write.d.ts +104 -0
- package/dist/context/plugin-field-write.d.ts.map +1 -0
- package/dist/context/plugin-field-write.js +184 -0
- package/dist/context/plugin-field-write.js.map +1 -0
- package/dist/context/relationship-input.d.ts +146 -0
- package/dist/context/relationship-input.d.ts.map +1 -0
- package/dist/context/relationship-input.js +453 -0
- package/dist/context/relationship-input.js.map +1 -0
- package/dist/context/transaction-boundary.d.ts +54 -28
- package/dist/context/transaction-boundary.d.ts.map +1 -1
- package/dist/context/transaction-boundary.js +87 -169
- package/dist/context/transaction-boundary.js.map +1 -1
- package/dist/context/write-pipeline.d.ts +33 -51
- package/dist/context/write-pipeline.d.ts.map +1 -1
- package/dist/context/write-pipeline.js +372 -185
- package/dist/context/write-pipeline.js.map +1 -1
- package/dist/contract/dependencies.d.ts +88 -0
- package/dist/contract/dependencies.d.ts.map +1 -0
- package/dist/contract/dependencies.js +237 -0
- package/dist/contract/dependencies.js.map +1 -0
- package/dist/contract/derive.d.ts +49 -0
- package/dist/contract/derive.d.ts.map +1 -0
- package/dist/contract/derive.js +629 -0
- package/dist/contract/derive.js.map +1 -0
- package/dist/contract/fixtures/contract-configs.d.ts +64 -0
- package/dist/contract/fixtures/contract-configs.d.ts.map +1 -0
- package/dist/contract/fixtures/contract-configs.js +345 -0
- package/dist/contract/fixtures/contract-configs.js.map +1 -0
- package/dist/contract/id-boundary.d.ts +53 -0
- package/dist/contract/id-boundary.d.ts.map +1 -0
- package/dist/contract/id-boundary.js +76 -0
- package/dist/contract/id-boundary.js.map +1 -0
- package/dist/contract/index.d.ts +8 -0
- package/dist/contract/index.d.ts.map +1 -0
- package/dist/contract/index.js +11 -0
- package/dist/contract/index.js.map +1 -0
- package/dist/contract/prisma.d.ts +58 -0
- package/dist/contract/prisma.d.ts.map +1 -0
- package/dist/contract/prisma.js +294 -0
- package/dist/contract/prisma.js.map +1 -0
- package/dist/contract/relation-graph.d.ts +70 -0
- package/dist/contract/relation-graph.d.ts.map +1 -0
- package/dist/contract/relation-graph.js +113 -0
- package/dist/contract/relation-graph.js.map +1 -0
- package/dist/contract/types.d.ts +153 -0
- package/dist/contract/types.d.ts.map +1 -0
- package/dist/contract/types.js +2 -0
- package/dist/contract/types.js.map +1 -0
- package/dist/db/client.d.ts +55 -0
- package/dist/db/client.d.ts.map +1 -0
- package/dist/db/client.js +73 -0
- package/dist/db/client.js.map +1 -0
- package/dist/db/dev-database.d.ts +124 -0
- package/dist/db/dev-database.d.ts.map +1 -0
- package/dist/db/dev-database.js +291 -0
- package/dist/db/dev-database.js.map +1 -0
- package/dist/db/password-front.d.ts +10 -0
- package/dist/db/password-front.d.ts.map +1 -0
- package/dist/db/password-front.js +147 -0
- package/dist/db/password-front.js.map +1 -0
- package/dist/db/process-identity.d.ts +36 -0
- package/dist/db/process-identity.d.ts.map +1 -0
- package/dist/db/process-identity.js +114 -0
- package/dist/db/process-identity.js.map +1 -0
- package/dist/db/state-file.d.ts +54 -0
- package/dist/db/state-file.d.ts.map +1 -0
- package/dist/db/state-file.js +91 -0
- package/dist/db/state-file.js.map +1 -0
- package/dist/db/url.d.ts +89 -0
- package/dist/db/url.d.ts.map +1 -0
- package/dist/db/url.js +118 -0
- package/dist/db/url.js.map +1 -0
- package/dist/extend.d.ts +13 -2
- package/dist/extend.d.ts.map +1 -1
- package/dist/extend.js +24 -1
- package/dist/extend.js.map +1 -1
- package/dist/fields/index.d.ts +87 -42
- package/dist/fields/index.d.ts.map +1 -1
- package/dist/fields/index.js +462 -678
- package/dist/fields/index.js.map +1 -1
- package/dist/filter/collect.d.ts +3 -4
- package/dist/filter/collect.d.ts.map +1 -1
- package/dist/filter/collect.js +2 -2
- package/dist/filter/collect.js.map +1 -1
- package/dist/filter/index.d.ts +1 -2
- package/dist/filter/index.d.ts.map +1 -1
- package/dist/filter/index.js +0 -1
- package/dist/filter/index.js.map +1 -1
- package/dist/filter/map.d.ts +12 -5
- package/dist/filter/map.d.ts.map +1 -1
- package/dist/filter/map.js +47 -19
- package/dist/filter/map.js.map +1 -1
- package/dist/filter/types.d.ts +10 -24
- package/dist/filter/types.d.ts.map +1 -1
- package/dist/filter/types.js +2 -11
- package/dist/filter/types.js.map +1 -1
- package/dist/hooks/index.d.ts +22 -31
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.js +33 -37
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/transition-guard.d.ts +61 -0
- package/dist/hooks/transition-guard.d.ts.map +1 -0
- package/dist/hooks/transition-guard.js +93 -0
- package/dist/hooks/transition-guard.js.map +1 -0
- package/dist/index.d.ts +52 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +105 -51
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +4 -2
- package/dist/internal.d.ts.map +1 -1
- package/dist/internal.js +16 -0
- package/dist/internal.js.map +1 -1
- package/dist/lib/case-utils.d.ts +0 -1
- package/dist/lib/case-utils.d.ts.map +1 -1
- package/dist/lib/case-utils.js +0 -3
- package/dist/lib/case-utils.js.map +1 -1
- package/dist/lib/client-safe-error.d.ts +9 -0
- package/dist/lib/client-safe-error.d.ts.map +1 -0
- package/dist/lib/client-safe-error.js +23 -0
- package/dist/lib/client-safe-error.js.map +1 -0
- package/dist/lib/database-errors.d.ts +78 -0
- package/dist/lib/database-errors.d.ts.map +1 -0
- package/dist/lib/database-errors.js +148 -0
- package/dist/lib/database-errors.js.map +1 -0
- package/dist/lib/null-prototype-registry.d.ts +9 -0
- package/dist/lib/null-prototype-registry.d.ts.map +1 -0
- package/dist/lib/null-prototype-registry.js +11 -0
- package/dist/lib/null-prototype-registry.js.map +1 -0
- package/dist/lib/prisma-errors.d.ts +30 -32
- package/dist/lib/prisma-errors.d.ts.map +1 -1
- package/dist/lib/prisma-errors.js +86 -46
- package/dist/lib/prisma-errors.js.map +1 -1
- package/dist/lib/process-global.d.ts +23 -0
- package/dist/lib/process-global.d.ts.map +1 -0
- package/dist/lib/process-global.js +33 -0
- package/dist/lib/process-global.js.map +1 -0
- package/dist/lib/warn-once.d.ts +7 -0
- package/dist/lib/warn-once.d.ts.map +1 -0
- package/dist/lib/warn-once.js +13 -0
- package/dist/lib/warn-once.js.map +1 -0
- package/dist/mcp/advertise.d.ts +16 -0
- package/dist/mcp/advertise.d.ts.map +1 -0
- package/dist/mcp/advertise.js +25 -0
- package/dist/mcp/advertise.js.map +1 -0
- package/dist/mcp/arguments.d.ts +10 -0
- package/dist/mcp/arguments.d.ts.map +1 -0
- package/dist/mcp/arguments.js +143 -0
- package/dist/mcp/arguments.js.map +1 -0
- package/dist/mcp/field-schema.d.ts +50 -5
- package/dist/mcp/field-schema.d.ts.map +1 -1
- package/dist/mcp/field-schema.js +137 -14
- package/dist/mcp/field-schema.js.map +1 -1
- package/dist/mcp/handler.d.ts +16 -5
- package/dist/mcp/handler.d.ts.map +1 -1
- package/dist/mcp/handler.js +404 -126
- package/dist/mcp/handler.js.map +1 -1
- package/dist/mcp/index.d.ts +1 -0
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +1 -0
- package/dist/mcp/index.js.map +1 -1
- package/dist/mcp/projection.d.ts +42 -34
- package/dist/mcp/projection.d.ts.map +1 -1
- package/dist/mcp/projection.js +246 -200
- package/dist/mcp/projection.js.map +1 -1
- package/dist/mcp/tool-error.d.ts +8 -0
- package/dist/mcp/tool-error.d.ts.map +1 -0
- package/dist/mcp/tool-error.js +11 -0
- package/dist/mcp/tool-error.js.map +1 -0
- package/dist/mcp/types.d.ts +3 -2
- package/dist/mcp/types.d.ts.map +1 -1
- package/dist/mcp/where-id-boundary.d.ts +28 -0
- package/dist/mcp/where-id-boundary.d.ts.map +1 -0
- package/dist/mcp/where-id-boundary.js +187 -0
- package/dist/mcp/where-id-boundary.js.map +1 -0
- package/dist/origin.d.ts +147 -0
- package/dist/origin.d.ts.map +1 -0
- package/dist/origin.js +191 -0
- package/dist/origin.js.map +1 -0
- package/dist/query/relationship-options.d.ts +35 -6
- package/dist/query/relationship-options.d.ts.map +1 -1
- package/dist/query/relationship-options.js +33 -23
- package/dist/query/relationship-options.js.map +1 -1
- package/dist/secured/aggregate.d.ts +32 -0
- package/dist/secured/aggregate.d.ts.map +1 -0
- package/dist/secured/aggregate.js +31 -0
- package/dist/secured/aggregate.js.map +1 -0
- package/dist/secured/include.d.ts +320 -0
- package/dist/secured/include.d.ts.map +1 -0
- package/dist/secured/include.js +650 -0
- package/dist/secured/include.js.map +1 -0
- package/dist/secured/lock.d.ts +127 -0
- package/dist/secured/lock.d.ts.map +1 -0
- package/dist/secured/lock.js +275 -0
- package/dist/secured/lock.js.map +1 -0
- package/dist/secured/lower.d.ts +82 -0
- package/dist/secured/lower.d.ts.map +1 -0
- package/dist/secured/lower.js +162 -0
- package/dist/secured/lower.js.map +1 -0
- package/dist/secured/operators.d.ts +7 -0
- package/dist/secured/operators.d.ts.map +1 -0
- package/dist/secured/operators.js +18 -0
- package/dist/secured/operators.js.map +1 -0
- package/dist/secured/read.d.ts +208 -0
- package/dist/secured/read.d.ts.map +1 -0
- package/dist/secured/read.js +1245 -0
- package/dist/secured/read.js.map +1 -0
- package/dist/secured/select.d.ts +87 -0
- package/dist/secured/select.d.ts.map +1 -0
- package/dist/secured/select.js +250 -0
- package/dist/secured/select.js.map +1 -0
- package/dist/secured/vector.d.ts +58 -0
- package/dist/secured/vector.d.ts.map +1 -0
- package/dist/secured/vector.js +113 -0
- package/dist/secured/vector.js.map +1 -0
- package/dist/secured/vocabulary.d.ts +227 -0
- package/dist/secured/vocabulary.d.ts.map +1 -0
- package/dist/secured/vocabulary.js +578 -0
- package/dist/secured/vocabulary.js.map +1 -0
- package/dist/secured/write.d.ts +58 -0
- package/dist/secured/write.d.ts.map +1 -0
- package/dist/secured/write.js +87 -0
- package/dist/secured/write.js.map +1 -0
- package/dist/testing/context.d.ts +236 -0
- package/dist/testing/context.d.ts.map +1 -0
- package/dist/testing/context.js +457 -0
- package/dist/testing/context.js.map +1 -0
- package/dist/testing/escape.d.ts +83 -0
- package/dist/testing/escape.d.ts.map +1 -0
- package/dist/testing/escape.js +148 -0
- package/dist/testing/escape.js.map +1 -0
- package/dist/testing/extensions.d.ts +48 -0
- package/dist/testing/extensions.d.ts.map +1 -0
- package/dist/testing/extensions.js +107 -0
- package/dist/testing/extensions.js.map +1 -0
- package/dist/testing/index.d.ts +5 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +11 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/plans.d.ts +64 -0
- package/dist/testing/plans.d.ts.map +1 -0
- package/dist/testing/plans.js +47 -0
- package/dist/testing/plans.js.map +1 -0
- package/dist/types/context.d.ts +110 -0
- package/dist/types/context.d.ts.map +1 -0
- package/dist/types/context.js +2 -0
- package/dist/types/context.js.map +1 -0
- package/dist/types/contract.d.ts +198 -0
- package/dist/types/contract.d.ts.map +1 -0
- package/dist/types/contract.js +2 -0
- package/dist/types/contract.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +11 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/inputs.d.ts +83 -0
- package/dist/types/inputs.d.ts.map +1 -0
- package/dist/types/inputs.js +2 -0
- package/dist/types/inputs.js.map +1 -0
- package/dist/types/rows.d.ts +76 -0
- package/dist/types/rows.d.ts.map +1 -0
- package/dist/types/rows.js +2 -0
- package/dist/types/rows.js.map +1 -0
- package/dist/types/secured-list.d.ts +396 -0
- package/dist/types/secured-list.d.ts.map +1 -0
- package/dist/types/secured-list.js +2 -0
- package/dist/types/secured-list.js.map +1 -0
- package/dist/unsafe.d.ts +162 -0
- package/dist/unsafe.d.ts.map +1 -0
- package/dist/unsafe.js +177 -0
- package/dist/unsafe.js.map +1 -0
- package/dist/validation/config-refusal.d.ts +16 -0
- package/dist/validation/config-refusal.d.ts.map +1 -0
- package/dist/validation/config-refusal.js +2 -0
- package/dist/validation/config-refusal.js.map +1 -0
- package/dist/validation/database-config.d.ts +19 -0
- package/dist/validation/database-config.d.ts.map +1 -0
- package/dist/validation/database-config.js +237 -0
- package/dist/validation/database-config.js.map +1 -0
- package/dist/validation/default-value.d.ts +21 -0
- package/dist/validation/default-value.d.ts.map +1 -0
- package/dist/validation/default-value.js +72 -0
- package/dist/validation/default-value.js.map +1 -0
- package/dist/validation/extension-packs.d.ts +11 -0
- package/dist/validation/extension-packs.d.ts.map +1 -0
- package/dist/validation/extension-packs.js +53 -0
- package/dist/validation/extension-packs.js.map +1 -0
- package/dist/validation/field-config.d.ts +35 -21
- package/dist/validation/field-config.d.ts.map +1 -1
- package/dist/validation/field-config.js +51 -41
- package/dist/validation/field-config.js.map +1 -1
- package/dist/validation/field-names.d.ts +15 -0
- package/dist/validation/field-names.d.ts.map +1 -0
- package/dist/validation/field-names.js +155 -0
- package/dist/validation/field-names.js.map +1 -0
- package/dist/validation/identifier-names.d.ts +4 -0
- package/dist/validation/identifier-names.d.ts.map +1 -0
- package/dist/validation/identifier-names.js +39 -0
- package/dist/validation/identifier-names.js.map +1 -0
- package/dist/validation/needs-closure.d.ts +20 -30
- package/dist/validation/needs-closure.d.ts.map +1 -1
- package/dist/validation/needs-closure.js +29 -105
- package/dist/validation/needs-closure.js.map +1 -1
- package/dist/validation/relations.d.ts +17 -0
- package/dist/validation/relations.d.ts.map +1 -0
- package/dist/validation/relations.js +268 -0
- package/dist/validation/relations.js.map +1 -0
- package/dist/validation/schema.d.ts.map +1 -1
- package/dist/validation/schema.js +1 -3
- package/dist/validation/schema.js.map +1 -1
- package/dist/where/like.d.ts +32 -0
- package/dist/where/like.d.ts.map +1 -0
- package/dist/where/like.js +42 -0
- package/dist/where/like.js.map +1 -0
- package/package.json +70 -8
- package/.turbo/turbo-build.log +0 -4
- package/CLAUDE.md +0 -609
- package/dist/access/access-filter.d.ts +0 -241
- package/dist/access/access-filter.d.ts.map +0 -1
- package/dist/access/access-filter.js +0 -692
- package/dist/access/access-filter.js.map +0 -1
- package/dist/access/access-filter.test.d.ts +0 -2
- package/dist/access/access-filter.test.d.ts.map +0 -1
- package/dist/access/access-filter.test.js +0 -1256
- package/dist/access/access-filter.test.js.map +0 -1
- package/dist/access/declared-dependencies.test.d.ts +0 -2
- package/dist/access/declared-dependencies.test.d.ts.map +0 -1
- package/dist/access/declared-dependencies.test.js +0 -226
- package/dist/access/declared-dependencies.test.js.map +0 -1
- package/dist/access/denied-relation-visibility.test.d.ts +0 -2
- package/dist/access/denied-relation-visibility.test.d.ts.map +0 -1
- package/dist/access/denied-relation-visibility.test.js +0 -114
- package/dist/access/denied-relation-visibility.test.js.map +0 -1
- package/dist/access/engine.test.d.ts +0 -2
- package/dist/access/engine.test.d.ts.map +0 -1
- package/dist/access/engine.test.js +0 -56
- package/dist/access/engine.test.js.map +0 -1
- package/dist/access/field-access.test.d.ts +0 -2
- package/dist/access/field-access.test.d.ts.map +0 -1
- package/dist/access/field-access.test.js +0 -635
- package/dist/access/field-access.test.js.map +0 -1
- package/dist/access/multi-column-read-write.test.d.ts +0 -2
- package/dist/access/multi-column-read-write.test.d.ts.map +0 -1
- package/dist/access/multi-column-read-write.test.js +0 -183
- package/dist/access/multi-column-read-write.test.js.map +0 -1
- package/dist/access/relationship-count.d.ts +0 -98
- package/dist/access/relationship-count.d.ts.map +0 -1
- package/dist/access/relationship-count.js +0 -262
- package/dist/access/relationship-count.js.map +0 -1
- package/dist/access/relationship-count.test.d.ts +0 -2
- package/dist/access/relationship-count.test.d.ts.map +0 -1
- package/dist/access/relationship-count.test.js +0 -247
- package/dist/access/relationship-count.test.js.map +0 -1
- package/dist/access/relationship-label-filter.d.ts +0 -45
- package/dist/access/relationship-label-filter.d.ts.map +0 -1
- package/dist/access/relationship-label-filter.js +0 -22
- package/dist/access/relationship-label-filter.js.map +0 -1
- package/dist/access/relationship-label-filter.test.d.ts +0 -2
- package/dist/access/relationship-label-filter.test.d.ts.map +0 -1
- package/dist/access/relationship-label-filter.test.js +0 -89
- package/dist/access/relationship-label-filter.test.js.map +0 -1
- package/dist/access/synthetic-include-read.test.d.ts +0 -2
- package/dist/access/synthetic-include-read.test.d.ts.map +0 -1
- package/dist/access/synthetic-include-read.test.js +0 -121
- package/dist/access/synthetic-include-read.test.js.map +0 -1
- package/dist/context/nested-operations.d.ts +0 -74
- package/dist/context/nested-operations.d.ts.map +0 -1
- package/dist/context/nested-operations.js +0 -847
- package/dist/context/nested-operations.js.map +0 -1
- package/dist/fields/calendar-day.test.d.ts +0 -2
- package/dist/fields/calendar-day.test.d.ts.map +0 -1
- package/dist/fields/calendar-day.test.js +0 -246
- package/dist/fields/calendar-day.test.js.map +0 -1
- package/dist/fields/format-prisma-default.d.ts +0 -35
- package/dist/fields/format-prisma-default.d.ts.map +0 -1
- package/dist/fields/format-prisma-default.js +0 -49
- package/dist/fields/format-prisma-default.js.map +0 -1
- package/dist/fields/format-prisma-default.test.d.ts +0 -2
- package/dist/fields/format-prisma-default.test.d.ts.map +0 -1
- package/dist/fields/format-prisma-default.test.js +0 -54
- package/dist/fields/format-prisma-default.test.js.map +0 -1
- package/dist/fields/relationship-foreign-key.test.d.ts +0 -2
- package/dist/fields/relationship-foreign-key.test.d.ts.map +0 -1
- package/dist/fields/relationship-foreign-key.test.js +0 -18
- package/dist/fields/relationship-foreign-key.test.js.map +0 -1
- package/dist/fields/select.test.d.ts +0 -2
- package/dist/fields/select.test.d.ts.map +0 -1
- package/dist/fields/select.test.js +0 -279
- package/dist/fields/select.test.js.map +0 -1
- package/dist/fields/text-keystone-compat.test.d.ts +0 -2
- package/dist/fields/text-keystone-compat.test.d.ts.map +0 -1
- package/dist/fields/text-keystone-compat.test.js +0 -93
- package/dist/fields/text-keystone-compat.test.js.map +0 -1
- package/dist/filter/filter.test.d.ts +0 -2
- package/dist/filter/filter.test.d.ts.map +0 -1
- package/dist/filter/filter.test.js +0 -431
- package/dist/filter/filter.test.js.map +0 -1
- package/dist/filter/serialize.test.d.ts +0 -2
- package/dist/filter/serialize.test.d.ts.map +0 -1
- package/dist/filter/serialize.test.js +0 -83
- package/dist/filter/serialize.test.js.map +0 -1
- package/dist/index.test.d.ts +0 -2
- package/dist/index.test.d.ts.map +0 -1
- package/dist/index.test.js +0 -33
- package/dist/index.test.js.map +0 -1
- package/dist/lib/case-utils.test.d.ts +0 -2
- package/dist/lib/case-utils.test.d.ts.map +0 -1
- package/dist/lib/case-utils.test.js +0 -123
- package/dist/lib/case-utils.test.js.map +0 -1
- package/dist/query/index.d.ts +0 -127
- package/dist/query/index.d.ts.map +0 -1
- package/dist/query/index.js +0 -179
- package/dist/query/index.js.map +0 -1
- package/dist/query/index.test.d.ts +0 -2
- package/dist/query/index.test.d.ts.map +0 -1
- package/dist/query/index.test.js +0 -632
- package/dist/query/index.test.js.map +0 -1
- package/dist/query/relationship-options.test.d.ts +0 -2
- package/dist/query/relationship-options.test.d.ts.map +0 -1
- package/dist/query/relationship-options.test.js +0 -150
- package/dist/query/relationship-options.test.js.map +0 -1
- package/dist/validation/field-config.test.d.ts +0 -2
- package/dist/validation/field-config.test.d.ts.map +0 -1
- package/dist/validation/field-config.test.js +0 -159
- package/dist/validation/field-config.test.js.map +0 -1
- package/dist/validation/schema.test.d.ts +0 -2
- package/dist/validation/schema.test.d.ts.map +0 -1
- package/dist/validation/schema.test.js +0 -409
- package/dist/validation/schema.test.js.map +0 -1
- package/docs/type-distribution-fix.md +0 -136
- package/src/access/access-filter.test.ts +0 -1919
- package/src/access/access-filter.ts +0 -1041
- package/src/access/declared-dependencies.test.ts +0 -277
- package/src/access/declared-dependencies.ts +0 -220
- package/src/access/denied-relation-visibility.test.ts +0 -173
- package/src/access/depth-limits.ts +0 -27
- package/src/access/engine.test.ts +0 -67
- package/src/access/engine.ts +0 -193
- package/src/access/errors.ts +0 -242
- package/src/access/field-access.test.ts +0 -792
- package/src/access/field-access.ts +0 -353
- package/src/access/field-transforms.ts +0 -68
- package/src/access/field-visibility.ts +0 -569
- package/src/access/index.ts +0 -82
- package/src/access/multi-column-read-write.test.ts +0 -305
- package/src/access/query-validation.ts +0 -486
- package/src/access/relationship-count.test.ts +0 -344
- package/src/access/relationship-count.ts +0 -346
- package/src/access/relationship-label-filter.test.ts +0 -128
- package/src/access/relationship-label-filter.ts +0 -63
- package/src/access/synthetic-include-read.test.ts +0 -179
- package/src/access/transaction-registry.ts +0 -58
- package/src/access/types.ts +0 -391
- package/src/config/index.ts +0 -177
- package/src/config/label.ts +0 -51
- package/src/config/nav-count.ts +0 -77
- package/src/config/plugin-engine.ts +0 -261
- package/src/config/types.ts +0 -3270
- package/src/context/apply-defaults.ts +0 -47
- package/src/context/hook-pipeline.ts +0 -137
- package/src/context/index.ts +0 -1737
- package/src/context/nested-operations.ts +0 -1470
- package/src/context/transaction-boundary.ts +0 -485
- package/src/context/write-pipeline.ts +0 -732
- package/src/extend.ts +0 -28
- package/src/fields/calendar-day.test.ts +0 -291
- package/src/fields/format-prisma-default.test.ts +0 -64
- package/src/fields/format-prisma-default.ts +0 -64
- package/src/fields/index.ts +0 -1901
- package/src/fields/relationship-foreign-key.test.ts +0 -29
- package/src/fields/select.test.ts +0 -336
- package/src/fields/text-keystone-compat.test.ts +0 -126
- package/src/filter/collect.ts +0 -108
- package/src/filter/filter.test.ts +0 -538
- package/src/filter/index.ts +0 -26
- package/src/filter/map.ts +0 -63
- package/src/filter/parse.ts +0 -99
- package/src/filter/serialize.test.ts +0 -115
- package/src/filter/serialize.ts +0 -73
- package/src/filter/types.ts +0 -120
- package/src/hooks/index.ts +0 -752
- package/src/index.test.ts +0 -50
- package/src/index.ts +0 -192
- package/src/internal.ts +0 -61
- package/src/lib/case-utils.test.ts +0 -159
- package/src/lib/case-utils.ts +0 -68
- package/src/lib/prisma-errors.ts +0 -86
- package/src/mcp/constants.ts +0 -24
- package/src/mcp/field-schema.ts +0 -84
- package/src/mcp/handler.ts +0 -633
- package/src/mcp/index.ts +0 -7
- package/src/mcp/projection.ts +0 -497
- package/src/mcp/types.ts +0 -18
- package/src/query/index.test.ts +0 -830
- package/src/query/index.ts +0 -352
- package/src/query/relationship-options.test.ts +0 -194
- package/src/query/relationship-options.ts +0 -78
- package/src/utils/password.ts +0 -80
- package/src/validation/field-config.test.ts +0 -199
- package/src/validation/field-config.ts +0 -139
- package/src/validation/needs-closure.ts +0 -188
- package/src/validation/schema.test.ts +0 -487
- package/src/validation/schema.ts +0 -51
- package/tests/access-relationships.test.ts +0 -646
- package/tests/access.test.ts +0 -590
- package/tests/apply-defaults.test.ts +0 -119
- package/tests/bare-read-scalars.test.ts +0 -147
- package/tests/computed-field-selective-evaluation.test.ts +0 -418
- package/tests/config.test.ts +0 -242
- package/tests/context.test.ts +0 -3146
- package/tests/default-value-create.test.ts +0 -299
- package/tests/field-hooks.test.ts +0 -410
- package/tests/field-types.test.ts +0 -1291
- package/tests/hook-context-secured.test.ts +0 -424
- package/tests/hook-pipeline.test.ts +0 -233
- package/tests/hooks.test.ts +0 -481
- package/tests/interactive-transaction.test.ts +0 -444
- package/tests/label.test.ts +0 -135
- package/tests/mcp-fields-projection-access.test.ts +0 -250
- package/tests/mcp-handler.test.ts +0 -1411
- package/tests/multi-column-validation-ordering.test.ts +0 -305
- package/tests/nav-count.test.ts +0 -189
- package/tests/needs-declared-dependencies.test.ts +0 -503
- package/tests/nested-access-and-hooks.test.ts +0 -2566
- package/tests/nested-operation-registry.test.ts +0 -317
- package/tests/nested-write-hooks.test.ts +0 -864
- package/tests/nested-write-synthetic-relation.test.ts +0 -437
- package/tests/password-type-distribution.test.ts +0 -154
- package/tests/password-types.test.ts +0 -174
- package/tests/password.test.ts +0 -263
- package/tests/plugin-engine.test.ts +0 -1155
- package/tests/prisma-errors.test.ts +0 -141
- package/tests/resolve-chain.test.ts +0 -411
- package/tests/singleton.test.ts +0 -421
- package/tests/sudo.test.ts +0 -676
- package/tests/to-one-include-access-filter.test.ts +0 -172
- package/tests/transaction-boundary-hooks.test.ts +0 -710
- package/tests/transaction-boundary-outermost.test.ts +0 -530
- package/tests/virtual-fields.test.ts +0 -421
- package/tests/with-session.test.ts +0 -243
- package/tests/write-pipeline.test.ts +0 -617
- package/tsconfig.json +0 -12
- package/tsconfig.tsbuildinfo +0 -1
- package/vitest.config.ts +0 -69
|
@@ -0,0 +1,1245 @@
|
|
|
1
|
+
// The secured read surface: `context.db.<List>` as an opaque wrapper over a
|
|
2
|
+
// Prisma 8 collection, its `where`/`orderBy` composition, and the
|
|
3
|
+
// `all()`/`first()`/`nearest()`/`aggregate()` terminals the engine owns. See ADR-0041,
|
|
4
|
+
// ADR-0044, ADR-0045, ADR-0046, ADR-0055 and ADR-0058.
|
|
5
|
+
import { assertAccessFilterConstrains, checkAccess, checkFieldAccess, filterReadableFields, getRelatedListConfig, } from '../access/index.js';
|
|
6
|
+
import { withOrigin } from '../origin.js';
|
|
7
|
+
import { lowerOrder, lowerWhere, vectorLowering, whereCombinators, } from './lower.js';
|
|
8
|
+
import { resolveColumns, resolveNearest, resolveOrderBy, resolveRelatedAccessPlan, assertWhereWithinLimits, resolveWhere, unqueryableKey, } from './vocabulary.js';
|
|
9
|
+
import { buildIncludeRequest, DECLARED_COUNT_BRANCH_KEY, DECLARED_ROWS_BRANCH_KEY, foreignKeyOwningRelations, orderList, resolveIncludes, } from './include.js';
|
|
10
|
+
import { dependencyAdditions, hasRowsBranch, reducedDeclaredKeys, resolveProjection, selectionScope, } from './select.js';
|
|
11
|
+
import { getListDependencies, } from '../access/declared-dependencies.js';
|
|
12
|
+
import { aggregations, checkSpec, specKeys, zeroed } from './aggregate.js';
|
|
13
|
+
import { distanceToScore, requireVector, vectorDistance } from './vector.js';
|
|
14
|
+
import { ROW_LOCK_MAX_KEYS, RowLockIdentityError, RowLockKeyLimitExceededError, RowLockUnavailableError, } from './lock.js';
|
|
15
|
+
import { ValidationError } from '../hooks/index.js';
|
|
16
|
+
export { AccessFilterRecursionError, ACCESS_FILTER_MAX_DEPTH } from './vocabulary.js';
|
|
17
|
+
export { NEAREST_DEFAULT_LIMIT } from './vocabulary.js';
|
|
18
|
+
export { DuplicateIncludeError, InvalidCombineBranchError, InvalidRefinementError, MultipleCombineRowBranchesError, ReducedToOneIncludeError, ReservedCombineKeyError, UnreducibleRefinementError, } from './include.js';
|
|
19
|
+
export { RelationSelectError } from './select.js';
|
|
20
|
+
export { ROW_LOCK_MAX_KEYS, RowLockIdentityError, RowLockKeyLimitExceededError, RowLockLaneUnavailableError, RowLockUnavailableError, } from './lock.js';
|
|
21
|
+
export { VectorDecodeError } from './vector.js';
|
|
22
|
+
/**
|
|
23
|
+
* Thrown when the ORM client carries no collection for a list the config
|
|
24
|
+
* declares — a generation or wiring fault rather than an access denial, so it
|
|
25
|
+
* is reported rather than silently read as an empty result.
|
|
26
|
+
*/
|
|
27
|
+
export class SecuredCollectionMissingError extends Error {
|
|
28
|
+
listName;
|
|
29
|
+
constructor(listName) {
|
|
30
|
+
super(`The ORM client has no collection for list "${listName}". Re-run \`opensaas generate\` so ` +
|
|
31
|
+
`the emitted contract matches the config.`);
|
|
32
|
+
this.listName = listName;
|
|
33
|
+
this.name = 'SecuredCollectionMissingError';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Presence, not completeness — the same rule `isDelegate` states in
|
|
38
|
+
* `access/orm-client.ts`, and for the same reason. A test double implements
|
|
39
|
+
* only the operations its test reaches, so requiring the full set would refuse
|
|
40
|
+
* a client the engine can drive, and would refuse it with advice ("re-run
|
|
41
|
+
* `opensaas generate`") that does not describe what is actually wrong. A
|
|
42
|
+
* member that is genuinely absent surfaces at its own call site.
|
|
43
|
+
*/
|
|
44
|
+
function isReadableCollection(value) {
|
|
45
|
+
return typeof value === 'object' && value !== null;
|
|
46
|
+
}
|
|
47
|
+
function collectionFor(ormHandle, listName) {
|
|
48
|
+
const collection = ormHandle[listName];
|
|
49
|
+
if (!isReadableCollection(collection))
|
|
50
|
+
throw new SecuredCollectionMissingError(listName);
|
|
51
|
+
return collection;
|
|
52
|
+
}
|
|
53
|
+
/** Whether the plan actually carries this member — an empty one refuses nothing. */
|
|
54
|
+
function carried(value) {
|
|
55
|
+
if (Array.isArray(value))
|
|
56
|
+
return value.length > 0;
|
|
57
|
+
return value !== undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Refuse every member this terminal marked `refused` that the plan carries,
|
|
61
|
+
* naming all of them at once.
|
|
62
|
+
*
|
|
63
|
+
* It takes the resolved plan rather than the composed state, so it runs after
|
|
64
|
+
* the operation-access check for the reason {@link resolvePlan} orders its own
|
|
65
|
+
* work that way: a denied caller gets the Silent failure and never learns
|
|
66
|
+
* which members the read composed (#912, #915).
|
|
67
|
+
*/
|
|
68
|
+
function refuseCarried(plan, dispositions, refuse) {
|
|
69
|
+
const table = dispositions;
|
|
70
|
+
const refused = Object.entries(plan)
|
|
71
|
+
.filter(([member, value]) => table[member] === 'refused' && carried(value))
|
|
72
|
+
.map(([member]) => member);
|
|
73
|
+
if (refused.length > 0)
|
|
74
|
+
refuse(refused.sort());
|
|
75
|
+
}
|
|
76
|
+
function resolveContext(binding, secured) {
|
|
77
|
+
return {
|
|
78
|
+
listName: binding.listName,
|
|
79
|
+
listConfig: binding.listConfig,
|
|
80
|
+
config: binding.config,
|
|
81
|
+
session: binding.context.session,
|
|
82
|
+
context: binding.context,
|
|
83
|
+
checkFieldRead: secured,
|
|
84
|
+
applyRelationAccess: secured,
|
|
85
|
+
accessFilterPath: [],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Resolve the read: operation access first, then the vocabulary.
|
|
90
|
+
*
|
|
91
|
+
* The order matters. Resolution names the offending key, so running it before
|
|
92
|
+
* the access check would tell a caller with no access at all that a field
|
|
93
|
+
* exists and whether it is read-gated (#912, #915). A denied caller gets the
|
|
94
|
+
* Silent failure and never sees a validation error.
|
|
95
|
+
*
|
|
96
|
+
* `sudo` skips access but not the vocabulary: an unknown key or operator is a
|
|
97
|
+
* bug rather than a permission, and letting it through would widen the read
|
|
98
|
+
* with a flag on it (ADR-0022, ADR-0055).
|
|
99
|
+
*/
|
|
100
|
+
async function resolvePlan(binding, state) {
|
|
101
|
+
const { listConfig, context } = binding;
|
|
102
|
+
const session = context.session;
|
|
103
|
+
const secured = context._isSudo !== true;
|
|
104
|
+
const ctx = resolveContext(binding, secured);
|
|
105
|
+
const access = secured
|
|
106
|
+
? await checkAccess(listConfig.access?.operation?.query, { session, context })
|
|
107
|
+
: true;
|
|
108
|
+
if (access === false)
|
|
109
|
+
return null;
|
|
110
|
+
const predicates = [];
|
|
111
|
+
for (const predicate of state.predicates) {
|
|
112
|
+
assertWhereWithinLimits(predicate);
|
|
113
|
+
predicates.push(await resolveWhere(predicate, ctx));
|
|
114
|
+
}
|
|
115
|
+
const orders = await resolveOrderBy(state.orders, ctx);
|
|
116
|
+
const projection = await resolveProjection(state.fields, state.includes.map((request) => request.name), ctx);
|
|
117
|
+
const includes = await resolveIncludes(state.includes, ctx, 0, projection.caller);
|
|
118
|
+
const request = onlyDistinct(binding.listName, state.distincts);
|
|
119
|
+
const distinct = request === undefined
|
|
120
|
+
? undefined
|
|
121
|
+
: {
|
|
122
|
+
kind: request.kind,
|
|
123
|
+
columns: await resolveColumns(request.fields, ctx, request.kind === 'on' ? 'distinctOn' : 'distinct'),
|
|
124
|
+
};
|
|
125
|
+
if (distinct?.kind === 'on') {
|
|
126
|
+
requireDistinctOnOrder(binding.listName, distinct.columns, orders);
|
|
127
|
+
}
|
|
128
|
+
const cursor = state.cursor === undefined ? undefined : resolveCursor(binding.listName, state.cursor, orders);
|
|
129
|
+
if (access !== true) {
|
|
130
|
+
const filter = access;
|
|
131
|
+
assertAccessFilterConstrains(filter);
|
|
132
|
+
// The Access Filter is trusted config, so its keys are not read-gated —
|
|
133
|
+
// but it is lowered through the same total seam, which is what stops a
|
|
134
|
+
// rule that resolved to `undefined` from matching every row.
|
|
135
|
+
predicates.push(await resolveWhere(filter, {
|
|
136
|
+
...resolveContext(binding, true),
|
|
137
|
+
checkFieldRead: false,
|
|
138
|
+
accessFilterPath: [binding.listName],
|
|
139
|
+
}));
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
predicates,
|
|
143
|
+
orders,
|
|
144
|
+
includes,
|
|
145
|
+
projection,
|
|
146
|
+
selection: selectionScope(projection, includes),
|
|
147
|
+
additions: dependencyAdditions(includes),
|
|
148
|
+
reducedDeclared: reducedDeclaredKeys(includes),
|
|
149
|
+
limit: state.limit,
|
|
150
|
+
offset: state.offset,
|
|
151
|
+
...(distinct ? { distinct } : {}),
|
|
152
|
+
...(cursor ? { cursor } : {}),
|
|
153
|
+
...(state.lock ? { forUpdate: true } : {}),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function requireOrder(listName, orders, member) {
|
|
157
|
+
if (orders.length > 0)
|
|
158
|
+
return;
|
|
159
|
+
throw new ValidationError([
|
|
160
|
+
`Cannot ${member} "${listName}" without an orderBy — it has no order to resume or keep the ` +
|
|
161
|
+
`first row of.`,
|
|
162
|
+
]);
|
|
163
|
+
}
|
|
164
|
+
function quoted(names) {
|
|
165
|
+
return [...names].map((name) => `"${name}"`).join(', ');
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* The one `distinct` a read may carry.
|
|
169
|
+
*
|
|
170
|
+
* `where`, `orderBy` and `include` accumulate because each composes with what
|
|
171
|
+
* came before. Two distincts do not: `distinct` and `distinctOn` collapse rows
|
|
172
|
+
* by different rules, and the variadic form already spells "on both columns"
|
|
173
|
+
* in a single call. So a second one is refused at the terminal — where every
|
|
174
|
+
* other refusal on this surface is made — rather than replacing the first.
|
|
175
|
+
*/
|
|
176
|
+
function onlyDistinct(listName, requests) {
|
|
177
|
+
if (requests.length <= 1)
|
|
178
|
+
return requests[0];
|
|
179
|
+
const composed = requests
|
|
180
|
+
.map((request) => `${request.kind === 'on' ? 'distinctOn' : 'distinct'}(${quoted(request.fields)})`)
|
|
181
|
+
.join(' and ');
|
|
182
|
+
throw new ValidationError([
|
|
183
|
+
`Cannot read "${listName}" through more than one distinct — it composed ${composed}. Name ` +
|
|
184
|
+
`every column in one call instead.`,
|
|
185
|
+
]);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Refuse a `distinctOn` the read's sort does not lead with.
|
|
189
|
+
*
|
|
190
|
+
* Postgres requires the `DISTINCT ON` expressions to match the leftmost
|
|
191
|
+
* `ORDER BY` expressions and raises `42P10` otherwise
|
|
192
|
+
* (https://www.postgresql.org/docs/current/sql-select.html#SQL-DISTINCT), so
|
|
193
|
+
* a merely-present order is not enough. Position within the leading group does
|
|
194
|
+
* not matter to Postgres, which is why this compares the group as a set.
|
|
195
|
+
*/
|
|
196
|
+
function requireDistinctOnOrder(listName, columns, orders) {
|
|
197
|
+
requireOrder(listName, orders, 'distinctOn');
|
|
198
|
+
const named = new Set(columns.map((column) => column.column));
|
|
199
|
+
const leading = orders.slice(0, named.size).map((order) => order.column);
|
|
200
|
+
if (leading.length === named.size && leading.every((column) => named.has(column)))
|
|
201
|
+
return;
|
|
202
|
+
throw new ValidationError([
|
|
203
|
+
`Cannot distinctOn "${listName}" by ${quoted(named)} — it keeps the first row per key in the ` +
|
|
204
|
+
`order the read established, so the orderBy has to lead with those columns and this one ` +
|
|
205
|
+
`leads with ${quoted(orders.map((order) => order.column))}.`,
|
|
206
|
+
]);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Resolve a cursor's keys against the sort the read already established.
|
|
210
|
+
*
|
|
211
|
+
* A key the active `orderBy` does not name is refused with the message an
|
|
212
|
+
* undeclared key gets. That is what makes the refusal indistinguishable: a
|
|
213
|
+
* read-denied column never reaches a resolved order in the first place
|
|
214
|
+
* (`resolveOrderBy` refuses it there), so an undeclared column, a denied one
|
|
215
|
+
* and one the caller simply did not sort by all answer the same way
|
|
216
|
+
* (ADR-0031).
|
|
217
|
+
*/
|
|
218
|
+
function isPlainStructure(value) {
|
|
219
|
+
if (Array.isArray(value))
|
|
220
|
+
return true;
|
|
221
|
+
if (typeof value !== 'object' || value === null)
|
|
222
|
+
return false;
|
|
223
|
+
const proto = Object.getPrototypeOf(value);
|
|
224
|
+
return proto === Object.prototype || proto === null;
|
|
225
|
+
}
|
|
226
|
+
function resolveCursor(listName, values, orders) {
|
|
227
|
+
requireOrder(listName, orders, 'cursor');
|
|
228
|
+
const sorted = new Set(orders.map((order) => order.column));
|
|
229
|
+
for (const key of Object.keys(values)) {
|
|
230
|
+
if (!sorted.has(key))
|
|
231
|
+
throw unqueryableKey(listName, key);
|
|
232
|
+
if (isPlainStructure(values[key])) {
|
|
233
|
+
throw new ValidationError([
|
|
234
|
+
`Cannot page "${listName}" — the cursor value for "${key}" must be a scalar.`,
|
|
235
|
+
]);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return values;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Apply one resolved include to the refinement collection Prisma hands the
|
|
242
|
+
* callback. Every predicate is a separate `where` for the same reason the
|
|
243
|
+
* top-level ones are: the ORM ANDs them natively, so the Access Filter stays
|
|
244
|
+
* a filter entry beside the caller's rather than something hand-merged into
|
|
245
|
+
* it (ADR-0044).
|
|
246
|
+
*/
|
|
247
|
+
function refine(collection, plan, ops) {
|
|
248
|
+
let refined = collection;
|
|
249
|
+
for (const predicate of plan.predicates) {
|
|
250
|
+
refined = refined.where((model) => lowerWhere(predicate, model, ops));
|
|
251
|
+
}
|
|
252
|
+
// A reduction replaces the rows, so nothing below it applies: `resolveReduce`
|
|
253
|
+
// refuses a refinement that composed anything a count cannot honour.
|
|
254
|
+
if (plan.reduce !== undefined) {
|
|
255
|
+
if (plan.declaredRows === undefined) {
|
|
256
|
+
if (plan.reduce.kind === 'count')
|
|
257
|
+
return refined.count();
|
|
258
|
+
return refined.combine(combineSpec(refined, plan, plan.reduce.branches, ops));
|
|
259
|
+
}
|
|
260
|
+
// Both the caller's own reduce AND the declared dependency's rows ride in
|
|
261
|
+
// this one query, under keys the caller cannot reach (#1357) —
|
|
262
|
+
// `maskReductions`/`restoreReductions` are where the two get split apart
|
|
263
|
+
// again on the way out.
|
|
264
|
+
const spec = {
|
|
265
|
+
[DECLARED_ROWS_BRANCH_KEY]: declaredRowsBranch(collection, plan, ops),
|
|
266
|
+
...(plan.reduce.kind === 'count'
|
|
267
|
+
? { [DECLARED_COUNT_BRANCH_KEY]: refined.count() }
|
|
268
|
+
: combineSpec(refined, plan, plan.reduce.branches, ops)),
|
|
269
|
+
};
|
|
270
|
+
return refined.combine(spec);
|
|
271
|
+
}
|
|
272
|
+
return shape(refined, plan, plan, ops);
|
|
273
|
+
}
|
|
274
|
+
function combineSpec(refined, plan, branches, ops) {
|
|
275
|
+
const spec = {};
|
|
276
|
+
for (const branch of branches) {
|
|
277
|
+
let branched = refined;
|
|
278
|
+
for (const predicate of branch.predicates) {
|
|
279
|
+
branched = branched.where((model) => lowerWhere(predicate, model, ops));
|
|
280
|
+
}
|
|
281
|
+
spec[branch.key] =
|
|
282
|
+
branch.kind === 'count' ? branched.count() : shape(branched, plan, branch, ops);
|
|
283
|
+
}
|
|
284
|
+
return spec;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* The internal, caller-invisible rows branch a reduced-and-declared relation
|
|
288
|
+
* rides alongside its own reduction (`IncludePlan.declaredRows`). Built off
|
|
289
|
+
* the UNFILTERED `collection` rather than `refined` — it is scoped by the
|
|
290
|
+
* Access Filter alone, never by the caller's own `where()` on the reduce,
|
|
291
|
+
* because it stands in for an ordinary declared branch (full stored width,
|
|
292
|
+
* one hop), not for the caller's own view of the relation.
|
|
293
|
+
*/
|
|
294
|
+
function declaredRowsBranch(collection, plan, ops) {
|
|
295
|
+
let branched = collection;
|
|
296
|
+
for (const predicate of plan.declaredRows ?? []) {
|
|
297
|
+
branched = branched.where((model) => lowerWhere(predicate, model, ops));
|
|
298
|
+
}
|
|
299
|
+
return branched;
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
302
|
+
* Apply the projection, the sort, the page and the next level to a relation's
|
|
303
|
+
* rows. The projection and the nested includes come from the plan, which for a
|
|
304
|
+
* `combine` already carries its rows branch's own (`rowsBranchOf`); the sort
|
|
305
|
+
* and the page come from whichever branch is being built, so the count branch
|
|
306
|
+
* beside it is never chained after this one's bound.
|
|
307
|
+
*/
|
|
308
|
+
function shape(collection, plan, rows, ops) {
|
|
309
|
+
let shaped = collection;
|
|
310
|
+
if (plan.projection.columns !== undefined) {
|
|
311
|
+
shaped = shaped.select(...plan.projection.columns);
|
|
312
|
+
}
|
|
313
|
+
if (rows.orders.length > 0) {
|
|
314
|
+
shaped = shaped.orderBy(rows.orders.map((order) => (model) => lowerOrder(order, model)));
|
|
315
|
+
}
|
|
316
|
+
if (rows.offset !== undefined)
|
|
317
|
+
shaped = shaped.offset(rows.offset);
|
|
318
|
+
if (rows.limit !== undefined)
|
|
319
|
+
shaped = shaped.limit(rows.limit);
|
|
320
|
+
for (const nested of plan.includes) {
|
|
321
|
+
shaped = shaped.include(nested.relation, (child) => refine(child, nested, ops));
|
|
322
|
+
}
|
|
323
|
+
return shaped;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Build the scoped collection, member by member, under this terminal's
|
|
327
|
+
* {@link PlanDispositions}.
|
|
328
|
+
*
|
|
329
|
+
* Every terminal reaches the database through here, so the dispositions are
|
|
330
|
+
* not advice: a member the terminal did not mark `applied` is either refused
|
|
331
|
+
* out loud or declared `inapplicable` beside the reason.
|
|
332
|
+
*/
|
|
333
|
+
function scope(binding, plan, ops, dispositions, refuse) {
|
|
334
|
+
refuseCarried(plan, dispositions, refuse);
|
|
335
|
+
const applies = (member) => dispositions[member] === 'applied';
|
|
336
|
+
let collection = collectionFor(binding.ormHandle, binding.listName);
|
|
337
|
+
if (applies('predicates')) {
|
|
338
|
+
for (const predicate of plan.predicates) {
|
|
339
|
+
collection = collection.where((model) => lowerWhere(predicate, model, ops));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
if (applies('projection') && plan.projection.columns !== undefined) {
|
|
343
|
+
collection = collection.select(...plan.projection.columns);
|
|
344
|
+
}
|
|
345
|
+
if (applies('orders') && plan.orders.length > 0) {
|
|
346
|
+
collection = collection.orderBy(plan.orders.map((order) => (model) => lowerOrder(order, model)));
|
|
347
|
+
}
|
|
348
|
+
if (applies('distinct') && plan.distinct !== undefined) {
|
|
349
|
+
const fields = plan.distinct.columns.map((column) => column.column);
|
|
350
|
+
collection =
|
|
351
|
+
plan.distinct.kind === 'on'
|
|
352
|
+
? collection.distinctOn(...fields)
|
|
353
|
+
: collection.distinct(...fields);
|
|
354
|
+
}
|
|
355
|
+
if (applies('cursor') && plan.cursor !== undefined)
|
|
356
|
+
collection = collection.cursor(plan.cursor);
|
|
357
|
+
if (applies('includes')) {
|
|
358
|
+
for (const include of plan.includes) {
|
|
359
|
+
collection = collection.include(include.relation, (child) => refine(child, include, ops));
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
if (applies('offset') && plan.offset !== undefined)
|
|
363
|
+
collection = collection.offset(plan.offset);
|
|
364
|
+
if (applies('limit') && plan.limit !== undefined)
|
|
365
|
+
collection = collection.limit(plan.limit);
|
|
366
|
+
return collection;
|
|
367
|
+
}
|
|
368
|
+
function isRow(value) {
|
|
369
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* The rows one include plan contributed to a row, whichever shape the key
|
|
373
|
+
* holds them in: a to-many's array, a to-one's single row, and a reduction's
|
|
374
|
+
* envelope before {@link maskReductions} substitutes its rows back out. Both
|
|
375
|
+
* foreign-key passes walk a row through here, so neither can be reading a
|
|
376
|
+
* shape the other is not.
|
|
377
|
+
*/
|
|
378
|
+
function relatedRowsOf(row, plan) {
|
|
379
|
+
const value = row[plan.relation];
|
|
380
|
+
if (Array.isArray(value))
|
|
381
|
+
return value.filter(isRow);
|
|
382
|
+
if (plan.reduce !== undefined)
|
|
383
|
+
return combinedRows(value, rowsKeyOf(plan));
|
|
384
|
+
return isRow(value) ? [value] : [];
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* Narrow the foreign-key column of every included to-one to what the caller
|
|
388
|
+
* may see.
|
|
389
|
+
*
|
|
390
|
+
* The foreign key is a second name for the related row's identity, so a
|
|
391
|
+
* relation the Access Filter scoped away or Field Visibility stripped would
|
|
392
|
+
* otherwise survive under it as the invisible row's id.
|
|
393
|
+
*
|
|
394
|
+
* The pass is driven by the contract's own foreign-key member
|
|
395
|
+
* (`IncludePlan.foreignKey`) rather than by the shape of whatever is stored,
|
|
396
|
+
* which makes it independent of `db: { foreignKey: { map } }`; and it runs
|
|
397
|
+
* AFTER Field Visibility, which makes visibility authoritative over the
|
|
398
|
+
* column. The value written is the one the caller may see: the related row's
|
|
399
|
+
* id when the relation is visible, `null` when it is not — denied, scoped away
|
|
400
|
+
* and stripped alike, which is what a to-one the caller may not see means
|
|
401
|
+
* everywhere else (ADR-0058).
|
|
402
|
+
*
|
|
403
|
+
* A relation the caller's own `where()` refined is the exception: a `null`
|
|
404
|
+
* there may be the caller's filter rather than access, so the column is left
|
|
405
|
+
* for {@link narrowUnincludedForeignKeys}, which decides on access alone.
|
|
406
|
+
*/
|
|
407
|
+
function applyForeignKeys(row, plans) {
|
|
408
|
+
for (const plan of plans) {
|
|
409
|
+
if (!plan.declared &&
|
|
410
|
+
plan.arity === 'one' &&
|
|
411
|
+
plan.foreignKey !== undefined &&
|
|
412
|
+
plan.foreignKey in row) {
|
|
413
|
+
const value = row[plan.relation];
|
|
414
|
+
if (isRow(value))
|
|
415
|
+
row[plan.foreignKey] = value.id;
|
|
416
|
+
else if (!plan.callerRefined)
|
|
417
|
+
row[plan.foreignKey] = null;
|
|
418
|
+
}
|
|
419
|
+
if (plan.includes.length === 0)
|
|
420
|
+
continue;
|
|
421
|
+
for (const related of relatedRowsOf(row, plan))
|
|
422
|
+
applyForeignKeys(related, plan.includes);
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
/** The `id IN (...)` predicate a batched existence check narrows its scan to. */
|
|
426
|
+
function identityIn(model, listName, keys, column = 'id') {
|
|
427
|
+
const expression = model[column]?.in?.(keys);
|
|
428
|
+
if (expression === undefined)
|
|
429
|
+
throw unqueryableKey(listName, column);
|
|
430
|
+
return expression;
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Resolve the read-time context `foreignKeyOwningRelations` and
|
|
434
|
+
* `resolveRelatedAccessPlan` need for one list, mirroring what `resolveInclude`
|
|
435
|
+
* builds for a relation it actually resolves.
|
|
436
|
+
*/
|
|
437
|
+
function relatedResolveContext(binding, listName, listConfig) {
|
|
438
|
+
return {
|
|
439
|
+
listName,
|
|
440
|
+
listConfig,
|
|
441
|
+
config: binding.config,
|
|
442
|
+
session: binding.context.session,
|
|
443
|
+
context: binding.context,
|
|
444
|
+
checkFieldRead: false,
|
|
445
|
+
applyRelationAccess: true,
|
|
446
|
+
accessFilterPath: [],
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function assembleColumnFields(row, fieldConfigs) {
|
|
450
|
+
const assembled = { ...row };
|
|
451
|
+
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
|
|
452
|
+
if (!fieldConfig.assembleColumns || !fieldConfig.getColumnNames)
|
|
453
|
+
continue;
|
|
454
|
+
const columnNames = fieldConfig.getColumnNames(fieldName);
|
|
455
|
+
if (!columnNames.some((name) => name in assembled))
|
|
456
|
+
continue;
|
|
457
|
+
const value = fieldConfig.assembleColumns(fieldName, assembled);
|
|
458
|
+
for (const name of columnNames)
|
|
459
|
+
delete assembled[name];
|
|
460
|
+
assembled[fieldName] = value;
|
|
461
|
+
}
|
|
462
|
+
return assembled;
|
|
463
|
+
}
|
|
464
|
+
function callerIndependent(row, plans) {
|
|
465
|
+
const refined = plans.filter((plan) => plan.callerRefined);
|
|
466
|
+
if (refined.length === 0)
|
|
467
|
+
return row;
|
|
468
|
+
const copy = { ...row };
|
|
469
|
+
for (const plan of refined)
|
|
470
|
+
delete copy[plan.relation];
|
|
471
|
+
return copy;
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
* Narrow the foreign-key column of every to-one relationship this read did
|
|
475
|
+
* NOT include or declare — the gap {@link applyForeignKeys} cannot close,
|
|
476
|
+
* because a relation nobody named has no {@link IncludePlan} entry for it to
|
|
477
|
+
* walk (issue #1243).
|
|
478
|
+
*
|
|
479
|
+
* The column is on the row regardless of `include` (ADR-0043), so it needs
|
|
480
|
+
* its own visibility decision: the owning relationship field's own `read`
|
|
481
|
+
* rule, evaluated per row against the RAW row (before Field Visibility
|
|
482
|
+
* strips anything else), and the related list's own `query` access — `true`
|
|
483
|
+
* leaves the column as the plain pass-through value it already is, `false`
|
|
484
|
+
* nulls it outright, and a filter is resolved with one batched existence
|
|
485
|
+
* check per relation across the whole page, keyed off the column's own value
|
|
486
|
+
* rather than a fetched related row (there is no fetched related row here to
|
|
487
|
+
* key off).
|
|
488
|
+
*
|
|
489
|
+
* That existence check cannot tell a row the filter denies from a row deleted
|
|
490
|
+
* between the two statements — both come back absent from it. This function
|
|
491
|
+
* does not try to: absence nulls the column either way, so a vanished row
|
|
492
|
+
* fails exactly as closed as a denied one rather than surviving under the id
|
|
493
|
+
* this statement read before it vanished (issue #1386).
|
|
494
|
+
*
|
|
495
|
+
* Runs AFTER {@link applyForeignKeys}, on its output — mutated in place —
|
|
496
|
+
* and recurses into every relation THAT pass over its results the same way
|
|
497
|
+
* {@link applyForeignKeys} does, so a related list reached only through an
|
|
498
|
+
* include gets its own un-included to-ones narrowed too.
|
|
499
|
+
*/
|
|
500
|
+
/**
|
|
501
|
+
* Pairs each shown related row with its raw twin. Field Visibility may drop
|
|
502
|
+
* rows from a to-many (a synthetic back-relation's source `read` rule), so
|
|
503
|
+
* once the lengths differ the pairing is by `id`, never by position.
|
|
504
|
+
*/
|
|
505
|
+
function pairRelatedRows(shown, raw) {
|
|
506
|
+
const pairs = [];
|
|
507
|
+
if (shown.length === raw.length) {
|
|
508
|
+
shown.forEach((related, index) => {
|
|
509
|
+
const original = raw[index];
|
|
510
|
+
if (isRow(related) && isRow(original))
|
|
511
|
+
pairs.push([related, original]);
|
|
512
|
+
});
|
|
513
|
+
return pairs;
|
|
514
|
+
}
|
|
515
|
+
const rawById = new Map();
|
|
516
|
+
for (const original of raw)
|
|
517
|
+
if (isRow(original))
|
|
518
|
+
rawById.set(String(original.id), original);
|
|
519
|
+
for (const related of shown) {
|
|
520
|
+
if (!isRow(related))
|
|
521
|
+
continue;
|
|
522
|
+
const original = rawById.get(String(related.id));
|
|
523
|
+
if (original !== undefined)
|
|
524
|
+
pairs.push([related, original]);
|
|
525
|
+
}
|
|
526
|
+
return pairs;
|
|
527
|
+
}
|
|
528
|
+
async function narrowUnincludedForeignKeys(binding, filteredRows, rawRows, listName, listConfig, resolvedIncludes) {
|
|
529
|
+
if (binding.context._isSudo === true)
|
|
530
|
+
return;
|
|
531
|
+
const ctx = relatedResolveContext(binding, listName, listConfig);
|
|
532
|
+
const alreadyIncluded = new Set(resolvedIncludes
|
|
533
|
+
.filter((plan) => !plan.declared && !plan.callerRefined)
|
|
534
|
+
.map((plan) => plan.relation));
|
|
535
|
+
for (const owner of foreignKeyOwningRelations(ctx)) {
|
|
536
|
+
if (alreadyIncluded.has(owner.relation))
|
|
537
|
+
continue;
|
|
538
|
+
if (!rawRows.some((row) => owner.foreignKey in row))
|
|
539
|
+
continue;
|
|
540
|
+
const related = getRelatedListConfig(owner.ref, binding.config);
|
|
541
|
+
if (!related)
|
|
542
|
+
continue;
|
|
543
|
+
const access = await resolveRelatedAccessPlan({ listName: related.listName, listConfig: related.listConfig }, ctx);
|
|
544
|
+
for (let i = 0; i < filteredRows.length; i++) {
|
|
545
|
+
const raw = rawRows[i];
|
|
546
|
+
if (!(owner.foreignKey in raw) || !(owner.foreignKey in filteredRows[i]))
|
|
547
|
+
continue;
|
|
548
|
+
const canReadField = await checkFieldAccess(owner.fieldConfig.access, 'read', {
|
|
549
|
+
session: binding.context.session,
|
|
550
|
+
context: binding.context,
|
|
551
|
+
item: assembleColumnFields(callerIndependent(raw, resolvedIncludes), listConfig.fields),
|
|
552
|
+
});
|
|
553
|
+
if (!canReadField || access.kind === 'false')
|
|
554
|
+
filteredRows[i][owner.foreignKey] = null;
|
|
555
|
+
}
|
|
556
|
+
if (access.kind === 'true' || access.kind === 'false')
|
|
557
|
+
continue;
|
|
558
|
+
const idsByRow = rawRows.map((row) => row[owner.foreignKey]);
|
|
559
|
+
const idMap = new Map();
|
|
560
|
+
for (let i = 0; i < filteredRows.length; i++) {
|
|
561
|
+
if (!(owner.foreignKey in filteredRows[i]) || filteredRows[i][owner.foreignKey] === null)
|
|
562
|
+
continue;
|
|
563
|
+
const value = idsByRow[i];
|
|
564
|
+
if (value !== null && value !== undefined)
|
|
565
|
+
idMap.set(String(value), value);
|
|
566
|
+
}
|
|
567
|
+
if (idMap.size === 0)
|
|
568
|
+
continue;
|
|
569
|
+
const ops = await whereCombinators();
|
|
570
|
+
const visible = await engineRead(binding, () => collectionFor(binding.ormHandle, related.listName)
|
|
571
|
+
.where((model) => lowerWhere(access, model, ops))
|
|
572
|
+
.where((model) => identityIn(model, related.listName, [...idMap.values()]))
|
|
573
|
+
.select('id')
|
|
574
|
+
.all());
|
|
575
|
+
const visibleIds = new Set(visible.map((row) => String(row.id)));
|
|
576
|
+
for (let i = 0; i < filteredRows.length; i++) {
|
|
577
|
+
if (!(owner.foreignKey in filteredRows[i]) || filteredRows[i][owner.foreignKey] === null)
|
|
578
|
+
continue;
|
|
579
|
+
const value = idsByRow[i];
|
|
580
|
+
if (value === null || value === undefined)
|
|
581
|
+
continue;
|
|
582
|
+
if (!visibleIds.has(String(value)))
|
|
583
|
+
filteredRows[i][owner.foreignKey] = null;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
for (const plan of resolvedIncludes) {
|
|
587
|
+
const relatedListConfig = binding.config.lists[plan.relatedListName];
|
|
588
|
+
if (relatedListConfig === undefined)
|
|
589
|
+
continue;
|
|
590
|
+
const nestedFiltered = [];
|
|
591
|
+
const nestedRaw = [];
|
|
592
|
+
for (let i = 0; i < filteredRows.length; i++) {
|
|
593
|
+
const filteredValue = filteredRows[i][plan.relation];
|
|
594
|
+
const rawValue = rawRows[i][plan.relation];
|
|
595
|
+
if (Array.isArray(filteredValue) && Array.isArray(rawValue)) {
|
|
596
|
+
for (const [related, original] of pairRelatedRows(filteredValue, rawValue)) {
|
|
597
|
+
nestedFiltered.push(related);
|
|
598
|
+
nestedRaw.push(original);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
else if (plan.reduce !== undefined) {
|
|
602
|
+
const key = rowsKeyOf(plan);
|
|
603
|
+
for (const [related, original] of pairRelatedRows(combinedRows(filteredValue, key), combinedRows(rawValue, key))) {
|
|
604
|
+
nestedFiltered.push(related);
|
|
605
|
+
nestedRaw.push(original);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
else if (isRow(filteredValue) && isRow(rawValue)) {
|
|
609
|
+
nestedFiltered.push(filteredValue);
|
|
610
|
+
nestedRaw.push(rawValue);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
if (nestedFiltered.length === 0)
|
|
614
|
+
continue;
|
|
615
|
+
await narrowUnincludedForeignKeys(binding, nestedFiltered, nestedRaw, plan.relatedListName, relatedListConfig, plan.includes);
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
function reduces(plans) {
|
|
619
|
+
return plans.some((plan) => plan.reduce !== undefined || reduces(plan.includes));
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Stand an empty to-many in for every relation the read reduced.
|
|
623
|
+
*
|
|
624
|
+
* Field Visibility decides whether a relation key survives by evaluating the
|
|
625
|
+
* relationship field's own `read` rule, and it reaches that decision on the
|
|
626
|
+
* path it takes for a relation's ROWS. A count is not rows, so handing it the
|
|
627
|
+
* reduced value would take it down neither path and the key would fall out of
|
|
628
|
+
* the result whether or not the session may read it. Substituting `[]` puts
|
|
629
|
+
* the decision back on the one path that makes it — Field Visibility runs the
|
|
630
|
+
* same rule over the same parent row — and {@link restoreReductions} then
|
|
631
|
+
* writes the count back under exactly the keys that survived. A reduction is
|
|
632
|
+
* to-many only ({@link ReducedToOneIncludeError}), so `[]` is the shape the
|
|
633
|
+
* relation would otherwise have had.
|
|
634
|
+
*
|
|
635
|
+
* A `combine` that named the relation's rows under one key stands in that
|
|
636
|
+
* array instead of `[]`, so those rows reach Field Visibility exactly as an
|
|
637
|
+
* unreduced relation's do and {@link restoreReductions} writes them back
|
|
638
|
+
* beside the counts.
|
|
639
|
+
*
|
|
640
|
+
* A relation that is both reduced and a live declared dependency of a
|
|
641
|
+
* computed field returned alongside it (#1357) is the third case: its
|
|
642
|
+
* `IncludePlan.declaredRows` fetched real rows purely for that declaration,
|
|
643
|
+
* under {@link DECLARED_ROWS_BRANCH_KEY}, and those are what stand in here —
|
|
644
|
+
* never `[]` — so the declaring hook's `needs` sees them via its own `item`,
|
|
645
|
+
* and a relationship `read` rule that inspects `item.<reducedRelation>`
|
|
646
|
+
* decides against real rows rather than an empty array. Field Visibility
|
|
647
|
+
* still must not compute over them as though they were an ordinary relation
|
|
648
|
+
* (ADR-0051): `ReducedDeclaredKeys` (`select.ts`) is what tells it to treat
|
|
649
|
+
* this key as a raw pass-through, and {@link restoreReductions} overwrites it
|
|
650
|
+
* with the reduction's own value regardless of what Field Visibility computed
|
|
651
|
+
* for it.
|
|
652
|
+
*
|
|
653
|
+
* Known limits: a relation reduced with NEITHER a rows branch of its own NOR
|
|
654
|
+
* a live declared dependency still stands in `[]`, so a relationship `read`
|
|
655
|
+
* rule inspecting `item.<reducedRelation>` without declaring it via `needs`
|
|
656
|
+
* decides on an empty array rather than on what the count counted — the
|
|
657
|
+
* common `item.posts.length > 0` shape fails closed there, and an inverted
|
|
658
|
+
* `item.posts.length === 0` fails open. A rule that reaches into a relation
|
|
659
|
+
* must declare it, the same requirement `needs` already states for a computed
|
|
660
|
+
* field's `resolveOutput`.
|
|
661
|
+
*/
|
|
662
|
+
function maskReductions(row, plans) {
|
|
663
|
+
if (!reduces(plans))
|
|
664
|
+
return row;
|
|
665
|
+
const masked = { ...row };
|
|
666
|
+
for (const plan of plans) {
|
|
667
|
+
if (plan.reduce !== undefined) {
|
|
668
|
+
const rows = plan.declaredRows !== undefined && !hasRowsBranch(plan)
|
|
669
|
+
? declaredRowsOf(row[plan.relation])
|
|
670
|
+
: combinedRows(row[plan.relation], rowsKeyOf(plan));
|
|
671
|
+
masked[plan.relation] = reduces(plan.includes)
|
|
672
|
+
? rows.map((related) => maskReductions(related, plan.includes))
|
|
673
|
+
: rows;
|
|
674
|
+
continue;
|
|
675
|
+
}
|
|
676
|
+
if (!reduces(plan.includes))
|
|
677
|
+
continue;
|
|
678
|
+
const value = masked[plan.relation];
|
|
679
|
+
if (Array.isArray(value)) {
|
|
680
|
+
masked[plan.relation] = value.map((related) => isRow(related) ? maskReductions(related, plan.includes) : related);
|
|
681
|
+
}
|
|
682
|
+
else if (isRow(value)) {
|
|
683
|
+
masked[plan.relation] = maskReductions(value, plan.includes);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
return masked;
|
|
687
|
+
}
|
|
688
|
+
/** The key a `combine` returned the relation's own rows under, if it named one. */
|
|
689
|
+
function rowsKeyOf(plan) {
|
|
690
|
+
if (plan.reduce?.kind !== 'combine')
|
|
691
|
+
return undefined;
|
|
692
|
+
for (const branch of plan.reduce.branches) {
|
|
693
|
+
if (branch.kind === 'rows')
|
|
694
|
+
return branch.key;
|
|
695
|
+
}
|
|
696
|
+
return undefined;
|
|
697
|
+
}
|
|
698
|
+
/** The rows an internal declared-dependency branch fetched (`IncludePlan.declaredRows`). */
|
|
699
|
+
function declaredRowsOf(value) {
|
|
700
|
+
return combinedRows(value, DECLARED_ROWS_BRANCH_KEY);
|
|
701
|
+
}
|
|
702
|
+
/** The rows a `combine` returned under {@link rowsKeyOf}, or `[]` for a bare count. */
|
|
703
|
+
function combinedRows(value, key) {
|
|
704
|
+
if (key === undefined || !isRow(value))
|
|
705
|
+
return [];
|
|
706
|
+
const rows = value[key];
|
|
707
|
+
return Array.isArray(rows) ? rows.filter(isRow) : [];
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* The reduction's own value, once {@link declaredRowsBranch}'s internal
|
|
711
|
+
* branch is spliced back out: a bare count unwrapped from
|
|
712
|
+
* {@link DECLARED_COUNT_BRANCH_KEY}, or a caller's own combine object with
|
|
713
|
+
* {@link DECLARED_ROWS_BRANCH_KEY} removed. The caller never sees either
|
|
714
|
+
* reserved key.
|
|
715
|
+
*/
|
|
716
|
+
function publicReductionValue(raw, reduce) {
|
|
717
|
+
if (!isRow(raw))
|
|
718
|
+
return raw;
|
|
719
|
+
if (reduce.kind === 'count')
|
|
720
|
+
return raw[DECLARED_COUNT_BRANCH_KEY] ?? 0;
|
|
721
|
+
const publicValue = { ...raw };
|
|
722
|
+
delete publicValue[DECLARED_ROWS_BRANCH_KEY];
|
|
723
|
+
return publicValue;
|
|
724
|
+
}
|
|
725
|
+
/** Write each reduced relation back, under the keys Field Visibility kept. */
|
|
726
|
+
function restoreReductions(shown, source, plans) {
|
|
727
|
+
for (const plan of plans) {
|
|
728
|
+
if (plan.reduce !== undefined) {
|
|
729
|
+
if (!(plan.relation in shown))
|
|
730
|
+
continue;
|
|
731
|
+
const raw = source[plan.relation];
|
|
732
|
+
if (plan.declaredRows !== undefined && !hasRowsBranch(plan)) {
|
|
733
|
+
shown[plan.relation] = publicReductionValue(raw, plan.reduce);
|
|
734
|
+
continue;
|
|
735
|
+
}
|
|
736
|
+
const key = rowsKeyOf(plan);
|
|
737
|
+
if (key === undefined || !isRow(raw)) {
|
|
738
|
+
shown[plan.relation] = raw;
|
|
739
|
+
continue;
|
|
740
|
+
}
|
|
741
|
+
const kept = shown[plan.relation];
|
|
742
|
+
if (reduces(plan.includes) && Array.isArray(kept)) {
|
|
743
|
+
const rows = combinedRows(raw, key);
|
|
744
|
+
for (const [related, original] of pairRelatedRows(kept, rows)) {
|
|
745
|
+
restoreReductions(related, original, plan.includes);
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
const base = publicReductionValue(raw, plan.reduce);
|
|
749
|
+
shown[plan.relation] = { ...(isRow(base) ? base : raw), [key]: kept };
|
|
750
|
+
continue;
|
|
751
|
+
}
|
|
752
|
+
if (!reduces(plan.includes))
|
|
753
|
+
continue;
|
|
754
|
+
const kept = shown[plan.relation];
|
|
755
|
+
const raw = source[plan.relation];
|
|
756
|
+
if (Array.isArray(kept) && Array.isArray(raw)) {
|
|
757
|
+
for (const [related, original] of pairRelatedRows(kept, raw)) {
|
|
758
|
+
restoreReductions(related, original, plan.includes);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
else if (isRow(kept) && isRow(raw)) {
|
|
762
|
+
restoreReductions(kept, raw, plan.includes);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
async function unshapedDeclaredRows(binding, listName, rows, plans) {
|
|
767
|
+
const byRelation = new Map();
|
|
768
|
+
for (const plan of plans) {
|
|
769
|
+
if (plan.reduce === undefined || plan.declaredRows === undefined || !hasRowsBranch(plan)) {
|
|
770
|
+
continue;
|
|
771
|
+
}
|
|
772
|
+
const values = new Map();
|
|
773
|
+
for (const row of rows)
|
|
774
|
+
values.set(String(row['id']), declaredRowsOf(row[plan.relation]));
|
|
775
|
+
byRelation.set(plan.relation, values);
|
|
776
|
+
}
|
|
777
|
+
const fetched = plans.filter((plan) => plan.declaredFetch !== undefined);
|
|
778
|
+
const ids = rows
|
|
779
|
+
.map((row) => row['id'])
|
|
780
|
+
.filter((id) => typeof id === 'string' || typeof id === 'number');
|
|
781
|
+
if (fetched.length === 0 || ids.length === 0)
|
|
782
|
+
return byRelation;
|
|
783
|
+
const ops = await whereCombinators();
|
|
784
|
+
for (const plan of fetched) {
|
|
785
|
+
const owners = await withOrigin('engine', () => collectionFor(binding.ormHandle, listName)
|
|
786
|
+
.where((model) => identityIn(model, listName, ids))
|
|
787
|
+
.select('id')
|
|
788
|
+
.include(plan.relation, (child) => {
|
|
789
|
+
let scoped = child;
|
|
790
|
+
for (const predicate of plan.declaredFetch ?? []) {
|
|
791
|
+
scoped = scoped.where((model) => lowerWhere(predicate, model, ops));
|
|
792
|
+
}
|
|
793
|
+
return scoped;
|
|
794
|
+
})
|
|
795
|
+
.all());
|
|
796
|
+
const values = new Map();
|
|
797
|
+
for (const owner of owners)
|
|
798
|
+
values.set(String(owner['id']), owner[plan.relation]);
|
|
799
|
+
byRelation.set(plan.relation, values);
|
|
800
|
+
}
|
|
801
|
+
return byRelation;
|
|
802
|
+
}
|
|
803
|
+
async function ruleViewTreeFor(binding, listName, rows, plans) {
|
|
804
|
+
const unshaped = await unshapedDeclaredRows(binding, listName, rows, plans);
|
|
805
|
+
const nested = {};
|
|
806
|
+
for (const plan of plans) {
|
|
807
|
+
if (plan.declared || plan.includes.length === 0)
|
|
808
|
+
continue;
|
|
809
|
+
const rowsKey = rowsKeyOf(plan);
|
|
810
|
+
if (plan.reduce !== undefined && rowsKey === undefined)
|
|
811
|
+
continue;
|
|
812
|
+
const children = [];
|
|
813
|
+
for (const row of rows) {
|
|
814
|
+
const value = row[plan.relation];
|
|
815
|
+
if (plan.reduce !== undefined)
|
|
816
|
+
children.push(...combinedRows(value, rowsKey));
|
|
817
|
+
else if (Array.isArray(value))
|
|
818
|
+
children.push(...value.filter(isRow));
|
|
819
|
+
else if (isRow(value))
|
|
820
|
+
children.push(value);
|
|
821
|
+
}
|
|
822
|
+
if (children.length === 0)
|
|
823
|
+
continue;
|
|
824
|
+
nested[plan.relation] = await ruleViewTreeFor(binding, plan.relatedListName, children, plan.includes);
|
|
825
|
+
}
|
|
826
|
+
return { viewFor: (row) => ruleViewFor(row, plans, unshaped), nested };
|
|
827
|
+
}
|
|
828
|
+
function ruleViewFor(row, plans, unshaped) {
|
|
829
|
+
const removed = new Set();
|
|
830
|
+
const declared = {};
|
|
831
|
+
for (const plan of plans) {
|
|
832
|
+
if (plan.declared)
|
|
833
|
+
continue;
|
|
834
|
+
if (plan.reduce !== undefined) {
|
|
835
|
+
const stored = unshaped.get(plan.relation);
|
|
836
|
+
if (plan.declaredRows !== undefined && hasRowsBranch(plan) && stored !== undefined) {
|
|
837
|
+
declared[plan.relation] = stored.get(String(row['id']));
|
|
838
|
+
}
|
|
839
|
+
continue;
|
|
840
|
+
}
|
|
841
|
+
if (!plan.callerShaped)
|
|
842
|
+
continue;
|
|
843
|
+
const values = unshaped.get(plan.relation);
|
|
844
|
+
if (plan.declaredFetch === undefined || values === undefined) {
|
|
845
|
+
removed.add(plan.relation);
|
|
846
|
+
continue;
|
|
847
|
+
}
|
|
848
|
+
declared[plan.relation] = values.get(String(row['id']));
|
|
849
|
+
}
|
|
850
|
+
return { removed, declared };
|
|
851
|
+
}
|
|
852
|
+
/**
|
|
853
|
+
* What every terminal returns rows through — the one place a row this engine
|
|
854
|
+
* read becomes a row a caller may see, and the only place any foreign-key
|
|
855
|
+
* pass runs. `all()`, `first()`, the `forUpdate()` lane and `nearest()` all
|
|
856
|
+
* come through here, so none of them can drift apart on what a column reads
|
|
857
|
+
* as, and neither can a hook or a `read` rule.
|
|
858
|
+
*
|
|
859
|
+
* The batch is what lets {@link narrowUnincludedForeignKeys} resolve a
|
|
860
|
+
* to-one this read never named at all for a whole page in one query per
|
|
861
|
+
* relation rather than one per row.
|
|
862
|
+
*/
|
|
863
|
+
async function visibleRows(binding, rows, plan) {
|
|
864
|
+
const { listConfig, context, config, listName } = binding;
|
|
865
|
+
const ruleViews = await ruleViewTreeFor(binding, listName, rows, plan.includes);
|
|
866
|
+
const results = await Promise.all(rows.map(async (row) => {
|
|
867
|
+
const filtered = await filterReadableFields(maskReductions(row, plan.includes), listConfig.fields, { session: context.session, context }, config, 0, listName, plan.additions, plan.selection, plan.reducedDeclared, ruleViews);
|
|
868
|
+
applyForeignKeys(filtered, plan.includes);
|
|
869
|
+
restoreReductions(filtered, row, plan.includes);
|
|
870
|
+
return filtered;
|
|
871
|
+
}));
|
|
872
|
+
await narrowUnincludedForeignKeys(binding, results, rows, listName, listConfig, plan.includes);
|
|
873
|
+
return results;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Whether the session's operation-level `query` access reaches the row with
|
|
877
|
+
* this id, resolved as a read would: a filter rule is run as a scoped read of
|
|
878
|
+
* the row's identity on the binding's own handle.
|
|
879
|
+
*/
|
|
880
|
+
export async function writtenRowQueryable(binding, id) {
|
|
881
|
+
if (binding.context._isSudo === true)
|
|
882
|
+
return true;
|
|
883
|
+
if (typeof id !== 'string' && typeof id !== 'number')
|
|
884
|
+
return false;
|
|
885
|
+
const state = {
|
|
886
|
+
predicates: [{ id: { equals: id } }],
|
|
887
|
+
orders: [],
|
|
888
|
+
includes: [],
|
|
889
|
+
fields: ['id'],
|
|
890
|
+
distincts: [],
|
|
891
|
+
lock: false,
|
|
892
|
+
};
|
|
893
|
+
const plan = await resolvePlan(binding, state);
|
|
894
|
+
if (plan === null)
|
|
895
|
+
return false;
|
|
896
|
+
if (plan.predicates.length === 1)
|
|
897
|
+
return true;
|
|
898
|
+
const collection = scope(binding, plan, await whereCombinators(), FIRST_DISPOSITIONS, unreachableRefusal);
|
|
899
|
+
return (await withOrigin('engine', () => collection.first())) !== null;
|
|
900
|
+
}
|
|
901
|
+
/**
|
|
902
|
+
* What a write hands back: the row Field Visibility leaves, then the
|
|
903
|
+
* foreign-key pass a read of the same row would give it, so `create()` and
|
|
904
|
+
* `update()` never return an id `first()` hides. A row the session cannot
|
|
905
|
+
* `query` comes back as the list's system fields alone.
|
|
906
|
+
*/
|
|
907
|
+
export async function visibleWrittenRow(binding, row, queryable) {
|
|
908
|
+
const { listConfig, context, config, listName } = binding;
|
|
909
|
+
if (!queryable) {
|
|
910
|
+
const kept = {};
|
|
911
|
+
for (const key of getListDependencies(config, listName).systemFields) {
|
|
912
|
+
if (key in row)
|
|
913
|
+
kept[key] = row[key];
|
|
914
|
+
}
|
|
915
|
+
return kept;
|
|
916
|
+
}
|
|
917
|
+
const filtered = await filterReadableFields(row, listConfig.fields, { session: context.session, context: { ...context, _isSudo: context._isSudo } }, config, 0, listName);
|
|
918
|
+
await narrowUnincludedForeignKeys(binding, [filtered], [row], listName, listConfig, []);
|
|
919
|
+
return filtered;
|
|
920
|
+
}
|
|
921
|
+
/** `all()` is the terminal every plan member was designed for. */
|
|
922
|
+
const ALL_DISPOSITIONS = {
|
|
923
|
+
predicates: 'applied',
|
|
924
|
+
orders: 'applied',
|
|
925
|
+
includes: 'applied',
|
|
926
|
+
projection: 'applied',
|
|
927
|
+
selection: 'applied',
|
|
928
|
+
additions: 'applied',
|
|
929
|
+
reducedDeclared: 'applied',
|
|
930
|
+
limit: 'applied',
|
|
931
|
+
offset: 'applied',
|
|
932
|
+
distinct: 'applied',
|
|
933
|
+
cursor: 'applied',
|
|
934
|
+
forUpdate: 'applied',
|
|
935
|
+
};
|
|
936
|
+
/**
|
|
937
|
+
* `first()` differs from `all()` in one member: the terminal is its own row
|
|
938
|
+
* bound, so a `limit` beside it changes nothing.
|
|
939
|
+
*
|
|
940
|
+
* `offset` is NOT that — `first()` has no offset of its own, so
|
|
941
|
+
* `.orderBy(…).offset(10).first()` is `LIMIT 1 OFFSET 10` and answers with the
|
|
942
|
+
* eleventh row rather than the first.
|
|
943
|
+
*/
|
|
944
|
+
const FIRST_DISPOSITIONS = { ...ALL_DISPOSITIONS, limit: 'inapplicable' };
|
|
945
|
+
/**
|
|
946
|
+
* What `all()` and `first()` pass, which mark no member `refused` — reached
|
|
947
|
+
* only if one of their dispositions above ever becomes one.
|
|
948
|
+
*/
|
|
949
|
+
function unreachableRefusal(members) {
|
|
950
|
+
throw new ValidationError([`Nothing is refused here, yet ${quoted(members)} was.`]);
|
|
951
|
+
}
|
|
952
|
+
/**
|
|
953
|
+
* The lane this read locks through, and where its rows' identity lives —
|
|
954
|
+
* resolved before the scoped read runs, so a list the contract cannot lock and
|
|
955
|
+
* a bound the terminal will not honour are both refused before any statement
|
|
956
|
+
* is issued (ADR-0062).
|
|
957
|
+
*/
|
|
958
|
+
function lockLane(binding, plan, member, bound) {
|
|
959
|
+
if (plan.forUpdate !== true)
|
|
960
|
+
return undefined;
|
|
961
|
+
if (binding.lock === undefined)
|
|
962
|
+
throw new RowLockUnavailableError(member);
|
|
963
|
+
if (bound !== undefined && bound > ROW_LOCK_MAX_KEYS) {
|
|
964
|
+
throw new RowLockKeyLimitExceededError(binding.listName, bound);
|
|
965
|
+
}
|
|
966
|
+
return { lane: binding.lock, identity: binding.lock.identity(binding.listName) };
|
|
967
|
+
}
|
|
968
|
+
/**
|
|
969
|
+
* Lock the rows the scoped read returned, then read them again.
|
|
970
|
+
*
|
|
971
|
+
* Each statement takes its own snapshot, so a row another transaction changed
|
|
972
|
+
* between the scoped read and the lock is stale in the first read. The locked
|
|
973
|
+
* keys are therefore re-read under the same predicates and Access Filter: a row
|
|
974
|
+
* that is gone or no longer in scope drops out, and the rest carry their
|
|
975
|
+
* current values. The re-read never widens the locked key set, and keeps the
|
|
976
|
+
* cursor and distinct the caller composed so the refreshed rows still honour
|
|
977
|
+
* them; the key set already carries the limit and offset.
|
|
978
|
+
*
|
|
979
|
+
* No keys, no statement: an empty scoped read has nothing to lock, and `IN ()`
|
|
980
|
+
* is a Postgres syntax error rather than an empty result (ADR-0062).
|
|
981
|
+
*/
|
|
982
|
+
async function locked(binding, taken, plan, rows) {
|
|
983
|
+
const { listName } = binding;
|
|
984
|
+
const column = taken.identity.column;
|
|
985
|
+
const keys = [];
|
|
986
|
+
for (const row of rows) {
|
|
987
|
+
const value = row[column];
|
|
988
|
+
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
989
|
+
throw new RowLockIdentityError(listName, `the read returned a row with no "${column}" to lock it by`);
|
|
990
|
+
}
|
|
991
|
+
keys.push(value);
|
|
992
|
+
}
|
|
993
|
+
if (keys.length === 0)
|
|
994
|
+
return [];
|
|
995
|
+
const held = await engineRead(binding, () => taken.lane.lock(listName, taken.identity, keys));
|
|
996
|
+
if (held.length === 0)
|
|
997
|
+
return [];
|
|
998
|
+
const refreshed = scope(binding, { ...plan, limit: undefined, offset: undefined }, await whereCombinators(), ALL_DISPOSITIONS, unreachableRefusal).where((model) => identityIn(model, listName, held, column));
|
|
999
|
+
return await engineRead(binding, () => refreshed.all());
|
|
1000
|
+
}
|
|
1001
|
+
async function engineRead(binding, run) {
|
|
1002
|
+
try {
|
|
1003
|
+
return await withOrigin('engine', run);
|
|
1004
|
+
}
|
|
1005
|
+
catch (error) {
|
|
1006
|
+
binding.context._transactionOwner?.poison(error);
|
|
1007
|
+
throw error;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
async function runAll(binding, state) {
|
|
1011
|
+
const plan = await resolvePlan(binding, state);
|
|
1012
|
+
if (plan === null)
|
|
1013
|
+
return [];
|
|
1014
|
+
const taken = lockLane(binding, plan, 'forUpdate().all()', plan.limit);
|
|
1015
|
+
const collection = scope(binding, plan, await whereCombinators(), ALL_DISPOSITIONS, unreachableRefusal);
|
|
1016
|
+
const read = await engineRead(binding, () => collection.all());
|
|
1017
|
+
const rows = taken === undefined ? read : await locked(binding, taken, plan, read);
|
|
1018
|
+
return await visibleRows(binding, rows, plan);
|
|
1019
|
+
}
|
|
1020
|
+
async function runFirst(binding, state) {
|
|
1021
|
+
const plan = await resolvePlan(binding, state);
|
|
1022
|
+
if (plan === null)
|
|
1023
|
+
return null;
|
|
1024
|
+
const taken = lockLane(binding, plan, 'forUpdate().first()', 1);
|
|
1025
|
+
const collection = scope(binding, plan, await whereCombinators(), FIRST_DISPOSITIONS, unreachableRefusal);
|
|
1026
|
+
const read = await engineRead(binding, () => collection.first());
|
|
1027
|
+
if (read === null)
|
|
1028
|
+
return null;
|
|
1029
|
+
const rows = taken === undefined ? [read] : await locked(binding, taken, plan, [read]);
|
|
1030
|
+
if (rows.length === 0)
|
|
1031
|
+
return null;
|
|
1032
|
+
return (await visibleRows(binding, rows, plan))[0];
|
|
1033
|
+
}
|
|
1034
|
+
function countOf(result, listName, key) {
|
|
1035
|
+
const value = result[key];
|
|
1036
|
+
if (typeof value !== 'number') {
|
|
1037
|
+
throw new ValidationError([
|
|
1038
|
+
`The database answered "${key}" for "${listName}" with something other than a count. ` +
|
|
1039
|
+
`Re-run \`opensaas generate\` so the emitted contract matches the config.`,
|
|
1040
|
+
]);
|
|
1041
|
+
}
|
|
1042
|
+
return value;
|
|
1043
|
+
}
|
|
1044
|
+
/**
|
|
1045
|
+
* An aggregate counts the rows the predicates match.
|
|
1046
|
+
*
|
|
1047
|
+
* A sort, an include and a projection cannot change that count — an include is
|
|
1048
|
+
* an eager load, a sort is over rows this terminal returns none of (and
|
|
1049
|
+
* Postgres refuses an `ORDER BY` beside a bare aggregate outright), and a count
|
|
1050
|
+
* materialises no rows for a projection to narrow or for Field Visibility to
|
|
1051
|
+
* strip. `distinct`, `cursor`, `limit` and `offset` all WOULD change the
|
|
1052
|
+
* answer, and none of them can be expressed beside a bare aggregate — SQL's
|
|
1053
|
+
* `LIMIT`/`OFFSET` bound the aggregate's own single result row, not the rows
|
|
1054
|
+
* it counts — so each is refused rather than dropped.
|
|
1055
|
+
*/
|
|
1056
|
+
const AGGREGATE_DISPOSITIONS = {
|
|
1057
|
+
predicates: 'applied',
|
|
1058
|
+
orders: 'inapplicable',
|
|
1059
|
+
includes: 'inapplicable',
|
|
1060
|
+
projection: 'inapplicable',
|
|
1061
|
+
selection: 'inapplicable',
|
|
1062
|
+
additions: 'inapplicable',
|
|
1063
|
+
reducedDeclared: 'inapplicable',
|
|
1064
|
+
limit: 'refused',
|
|
1065
|
+
offset: 'refused',
|
|
1066
|
+
distinct: 'refused',
|
|
1067
|
+
cursor: 'refused',
|
|
1068
|
+
forUpdate: 'refused',
|
|
1069
|
+
};
|
|
1070
|
+
function refuseUncountable(listName) {
|
|
1071
|
+
return (members) => {
|
|
1072
|
+
throw new ValidationError([
|
|
1073
|
+
`Cannot aggregate "${listName}" over a read that composed ${quoted(members)} — an ` +
|
|
1074
|
+
`aggregate counts the rows the predicates match, so it returns no rows to bound, page, ` +
|
|
1075
|
+
`collapse or lock. Aggregate the predicates alone, or read the rows.`,
|
|
1076
|
+
]);
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
1079
|
+
/**
|
|
1080
|
+
* Run an aggregate: the same scoped read every other terminal runs, counted in
|
|
1081
|
+
* the database instead of materialised.
|
|
1082
|
+
*
|
|
1083
|
+
* Known limits: `count()` throws rather than rounding beyond
|
|
1084
|
+
* ±(2^53 − 1) — the guarded integer codec the ORM decodes it through
|
|
1085
|
+
* (`RUNTIME.DECODE_FAILED`). A lossless `countBigInt` is on the ORM's
|
|
1086
|
+
* aggregate accessor and not on this vocabulary; re-check at GA (ADR-0041).
|
|
1087
|
+
*/
|
|
1088
|
+
async function runAggregate(binding, state, build) {
|
|
1089
|
+
const spec = build(aggregations);
|
|
1090
|
+
const keys = specKeys(spec);
|
|
1091
|
+
const plan = await resolvePlan(binding, state);
|
|
1092
|
+
if (plan === null)
|
|
1093
|
+
return zeroed(keys);
|
|
1094
|
+
checkSpec(binding.listName, spec);
|
|
1095
|
+
const collection = scope(binding, plan, await whereCombinators(), AGGREGATE_DISPOSITIONS, refuseUncountable(binding.listName));
|
|
1096
|
+
const result = await engineRead(binding, () => collection.aggregate((aggregate) => Object.fromEntries(keys.map((key) => [key, aggregate.count()]))));
|
|
1097
|
+
return Object.fromEntries(keys.map((key) => [key, countOf(result, binding.listName, key)]));
|
|
1098
|
+
}
|
|
1099
|
+
/**
|
|
1100
|
+
* A row with no vector has no distance, so it is excluded in the query rather
|
|
1101
|
+
* than dropped from the result — which is what keeps `limit` exact.
|
|
1102
|
+
*/
|
|
1103
|
+
function present(near) {
|
|
1104
|
+
return {
|
|
1105
|
+
kind: 'scalar',
|
|
1106
|
+
listName: near.listName,
|
|
1107
|
+
column: near.column,
|
|
1108
|
+
steps: [{ op: 'isNotNull' }],
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* A search ranks by distance, and the ranking is this query's leading order.
|
|
1113
|
+
*
|
|
1114
|
+
* So a cursor has no axis left to resume along and a `distinctOn` can never be
|
|
1115
|
+
* the leading sort; a plain `distinct` would collapse rows the top-K has
|
|
1116
|
+
* already been computed over, which makes `limit` inexact; and an `offset`
|
|
1117
|
+
* would page a ranking whose scores are recomputed here in float64 over a
|
|
1118
|
+
* float4 column, so the page boundary is exactly where the two can disagree.
|
|
1119
|
+
* `options.limit` is this terminal's own bound (ADR-0045), and `orders` is
|
|
1120
|
+
* rebuilt distance-first below so a caller's own sort becomes the tiebreak
|
|
1121
|
+
* rather than competing with the ranking.
|
|
1122
|
+
*/
|
|
1123
|
+
const NEAREST_DISPOSITIONS = {
|
|
1124
|
+
predicates: 'applied',
|
|
1125
|
+
orders: 'inapplicable',
|
|
1126
|
+
includes: 'applied',
|
|
1127
|
+
projection: 'applied',
|
|
1128
|
+
selection: 'applied',
|
|
1129
|
+
additions: 'applied',
|
|
1130
|
+
reducedDeclared: 'applied',
|
|
1131
|
+
limit: 'inapplicable',
|
|
1132
|
+
offset: 'refused',
|
|
1133
|
+
distinct: 'refused',
|
|
1134
|
+
cursor: 'refused',
|
|
1135
|
+
forUpdate: 'refused',
|
|
1136
|
+
};
|
|
1137
|
+
function refuseUnrankable(listName) {
|
|
1138
|
+
return (members) => {
|
|
1139
|
+
throw new ValidationError([
|
|
1140
|
+
`Cannot search "${listName}" over a read that composed ${quoted(members)} — the ranking is ` +
|
|
1141
|
+
`this query's leading order, so a cursor has no axis to resume along, an offset pages a ` +
|
|
1142
|
+
`ranking whose scores this engine recomputes, and a distinct would collapse rows the ` +
|
|
1143
|
+
`nearest limit is already counted over. A ranking is not a gate, so it takes no row lock ` +
|
|
1144
|
+
`either. Search the predicates alone.`,
|
|
1145
|
+
]);
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Run a vector search: one scoped query carrying the caller's predicates, the
|
|
1150
|
+
* Access Filter, the `minScore` distance bound, the distance ordering and the
|
|
1151
|
+
* limit.
|
|
1152
|
+
*
|
|
1153
|
+
* Known limits:
|
|
1154
|
+
* - The score is computed here from the row's own vector rather than projected.
|
|
1155
|
+
* A Prisma 8 collection at `8.0.0-rc.8` projects columns only — an expression
|
|
1156
|
+
* cannot be selected — so the database owns the ranking and the bound, and
|
|
1157
|
+
* the number beside the row is the same function evaluated over the same
|
|
1158
|
+
* values. Re-check at GA (ADR-0045).
|
|
1159
|
+
* - pgvector's index authoring is not on the `rc.8` pack, so a contract-managed
|
|
1160
|
+
* schema carries no HNSW or IVFFlat index and every search here is an exact
|
|
1161
|
+
* scan, whose top-K under the Access Filter is exact. `hnsw.iterative_scan`
|
|
1162
|
+
* therefore has nothing to apply to and is not set; enabling it needs a
|
|
1163
|
+
* connection-scoped statement the read binding has no seam for, and belongs
|
|
1164
|
+
* with the index declaration (#1128).
|
|
1165
|
+
*/
|
|
1166
|
+
async function runNearest(binding, state, field, vector, options) {
|
|
1167
|
+
const plan = await resolvePlan(binding, state);
|
|
1168
|
+
if (plan === null)
|
|
1169
|
+
return [];
|
|
1170
|
+
const near = await resolveNearest(field, vector, options, resolveContext(binding, binding.context._isSudo !== true));
|
|
1171
|
+
const ops = await whereCombinators();
|
|
1172
|
+
const vectors = await vectorLowering();
|
|
1173
|
+
const bound = near.distanceBound;
|
|
1174
|
+
// Derived from the resolved plan rather than rebuilt beside it, so a member
|
|
1175
|
+
// added to `ReadPlan` reaches this query too.
|
|
1176
|
+
let collection = scope(binding, {
|
|
1177
|
+
...plan,
|
|
1178
|
+
predicates: [...plan.predicates, present(near)],
|
|
1179
|
+
// The score is recomputed from the row's own vector, so the column has
|
|
1180
|
+
// to survive the projection even when the caller did not name it. It is
|
|
1181
|
+
// outside `projection.caller`, so Field Visibility strips it back out.
|
|
1182
|
+
projection: plan.projection.columns === undefined || plan.projection.columns.includes(near.column)
|
|
1183
|
+
? plan.projection
|
|
1184
|
+
: { ...plan.projection, columns: [...plan.projection.columns, near.column] },
|
|
1185
|
+
}, ops, NEAREST_DISPOSITIONS, refuseUnrankable(binding.listName)).orderBy([
|
|
1186
|
+
(model) => vectors.order(near, model),
|
|
1187
|
+
...plan.orders.map((order) => (model) => lowerOrder(order, model)),
|
|
1188
|
+
]);
|
|
1189
|
+
if (bound !== null) {
|
|
1190
|
+
collection = collection.where((model) => vectors.bound(near, model, bound));
|
|
1191
|
+
}
|
|
1192
|
+
const rows = await engineRead(binding, () => collection.limit(near.limit).all());
|
|
1193
|
+
const scores = rows.map((row) => score(near, row));
|
|
1194
|
+
const items = await visibleRows(binding, rows, plan);
|
|
1195
|
+
return items.map((item, index) => ({ item, score: scores[index] }));
|
|
1196
|
+
}
|
|
1197
|
+
function score(near, row) {
|
|
1198
|
+
const stored = requireVector(row[near.column], near.listName, near.column);
|
|
1199
|
+
return distanceToScore(near.distanceFunction, vectorDistance(near.distanceFunction, stored, near.vector));
|
|
1200
|
+
}
|
|
1201
|
+
/**
|
|
1202
|
+
* `SecuredQuery<VisibleRow>` rather than the default-instantiated
|
|
1203
|
+
* `SecuredQuery` — the row type this function's own terminals promise is the
|
|
1204
|
+
* branded one only {@link visibleRows} mints (issue #1386), so a future
|
|
1205
|
+
* terminal wired in here that returns a plain `OrmRow` fails this function's
|
|
1206
|
+
* own return type rather than merely the public one {@link createSecuredRead}
|
|
1207
|
+
* widens to below.
|
|
1208
|
+
*/
|
|
1209
|
+
function query(binding, state) {
|
|
1210
|
+
return {
|
|
1211
|
+
where: (predicate) => query(binding, { ...state, predicates: [...state.predicates, predicate] }),
|
|
1212
|
+
orderBy: (order) => query(binding, { ...state, orders: [...state.orders, ...orderList(order)] }),
|
|
1213
|
+
select: (...fields) => query(binding, { ...state, fields }),
|
|
1214
|
+
limit: (count) => query(binding, { ...state, limit: count }),
|
|
1215
|
+
offset: (count) => query(binding, { ...state, offset: count }),
|
|
1216
|
+
include: (name, refinement) => query(binding, {
|
|
1217
|
+
...state,
|
|
1218
|
+
includes: [...state.includes, buildIncludeRequest(name, refinement)],
|
|
1219
|
+
}),
|
|
1220
|
+
distinct: (...fields) => query(binding, { ...state, distincts: [...state.distincts, { kind: 'all', fields }] }),
|
|
1221
|
+
distinctOn: (...fields) => query(binding, { ...state, distincts: [...state.distincts, { kind: 'on', fields }] }),
|
|
1222
|
+
cursor: (values) => query(binding, { ...state, cursor: values }),
|
|
1223
|
+
forUpdate: () => query(binding, { ...state, lock: true }),
|
|
1224
|
+
all: () => runAll(binding, state),
|
|
1225
|
+
first: () => runFirst(binding, state),
|
|
1226
|
+
nearest: (field, vector, options = {}) => runNearest(binding, state, field, vector, options),
|
|
1227
|
+
aggregate: (build) => runAggregate(binding, state, build),
|
|
1228
|
+
};
|
|
1229
|
+
}
|
|
1230
|
+
/**
|
|
1231
|
+
* The read members of one list's secured surface, bound to `ormHandle` and
|
|
1232
|
+
* `context`. The collection is held by the closure and never handed out: no
|
|
1233
|
+
* `Collection` and no `CollectionState` is reachable from the returned value
|
|
1234
|
+
* or its type (ADR-0041, ADR-0057).
|
|
1235
|
+
*/
|
|
1236
|
+
export function createSecuredRead(binding) {
|
|
1237
|
+
return query(binding, {
|
|
1238
|
+
predicates: [],
|
|
1239
|
+
orders: [],
|
|
1240
|
+
includes: [],
|
|
1241
|
+
distincts: [],
|
|
1242
|
+
lock: false,
|
|
1243
|
+
});
|
|
1244
|
+
}
|
|
1245
|
+
//# sourceMappingURL=read.js.map
|