@opensaas/stack-core 0.43.0 → 0.44.0-alpha.1
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 +2720 -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 +966 -614
- 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 +540 -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 +282 -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 +135 -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 +457 -677
- 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 +221 -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
package/CLAUDE.md
DELETED
|
@@ -1,609 +0,0 @@
|
|
|
1
|
-
# @opensaas/stack-core
|
|
2
|
-
|
|
3
|
-
Core stack providing config system, access control engine, hooks, field types, and generators.
|
|
4
|
-
|
|
5
|
-
## Purpose
|
|
6
|
-
|
|
7
|
-
The foundation of OpenSaas Stack. Defines the config DSL, executes access control, runs hooks, and generates Prisma schema and TypeScript types from config.
|
|
8
|
-
|
|
9
|
-
## Entry Points
|
|
10
|
-
|
|
11
|
-
The package exposes a curated surface across several import paths. Use the narrowest one that fits:
|
|
12
|
-
|
|
13
|
-
- **`@opensaas/stack-core`** (root) — the everyday consumer surface: `config`, `list`, `getContext`, the naming helpers (`getDbKey`, `getUrlKey`, `getListKeyFromUrl`), `ValidationError`, and the config/access types you annotate with (`OpenSaasConfig`, `ListConfig`, `FieldConfig`, `AccessControl`, `FieldAccess`, `Session`, `AccessContext`, `PrismaFilter`, `OperationAccess`).
|
|
14
|
-
- **`@opensaas/stack-core/fields`** — field builder functions (`text()`, `integer()`, …) and their config types (`TextField`, `IntegerField`, `DecimalField`, `CalendarDayField`, …, plus `PrismaRelationResult`). The builders and the types they produce live together here.
|
|
15
|
-
- **`@opensaas/stack-core/extend`** — authoring contracts: implement these to build a plugin (`Plugin`, `PluginContext`, `GeneratedFiles`) or a third-party field package (`BaseFieldConfig`, `TypeInfo`, `TypeDescriptor`).
|
|
16
|
-
- **`@opensaas/stack-core/mcp`** — MCP runtime handlers.
|
|
17
|
-
- **`@opensaas/stack-core/internal`** — `@internal` plumbing shared between the `@opensaas/*` packages and generated `.opensaas/` code. **No semver guarantees**; application code should never import from here.
|
|
18
|
-
|
|
19
|
-
`Session` deliberately stays on the root entry point because it is the module-augmentation target (`declare module '@opensaas/stack-core'`).
|
|
20
|
-
|
|
21
|
-
## Key Files & Exports
|
|
22
|
-
|
|
23
|
-
### Config (`src/config/`)
|
|
24
|
-
|
|
25
|
-
- `types.ts` - Core type definitions (`OpenSaasConfig`, `ListConfig`, `FieldConfig`, etc.)
|
|
26
|
-
- `index.ts` - Builder functions (`config()`, `list()`)
|
|
27
|
-
- `schema.ts` - Zod schemas for validation
|
|
28
|
-
|
|
29
|
-
### Fields (`src/fields/index.ts`)
|
|
30
|
-
|
|
31
|
-
Field builder functions, each returning object with:
|
|
32
|
-
|
|
33
|
-
- `getZodSchema(fieldName, operation)` - Validation schema
|
|
34
|
-
- `getPrismaType(fieldName)` - Prisma type and modifiers
|
|
35
|
-
- `getTypeScriptType()` - TypeScript type and optionality
|
|
36
|
-
|
|
37
|
-
Built-in fields:
|
|
38
|
-
|
|
39
|
-
- `text({ validation, ui, hooks })` - String field
|
|
40
|
-
- `integer({ validation, ui, hooks })` - Number field
|
|
41
|
-
- `checkbox({ defaultValue, ui, hooks })` - Boolean field
|
|
42
|
-
- `timestamp({ defaultValue, ui, hooks })` - DateTime field
|
|
43
|
-
- `password({ validation, ui, hooks })` - Hashed password (excluded from reads)
|
|
44
|
-
- `select({ options, validation, ui, hooks })` - Enum field
|
|
45
|
-
- `relationship({ ref, many, ui })` - Foreign key relationship
|
|
46
|
-
- `json({ validation, ui, hooks })` - JSON field for arbitrary JSON data
|
|
47
|
-
- `virtual({ type, hooks })` - Computed field not stored in database
|
|
48
|
-
|
|
49
|
-
### Access Control (`src/access/`)
|
|
50
|
-
|
|
51
|
-
- `engine.ts` - Core execution logic (`applyAccessControl()`)
|
|
52
|
-
- `types.ts` - Type definitions (`AccessControl`, `OperationAccessControl`, `Session`, etc.)
|
|
53
|
-
|
|
54
|
-
Access control functions receive `{ session, context, item, operation }` and return:
|
|
55
|
-
|
|
56
|
-
- `boolean` - Allow/deny
|
|
57
|
-
- `Prisma filter object` - Scope access to matching records
|
|
58
|
-
|
|
59
|
-
A plugin's `runtime(context, sudo)` factory receives a `sudo` helper as a plain **second argument** — `sudo(): AccessContext` returns an access-bypassing (but still hook-firing) context, for reads/writes that must not depend on the caller's own list access policy (e.g. an auth plugin resolving "who is this session" independent of the User list's access rules). It's the same escape hatch as `StackContext.sudo()`, exposed one layer lower. It is deliberately **not** a method on `AccessContext` itself — a self-referential `sudo(): AccessContext` field on that shared, widely-instantiated interface was found to break TypeScript's structural checking of unrelated generated Prisma types (nullable JSON `CreateInput` fields) in a downstream app.
|
|
60
|
-
|
|
61
|
-
### Session Typing (`src/access/types.ts`)
|
|
62
|
-
|
|
63
|
-
The `Session` interface can be augmented to provide type safety and autocomplete for session fields.
|
|
64
|
-
|
|
65
|
-
**Default:** Session is a permissive object: `interface Session { [key: string]: unknown }`
|
|
66
|
-
|
|
67
|
-
**Module Augmentation Pattern:**
|
|
68
|
-
|
|
69
|
-
```typescript
|
|
70
|
-
// types/session.d.ts (create this file in your project)
|
|
71
|
-
import '@opensaas/stack-core'
|
|
72
|
-
|
|
73
|
-
declare module '@opensaas/stack-core' {
|
|
74
|
-
interface Session {
|
|
75
|
-
userId: string
|
|
76
|
-
email: string
|
|
77
|
-
role: 'admin' | 'user'
|
|
78
|
-
organizationId?: string
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
**Benefits:**
|
|
84
|
-
|
|
85
|
-
- Autocomplete in access control functions
|
|
86
|
-
- Type safety when accessing session properties
|
|
87
|
-
- Single source of truth for session shape
|
|
88
|
-
- Works with any auth provider (Better Auth, custom, etc.)
|
|
89
|
-
|
|
90
|
-
**Usage after augmentation:**
|
|
91
|
-
|
|
92
|
-
```typescript
|
|
93
|
-
// Access control - fully typed
|
|
94
|
-
const isAdmin: AccessControl = ({ session }) => {
|
|
95
|
-
return session?.role === 'admin' // ✅ 'role' is typed as 'admin' | 'user'
|
|
96
|
-
// ↑ Autocomplete shows: userId, email, role, organizationId
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// In server actions
|
|
100
|
-
const context = await getContext(session)
|
|
101
|
-
context.session?.email // ✅ Typed as string
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
**For Better Auth users:** See `@opensaas/stack-auth` documentation for examples of extracting Better Auth session types.
|
|
105
|
-
|
|
106
|
-
### Hooks (`src/hooks/`)
|
|
107
|
-
|
|
108
|
-
- `index.ts` - Hook execution logic
|
|
109
|
-
- Types in `config/types.ts`
|
|
110
|
-
|
|
111
|
-
Hook types:
|
|
112
|
-
|
|
113
|
-
- `resolveInput` - Transform input data
|
|
114
|
-
- `resolveOutput` - Transform output data
|
|
115
|
-
- `validateInput` - Custom validation
|
|
116
|
-
- `beforeOperation` - Side effects before DB operation
|
|
117
|
-
- `afterOperation` - Side effects after DB operation
|
|
118
|
-
|
|
119
|
-
### Context (`src/context/index.ts`)
|
|
120
|
-
|
|
121
|
-
- `createContext(config, prisma, session?)` - Creates context wrapper
|
|
122
|
-
- Returns `{ db, session }` where `db` is Prisma client with access control
|
|
123
|
-
- `context.transaction(fn, options?)` - Interactive, hook-firing transaction (see below)
|
|
124
|
-
|
|
125
|
-
#### Interactive transactions (`context.transaction`)
|
|
126
|
-
|
|
127
|
-
`context.transaction(async (txContext) => { … }, { isolationLevel })` runs the
|
|
128
|
-
callback inside **one** Prisma interactive transaction. `txContext` is a full
|
|
129
|
-
context whose `db.*` operations are access-checked and hook-firing exactly like
|
|
130
|
-
the request context, but persist against the transaction client — so every write
|
|
131
|
-
in the callback is **atomic** and a throw anywhere rolls the whole transaction
|
|
132
|
-
back. This is the secured alternative to raw `prisma.$transaction`, which
|
|
133
|
-
bypasses access control and hooks.
|
|
134
|
-
|
|
135
|
-
Use it to atomically enforce concurrency-sensitive invariants (e.g. a
|
|
136
|
-
capacity/quota gate) while preserving the access/hook boundary:
|
|
137
|
-
|
|
138
|
-
```typescript
|
|
139
|
-
async function bookSlot(context: StackContext, slotId: string) {
|
|
140
|
-
for (let attempt = 0; attempt < 5; attempt++) {
|
|
141
|
-
try {
|
|
142
|
-
return await context.transaction(
|
|
143
|
-
async (tx) => {
|
|
144
|
-
const count = await tx.db.booking.count({ where: { slotId } })
|
|
145
|
-
if (count >= CAPACITY) return { booked: false }
|
|
146
|
-
return { booked: true, item: await tx.db.booking.create({ data: { slotId } }) }
|
|
147
|
-
},
|
|
148
|
-
{ isolationLevel: 'Serializable' },
|
|
149
|
-
)
|
|
150
|
-
} catch (err) {
|
|
151
|
-
// Serialization failures (Prisma P2034) PROPAGATE — the caller owns retry.
|
|
152
|
-
if (err && typeof err === 'object' && 'code' in err && err.code === 'P2034') continue
|
|
153
|
-
throw err
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
throw new Error('exceeded retry budget')
|
|
157
|
-
}
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
Key points:
|
|
161
|
-
|
|
162
|
-
- **Options pass through** to Prisma — `isolationLevel` (incl. `'Serializable'`),
|
|
163
|
-
`maxWait`, `timeout`.
|
|
164
|
-
- **Serialization failures propagate** (they are NOT converted to a silent
|
|
165
|
-
`null`), so the caller can implement a retry loop. Built-in retry is not
|
|
166
|
-
provided — the caller owns it (matching Keystone 6's `context.transaction`).
|
|
167
|
-
- **Nested `context.db` writes join** the outer transaction (a Prisma tx client
|
|
168
|
-
exposes no `$transaction`, so the Write Pipeline's existing fallback runs them
|
|
169
|
-
against the active client).
|
|
170
|
-
- If the client cannot open an interactive transaction (e.g. a test mock, or you
|
|
171
|
-
are already inside one), `fn` runs directly with identical hook/access
|
|
172
|
-
semantics. See ADR-0012.
|
|
173
|
-
- **A transaction-boundary `afterTransaction` reports the OUTERMOST transaction,
|
|
174
|
-
not its own write's return (ADR-0028).** A write nested in `context.transaction()`
|
|
175
|
-
(or a hook's own `context.db` write) cannot itself observe when the enclosing
|
|
176
|
-
transaction settles, so its `afterTransaction` is deferred until the owner —
|
|
177
|
-
`context.transaction()`, or the Write Pipeline when it opened the transaction —
|
|
178
|
-
observes the real commit/rollback. `beforeTransaction` stays eager in every
|
|
179
|
-
case, so under `context.transaction()` it runs with that transaction already
|
|
180
|
-
open (keep it fast; a `context.db` write from it can block on rows the
|
|
181
|
-
transaction itself is writing). Status is a conjunction — `committed` iff the
|
|
182
|
-
write itself succeeded AND the enclosing transaction committed, the write's
|
|
183
|
-
own error always winning otherwise — and the deferred `item` is the row as
|
|
184
|
-
that write persisted it, not re-read at flush (so a later same-record write in
|
|
185
|
-
the same transaction leaves it stale). Because of this, a **rejected
|
|
186
|
-
`context.transaction()` no longer implies rollback**: a deferred hook that
|
|
187
|
-
throws after a successful commit rejects the call with `AfterTransactionError`
|
|
188
|
-
over already-final data, though a transaction/serialization error (e.g.
|
|
189
|
-
`P2034`) still takes precedence and propagates unwrapped. A write with no
|
|
190
|
-
transaction owner at all (an app-managed `prisma.$transaction`, or a client —
|
|
191
|
-
e.g. a bare test mock — that cannot open one) still fires `afterTransaction`
|
|
192
|
-
optimistically at write time, unchanged. See ADR-0028 and the hooks concept
|
|
193
|
-
doc.
|
|
194
|
-
|
|
195
|
-
#### Hook context is the same secured context as a transaction's (#1176)
|
|
196
|
-
|
|
197
|
-
A list/field `resolveInput` / `validate` / `beforeOperation` / `afterOperation`
|
|
198
|
-
hook's `context` argument is a full `StackContext` — `sudo()`, `withSession()`,
|
|
199
|
-
`transaction()` and `serverAction` are all present, exactly like the `txContext`
|
|
200
|
-
a `context.transaction()` callback receives — bound to the WRITE'S OWN
|
|
201
|
-
transaction client, not the base one. This amends ADR-0012: the Write
|
|
202
|
-
Pipeline's transaction rebind (`bindContextToTransaction`) goes through the
|
|
203
|
-
same `getContext` factory `context.transaction()` already rebuilds through,
|
|
204
|
-
instead of hand-assembling a bare `AccessContext` object literal.
|
|
205
|
-
|
|
206
|
-
```typescript
|
|
207
|
-
Order: list({
|
|
208
|
-
hooks: {
|
|
209
|
-
beforeOperation: async ({ context }) => {
|
|
210
|
-
// Elevated AND atomic with this write: rolls back together if the
|
|
211
|
-
// write (or a later hook in it) throws — unlike `getContext(session)
|
|
212
|
-
// .sudo()`, which would bind to a SEPARATE connection.
|
|
213
|
-
await context.sudo().db.auditLog.create({ data: { action: 'order-write' } })
|
|
214
|
-
},
|
|
215
|
-
},
|
|
216
|
-
})
|
|
217
|
-
```
|
|
218
|
-
|
|
219
|
-
- `context.sudo()` / `context.withSession(s)` from one of these hooks stays
|
|
220
|
-
bound to the SAME transaction client the hook itself was given — carrying
|
|
221
|
-
the write's transaction owner (ADR-0028, so a write through them defers its
|
|
222
|
-
`afterTransaction` to that owner) and the hook's own resolve chain
|
|
223
|
-
(ADR-0023, so a write issued from inside a `resolveOutput` hook keeps that
|
|
224
|
-
hook's cycle-guard chain into its own Field Visibility pass).
|
|
225
|
-
- `context.transaction(fn)` called from inside one of these hooks **joins**
|
|
226
|
-
the write's transaction (the existing "no interactive client → run
|
|
227
|
-
directly" fallback) — it never opens a nested one.
|
|
228
|
-
- Plugin runtimes are **not** re-executed on this rebind (same rule as the
|
|
229
|
-
nested-write rebind ADR-0010 already established).
|
|
230
|
-
- **Unaffected:** `beforeTransaction` / `afterTransaction` (list and field) keep
|
|
231
|
-
the plain `AccessContext`, bound to the BASE client, always — see ADR-0028
|
|
232
|
-
for why boundary hooks must not run through a client that may already be
|
|
233
|
-
closed by flush time.
|
|
234
|
-
- **A field's `resolveOutput` keeps the plain `AccessContext` type**, but which
|
|
235
|
-
client it's bound to already depended — before this record and after it
|
|
236
|
-
alike — on how the read that triggered it arose: a plain top-level read
|
|
237
|
-
resolves against the base client; a `resolveOutput` that runs as part of a
|
|
238
|
-
create/update's OWN result (the write's Field Visibility pass, Phase 11)
|
|
239
|
-
resolves against THAT write's transaction client (ADR-0010) — so a
|
|
240
|
-
`context.db` read/write from inside such a hook is already atomic with the
|
|
241
|
-
write, same as `beforeOperation`/`afterOperation`. Not a consequence of this
|
|
242
|
-
record; stated here only so it isn't mistaken for one.
|
|
243
|
-
|
|
244
|
-
See ADR-0066 and the hooks concept doc's "In-transaction vs
|
|
245
|
-
transaction-boundary hooks" section.
|
|
246
|
-
|
|
247
|
-
#### Substituting a session (`context.withSession`, #980)
|
|
248
|
-
|
|
249
|
-
`context.withSession(session)` sits beside `sudo()` on the other axis:
|
|
250
|
-
`sudo()` keeps the session and drops access control; `withSession()` keeps
|
|
251
|
-
access control (and hooks) and swaps the session. It reuses the receiver's
|
|
252
|
-
already-resolved config, client (including a transaction client — a call
|
|
253
|
-
inside `context.transaction()` stays in that transaction), and storage.
|
|
254
|
-
|
|
255
|
-
Like `sudo()` (and unlike `transaction()`), plugin runtime services are
|
|
256
|
-
**rebuilt, not reused** — `plugin.runtime(context, sudo)` factories are
|
|
257
|
-
re-run so any plugin service that closes over the session (e.g. an auth
|
|
258
|
-
plugin's "who is this session" lookup) binds to the _new_ one rather than
|
|
259
|
-
staying stale. A hot loop calling `withSession()` per iteration re-runs
|
|
260
|
-
every plugin's `runtime()` each time; batch via a shared derived context
|
|
261
|
-
where the loop body doesn't need a different session per iteration.
|
|
262
|
-
|
|
263
|
-
```typescript
|
|
264
|
-
// An unattended dispatcher running a committed intent, or any job runner
|
|
265
|
-
// that is legitimately authorised but arrives without the session a list's
|
|
266
|
-
// validate hook wants to see in `args.context.session`.
|
|
267
|
-
async function dispatchJob(context: StackContext, job: { ownerSession: Session }) {
|
|
268
|
-
const asOwner = context.withSession(job.ownerSession)
|
|
269
|
-
await asOwner.db.task.update({ where: { id: job.taskId }, data: { status: 'done' } })
|
|
270
|
-
}
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
**`withSession` grants no authority of its own.** It is not an
|
|
274
|
-
authorisation — the derived context can do exactly what any context built
|
|
275
|
-
with that session directly could do; access rules still evaluate against
|
|
276
|
-
the new session. The application decides who may call it.
|
|
277
|
-
|
|
278
|
-
**Orthogonal to `sudo()`.** `context.withSession(s).sudo()` and
|
|
279
|
-
`context.sudo().withSession(s)` are equivalent — both elevated and both
|
|
280
|
-
carrying `s`. `withSession` preserves the receiver's sudo state rather than
|
|
281
|
-
resetting it: called on an already-sudo context it stays sudo, called on a
|
|
282
|
-
plain context it does not grant sudo.
|
|
283
|
-
|
|
284
|
-
`withSession(null)` is a legitimate way to drop to an anonymous context.
|
|
285
|
-
|
|
286
|
-
### Generators (`src/generator/`)
|
|
287
|
-
|
|
288
|
-
- `prisma.ts` - Generates `prisma/schema.prisma` from config
|
|
289
|
-
- `types.ts` - Generates `.opensaas/types.ts` TypeScript definitions
|
|
290
|
-
- `context.ts` - Generates `.opensaas/context.ts` context factory
|
|
291
|
-
|
|
292
|
-
Run via CLI: `pnpm generate`
|
|
293
|
-
|
|
294
|
-
### Utilities (`src/lib/case-utils.ts`)
|
|
295
|
-
|
|
296
|
-
Public naming helpers (exported from the root entry point):
|
|
297
|
-
|
|
298
|
-
- `getDbKey(listKey)` - PascalCase → camelCase (e.g., `BlogPost` → `blogPost`)
|
|
299
|
-
- `getUrlKey(listKey)` - PascalCase → kebab-case (e.g., `BlogPost` → `blog-post`)
|
|
300
|
-
- `getListKeyFromUrl(urlKey)` - kebab-case → PascalCase (e.g., `blog-post` → `BlogPost`)
|
|
301
|
-
|
|
302
|
-
The lower-level converters (`pascalToCamel`, `pascalToKebab`, `kebabToPascal`, `kebabToCamel`) are internal plumbing on `@opensaas/stack-core/internal`.
|
|
303
|
-
|
|
304
|
-
## Architecture Patterns
|
|
305
|
-
|
|
306
|
-
### Field Self-Containment
|
|
307
|
-
|
|
308
|
-
Fields are fully self-contained. No switch statements in core:
|
|
309
|
-
|
|
310
|
-
```typescript
|
|
311
|
-
// Field defines its own behavior
|
|
312
|
-
export function text(options) {
|
|
313
|
-
return {
|
|
314
|
-
type: 'text',
|
|
315
|
-
...options,
|
|
316
|
-
getPrismaType: () => ({ type: 'String', modifiers: '?' }),
|
|
317
|
-
getTypeScriptType: () => ({ type: 'string', optional: true }),
|
|
318
|
-
getZodSchema: (fieldName, operation) => z.string().optional(),
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
// Generator delegates to field
|
|
323
|
-
const prismaType = field.getPrismaType(fieldName)
|
|
324
|
-
```
|
|
325
|
-
|
|
326
|
-
### Access Control Execution Flow
|
|
327
|
-
|
|
328
|
-
1. User calls `context.db.post.update({ where, data })`
|
|
329
|
-
2. Context wrapper intercepts call
|
|
330
|
-
3. Check operation-level access → returns boolean or filter
|
|
331
|
-
4. Merge filter with user's `where` clause
|
|
332
|
-
5. Execute Prisma operation
|
|
333
|
-
6. Apply field-level read access (filter readable fields)
|
|
334
|
-
7. Return result or `null`/`[]` on access denial
|
|
335
|
-
|
|
336
|
-
### Hook Execution Order (Write)
|
|
337
|
-
|
|
338
|
-
1. List `resolveInput`
|
|
339
|
-
2. Field `resolveInput` (e.g., hash password)
|
|
340
|
-
3. List `validate`
|
|
341
|
-
4. Field `validate`
|
|
342
|
-
5. Field validation (isRequired, length, min/max)
|
|
343
|
-
6. Field-level access control (filter writable fields)
|
|
344
|
-
7. Field `beforeOperation`
|
|
345
|
-
8. List `beforeOperation`
|
|
346
|
-
9. **Database operation**
|
|
347
|
-
10. List `afterOperation`
|
|
348
|
-
11. Field `afterOperation`
|
|
349
|
-
|
|
350
|
-
### Hook Execution Order (Read)
|
|
351
|
-
|
|
352
|
-
Reads run no `afterOperation` (list or field):
|
|
353
|
-
|
|
354
|
-
1. **Database operation**
|
|
355
|
-
2. Field-level access control (filter readable fields)
|
|
356
|
-
3. Field `resolveOutput`
|
|
357
|
-
|
|
358
|
-
### Narrowing Reads (`select` is not honoured)
|
|
359
|
-
|
|
360
|
-
`context.db` reads (`findUnique`, `findMany`) do **not** apply Prisma's `select` semantics. Narrow a read with `include` (for relationships) or a fragment `query` instead. Passing `select` is a visible no-op: the op logs a one-time `console.warn` and still returns the full, access-filtered record. Field-level visibility is always enforced by access control regardless of `select`, so there is no leak — only a correctness/perf footgun the warning surfaces.
|
|
361
|
-
|
|
362
|
-
### A Bare Read Fetches Scalars, Not Relations (ADR-0024)
|
|
363
|
-
|
|
364
|
-
A read with no `include` and no fragment `query` returns the row's own columns plus its virtual fields — **never relations** — matching Prisma's own default. `findUnique`, `findMany`, and a singleton's `get()` all follow this rule uniformly, under sudo and under a session alike. Relations are fetched only when a caller names them via `include` or a fragment `query`, at which point the caller-directed access-scoping walk (`buildAccessScopedInclude`, ADR-0026) applies (#566/#830 unaffected). Foreign-key columns (e.g. `authorId`) are unaffected and always returned, so a relation stays reachable by id without an `include`. A `resolveOutput` hook that issues its own bare `context.db` read is subject to the same rule — reading `item.<relation>` inside such a hook silently returns `undefined` unless the hook's own read names that relation. See `docs/adr/0024-a-read-with-no-include-fetches-scalars-not-relations.md`.
|
|
365
|
-
|
|
366
|
-
### Naming a Relation Fetches Its Columns, Not Its Subtree (ADR-0026)
|
|
367
|
-
|
|
368
|
-
The bare-read rule above applies at **every level**, not only the root: naming a relation in an `include` fetches that relation's own columns and stops — its own further relations are returned only if the request nests an `include` for them too. `include: { author: true }` returns `author`'s scalar columns; reaching `author.organization` means writing `include: { author: { include: { organization: true } } }`. This is the "One hop" rule (see the glossary entry in `CONTEXT.md`).
|
|
369
|
-
|
|
370
|
-
The read pipeline is **caller-directed**: `buildAccessScopedInclude` walks only the branches a request (a caller `include`, a fragment `query`'s projection, or a field's folded `needs`) itself names, and never evaluates a related list's operation-level `query` access for a relation nobody asked for. There is no separate "build the full access-scoped tree for the whole list, then reconcile against what was requested" pass — the old `buildIncludeWithAccessControl` + `mergeIncludeWithAccessControl` two-step this replaced. `READ_INCLUDE_MAX_DEPTH` is a cost limit on how deep a request may reach, not a security boundary: nothing walks the relationship graph unprompted anymore, so there is no unscoped subtree left to fail open on past the cap — a request naming a relation at or beyond the cap still throws `AccessScopeDepthExceededError` (ADR-0022), now worded as a cost refusal rather than a scoping failure.
|
|
371
|
-
|
|
372
|
-
**Migration note (silent break):** an `include` that named a relation bare and read past it one hop (`item.<named>[0].<unnamed>`) now gets `undefined` for the unnamed part — no error. Grep for caller includes whose consumers read past the relation actually named, and add the deeper relation explicitly to the `include`.
|
|
373
|
-
|
|
374
|
-
A computed field's declared dependency (`needs`, ADR-0025, below) folds in at **every** relation it's reached through, including one added purely to satisfy another field's own `needs` — the fold recurses through `foldDeclaredDependencies` rather than riding a caller-named relation's auto-expanded subtree, since nothing auto-expands anymore. See `docs/adr/0026-naming-a-relation-fetches-its-columns-not-its-subtree.md`.
|
|
375
|
-
|
|
376
|
-
### A Computed Field Runs Only When It Is Going To Be Returned (ADR-0027)
|
|
377
|
-
|
|
378
|
-
A computed field — any field carrying a `resolveOutput` hook, virtual or not — is computed **if and only if the read is actually going to return it**, and its declared relations (`needs`) are fetched under exactly the same condition. A fragment `query` selecting three fields runs only those three fields' hooks (and folds only their `needs`); a field it doesn't select does no work at all — neither its field-level `read` access nor its hook runs. This is **projection-aware, never access-aware**: a fragment's own field selection is the only thing that restricts a level this way. A bare read or an `include`-based read is unaffected — every computed field on the list still computes, exactly as before, since neither ever had a narrower field selection to restrict by. The rule applies at every nesting level: a nested fragment selecting a subset computes only that subset there; a nested `include` still computes every computed field at that level.
|
|
379
|
-
|
|
380
|
-
**A computed field's hook never sees another computed field's resolved output**, on any read path — only the row's stored columns and its own declared dependencies. A sibling field that was skipped (unselected by a fragment) or denied by field-level access is absent from what the hook sees, never present holding its raw pre-hook value — reaching for it finds nothing there, the same as reaching for a relation never declared via `needs`. Before this, a virtual field received the already-assembled, already-resolved object, so a virtual field could accidentally read an earlier-declared virtual's resolved value purely by declaration order; reordering two such fields silently changed the result. That accidental coupling is gone: recompute from the stored columns both fields share instead.
|
|
381
|
-
|
|
382
|
-
A hookless virtual field (one with `access.read` but no `resolveOutput`) has its read access evaluated on no read at all — such a field can never produce output, so there's nothing to preserve access side effects for. See `docs/adr/0027-a-computed-field-runs-only-when-it-is-going-to-be-returned.md` and the "Computed field" glossary entry in `CONTEXT.md`.
|
|
383
|
-
|
|
384
|
-
### Context Type Safety
|
|
385
|
-
|
|
386
|
-
Context uses generic typing to preserve Prisma types:
|
|
387
|
-
|
|
388
|
-
```typescript
|
|
389
|
-
const context = createContext<typeof prisma>(config, prisma, session)
|
|
390
|
-
// context.db.post.findMany() is fully typed
|
|
391
|
-
```
|
|
392
|
-
|
|
393
|
-
## Integration Points
|
|
394
|
-
|
|
395
|
-
### With @opensaas/stack-ui
|
|
396
|
-
|
|
397
|
-
- UI reads config to generate admin interface
|
|
398
|
-
- Field `ui` options pass through to components
|
|
399
|
-
- Component registry pattern for field rendering
|
|
400
|
-
|
|
401
|
-
### With @opensaas/stack-auth
|
|
402
|
-
|
|
403
|
-
- Auth merges lists into config
|
|
404
|
-
- Session flows through context to access control
|
|
405
|
-
- Generator creates auth tables in Prisma schema
|
|
406
|
-
|
|
407
|
-
### With MCP (Model Context Protocol)
|
|
408
|
-
|
|
409
|
-
- Core provides auth-agnostic MCP runtime via `@opensaas/stack-core/mcp`
|
|
410
|
-
- MCP handler reads config to derive tools at request time (CRUD per list, list `mcp.customTools`, plugin-registered tools)
|
|
411
|
-
- Uses context for all operations (access control enforced); writes are validated by the normal `context.db` pipeline (field Zod schemas, hooks, access control)
|
|
412
|
-
- Custom tools may declare a Zod `inputSchema` — validated on `tools/call`, converted to JSON Schema for `tools/list` — or a plain JSON Schema object
|
|
413
|
-
- Auth adapters (like `@opensaas/stack-auth/mcp`) provide session integration; custom session fields pass through to access control
|
|
414
|
-
- The derived `query` tool accepts an optional `fields` projection (`packages/core/src/mcp/projection.ts`) — the wire form of a fragment field selection, translated into a `context.db` `include` so the ordinary read pipeline (access scoping, `needs` folding, the depth cap) applies with no parallel read path. The generated schema enumerates two fixed levels of each list's vocabulary, per session; `tools/list` itself is per-session as a result (ADR-0033)
|
|
415
|
-
|
|
416
|
-
### The MCP `query` Tool's `fields` Projection (ADR-0033)
|
|
417
|
-
|
|
418
|
-
An optional `fields` argument on the derived `query` tool narrows (or widens, up to two levels) what a read returns, in the wire form of a fragment field selection: `{ scalarField: true, relation: { fields: {...} } }`. A to-many relation additionally accepts `where`/`orderBy`/`take`/`skip` and a `count`. Omitting `fields` is unchanged — a bare read, per ADR-0024.
|
|
419
|
-
|
|
420
|
-
`projection.ts` does the work in two passes, both walking the same per-session vocabulary (`relatedListIfVisible`):
|
|
421
|
-
|
|
422
|
-
- `generateFieldsProjectionSchema` builds the JSON Schema advertised on `tools/list` — a relation whose target list denies this session's operation-level `query` access, or has `mcp.enabled: false`, is omitted from the vocabulary entirely, not merely left unusable.
|
|
423
|
-
- `resolveFieldsProjection` validates a caller's `fields` against that same vocabulary and translates it into a plain `context.db` `include` (deliberately NOT the fragment `query:` argument — going through the ordinary caller-`include` path is what gets `buildAccessScopedInclude`'s nested-`where` AND-fold, the to-one existence check, and the depth cap for free; the trade-off is that every computed field at a traversed level still computes server-side, same as any other `include`-based read, even one the projection didn't select — only the wire response is narrowed). Throws `McpProjectionRefusedError` (caught in the handler and turned into an `isError` tool result, never a JSON-RPC error) naming what was asked for and what's available on any mismatch.
|
|
424
|
-
|
|
425
|
-
A to-many's `count` is resolved separately via Prisma's `_count.select`, access-scoped by ANDing the related list's `query` access with any caller `where` on that same relation (mirroring, but not reusing, `relationship-count.ts`'s `_count` scoping — that module's own helper isn't exported and was built for a different feature, issue #732's admin list view).
|
|
426
|
-
|
|
427
|
-
### With Third-Party Field Packages
|
|
428
|
-
|
|
429
|
-
- Packages export field builders implementing `BaseFieldConfig` (imported from `@opensaas/stack-core/extend`)
|
|
430
|
-
- No changes needed to core - fields are self-contained
|
|
431
|
-
- Example: `@opensaas/stack-tiptap` provides `richText()` field
|
|
432
|
-
|
|
433
|
-
## Common Patterns
|
|
434
|
-
|
|
435
|
-
### Basic Config
|
|
436
|
-
|
|
437
|
-
```typescript
|
|
438
|
-
import { config, list } from '@opensaas/stack-core'
|
|
439
|
-
import { text, integer, relationship } from '@opensaas/stack-core/fields'
|
|
440
|
-
|
|
441
|
-
export default config({
|
|
442
|
-
db: {
|
|
443
|
-
provider: 'sqlite',
|
|
444
|
-
url: process.env.DATABASE_URL || 'file:./dev.db',
|
|
445
|
-
},
|
|
446
|
-
lists: {
|
|
447
|
-
Post: list({
|
|
448
|
-
fields: {
|
|
449
|
-
title: text({ validation: { isRequired: true } }),
|
|
450
|
-
views: integer({ defaultValue: 0 }),
|
|
451
|
-
author: relationship({ ref: 'User.posts' }),
|
|
452
|
-
},
|
|
453
|
-
access: {
|
|
454
|
-
operation: {
|
|
455
|
-
query: () => true,
|
|
456
|
-
create: ({ session }) => !!session,
|
|
457
|
-
update: ({ session, item }) => session?.userId === item.authorId,
|
|
458
|
-
},
|
|
459
|
-
},
|
|
460
|
-
}),
|
|
461
|
-
},
|
|
462
|
-
})
|
|
463
|
-
```
|
|
464
|
-
|
|
465
|
-
### Silent Failures
|
|
466
|
-
|
|
467
|
-
Access-controlled operations return `null` or `[]` instead of throwing:
|
|
468
|
-
|
|
469
|
-
```typescript
|
|
470
|
-
const post = await context.db.post.update({ where: { id }, data })
|
|
471
|
-
if (!post) {
|
|
472
|
-
// Either doesn't exist OR access denied
|
|
473
|
-
return { error: 'Access denied or not found' }
|
|
474
|
-
}
|
|
475
|
-
```
|
|
476
|
-
|
|
477
|
-
### Field-Level Hooks
|
|
478
|
-
|
|
479
|
-
```typescript
|
|
480
|
-
password: password({
|
|
481
|
-
hooks: {
|
|
482
|
-
resolveInput: async ({ value }) => {
|
|
483
|
-
if (value) return await bcrypt.hash(value, 10)
|
|
484
|
-
return value
|
|
485
|
-
},
|
|
486
|
-
resolveOutput: ({ value }) => {
|
|
487
|
-
return new HashedPassword(value) // Wrap for security
|
|
488
|
-
},
|
|
489
|
-
},
|
|
490
|
-
})
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
### Custom Prisma Client Constructor
|
|
494
|
-
|
|
495
|
-
```typescript
|
|
496
|
-
config({
|
|
497
|
-
db: {
|
|
498
|
-
provider: 'postgresql',
|
|
499
|
-
url: process.env.DATABASE_URL,
|
|
500
|
-
prismaClientConstructor: (PrismaClient) => {
|
|
501
|
-
const adapter = new PrismaNeon({ connectionString: process.env.DATABASE_URL })
|
|
502
|
-
return new PrismaClient({ adapter })
|
|
503
|
-
},
|
|
504
|
-
},
|
|
505
|
-
})
|
|
506
|
-
```
|
|
507
|
-
|
|
508
|
-
### Relationship Patterns
|
|
509
|
-
|
|
510
|
-
```typescript
|
|
511
|
-
// One-to-many
|
|
512
|
-
User: list({
|
|
513
|
-
fields: {
|
|
514
|
-
posts: relationship({ ref: 'Post.author', many: true }),
|
|
515
|
-
},
|
|
516
|
-
})
|
|
517
|
-
|
|
518
|
-
// Many-to-one (other side)
|
|
519
|
-
Post: list({
|
|
520
|
-
fields: {
|
|
521
|
-
author: relationship({ ref: 'User.posts' }),
|
|
522
|
-
},
|
|
523
|
-
})
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
### Virtual Fields
|
|
527
|
-
|
|
528
|
-
Virtual fields are computed fields that are not stored in the database:
|
|
529
|
-
|
|
530
|
-
```typescript
|
|
531
|
-
// Read-only computed field
|
|
532
|
-
User: list({
|
|
533
|
-
fields: {
|
|
534
|
-
firstName: text(),
|
|
535
|
-
lastName: text(),
|
|
536
|
-
fullName: virtual({
|
|
537
|
-
type: 'string', // TypeScript output type
|
|
538
|
-
hooks: {
|
|
539
|
-
resolveOutput: ({ item }) => `${item.firstName} ${item.lastName}`,
|
|
540
|
-
},
|
|
541
|
-
}),
|
|
542
|
-
},
|
|
543
|
-
})
|
|
544
|
-
|
|
545
|
-
// Usage
|
|
546
|
-
const user = await context.db.user.findUnique({ where: { id } })
|
|
547
|
-
console.log(user.fullName) // "John Doe" — computed via resolveOutput whenever the read returns it
|
|
548
|
-
```
|
|
549
|
-
|
|
550
|
-
**Key characteristics:**
|
|
551
|
-
|
|
552
|
-
- Not stored in database (no Prisma column created)
|
|
553
|
-
- Computed via `resolveOutput` on every bare/`include`-based read; on a fragment `query` read, only when the fragment selects it (ADR-0027) — `select` is still not honoured, narrow with `include`/fragment `query`
|
|
554
|
-
- Must provide `type` (TypeScript type string) and `resolveOutput` hook
|
|
555
|
-
- Can optionally provide `resolveInput` for write side effects
|
|
556
|
-
- Useful for derived values, computed properties, and external API sync
|
|
557
|
-
|
|
558
|
-
### Declaring Relation Dependencies (`needs`, ADR-0025)
|
|
559
|
-
|
|
560
|
-
Since ADR-0024, a bare read (no caller `include`) returns a row's own columns
|
|
561
|
-
only — never its relations. A virtual field whose `resolveOutput` reads a
|
|
562
|
-
relation off `item` (`item.lineItems`, `item.posts.length`, …) would
|
|
563
|
-
otherwise silently compute over `undefined` whenever the caller didn't happen
|
|
564
|
-
to include that relation. `needs` is the fix: declare the immediate
|
|
565
|
-
relations a field's hook cannot compute without, and the read fetches
|
|
566
|
-
exactly those — wherever that field is computed, at the root of a read and
|
|
567
|
-
at every nested level alike.
|
|
568
|
-
|
|
569
|
-
```typescript
|
|
570
|
-
Order: list({
|
|
571
|
-
fields: {
|
|
572
|
-
lineItems: relationship({ ref: 'LineItem.order', many: true }),
|
|
573
|
-
total: virtual({
|
|
574
|
-
type: 'number',
|
|
575
|
-
needs: ['lineItems'], // fetched for this hook, even on a bare read
|
|
576
|
-
hooks: {
|
|
577
|
-
resolveOutput: ({ item }) =>
|
|
578
|
-
item.lineItems.reduce((sum, li) => sum + li.price * li.quantity, 0),
|
|
579
|
-
},
|
|
580
|
-
}),
|
|
581
|
-
},
|
|
582
|
-
})
|
|
583
|
-
|
|
584
|
-
// The caller never asked for lineItems, and never receives it:
|
|
585
|
-
const order = await context.db.order.findMany()
|
|
586
|
-
order[0].total // computed correctly
|
|
587
|
-
order[0].lineItems // undefined — `needs` is private plumbing, not an implicit `include`
|
|
588
|
-
```
|
|
589
|
-
|
|
590
|
-
**Key characteristics:**
|
|
591
|
-
|
|
592
|
-
- Available on every field type (via `BaseFieldConfig`), not only `virtual()` — any field whose `resolveOutput` reads a relation can declare it.
|
|
593
|
-
- Names **immediate relations only** — no dotted paths. A dependency of a dependency is pulled in by the next list's own `needs` declaration, not by a path grammar.
|
|
594
|
-
- **Never widens what the caller receives.** A declared relation is stripped from the result unless the caller named it too, for both `include` reads and fragment `query` reads alike.
|
|
595
|
-
- **Scoped by the Access Filter like any other relation a read asks for — never a bypass.** A dependency the session cannot query (operation-level `query` access) is not fetched; a dependency a relationship field denies field-level `read` on does not reach the hook; a dependency the Access Filter scopes with a filter yields only the visible rows.
|
|
596
|
-
- **Session-relative, with no escape.** The field always computes, on whatever its session can see — a field declaring two dependencies and granted access to one still produces a value from that one. A total over a scoped relation is a projection of the visible rows, not a fact about the underlying row. Computing the "true" figure is not something a declared dependency can do, because that would leak the values of rows the session was denied. **A field that genuinely needs the unscoped view must issue a privileged read inside its own hook (`context.sudo()`) and explicitly own that decision** — `needs` does not provide an escape hatch from access control.
|
|
597
|
-
- **A declaration closure that can't fit is refused, not silently truncated.** Declarations fold in recursively (a related list's own `needs` are satisfied too, wherever it's reached), so a long chain can exceed the read-include depth cap. A chain that cannot fit from any starting point fails `pnpm generate`; a caller-triggered overflow hits the ordinary runtime depth denial.
|
|
598
|
-
- **Typed as a plain `string[]`, not compile-checked against the list's relation names.** `BaseFieldConfig` is the contextual type every field builder's return value is checked against — including non-generic third-party fields (`richText(): RichTextField`, with no `TTypeInfo` parameter of its own, the documented third-party field pattern). Narrowing `needs`'s type per-list would make it disagree with the type a fixed, unparameterized field config presents, breaking assignability for every such field regardless of whether it uses `needs` at all. Instead, `pnpm generate` (`validateNeedsDeclarations`) rejects a `needs` entry that isn't an immediate relationship field on the same list, naming the field and the bad entry.
|
|
599
|
-
|
|
600
|
-
See `docs/adr/0025-a-computed-field-declares-the-relations-it-needs.md` and the "Declared dependency" / "Session-relative value" glossary entries in `CONTEXT.md`.
|
|
601
|
-
|
|
602
|
-
## Type Safety
|
|
603
|
-
|
|
604
|
-
All types are strongly typed with TypeScript:
|
|
605
|
-
|
|
606
|
-
- Config is validated via Zod schemas
|
|
607
|
-
- Context provides full Prisma type inference
|
|
608
|
-
- Access control functions are typed with proper generics
|
|
609
|
-
- Avoid `any` and `unknown` in external APIs (internal use only where necessary)
|