@prisma/orm-family-sql 0.16.0-dev.36
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/LICENSE +201 -0
- package/README.md +28 -0
- package/dist/ast-BstHjAoG.mjs +325 -0
- package/dist/ast-BstHjAoG.mjs.map +1 -0
- package/dist/ast-DSuzmsWn.d.mts +287 -0
- package/dist/ast-DSuzmsWn.d.mts.map +1 -0
- package/dist/authoring-type-constructors-Blqoa2Ua-DyCmD6mp.mjs +316 -0
- package/dist/authoring-type-constructors-Blqoa2Ua-DyCmD6mp.mjs.map +1 -0
- package/dist/build-contract-DBCz_YSs-DO0wMHai.mjs +778 -0
- package/dist/build-contract-DBCz_YSs-DO0wMHai.mjs.map +1 -0
- package/dist/builder.d.mts +3 -0
- package/dist/builder.mjs +1 -0
- package/dist/builder__runtime.d.mts +42 -0
- package/dist/builder__runtime.d.mts.map +1 -0
- package/dist/builder__runtime.mjs +955 -0
- package/dist/builder__runtime.mjs.map +1 -0
- package/dist/builder__types.d.mts +3 -0
- package/dist/builder__types.mjs +1 -0
- package/dist/canonicalization-hooks-CYnW_sGn.d.mts +10 -0
- package/dist/canonicalization-hooks-CYnW_sGn.d.mts.map +1 -0
- package/dist/canonicalization-hooks-DfIc_lOO.mjs +87 -0
- package/dist/canonicalization-hooks-DfIc_lOO.mjs.map +1 -0
- package/dist/codec-descriptor-registry-BJVJRftS.mjs +96 -0
- package/dist/codec-descriptor-registry-BJVJRftS.mjs.map +1 -0
- package/dist/codec-types-MOaceoPi-C5eL2sao.d.mts +116 -0
- package/dist/codec-types-MOaceoPi-C5eL2sao.d.mts.map +1 -0
- package/dist/config-types-47qB33rW.d.mts +21 -0
- package/dist/config-types-47qB33rW.d.mts.map +1 -0
- package/dist/config-types-N7PJc6VN.mjs +65 -0
- package/dist/config-types-N7PJc6VN.mjs.map +1 -0
- package/dist/contract-builder-MvJJrQ-5.d.mts +1306 -0
- package/dist/contract-builder-MvJJrQ-5.d.mts.map +1 -0
- package/dist/contract-builder-YjMQcvZ1.mjs +1615 -0
- package/dist/contract-builder-YjMQcvZ1.mjs.map +1 -0
- package/dist/contract-emitter.d.mts +25 -0
- package/dist/contract-emitter.d.mts.map +1 -0
- package/dist/contract-emitter.mjs +2 -0
- package/dist/contract-errors-BCN6RVI5-5IHBF_lg.mjs +9 -0
- package/dist/contract-errors-BCN6RVI5-5IHBF_lg.mjs.map +1 -0
- package/dist/contract-psl.d.mts +65 -0
- package/dist/contract-psl.d.mts.map +1 -0
- package/dist/contract-psl.mjs +2 -0
- package/dist/contract-psl__provider.d.mts +23 -0
- package/dist/contract-psl__provider.d.mts.map +1 -0
- package/dist/contract-psl__provider.mjs +105 -0
- package/dist/contract-psl__provider.mjs.map +1 -0
- package/dist/contract-ts.d.mts +3 -0
- package/dist/contract-ts.mjs +4 -0
- package/dist/contract-ts__config-types.d.mts +2 -0
- package/dist/contract-ts__config-types.mjs +2 -0
- package/dist/contract-ts__contract-builder.d.mts +2 -0
- package/dist/contract-ts__contract-builder.mjs +3 -0
- package/dist/contract-view-BMR5IWk4.mjs +39 -0
- package/dist/contract-view-BMR5IWk4.mjs.map +1 -0
- package/dist/contract-view-BbWQp6r4.d.mts +61 -0
- package/dist/contract-view-BbWQp6r4.d.mts.map +1 -0
- package/dist/contract.d.mts +26 -0
- package/dist/contract.mjs +17 -0
- package/dist/contract__canonicalization-hooks.d.mts +2 -0
- package/dist/contract__canonicalization-hooks.mjs +2 -0
- package/dist/contract__contract-view.d.mts +2 -0
- package/dist/contract__contract-view.mjs +2 -0
- package/dist/contract__entity-handle-lowering-hook.d.mts +2 -0
- package/dist/contract__entity-handle-lowering-hook.mjs +2 -0
- package/dist/contract__entity-kinds.d.mts +2 -0
- package/dist/contract__entity-kinds.mjs +2 -0
- package/dist/contract__factories.d.mts +2 -0
- package/dist/contract__factories.mjs +2 -0
- package/dist/contract__foreign-key-materialization.d.mts +2 -0
- package/dist/contract__foreign-key-materialization.mjs +2 -0
- package/dist/contract__index-naming.d.mts +2 -0
- package/dist/contract__index-naming.mjs +2 -0
- package/dist/contract__index-type-validation.d.mts +2 -0
- package/dist/contract__index-type-validation.mjs +2 -0
- package/dist/contract__index-types.d.mts +3 -0
- package/dist/contract__index-types.mjs +2 -0
- package/dist/contract__pack-types.d.mts +2 -0
- package/dist/contract__pack-types.mjs +1 -0
- package/dist/contract__referential-action-sql.d.mts +2 -0
- package/dist/contract__referential-action-sql.mjs +2 -0
- package/dist/contract__resolve-storage-table.d.mts +2 -0
- package/dist/contract__resolve-storage-table.mjs +2 -0
- package/dist/contract__types.d.mts +11 -0
- package/dist/contract__types.mjs +4 -0
- package/dist/contract__validators.d.mts +2 -0
- package/dist/contract__validators.mjs +3 -0
- package/dist/contract__value-set-derivation-hook.d.mts +2 -0
- package/dist/contract__value-set-derivation-hook.mjs +2 -0
- package/dist/control-B7iS4jv4.mjs +1326 -0
- package/dist/control-B7iS4jv4.mjs.map +1 -0
- package/dist/control-BHmsZudH.d.mts +948 -0
- package/dist/control-BHmsZudH.d.mts.map +1 -0
- package/dist/control-adapter-4y1O8Ps3.d.mts +1 -0
- package/dist/control-adapter-C99J7aEF-C-17lFY8.d.mts +191 -0
- package/dist/control-adapter-C99J7aEF-C-17lFY8.d.mts.map +1 -0
- package/dist/db-DhMsmBS--DKENDi3l.d.mts +365 -0
- package/dist/db-DhMsmBS--DKENDi3l.d.mts.map +1 -0
- package/dist/ddl-types-BMp2bWVS-D1waZJ4n.mjs +153 -0
- package/dist/ddl-types-BMp2bWVS-D1waZJ4n.mjs.map +1 -0
- package/dist/ddl-types-rrMsf77U-DGctYf-T.d.mts +149 -0
- package/dist/ddl-types-rrMsf77U-DGctYf-T.d.mts.map +1 -0
- package/dist/diff-B16bL_IR.mjs +13 -0
- package/dist/diff-B16bL_IR.mjs.map +1 -0
- package/dist/diff-DhlzHOMD.d.mts +104 -0
- package/dist/diff-DhlzHOMD.d.mts.map +1 -0
- package/dist/dist-C_q1_QR9.mjs +9 -0
- package/dist/dist-C_q1_QR9.mjs.map +1 -0
- package/dist/dist-DrScXbhq.mjs +414 -0
- package/dist/dist-DrScXbhq.mjs.map +1 -0
- package/dist/dist-x6jvtkKM.mjs +68 -0
- package/dist/dist-x6jvtkKM.mjs.map +1 -0
- package/dist/entity-handle-lowering-hook-CIvRKBfa.d.mts +92 -0
- package/dist/entity-handle-lowering-hook-CIvRKBfa.d.mts.map +1 -0
- package/dist/entity-handle-lowering-hook-DwLIKNiS.mjs +11 -0
- package/dist/entity-handle-lowering-hook-DwLIKNiS.mjs.map +1 -0
- package/dist/entity-kinds-DKxwKolf-CcsY-iYV.mjs +136 -0
- package/dist/entity-kinds-DKxwKolf-CcsY-iYV.mjs.map +1 -0
- package/dist/entity-kinds-Dh6VH4uK.d.mts +29 -0
- package/dist/entity-kinds-Dh6VH4uK.d.mts.map +1 -0
- package/dist/errors-B5g0xWro-mWH2C07F.mjs +9 -0
- package/dist/errors-B5g0xWro-mWH2C07F.mjs.map +1 -0
- package/dist/errors.d.mts +63 -0
- package/dist/errors.d.mts.map +1 -0
- package/dist/errors.mjs +2 -0
- package/dist/expression-DIf8F4Mc.d.mts +167 -0
- package/dist/expression-DIf8F4Mc.d.mts.map +1 -0
- package/dist/expression-RUO52bJB.mjs +109 -0
- package/dist/expression-RUO52bJB.mjs.map +1 -0
- package/dist/factories-B7tA4y9D.d.mts +39 -0
- package/dist/factories-B7tA4y9D.d.mts.map +1 -0
- package/dist/factories-CDG9zBxC.mjs +77 -0
- package/dist/factories-CDG9zBxC.mjs.map +1 -0
- package/dist/family.d.mts +13 -0
- package/dist/family.mjs +12 -0
- package/dist/family__control-adapter.d.mts +3 -0
- package/dist/family__control-adapter.mjs +1 -0
- package/dist/family__control.d.mts +4 -0
- package/dist/family__control.mjs +4 -0
- package/dist/family__diff.d.mts +3 -0
- package/dist/family__diff.mjs +3 -0
- package/dist/family__ir.d.mts +2 -0
- package/dist/family__ir.mjs +3 -0
- package/dist/family__migration.d.mts +2 -0
- package/dist/family__migration.mjs +2 -0
- package/dist/family__pack.d.mts +249 -0
- package/dist/family__pack.d.mts.map +1 -0
- package/dist/family__pack.mjs +18 -0
- package/dist/family__pack.mjs.map +1 -0
- package/dist/family__psl-infer.d.mts +2 -0
- package/dist/family__psl-infer.mjs +2 -0
- package/dist/family__runtime.d.mts +3 -0
- package/dist/family__runtime.mjs +3 -0
- package/dist/family__verify.d.mts +2 -0
- package/dist/family__verify.mjs +2 -0
- package/dist/foreign-key-BSy9u4O4-BnsOe5Nf.d.mts +85 -0
- package/dist/foreign-key-BSy9u4O4-BnsOe5Nf.d.mts.map +1 -0
- package/dist/foreign-key-materialization-Ca-wRbmj.d.mts +65 -0
- package/dist/foreign-key-materialization-Ca-wRbmj.d.mts.map +1 -0
- package/dist/foreign-key-materialization-HI7BIQrG.mjs +72 -0
- package/dist/foreign-key-materialization-HI7BIQrG.mjs.map +1 -0
- package/dist/index-Bb9U36N4.d.mts +511 -0
- package/dist/index-Bb9U36N4.d.mts.map +1 -0
- package/dist/index-BgaTJRxj.d.mts +25 -0
- package/dist/index-BgaTJRxj.d.mts.map +1 -0
- package/dist/index-naming-BZWoh45T.d.mts +62 -0
- package/dist/index-naming-BZWoh45T.d.mts.map +1 -0
- package/dist/index-naming-BoGBcMmI-DiJAr66H.mjs +108 -0
- package/dist/index-naming-BoGBcMmI-DiJAr66H.mjs.map +1 -0
- package/dist/index-type-validation-COH4d78M.mjs +17 -0
- package/dist/index-type-validation-COH4d78M.mjs.map +1 -0
- package/dist/index-type-validation-DN0L2mqm.d.mts +10 -0
- package/dist/index-type-validation-DN0L2mqm.d.mts.map +1 -0
- package/dist/index-types-CNhHgKHi.d.mts +1 -0
- package/dist/index-types-DJQ_xwaE.mjs +40 -0
- package/dist/index-types-DJQ_xwaE.mjs.map +1 -0
- package/dist/index-types-DPVhsW0v-D-BikM4h.d.mts +31 -0
- package/dist/index-types-DPVhsW0v-D-BikM4h.d.mts.map +1 -0
- package/dist/interpreter-5WgpL-ax-_nqHLUTr.mjs +3526 -0
- package/dist/interpreter-5WgpL-ax-_nqHLUTr.mjs.map +1 -0
- package/dist/ir-B3bKMnGQ.d.mts +123 -0
- package/dist/ir-B3bKMnGQ.d.mts.map +1 -0
- package/dist/ir-I-xciGUH.mjs +36 -0
- package/dist/ir-I-xciGUH.mjs.map +1 -0
- package/dist/lane-query-builder.d.mts +236 -0
- package/dist/lane-query-builder.d.mts.map +1 -0
- package/dist/lane-query-builder.mjs +67 -0
- package/dist/lane-query-builder.mjs.map +1 -0
- package/dist/middleware-B4XbF4d4-tfwBl6mX.d.mts +119 -0
- package/dist/middleware-B4XbF4d4-tfwBl6mX.d.mts.map +1 -0
- package/dist/middleware-ByRsXxxq.d.mts +1 -0
- package/dist/middleware-CMr4CHNz-DHIUdxlm.mjs +80 -0
- package/dist/middleware-CMr4CHNz-DHIUdxlm.mjs.map +1 -0
- package/dist/migration-CK4oePIo.mjs +44 -0
- package/dist/migration-CK4oePIo.mjs.map +1 -0
- package/dist/migration-CW0ZJLxP.d.mts +44 -0
- package/dist/migration-CW0ZJLxP.d.mts.map +1 -0
- package/dist/naming-BwqqVcZy-BgTObIQw.mjs +177 -0
- package/dist/naming-BwqqVcZy-BgTObIQw.mjs.map +1 -0
- package/dist/naming-jLgdauLi-C84G_UHi.d.mts +133 -0
- package/dist/naming-jLgdauLi-C84G_UHi.d.mts.map +1 -0
- package/dist/operations.d.mts +2 -0
- package/dist/operations.mjs +2 -0
- package/dist/orm-client.d.mts +1316 -0
- package/dist/orm-client.d.mts.map +1 -0
- package/dist/orm-client.mjs +4544 -0
- package/dist/orm-client.mjs.map +1 -0
- package/dist/pack-types-BcLTpwbv.d.mts +14 -0
- package/dist/pack-types-BcLTpwbv.d.mts.map +1 -0
- package/dist/plan-BTD8xFfa-Dd_K-A1R.d.mts +45 -0
- package/dist/plan-BTD8xFfa-Dd_K-A1R.d.mts.map +1 -0
- package/dist/plan-CkpcgUaM.mjs +34 -0
- package/dist/plan-CkpcgUaM.mjs.map +1 -0
- package/dist/plan-Cm4jaoNM.d.mts +2 -0
- package/dist/psl-infer-6je61Z6J.d.mts +119 -0
- package/dist/psl-infer-6je61Z6J.d.mts.map +1 -0
- package/dist/psl-infer-DGftl-HW.mjs +362 -0
- package/dist/psl-infer-DGftl-HW.mjs.map +1 -0
- package/dist/query-lane-context-Bz8XJZ45.d.mts +1 -0
- package/dist/query-lane-context-DtdVMqoz-C2Tj5QHA.d.mts +94 -0
- package/dist/query-lane-context-DtdVMqoz-C2Tj5QHA.d.mts.map +1 -0
- package/dist/referential-action-sql-Csd6dA8G.d.mts +12 -0
- package/dist/referential-action-sql-Csd6dA8G.d.mts.map +1 -0
- package/dist/referential-action-sql-DSqkrySN.mjs +17 -0
- package/dist/referential-action-sql-DSqkrySN.mjs.map +1 -0
- package/dist/relational-core.d.mts +13 -0
- package/dist/relational-core.mjs +8 -0
- package/dist/relational-core__ast.d.mts +5 -0
- package/dist/relational-core__ast.mjs +5 -0
- package/dist/relational-core__codec-descriptor-registry.d.mts +39 -0
- package/dist/relational-core__codec-descriptor-registry.d.mts.map +1 -0
- package/dist/relational-core__codec-descriptor-registry.mjs +2 -0
- package/dist/relational-core__contract-free.d.mts +220 -0
- package/dist/relational-core__contract-free.d.mts.map +1 -0
- package/dist/relational-core__contract-free.mjs +393 -0
- package/dist/relational-core__contract-free.mjs.map +1 -0
- package/dist/relational-core__expression.d.mts +2 -0
- package/dist/relational-core__expression.mjs +2 -0
- package/dist/relational-core__middleware.d.mts +3 -0
- package/dist/relational-core__middleware.mjs +2 -0
- package/dist/relational-core__plan.d.mts +4 -0
- package/dist/relational-core__plan.mjs +2 -0
- package/dist/relational-core__query-lane-context.d.mts +3 -0
- package/dist/relational-core__query-lane-context.mjs +1 -0
- package/dist/relational-core__types.d.mts +3 -0
- package/dist/relational-core__types.mjs +1 -0
- package/dist/resolve-storage-table-DUwyRJmP.d.mts +21 -0
- package/dist/resolve-storage-table-DUwyRJmP.d.mts.map +1 -0
- package/dist/resolve-storage-table-DZsfnzeW.mjs +46 -0
- package/dist/resolve-storage-table-DZsfnzeW.mjs.map +1 -0
- package/dist/runtime-DNewRuec.d.mts +46 -0
- package/dist/runtime-DNewRuec.d.mts.map +1 -0
- package/dist/runtime-tIHpGCcG.mjs +55 -0
- package/dist/runtime-tIHpGCcG.mjs.map +1 -0
- package/dist/runtime.d.mts +4 -0
- package/dist/runtime.mjs +1606 -0
- package/dist/runtime.mjs.map +1 -0
- package/dist/schema-differ-DpG_gnur-g5_iks8G.d.mts +47 -0
- package/dist/schema-differ-DpG_gnur-g5_iks8G.d.mts.map +1 -0
- package/dist/schema-ir.d.mts +2 -0
- package/dist/schema-ir.mjs +2 -0
- package/dist/schema-ir__naming.d.mts +2 -0
- package/dist/schema-ir__naming.mjs +2 -0
- package/dist/schema-ir__types.d.mts +3 -0
- package/dist/schema-ir__types.mjs +2 -0
- package/dist/schema-verify-vtFZbmGq-BNfTr6fU.mjs +222 -0
- package/dist/schema-verify-vtFZbmGq-BNfTr6fU.mjs.map +1 -0
- package/dist/serialized-index-B96AwqtU-DcqcB4q_.d.mts +26 -0
- package/dist/serialized-index-B96AwqtU-DcqcB4q_.d.mts.map +1 -0
- package/dist/serialized-index-Dp5_EjXD-DMsw8jf3.mjs +64 -0
- package/dist/serialized-index-Dp5_EjXD-DMsw8jf3.mjs.map +1 -0
- package/dist/sql-contract-serializer-2oBWuxTe-B9hzBBv1.mjs +171 -0
- package/dist/sql-contract-serializer-2oBWuxTe-B9hzBBv1.mjs.map +1 -0
- package/dist/sql-execution-plan-CJmsCP7n-DoEpNn7r.d.mts +32 -0
- package/dist/sql-execution-plan-CJmsCP7n-DoEpNn7r.d.mts.map +1 -0
- package/dist/sql-index-DsI8HAeK-BJsn-jvD.d.mts +72 -0
- package/dist/sql-index-DsI8HAeK-BJsn-jvD.d.mts.map +1 -0
- package/dist/sql-node-kEz6YFZo-v2F7delx.d.mts +42 -0
- package/dist/sql-node-kEz6YFZo-v2F7delx.d.mts.map +1 -0
- package/dist/sql-storage-B0QwiTg6-B8DVctJm.d.mts +171 -0
- package/dist/sql-storage-B0QwiTg6-B8DVctJm.d.mts.map +1 -0
- package/dist/storage-table-BBaAmanc-BwR37LMA.d.mts +130 -0
- package/dist/storage-table-BBaAmanc-BwR37LMA.d.mts.map +1 -0
- package/dist/storage-table-_LWDmPfv-DFhU2dML.mjs +277 -0
- package/dist/storage-table-_LWDmPfv-DFhU2dML.mjs.map +1 -0
- package/dist/storage-value-set-BGlj5Uus-CvZD--GG.d.mts +41 -0
- package/dist/storage-value-set-BGlj5Uus-CvZD--GG.d.mts.map +1 -0
- package/dist/timestamp-now-generator-DRXygu32-DN9Qb1uI.mjs +173 -0
- package/dist/timestamp-now-generator-DRXygu32-DN9Qb1uI.mjs.map +1 -0
- package/dist/types-BchwQpmO.d.mts +16 -0
- package/dist/types-BchwQpmO.d.mts.map +1 -0
- package/dist/types-BgkZru_1-H7yfikSQ.mjs +1444 -0
- package/dist/types-BgkZru_1-H7yfikSQ.mjs.map +1 -0
- package/dist/types-CMMQS6aG.mjs +111 -0
- package/dist/types-CMMQS6aG.mjs.map +1 -0
- package/dist/types-CuDrWc5v-ISVXBmBs.d.mts +134 -0
- package/dist/types-CuDrWc5v-ISVXBmBs.d.mts.map +1 -0
- package/dist/types-CvbBw1Jo-3KQdErJY.d.mts +688 -0
- package/dist/types-CvbBw1Jo-3KQdErJY.d.mts.map +1 -0
- package/dist/types-DIBA80mc-wf5wLmON.d.mts +723 -0
- package/dist/types-DIBA80mc-wf5wLmON.d.mts.map +1 -0
- package/dist/types-DMxc5KZa.d.mts +1 -0
- package/dist/types-Dl20lxky-DC_jdA0T.d.mts +247 -0
- package/dist/types-Dl20lxky-DC_jdA0T.d.mts.map +1 -0
- package/dist/types-DmhUVq4u-BpqqjPJx.mjs +719 -0
- package/dist/types-DmhUVq4u-BpqqjPJx.mjs.map +1 -0
- package/dist/types-IqZMLrFC-DtdJNICr.d.mts +426 -0
- package/dist/types-IqZMLrFC-DtdJNICr.d.mts.map +1 -0
- package/dist/types-TvBZ2Ne9.d.mts +1 -0
- package/dist/unique-constraint-jQWfH7dg-zLWtFp99.d.mts +32 -0
- package/dist/unique-constraint-jQWfH7dg-zLWtFp99.d.mts.map +1 -0
- package/dist/util-DQQgv2j1-D6INZaRG.mjs +34 -0
- package/dist/util-DQQgv2j1-D6INZaRG.mjs.map +1 -0
- package/dist/validators-C4PItWnf.mjs +0 -0
- package/dist/validators-C4PItWnf.mjs.map +1 -0
- package/dist/validators-CxeHhHrf.d.mts +206 -0
- package/dist/validators-CxeHhHrf.d.mts.map +1 -0
- package/dist/value-set-derivation-hook-BhgGVtw0.d.mts +35 -0
- package/dist/value-set-derivation-hook-BhgGVtw0.d.mts.map +1 -0
- package/dist/value-set-derivation-hook-Di9JZ0X7.mjs +22 -0
- package/dist/value-set-derivation-hook-Di9JZ0X7.mjs.map +1 -0
- package/dist/verify-E6Dd2vYp-gYwnlhLm.mjs +86 -0
- package/dist/verify-E6Dd2vYp-gYwnlhLm.mjs.map +1 -0
- package/dist/verify-zDRnw0Rn.d.mts +26 -0
- package/dist/verify-zDRnw0Rn.d.mts.map +1 -0
- package/package.json +112 -0
|
@@ -0,0 +1,955 @@
|
|
|
1
|
+
import { C as LiteralExpr, D as OrExpr, F as SubqueryExpr, I as TableSource, L as UpdateAst, O as OrderByItem, P as SelectAst, S as ListExpression, T as NullCheckExpr, _ as InsertAst, c as DeleteAst, g as IdentifierRef, j as ProjectionItem, k as ParamRef, l as DerivedTableSource, n as AndExpr, o as ColumnRef, p as ExistsExpr, r as BinaryExpr, t as AggregateExpr, y as JoinAst } from "./types-BgkZru_1-H7yfikSQ.mjs";
|
|
2
|
+
import { t as collectOrderedParamRefs } from "./util-DQQgv2j1-D6INZaRG.mjs";
|
|
3
|
+
import { a as toExpr, n as codecOf, r as createRawSql } from "./expression-RUO52bJB.mjs";
|
|
4
|
+
import { n as codecRefForStorageColumn } from "./codec-descriptor-registry-BJVJRftS.mjs";
|
|
5
|
+
import { assertAnnotationsApplicable } from "@prisma/orm-framework/components/runtime";
|
|
6
|
+
//#region ../../../2-sql/4-lanes/sql-builder/dist/runtime/index.mjs
|
|
7
|
+
/**
|
|
8
|
+
* Runtime wrapper around a relational-core AST expression node. Carries ScopeField metadata (codecId, nullable) so aggregate-like combinators can propagate the input codec onto their result.
|
|
9
|
+
*
|
|
10
|
+
* `codec` records the column-bound {@link CodecRef} when the field-proxy knows the binding — both the namespaced form (`f.user.email` → `ColumnRef`) and the top-level shortcut (`f.email` → `IdentifierRef`) stamp the ref derived from contract storage. `codecOf(expression)` exposes it for operation implementations forwarding the ref to `toExpr`.
|
|
11
|
+
*/
|
|
12
|
+
var ExpressionImpl = class {
|
|
13
|
+
ast;
|
|
14
|
+
returnType;
|
|
15
|
+
codec;
|
|
16
|
+
constructor(ast, returnType, codec) {
|
|
17
|
+
this.ast = ast;
|
|
18
|
+
this.returnType = returnType;
|
|
19
|
+
this.codec = codec;
|
|
20
|
+
}
|
|
21
|
+
buildAst() {
|
|
22
|
+
return this.ast;
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
function createFieldProxy(scope) {
|
|
26
|
+
return new Proxy({}, { get(_target, prop) {
|
|
27
|
+
if (Object.hasOwn(scope.topLevel, prop)) {
|
|
28
|
+
const topField = scope.topLevel[prop];
|
|
29
|
+
if (topField) return new ExpressionImpl(IdentifierRef.of(prop), topField, topField.codec);
|
|
30
|
+
}
|
|
31
|
+
if (Object.hasOwn(scope.namespaces, prop)) {
|
|
32
|
+
const nsFields = scope.namespaces[prop];
|
|
33
|
+
if (nsFields) return createNamespaceProxy(prop, nsFields);
|
|
34
|
+
}
|
|
35
|
+
} });
|
|
36
|
+
}
|
|
37
|
+
function createNamespaceProxy(namespaceName, fields) {
|
|
38
|
+
return new Proxy({}, { get(_target, prop) {
|
|
39
|
+
if (Object.hasOwn(fields, prop)) {
|
|
40
|
+
const field = fields[prop];
|
|
41
|
+
if (field) return new ExpressionImpl(ColumnRef.of(namespaceName, prop), field, field.codec);
|
|
42
|
+
}
|
|
43
|
+
} });
|
|
44
|
+
}
|
|
45
|
+
const BOOL_FIELD = {
|
|
46
|
+
codecId: "pg/bool@1",
|
|
47
|
+
nullable: false
|
|
48
|
+
};
|
|
49
|
+
const resolve = toExpr;
|
|
50
|
+
/**
|
|
51
|
+
* Resolve a binary-comparison operand into an AST expression, threading the column-bound side's {@link CodecRef} to the raw-value side.
|
|
52
|
+
*
|
|
53
|
+
* For `fns.eq(f.email, 'alice@example.com')`, `f.email` is the column-bound expression carrying a `ColumnRef` AST and a `CodecRef` derived from contract storage; the raw string operand has no codec context. By deriving the codec context from the column-bound side and forwarding it via `toExpr(value, codec)`, the resulting `ParamRef` carries the `CodecRef` that encode-side dispatch needs to materialise the per-instance codec for parameterized codec ids (`vector(1024)` vs. `vector(1536)`).
|
|
54
|
+
*/
|
|
55
|
+
function resolveOperand(operand, otherCodec) {
|
|
56
|
+
if (isExpressionLike(operand)) return operand.buildAst();
|
|
57
|
+
return toExpr(operand, otherCodec);
|
|
58
|
+
}
|
|
59
|
+
function isExpressionLike(value) {
|
|
60
|
+
return typeof value === "object" && value !== null && "buildAst" in value && typeof value.buildAst === "function";
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Resolves an Expression via `buildAst()`, or wraps a raw value as a `LiteralExpr` — an SQL literal inlined into the query text, not a bound parameter.
|
|
64
|
+
*
|
|
65
|
+
* Used for `and` / `or` operands. The usual operand is an `Expression<bool>` (e.g. the result of `fns.eq`), which this function passes through by calling `buildAst()`. The only time the raw-value branch fires is when the caller writes `fns.and(true, x)` or similar — inlining `TRUE`/`FALSE` literals lets the SQL planner statically simplify `TRUE AND x` to `x`, which it cannot do for an opaque `ParamRef`.
|
|
66
|
+
*/
|
|
67
|
+
function toLiteralExpr(value) {
|
|
68
|
+
if (typeof value === "object" && value !== null && "buildAst" in value && typeof value.buildAst === "function") return value.buildAst();
|
|
69
|
+
return new LiteralExpr(value);
|
|
70
|
+
}
|
|
71
|
+
function boolExpr(astNode) {
|
|
72
|
+
return new ExpressionImpl(astNode, BOOL_FIELD);
|
|
73
|
+
}
|
|
74
|
+
function binaryWithSharedCodec(a, b, build) {
|
|
75
|
+
const aCodec = codecOf(a);
|
|
76
|
+
return build(resolveOperand(a, codecOf(b)), resolveOperand(b, aCodec));
|
|
77
|
+
}
|
|
78
|
+
function eq(a, b) {
|
|
79
|
+
if (b === null) return boolExpr(NullCheckExpr.isNull(resolve(a)));
|
|
80
|
+
if (a === null) return boolExpr(NullCheckExpr.isNull(resolve(b)));
|
|
81
|
+
return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr("eq", l, r)));
|
|
82
|
+
}
|
|
83
|
+
function ne(a, b) {
|
|
84
|
+
if (b === null) return boolExpr(NullCheckExpr.isNotNull(resolve(a)));
|
|
85
|
+
if (a === null) return boolExpr(NullCheckExpr.isNotNull(resolve(b)));
|
|
86
|
+
return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr("neq", l, r)));
|
|
87
|
+
}
|
|
88
|
+
function comparison(a, b, op) {
|
|
89
|
+
return boolExpr(binaryWithSharedCodec(a, b, (l, r) => new BinaryExpr(op, l, r)));
|
|
90
|
+
}
|
|
91
|
+
function inOrNotIn(expr, valuesOrSubquery, op) {
|
|
92
|
+
const left = expr.buildAst();
|
|
93
|
+
const leftCodec = codecOf(expr);
|
|
94
|
+
const binaryFn = op === "in" ? BinaryExpr.in : BinaryExpr.notIn;
|
|
95
|
+
if (Array.isArray(valuesOrSubquery)) {
|
|
96
|
+
const refs = valuesOrSubquery.map((v) => resolveOperand(v, leftCodec));
|
|
97
|
+
return boolExpr(binaryFn(left, ListExpression.of(refs)));
|
|
98
|
+
}
|
|
99
|
+
return boolExpr(binaryFn(left, SubqueryExpr.of(valuesOrSubquery.buildAst())));
|
|
100
|
+
}
|
|
101
|
+
function numericAgg(fn, expr) {
|
|
102
|
+
return new ExpressionImpl(AggregateExpr[fn](expr.buildAst()), {
|
|
103
|
+
codecId: expr.returnType.codecId,
|
|
104
|
+
nullable: true
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
function createBuiltinFunctions(rawCodecInferer) {
|
|
108
|
+
return {
|
|
109
|
+
eq: (a, b) => eq(a, b),
|
|
110
|
+
ne: (a, b) => ne(a, b),
|
|
111
|
+
gt: (a, b) => comparison(a, b, "gt"),
|
|
112
|
+
gte: (a, b) => comparison(a, b, "gte"),
|
|
113
|
+
lt: (a, b) => comparison(a, b, "lt"),
|
|
114
|
+
lte: (a, b) => comparison(a, b, "lte"),
|
|
115
|
+
and: (...exprs) => boolExpr(AndExpr.of(exprs.map(toLiteralExpr))),
|
|
116
|
+
or: (...exprs) => boolExpr(OrExpr.of(exprs.map(toLiteralExpr))),
|
|
117
|
+
exists: (subquery) => boolExpr(ExistsExpr.exists(subquery.buildAst())),
|
|
118
|
+
notExists: (subquery) => boolExpr(ExistsExpr.notExists(subquery.buildAst())),
|
|
119
|
+
in: (expr, valuesOrSubquery) => inOrNotIn(expr, valuesOrSubquery, "in"),
|
|
120
|
+
notIn: (expr, valuesOrSubquery) => inOrNotIn(expr, valuesOrSubquery, "notIn"),
|
|
121
|
+
raw: createRawSql(rawCodecInferer)
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function createAggregateOnlyFunctions() {
|
|
125
|
+
return {
|
|
126
|
+
count: (expr) => {
|
|
127
|
+
const astExpr = expr ? expr.buildAst() : void 0;
|
|
128
|
+
return new ExpressionImpl(AggregateExpr.count(astExpr), {
|
|
129
|
+
codecId: "pg/int8@1",
|
|
130
|
+
nullable: false
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
sum: (expr) => numericAgg("sum", expr),
|
|
134
|
+
avg: (expr) => numericAgg("avg", expr),
|
|
135
|
+
min: (expr) => numericAgg("min", expr),
|
|
136
|
+
max: (expr) => numericAgg("max", expr)
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
function createFunctions(operations, rawCodecInferer) {
|
|
140
|
+
const builtins = createBuiltinFunctions(rawCodecInferer);
|
|
141
|
+
return new Proxy({}, { get(_target, prop) {
|
|
142
|
+
if (Object.hasOwn(builtins, prop)) return builtins[prop];
|
|
143
|
+
const op = operations[prop];
|
|
144
|
+
if (op) return op.impl;
|
|
145
|
+
} });
|
|
146
|
+
}
|
|
147
|
+
function createAggregateFunctions(operations, rawCodecInferer) {
|
|
148
|
+
const baseFns = createFunctions(operations, rawCodecInferer);
|
|
149
|
+
const aggregates = createAggregateOnlyFunctions();
|
|
150
|
+
return new Proxy({}, { get(_target, prop) {
|
|
151
|
+
const agg = aggregates[prop];
|
|
152
|
+
if (agg) return agg;
|
|
153
|
+
return baseFns[prop];
|
|
154
|
+
} });
|
|
155
|
+
}
|
|
156
|
+
function resolveTableInNamespace(storage, namespaceId, tableName) {
|
|
157
|
+
const namespace = storage.namespaces[namespaceId];
|
|
158
|
+
if (namespace === void 0) return void 0;
|
|
159
|
+
const tables = namespace.entries.table;
|
|
160
|
+
if (tables === void 0 || !Object.hasOwn(tables, tableName)) return void 0;
|
|
161
|
+
return tables[tableName];
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* **Last-resort escape hatch for unsafe type assertions. Not a sanctioned tool to reach for.**
|
|
165
|
+
*
|
|
166
|
+
* Before reaching for `blindCast`, **rewrite the surrounding code so the cast becomes
|
|
167
|
+
* unnecessary**: tighten an input type, add a runtime check that narrows via a type
|
|
168
|
+
* predicate, restructure a generic so the compiler can see the relationship you're
|
|
169
|
+
* asserting, or use {@link castAs} when the value already satisfies the target type.
|
|
170
|
+
* Only when no rewrite is feasible does `blindCast` become the right answer — and at
|
|
171
|
+
* that point, the `Reason` literal you supply must articulate the compromise in
|
|
172
|
+
* language a reviewer can evaluate.
|
|
173
|
+
*
|
|
174
|
+
* The reviewer **will** validate the `Reason`. If it doesn't hold up under scrutiny,
|
|
175
|
+
* that is not a signal to soften the reason; it is a signal to go back and solve the
|
|
176
|
+
* underlying type-system problem properly. An unconvincing justification is rework,
|
|
177
|
+
* not a free pass.
|
|
178
|
+
*
|
|
179
|
+
* `blindCast` is the auditable form of `as Foo` / `as unknown as Foo`: it bypasses
|
|
180
|
+
* the compiler's checks (the input type is `unknown`, the output type is whatever the
|
|
181
|
+
* caller asks for), but it forces the unsafety to be named at the call site instead of
|
|
182
|
+
* smuggled in via a bare `as`. The `Reason` type parameter exists only at compile
|
|
183
|
+
* time — it is not present in the emitted JavaScript — but it is grep-able and
|
|
184
|
+
* visible to future readers.
|
|
185
|
+
*
|
|
186
|
+
* @example
|
|
187
|
+
* ```typescript
|
|
188
|
+
* const stringValue = blindCast<
|
|
189
|
+
* string,
|
|
190
|
+
* "JSON.parse returns `unknown`; this field is documented to be a string in the API contract"
|
|
191
|
+
* >(parsed[key]);
|
|
192
|
+
* ```
|
|
193
|
+
*
|
|
194
|
+
* @typeParam TargetType - The type the caller is asserting the input has.
|
|
195
|
+
* @typeParam _Reason - A string literal describing why bypassing the type system is necessary here.
|
|
196
|
+
* Only meaningful at compile time. The reviewer evaluates whether it justifies the unsafety.
|
|
197
|
+
*/
|
|
198
|
+
function blindCast(input) {
|
|
199
|
+
return input;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Returns an object with the key/value if value is defined, otherwise an empty object.
|
|
203
|
+
*
|
|
204
|
+
* Use with spread to conditionally include optional properties while satisfying
|
|
205
|
+
* exactOptionalPropertyTypes. This is explicit about which properties are optional
|
|
206
|
+
* and won't inadvertently strip other undefined values.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* // Instead of:
|
|
211
|
+
* const obj = {
|
|
212
|
+
* required: 'value',
|
|
213
|
+
* ...(optional ? { optional } : {}),
|
|
214
|
+
* };
|
|
215
|
+
*
|
|
216
|
+
* // Use:
|
|
217
|
+
* const obj = {
|
|
218
|
+
* required: 'value',
|
|
219
|
+
* ...ifDefined('optional', optional),
|
|
220
|
+
* };
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
function ifDefined(key, value) {
|
|
224
|
+
return value !== void 0 ? blindCast({ [key]: value }) : {};
|
|
225
|
+
}
|
|
226
|
+
function structuredError(code, message, options) {
|
|
227
|
+
const error = options?.cause !== void 0 ? new Error(message, { cause: options.cause }) : new Error(message);
|
|
228
|
+
Object.defineProperty(error, "name", {
|
|
229
|
+
value: "StructuredError",
|
|
230
|
+
configurable: true
|
|
231
|
+
});
|
|
232
|
+
return Object.assign(error, {
|
|
233
|
+
code,
|
|
234
|
+
...ifDefined("why", options?.why),
|
|
235
|
+
...ifDefined("fix", options?.fix),
|
|
236
|
+
...ifDefined("where", options?.where),
|
|
237
|
+
...ifDefined("severity", options?.severity),
|
|
238
|
+
...ifDefined("meta", options?.meta),
|
|
239
|
+
...ifDefined("docsUrl", options?.docsUrl)
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
var BuilderBase = class {
|
|
243
|
+
ctx;
|
|
244
|
+
constructor(ctx) {
|
|
245
|
+
this.ctx = ctx;
|
|
246
|
+
}
|
|
247
|
+
_gate(required, methodName, method) {
|
|
248
|
+
return ((...args) => {
|
|
249
|
+
assertCapability(this.ctx, required, methodName);
|
|
250
|
+
return method(...args);
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
/**
|
|
255
|
+
* Derive the canonical {@link CodecRef} for a `(table, column)` from the builder context's storage. Returns `undefined` when the builder context has no storage attached or when the column is unknown to the contract.
|
|
256
|
+
*/
|
|
257
|
+
function codecRefFor(ctx, namespaceId, tableName, columnName) {
|
|
258
|
+
if (!ctx.storage) return void 0;
|
|
259
|
+
return codecRefForStorageColumn(ctx.storage, namespaceId, tableName, columnName);
|
|
260
|
+
}
|
|
261
|
+
function emptyState(from, scope) {
|
|
262
|
+
return {
|
|
263
|
+
from,
|
|
264
|
+
joins: [],
|
|
265
|
+
projections: [],
|
|
266
|
+
where: [],
|
|
267
|
+
orderBy: [],
|
|
268
|
+
groupBy: [],
|
|
269
|
+
having: void 0,
|
|
270
|
+
limit: void 0,
|
|
271
|
+
offset: void 0,
|
|
272
|
+
distinct: void 0,
|
|
273
|
+
distinctOn: void 0,
|
|
274
|
+
scope,
|
|
275
|
+
rowFields: {},
|
|
276
|
+
annotations: /* @__PURE__ */ new Map()
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
function cloneState(state, overrides) {
|
|
280
|
+
return {
|
|
281
|
+
...state,
|
|
282
|
+
...overrides
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
function combineWhereExprs(exprs) {
|
|
286
|
+
if (exprs.length === 0) return void 0;
|
|
287
|
+
if (exprs.length === 1) return exprs[0];
|
|
288
|
+
return AndExpr.of(exprs);
|
|
289
|
+
}
|
|
290
|
+
function buildSelectAst(state) {
|
|
291
|
+
const where = combineWhereExprs(state.where);
|
|
292
|
+
return new SelectAst({
|
|
293
|
+
from: state.from,
|
|
294
|
+
joins: state.joins.length > 0 ? state.joins : void 0,
|
|
295
|
+
projection: state.projections,
|
|
296
|
+
where,
|
|
297
|
+
orderBy: state.orderBy.length > 0 ? state.orderBy : void 0,
|
|
298
|
+
distinct: state.distinct,
|
|
299
|
+
distinctOn: state.distinctOn && state.distinctOn.length > 0 ? state.distinctOn : void 0,
|
|
300
|
+
groupBy: state.groupBy.length > 0 ? state.groupBy : void 0,
|
|
301
|
+
having: state.having,
|
|
302
|
+
limit: state.limit,
|
|
303
|
+
offset: state.offset,
|
|
304
|
+
selectAllIntent: void 0
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
function buildQueryPlan(ast, ctx, annotations) {
|
|
308
|
+
const paramValues = collectOrderedParamRefs(ast).map((r) => r.kind === "param-ref" ? r.value : void 0);
|
|
309
|
+
const annotationsRecord = annotations !== void 0 && annotations.size > 0 ? Object.freeze(Object.fromEntries(annotations)) : void 0;
|
|
310
|
+
const meta = Object.freeze({
|
|
311
|
+
target: ctx.target,
|
|
312
|
+
storageHash: ctx.storageHash,
|
|
313
|
+
lane: "dsl",
|
|
314
|
+
...ifDefined("annotations", annotationsRecord)
|
|
315
|
+
});
|
|
316
|
+
return Object.freeze({
|
|
317
|
+
ast,
|
|
318
|
+
params: paramValues,
|
|
319
|
+
meta
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
function buildPlan(state, ctx) {
|
|
323
|
+
return buildQueryPlan(buildSelectAst(state), ctx, state.annotations);
|
|
324
|
+
}
|
|
325
|
+
function tableToScope(alias, table, options) {
|
|
326
|
+
const storage = options?.storage;
|
|
327
|
+
const lookupName = options?.tableName;
|
|
328
|
+
const namespaceId = options?.namespaceId;
|
|
329
|
+
const fields = {};
|
|
330
|
+
for (const [colName, col] of Object.entries(table.columns)) {
|
|
331
|
+
const codec = storage && lookupName && namespaceId !== void 0 ? codecRefForStorageColumn(storage, namespaceId, lookupName, colName) : void 0;
|
|
332
|
+
fields[colName] = {
|
|
333
|
+
codecId: col.codecId,
|
|
334
|
+
nullable: col.nullable,
|
|
335
|
+
...col.many ? { many: true } : {},
|
|
336
|
+
...codec !== void 0 ? { codec } : {}
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
topLevel: { ...fields },
|
|
341
|
+
namespaces: { [alias]: fields }
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
function mergeScopes(a, b) {
|
|
345
|
+
const topLevel = {};
|
|
346
|
+
for (const [k, v] of Object.entries(a.topLevel)) if (!(k in b.topLevel)) topLevel[k] = v;
|
|
347
|
+
for (const [k, v] of Object.entries(b.topLevel)) if (!(k in a.topLevel)) topLevel[k] = v;
|
|
348
|
+
return {
|
|
349
|
+
topLevel,
|
|
350
|
+
namespaces: {
|
|
351
|
+
...a.namespaces,
|
|
352
|
+
...b.namespaces
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
function nullableScope(scope) {
|
|
357
|
+
const mkNullable = (tbl) => {
|
|
358
|
+
const result = {};
|
|
359
|
+
for (const [k, v] of Object.entries(tbl)) result[k] = {
|
|
360
|
+
codecId: v.codecId,
|
|
361
|
+
nullable: true,
|
|
362
|
+
...v.codec !== void 0 ? { codec: v.codec } : {}
|
|
363
|
+
};
|
|
364
|
+
return result;
|
|
365
|
+
};
|
|
366
|
+
const namespaces = {};
|
|
367
|
+
for (const [k, v] of Object.entries(scope.namespaces)) namespaces[k] = mkNullable(v);
|
|
368
|
+
return {
|
|
369
|
+
topLevel: mkNullable(scope.topLevel),
|
|
370
|
+
namespaces
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
function orderByScopeOf(scope, rowFields) {
|
|
374
|
+
return {
|
|
375
|
+
topLevel: {
|
|
376
|
+
...scope.topLevel,
|
|
377
|
+
...rowFields
|
|
378
|
+
},
|
|
379
|
+
namespaces: scope.namespaces
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
function assertCapability(ctx, required, methodName) {
|
|
383
|
+
for (const [ns, keys] of Object.entries(required)) for (const key of Object.keys(keys)) if (!ctx.capabilities[ns]?.[key]) throw structuredError("ORM.CAPABILITY_MISSING", `${methodName}() requires capability ${ns}.${key}`, { meta: {
|
|
384
|
+
method: methodName,
|
|
385
|
+
capability: `${ns}.${key}`
|
|
386
|
+
} });
|
|
387
|
+
}
|
|
388
|
+
function resolveSelectArgs(args, scope, ctx) {
|
|
389
|
+
const projections = [];
|
|
390
|
+
const newRowFields = {};
|
|
391
|
+
if (args.length === 0) return {
|
|
392
|
+
projections,
|
|
393
|
+
newRowFields
|
|
394
|
+
};
|
|
395
|
+
if (typeof args[0] === "string" && (args.length === 1 || typeof args[1] !== "function")) {
|
|
396
|
+
for (const colName of args) {
|
|
397
|
+
const field = scope.topLevel[colName];
|
|
398
|
+
if (!field) throw structuredError("ORM.COLUMN_UNKNOWN", `Column "${colName}" not found in scope`, { meta: { column: colName } });
|
|
399
|
+
projections.push(ProjectionItem.of(colName, IdentifierRef.of(colName), field.codec));
|
|
400
|
+
newRowFields[colName] = field;
|
|
401
|
+
}
|
|
402
|
+
return {
|
|
403
|
+
projections,
|
|
404
|
+
newRowFields
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
if (typeof args[0] === "string" && typeof args[1] === "function") {
|
|
408
|
+
const alias = args[0];
|
|
409
|
+
const exprFn = args[1];
|
|
410
|
+
const fns = createAggregateFunctions(ctx.queryOperationTypes, ctx.rawCodecInferer);
|
|
411
|
+
const result = exprFn(createFieldProxy(scope), fns);
|
|
412
|
+
const field = result.returnType;
|
|
413
|
+
projections.push(ProjectionItem.of(alias, result.buildAst(), field.codec));
|
|
414
|
+
newRowFields[alias] = field;
|
|
415
|
+
return {
|
|
416
|
+
projections,
|
|
417
|
+
newRowFields
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
if (typeof args[0] === "function") {
|
|
421
|
+
const callbackFn = args[0];
|
|
422
|
+
const fns = createAggregateFunctions(ctx.queryOperationTypes, ctx.rawCodecInferer);
|
|
423
|
+
const record = callbackFn(createFieldProxy(scope), fns);
|
|
424
|
+
for (const [key, expr] of Object.entries(record)) {
|
|
425
|
+
const field = expr.returnType;
|
|
426
|
+
projections.push(ProjectionItem.of(key, expr.buildAst(), field.codec));
|
|
427
|
+
newRowFields[key] = field;
|
|
428
|
+
}
|
|
429
|
+
return {
|
|
430
|
+
projections,
|
|
431
|
+
newRowFields
|
|
432
|
+
};
|
|
433
|
+
}
|
|
434
|
+
throw structuredError("ORM.ARGUMENT_INVALID", "Invalid .select() arguments");
|
|
435
|
+
}
|
|
436
|
+
function resolveOrderBy(arg, options, scope, rowFields, ctx, useAggregateFns) {
|
|
437
|
+
const dir = options?.direction ?? "asc";
|
|
438
|
+
if (typeof arg === "string") {
|
|
439
|
+
if (!(arg in orderByScopeOf(scope, rowFields).topLevel)) throw structuredError("ORM.COLUMN_UNKNOWN", `Column "${arg}" not found in scope for orderBy`, { meta: { column: arg } });
|
|
440
|
+
const expr = IdentifierRef.of(arg);
|
|
441
|
+
return dir === "asc" ? OrderByItem.asc(expr) : OrderByItem.desc(expr);
|
|
442
|
+
}
|
|
443
|
+
if (typeof arg === "function") {
|
|
444
|
+
const combined = orderByScopeOf(scope, rowFields);
|
|
445
|
+
const fns = useAggregateFns ? createAggregateFunctions(ctx.queryOperationTypes, ctx.rawCodecInferer) : createFunctions(ctx.queryOperationTypes, ctx.rawCodecInferer);
|
|
446
|
+
const result = arg(createFieldProxy(combined), fns);
|
|
447
|
+
return dir === "asc" ? OrderByItem.asc(result.buildAst()) : OrderByItem.desc(result.buildAst());
|
|
448
|
+
}
|
|
449
|
+
throw structuredError("ORM.ARGUMENT_INVALID", "Invalid orderBy argument");
|
|
450
|
+
}
|
|
451
|
+
function resolveGroupBy(args, scope, rowFields, ctx) {
|
|
452
|
+
if (typeof args[0] === "string") {
|
|
453
|
+
const combined = orderByScopeOf(scope, rowFields);
|
|
454
|
+
return args.map((colName) => {
|
|
455
|
+
if (!(colName in combined.topLevel)) throw structuredError("ORM.COLUMN_UNKNOWN", `Column "${colName}" not found in scope for groupBy`, { meta: { column: colName } });
|
|
456
|
+
return IdentifierRef.of(colName);
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
if (typeof args[0] === "function") {
|
|
460
|
+
const combined = orderByScopeOf(scope, rowFields);
|
|
461
|
+
const fns = createFunctions(ctx.queryOperationTypes, ctx.rawCodecInferer);
|
|
462
|
+
return [args[0](createFieldProxy(combined), fns).buildAst()];
|
|
463
|
+
}
|
|
464
|
+
throw structuredError("ORM.ARGUMENT_INVALID", "Invalid groupBy arguments");
|
|
465
|
+
}
|
|
466
|
+
function resolveDistinctOn(args, scope, rowFields, ctx) {
|
|
467
|
+
if (args.length === 1 && typeof args[0] === "function") {
|
|
468
|
+
const combined = orderByScopeOf(scope, rowFields);
|
|
469
|
+
const fns = createFunctions(ctx.queryOperationTypes, ctx.rawCodecInferer);
|
|
470
|
+
return [args[0](createFieldProxy(combined), fns).buildAst()];
|
|
471
|
+
}
|
|
472
|
+
const combined = orderByScopeOf(scope, rowFields);
|
|
473
|
+
return args.map((colName) => {
|
|
474
|
+
if (!(colName in combined.topLevel)) throw structuredError("ORM.COLUMN_UNKNOWN", `Column "${colName}" not found in scope for distinctOn`, { meta: { column: colName } });
|
|
475
|
+
return IdentifierRef.of(colName);
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
var QueryBase = class extends BuilderBase {
|
|
479
|
+
state;
|
|
480
|
+
constructor(state, ctx) {
|
|
481
|
+
super(ctx);
|
|
482
|
+
this.state = state;
|
|
483
|
+
}
|
|
484
|
+
distinctOn = this._gate({ postgres: { distinctOn: true } }, "distinctOn", (...args) => {
|
|
485
|
+
const exprs = resolveDistinctOn(args, this.state.scope, this.state.rowFields, this.ctx);
|
|
486
|
+
return this.clone(cloneState(this.state, { distinctOn: [...this.state.distinctOn ?? [], ...exprs] }));
|
|
487
|
+
});
|
|
488
|
+
limit(count) {
|
|
489
|
+
const limit = typeof count === "number" ? count : toExpr(count);
|
|
490
|
+
return this.clone(cloneState(this.state, { limit }));
|
|
491
|
+
}
|
|
492
|
+
offset(count) {
|
|
493
|
+
const offset = typeof count === "number" ? count : toExpr(count);
|
|
494
|
+
return this.clone(cloneState(this.state, { offset }));
|
|
495
|
+
}
|
|
496
|
+
distinct() {
|
|
497
|
+
return this.clone(cloneState(this.state, { distinct: true }));
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Attach one or more annotations to this query plan.
|
|
501
|
+
*
|
|
502
|
+
* Read builders (`SelectQueryImpl`, `GroupedQueryImpl`) accept
|
|
503
|
+
* annotations whose declared `applicableTo` includes `'read'`.
|
|
504
|
+
* The type-level `As & ValidAnnotations<'read', As>` gate rejects
|
|
505
|
+
* write-only annotations at the call site; the runtime check below
|
|
506
|
+
* fails closed for callers that bypass the type gate (cast / `any`).
|
|
507
|
+
*
|
|
508
|
+
* Multiple `.annotate(...)` calls compose; duplicate namespaces use
|
|
509
|
+
* last-write-wins. The accumulated annotations are merged into
|
|
510
|
+
* `plan.meta.annotations` at `.build()` time, alongside any framework-
|
|
511
|
+
* internal metadata under reserved namespaces (e.g. `codecs`).
|
|
512
|
+
*
|
|
513
|
+
* Chainable in any position (before / after `.where`, `.select`,
|
|
514
|
+
* `.limit`, etc.); the returned builder has the same row type.
|
|
515
|
+
*/
|
|
516
|
+
annotate(...annotations) {
|
|
517
|
+
assertAnnotationsApplicable(annotations, "read", "sql-dsl.annotate");
|
|
518
|
+
const next = new Map(this.state.annotations);
|
|
519
|
+
for (const annotation of annotations) next.set(annotation.namespace, annotation);
|
|
520
|
+
return this.clone(cloneState(this.state, { annotations: next }));
|
|
521
|
+
}
|
|
522
|
+
groupBy(...args) {
|
|
523
|
+
const exprs = resolveGroupBy(args, this.state.scope, this.state.rowFields, this.ctx);
|
|
524
|
+
return new GroupedQueryImpl(cloneState(this.state, { groupBy: [...this.state.groupBy, ...exprs] }), this.ctx);
|
|
525
|
+
}
|
|
526
|
+
as(alias) {
|
|
527
|
+
const ast = buildSelectAst(this.state);
|
|
528
|
+
const derivedSource = DerivedTableSource.as(alias, ast);
|
|
529
|
+
const scope = {
|
|
530
|
+
topLevel: this.state.rowFields,
|
|
531
|
+
namespaces: { [alias]: this.state.rowFields }
|
|
532
|
+
};
|
|
533
|
+
return {
|
|
534
|
+
getJoinOuterScope: () => scope,
|
|
535
|
+
buildAst: () => derivedSource
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
getRowFields() {
|
|
539
|
+
return this.state.rowFields;
|
|
540
|
+
}
|
|
541
|
+
buildAst() {
|
|
542
|
+
return buildSelectAst(this.state);
|
|
543
|
+
}
|
|
544
|
+
build() {
|
|
545
|
+
return buildPlan(this.state, this.ctx);
|
|
546
|
+
}
|
|
547
|
+
};
|
|
548
|
+
var SelectQueryImpl = class SelectQueryImpl extends QueryBase {
|
|
549
|
+
clone(state) {
|
|
550
|
+
return new SelectQueryImpl(state, this.ctx);
|
|
551
|
+
}
|
|
552
|
+
select(...args) {
|
|
553
|
+
const { projections, newRowFields } = resolveSelectArgs(args, this.state.scope, this.ctx);
|
|
554
|
+
return new SelectQueryImpl(cloneState(this.state, {
|
|
555
|
+
projections: [...this.state.projections, ...projections],
|
|
556
|
+
rowFields: {
|
|
557
|
+
...this.state.rowFields,
|
|
558
|
+
...newRowFields
|
|
559
|
+
}
|
|
560
|
+
}), this.ctx);
|
|
561
|
+
}
|
|
562
|
+
where(expr) {
|
|
563
|
+
const result = expr(createFieldProxy(this.state.scope), createFunctions(this.ctx.queryOperationTypes, this.ctx.rawCodecInferer));
|
|
564
|
+
return new SelectQueryImpl(cloneState(this.state, { where: [...this.state.where, result.buildAst()] }), this.ctx);
|
|
565
|
+
}
|
|
566
|
+
orderBy(arg, options) {
|
|
567
|
+
const item = resolveOrderBy(arg, options, this.state.scope, this.state.rowFields, this.ctx, false);
|
|
568
|
+
return this.clone(cloneState(this.state, { orderBy: [...this.state.orderBy, item] }));
|
|
569
|
+
}
|
|
570
|
+
};
|
|
571
|
+
var GroupedQueryImpl = class GroupedQueryImpl extends QueryBase {
|
|
572
|
+
clone(state) {
|
|
573
|
+
return new GroupedQueryImpl(state, this.ctx);
|
|
574
|
+
}
|
|
575
|
+
having(expr) {
|
|
576
|
+
const combined = orderByScopeOf(this.state.scope, this.state.rowFields);
|
|
577
|
+
const fns = createAggregateFunctions(this.ctx.queryOperationTypes, this.ctx.rawCodecInferer);
|
|
578
|
+
const result = expr(createFieldProxy(combined), fns);
|
|
579
|
+
return new GroupedQueryImpl(cloneState(this.state, { having: result.buildAst() }), this.ctx);
|
|
580
|
+
}
|
|
581
|
+
orderBy(arg, options) {
|
|
582
|
+
const item = resolveOrderBy(arg, options, this.state.scope, this.state.rowFields, this.ctx, true);
|
|
583
|
+
return this.clone(cloneState(this.state, { orderBy: [...this.state.orderBy, item] }));
|
|
584
|
+
}
|
|
585
|
+
};
|
|
586
|
+
var JoinedTablesImpl = class JoinedTablesImpl extends BuilderBase {
|
|
587
|
+
#state;
|
|
588
|
+
constructor(state, ctx) {
|
|
589
|
+
super(ctx);
|
|
590
|
+
this.#state = state;
|
|
591
|
+
}
|
|
592
|
+
lateralJoin = this._gate({ sql: { lateral: true } }, "lateralJoin", (alias, builder) => {
|
|
593
|
+
const { derivedSource, lateralScope } = this.#buildLateral(alias, builder);
|
|
594
|
+
const resultScope = mergeScopes(this.#state.scope, lateralScope);
|
|
595
|
+
return this.#addLateralJoin("inner", resultScope, derivedSource);
|
|
596
|
+
});
|
|
597
|
+
outerLateralJoin = this._gate({ sql: { lateral: true } }, "outerLateralJoin", (alias, builder) => {
|
|
598
|
+
const { derivedSource, lateralScope } = this.#buildLateral(alias, builder);
|
|
599
|
+
const resultScope = mergeScopes(this.#state.scope, nullableScope(lateralScope));
|
|
600
|
+
return this.#addLateralJoin("left", resultScope, derivedSource);
|
|
601
|
+
});
|
|
602
|
+
select(...args) {
|
|
603
|
+
const { projections, newRowFields } = resolveSelectArgs(args, this.#state.scope, this.ctx);
|
|
604
|
+
return new SelectQueryImpl(cloneState(this.#state, {
|
|
605
|
+
projections: [...this.#state.projections, ...projections],
|
|
606
|
+
rowFields: {
|
|
607
|
+
...this.#state.rowFields,
|
|
608
|
+
...newRowFields
|
|
609
|
+
}
|
|
610
|
+
}), this.ctx);
|
|
611
|
+
}
|
|
612
|
+
innerJoin(other, on) {
|
|
613
|
+
const targetScope = mergeScopes(this.#state.scope, other.getJoinOuterScope());
|
|
614
|
+
return this.#addJoin(other, "inner", targetScope, on);
|
|
615
|
+
}
|
|
616
|
+
outerLeftJoin(other, on) {
|
|
617
|
+
const targetScope = mergeScopes(this.#state.scope, nullableScope(other.getJoinOuterScope()));
|
|
618
|
+
return this.#addJoin(other, "left", targetScope, on);
|
|
619
|
+
}
|
|
620
|
+
outerRightJoin(other, on) {
|
|
621
|
+
const targetScope = mergeScopes(nullableScope(this.#state.scope), other.getJoinOuterScope());
|
|
622
|
+
return this.#addJoin(other, "right", targetScope, on);
|
|
623
|
+
}
|
|
624
|
+
outerFullJoin(other, on) {
|
|
625
|
+
const targetScope = mergeScopes(nullableScope(this.#state.scope), nullableScope(other.getJoinOuterScope()));
|
|
626
|
+
return this.#addJoin(other, "full", targetScope, on);
|
|
627
|
+
}
|
|
628
|
+
#addJoin(other, joinType, resultScope, onExpr) {
|
|
629
|
+
const onResult = onExpr(createFieldProxy(mergeScopes(this.#state.scope, other.getJoinOuterScope())), createFunctions(this.ctx.queryOperationTypes, this.ctx.rawCodecInferer));
|
|
630
|
+
const joinAst = new JoinAst(joinType, other.buildAst(), onResult.buildAst());
|
|
631
|
+
return new JoinedTablesImpl(cloneState(this.#state, {
|
|
632
|
+
joins: [...this.#state.joins, joinAst],
|
|
633
|
+
scope: resultScope
|
|
634
|
+
}), this.ctx);
|
|
635
|
+
}
|
|
636
|
+
#buildLateral(alias, builderFn) {
|
|
637
|
+
const subquery = builderFn({ from: (other) => {
|
|
638
|
+
const otherScope = other.getJoinOuterScope();
|
|
639
|
+
const parentMerged = mergeScopes(this.#state.scope, otherScope);
|
|
640
|
+
return new SelectQueryImpl(emptyState(other.buildAst(), parentMerged), this.ctx);
|
|
641
|
+
} });
|
|
642
|
+
const subqueryAst = subquery.buildAst();
|
|
643
|
+
const derivedSource = DerivedTableSource.as(alias, subqueryAst);
|
|
644
|
+
const subqueryRowFields = subquery.getRowFields();
|
|
645
|
+
return {
|
|
646
|
+
derivedSource,
|
|
647
|
+
lateralScope: {
|
|
648
|
+
topLevel: subqueryRowFields,
|
|
649
|
+
namespaces: { [alias]: subqueryRowFields }
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
#addLateralJoin(joinType, resultScope, derivedSource) {
|
|
654
|
+
const joinAst = new JoinAst(joinType, derivedSource, AndExpr.of([]), true);
|
|
655
|
+
return new JoinedTablesImpl(cloneState(this.#state, {
|
|
656
|
+
joins: [...this.#state.joins, joinAst],
|
|
657
|
+
scope: resultScope
|
|
658
|
+
}), this.ctx);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
/**
|
|
662
|
+
* Validates and merges a variadic annotations call into a builder's
|
|
663
|
+
* accumulated user-annotations map. Used by `.annotate(...)` on each of
|
|
664
|
+
* the three mutation builders (`InsertQueryImpl`, `UpdateQueryImpl`,
|
|
665
|
+
* `DeleteQueryImpl`); the read builders share the same logic via
|
|
666
|
+
* `QueryBase.annotate()` in `./query-impl.ts`.
|
|
667
|
+
*
|
|
668
|
+
* Runs `assertAnnotationsApplicable` at call time (not at `.build()`) so
|
|
669
|
+
* inapplicable annotations forced through casts surface immediately
|
|
670
|
+
* rather than at plan-construction time.
|
|
671
|
+
*/
|
|
672
|
+
function mergeWriteAnnotations(current, annotations) {
|
|
673
|
+
assertAnnotationsApplicable(annotations, "write", "sql-dsl.annotate");
|
|
674
|
+
const next = new Map(current);
|
|
675
|
+
for (const annotation of annotations) next.set(annotation.namespace, annotation);
|
|
676
|
+
return next;
|
|
677
|
+
}
|
|
678
|
+
function buildParamValues(values, namespaceId, table, tableName, op, ctx) {
|
|
679
|
+
const params = {};
|
|
680
|
+
for (const [col, value] of Object.entries(values)) {
|
|
681
|
+
const codec = table.columns[col] ? codecRefFor(ctx, namespaceId, tableName, col) : void 0;
|
|
682
|
+
params[col] = ParamRef.of(value, codec ? { codec } : void 0);
|
|
683
|
+
}
|
|
684
|
+
for (const def of ctx.applyMutationDefaults({
|
|
685
|
+
op,
|
|
686
|
+
namespace: namespaceId,
|
|
687
|
+
table: tableName,
|
|
688
|
+
values
|
|
689
|
+
})) {
|
|
690
|
+
const codec = table.columns[def.column] ? codecRefFor(ctx, namespaceId, tableName, def.column) : void 0;
|
|
691
|
+
params[def.column] = ParamRef.of(def.value, codec ? { codec } : void 0);
|
|
692
|
+
}
|
|
693
|
+
return params;
|
|
694
|
+
}
|
|
695
|
+
function buildReturningProjections(tableName, columns, rowFields) {
|
|
696
|
+
return columns.map((col) => ProjectionItem.of(col, ColumnRef.of(tableName, col), rowFields[col]?.codec));
|
|
697
|
+
}
|
|
698
|
+
function evaluateWhere(whereCallback, scope, queryOperationTypes, rawCodecInferer) {
|
|
699
|
+
return whereCallback(createFieldProxy(scope), createFunctions(queryOperationTypes, rawCodecInferer)).buildAst();
|
|
700
|
+
}
|
|
701
|
+
function evaluateUpdateCallback(callback, scope, queryOperationTypes, rawCodecInferer) {
|
|
702
|
+
const result = callback(createFieldProxy(scope), createFunctions(queryOperationTypes, rawCodecInferer));
|
|
703
|
+
const set = {};
|
|
704
|
+
for (const [col, expr] of Object.entries(result)) if (expr !== void 0) set[col] = expr.buildAst();
|
|
705
|
+
return set;
|
|
706
|
+
}
|
|
707
|
+
function buildSetExpressions(exprs, namespaceId, table, tableName, op, ctx) {
|
|
708
|
+
const set = { ...exprs };
|
|
709
|
+
for (const def of ctx.applyMutationDefaults({
|
|
710
|
+
op,
|
|
711
|
+
namespace: namespaceId,
|
|
712
|
+
table: tableName,
|
|
713
|
+
values: exprs
|
|
714
|
+
})) if (!(def.column in set)) {
|
|
715
|
+
const codec = table.columns[def.column] ? codecRefFor(ctx, namespaceId, tableName, def.column) : void 0;
|
|
716
|
+
set[def.column] = ParamRef.of(def.value, ifDefined("codec", codec));
|
|
717
|
+
}
|
|
718
|
+
return set;
|
|
719
|
+
}
|
|
720
|
+
var InsertQueryImpl = class InsertQueryImpl extends BuilderBase {
|
|
721
|
+
#tableSource;
|
|
722
|
+
#tableName;
|
|
723
|
+
#namespaceId;
|
|
724
|
+
#table;
|
|
725
|
+
#scope;
|
|
726
|
+
#rows;
|
|
727
|
+
#returningColumns;
|
|
728
|
+
#rowFields;
|
|
729
|
+
#annotations;
|
|
730
|
+
constructor(tableSource, namespaceId, table, scope, rows, ctx, returningColumns = [], rowFields = {}, annotations = /* @__PURE__ */ new Map()) {
|
|
731
|
+
super(ctx);
|
|
732
|
+
this.#tableSource = tableSource;
|
|
733
|
+
this.#tableName = tableSource.name;
|
|
734
|
+
this.#namespaceId = namespaceId;
|
|
735
|
+
this.#table = table;
|
|
736
|
+
this.#scope = scope;
|
|
737
|
+
this.#rows = rows;
|
|
738
|
+
this.#returningColumns = returningColumns;
|
|
739
|
+
this.#rowFields = rowFields;
|
|
740
|
+
this.#annotations = annotations;
|
|
741
|
+
}
|
|
742
|
+
returning = this._gate({ sql: { returning: true } }, "returning", (...columns) => {
|
|
743
|
+
const newRowFields = {};
|
|
744
|
+
for (const col of columns) {
|
|
745
|
+
const field = this.#scope.topLevel[col];
|
|
746
|
+
if (!field) throw structuredError("ORM.COLUMN_UNKNOWN", `Column "${col}" not found in scope`, { meta: { column: col } });
|
|
747
|
+
newRowFields[col] = field;
|
|
748
|
+
}
|
|
749
|
+
return new InsertQueryImpl(this.#tableSource, this.#namespaceId, this.#table, this.#scope, this.#rows, this.ctx, columns, newRowFields, this.#annotations);
|
|
750
|
+
});
|
|
751
|
+
/**
|
|
752
|
+
* Attach one or more write-typed annotations to this query plan.
|
|
753
|
+
* The type-level `As & ValidAnnotations<'write', As>` gate rejects
|
|
754
|
+
* read-only annotations at the call site; the runtime check fails
|
|
755
|
+
* closed for callers that bypass the type gate. See `QueryBase.annotate`
|
|
756
|
+
* in `./query-impl.ts` for the read-builder counterpart.
|
|
757
|
+
*/
|
|
758
|
+
annotate(...annotations) {
|
|
759
|
+
return new InsertQueryImpl(this.#tableSource, this.#namespaceId, this.#table, this.#scope, this.#rows, this.ctx, this.#returningColumns, this.#rowFields, mergeWriteAnnotations(this.#annotations, annotations));
|
|
760
|
+
}
|
|
761
|
+
build() {
|
|
762
|
+
if (this.#rows.length === 0) throw structuredError("ORM.MUTATION_DATA_MISSING", "insert() called with an empty row array — at least one row is required");
|
|
763
|
+
const paramRows = this.#rows.map((rowValues) => buildParamValues(rowValues, this.#namespaceId, this.#table, this.#tableName, "create", this.ctx));
|
|
764
|
+
let ast = InsertAst.into(this.#tableSource).withRows(paramRows);
|
|
765
|
+
if (this.#returningColumns.length > 0) ast = ast.withReturning(buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields));
|
|
766
|
+
return buildQueryPlan(ast, this.ctx, this.#annotations);
|
|
767
|
+
}
|
|
768
|
+
};
|
|
769
|
+
var UpdateQueryImpl = class UpdateQueryImpl extends BuilderBase {
|
|
770
|
+
#tableSource;
|
|
771
|
+
#tableName;
|
|
772
|
+
#scope;
|
|
773
|
+
#setExpressions;
|
|
774
|
+
#whereExprs;
|
|
775
|
+
#returningColumns;
|
|
776
|
+
#rowFields;
|
|
777
|
+
#annotations;
|
|
778
|
+
constructor(tableSource, scope, setExpressions, ctx, whereExprs = [], returningColumns = [], rowFields = {}, annotations = /* @__PURE__ */ new Map()) {
|
|
779
|
+
super(ctx);
|
|
780
|
+
this.#tableSource = tableSource;
|
|
781
|
+
this.#tableName = tableSource.name;
|
|
782
|
+
this.#scope = scope;
|
|
783
|
+
this.#setExpressions = setExpressions;
|
|
784
|
+
this.#whereExprs = whereExprs;
|
|
785
|
+
this.#returningColumns = returningColumns;
|
|
786
|
+
this.#rowFields = rowFields;
|
|
787
|
+
this.#annotations = annotations;
|
|
788
|
+
}
|
|
789
|
+
where(expr) {
|
|
790
|
+
const result = expr(createFieldProxy(this.#scope), createFunctions(this.ctx.queryOperationTypes, this.ctx.rawCodecInferer));
|
|
791
|
+
return new UpdateQueryImpl(this.#tableSource, this.#scope, this.#setExpressions, this.ctx, [...this.#whereExprs, result.buildAst()], this.#returningColumns, this.#rowFields, this.#annotations);
|
|
792
|
+
}
|
|
793
|
+
returning = this._gate({ sql: { returning: true } }, "returning", (...columns) => {
|
|
794
|
+
const newRowFields = {};
|
|
795
|
+
for (const col of columns) {
|
|
796
|
+
const field = this.#scope.topLevel[col];
|
|
797
|
+
if (!field) throw structuredError("ORM.COLUMN_UNKNOWN", `Column "${col}" not found in scope`, { meta: { column: col } });
|
|
798
|
+
newRowFields[col] = field;
|
|
799
|
+
}
|
|
800
|
+
return new UpdateQueryImpl(this.#tableSource, this.#scope, this.#setExpressions, this.ctx, this.#whereExprs, columns, newRowFields, this.#annotations);
|
|
801
|
+
});
|
|
802
|
+
/**
|
|
803
|
+
* Attach one or more write-typed annotations to this query plan.
|
|
804
|
+
* See `InsertQueryImpl.annotate` for semantics; the runtime check
|
|
805
|
+
* fails closed for callers that bypass the type-level gate.
|
|
806
|
+
*/
|
|
807
|
+
annotate(...annotations) {
|
|
808
|
+
return new UpdateQueryImpl(this.#tableSource, this.#scope, this.#setExpressions, this.ctx, this.#whereExprs, this.#returningColumns, this.#rowFields, mergeWriteAnnotations(this.#annotations, annotations));
|
|
809
|
+
}
|
|
810
|
+
build() {
|
|
811
|
+
let ast = UpdateAst.table(this.#tableSource).withSet(this.#setExpressions).withWhere(combineWhereExprs(this.#whereExprs));
|
|
812
|
+
if (this.#returningColumns.length > 0) ast = ast.withReturning(buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields));
|
|
813
|
+
return buildQueryPlan(ast, this.ctx, this.#annotations);
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
var DeleteQueryImpl = class DeleteQueryImpl extends BuilderBase {
|
|
817
|
+
#tableSource;
|
|
818
|
+
#tableName;
|
|
819
|
+
#scope;
|
|
820
|
+
#whereCallbacks;
|
|
821
|
+
#returningColumns;
|
|
822
|
+
#rowFields;
|
|
823
|
+
#annotations;
|
|
824
|
+
constructor(tableSource, scope, ctx, whereCallbacks = [], returningColumns = [], rowFields = {}, annotations = /* @__PURE__ */ new Map()) {
|
|
825
|
+
super(ctx);
|
|
826
|
+
this.#tableSource = tableSource;
|
|
827
|
+
this.#tableName = tableSource.name;
|
|
828
|
+
this.#scope = scope;
|
|
829
|
+
this.#whereCallbacks = whereCallbacks;
|
|
830
|
+
this.#returningColumns = returningColumns;
|
|
831
|
+
this.#rowFields = rowFields;
|
|
832
|
+
this.#annotations = annotations;
|
|
833
|
+
}
|
|
834
|
+
where(expr) {
|
|
835
|
+
return new DeleteQueryImpl(this.#tableSource, this.#scope, this.ctx, [...this.#whereCallbacks, expr], this.#returningColumns, this.#rowFields, this.#annotations);
|
|
836
|
+
}
|
|
837
|
+
returning = this._gate({ sql: { returning: true } }, "returning", (...columns) => {
|
|
838
|
+
const newRowFields = {};
|
|
839
|
+
for (const col of columns) {
|
|
840
|
+
const field = this.#scope.topLevel[col];
|
|
841
|
+
if (!field) throw structuredError("ORM.COLUMN_UNKNOWN", `Column "${col}" not found in scope`, { meta: { column: col } });
|
|
842
|
+
newRowFields[col] = field;
|
|
843
|
+
}
|
|
844
|
+
return new DeleteQueryImpl(this.#tableSource, this.#scope, this.ctx, this.#whereCallbacks, columns, newRowFields, this.#annotations);
|
|
845
|
+
});
|
|
846
|
+
/**
|
|
847
|
+
* Attach one or more write-typed annotations to this query plan.
|
|
848
|
+
* See `InsertQueryImpl.annotate` for semantics.
|
|
849
|
+
*/
|
|
850
|
+
annotate(...annotations) {
|
|
851
|
+
return new DeleteQueryImpl(this.#tableSource, this.#scope, this.ctx, this.#whereCallbacks, this.#returningColumns, this.#rowFields, mergeWriteAnnotations(this.#annotations, annotations));
|
|
852
|
+
}
|
|
853
|
+
build() {
|
|
854
|
+
const whereExpr = combineWhereExprs(this.#whereCallbacks.map((cb) => evaluateWhere(cb, this.#scope, this.ctx.queryOperationTypes, this.ctx.rawCodecInferer)));
|
|
855
|
+
let ast = DeleteAst.from(this.#tableSource).withWhere(whereExpr);
|
|
856
|
+
if (this.#returningColumns.length > 0) ast = ast.withReturning(buildReturningProjections(this.#tableName, this.#returningColumns, this.#rowFields));
|
|
857
|
+
return buildQueryPlan(ast, this.ctx, this.#annotations);
|
|
858
|
+
}
|
|
859
|
+
};
|
|
860
|
+
function tableSourceForProxy(tableName, alias, namespaceId) {
|
|
861
|
+
return TableSource.named(tableName, alias !== tableName ? alias : void 0, namespaceId);
|
|
862
|
+
}
|
|
863
|
+
var TableProxyImpl = class TableProxyImpl extends BuilderBase {
|
|
864
|
+
#tableName;
|
|
865
|
+
#table;
|
|
866
|
+
#namespaceId;
|
|
867
|
+
#fromSource;
|
|
868
|
+
#scope;
|
|
869
|
+
constructor(tableName, table, alias, ctx, namespaceId) {
|
|
870
|
+
super(ctx);
|
|
871
|
+
this.#tableName = tableName;
|
|
872
|
+
this.#table = table;
|
|
873
|
+
this.#namespaceId = namespaceId;
|
|
874
|
+
this.#scope = tableToScope(alias, table, {
|
|
875
|
+
storage: ctx.storage,
|
|
876
|
+
tableName,
|
|
877
|
+
namespaceId
|
|
878
|
+
});
|
|
879
|
+
this.#fromSource = tableSourceForProxy(tableName, alias, namespaceId);
|
|
880
|
+
}
|
|
881
|
+
lateralJoin = this._gate({ sql: { lateral: true } }, "lateralJoin", (alias, builder) => {
|
|
882
|
+
return this.#toJoined().lateralJoin(alias, builder);
|
|
883
|
+
});
|
|
884
|
+
outerLateralJoin = this._gate({ sql: { lateral: true } }, "outerLateralJoin", (alias, builder) => {
|
|
885
|
+
return this.#toJoined().outerLateralJoin(alias, builder);
|
|
886
|
+
});
|
|
887
|
+
getJoinOuterScope() {
|
|
888
|
+
return this.#scope;
|
|
889
|
+
}
|
|
890
|
+
buildAst() {
|
|
891
|
+
return this.#fromSource;
|
|
892
|
+
}
|
|
893
|
+
as(newAlias) {
|
|
894
|
+
return new TableProxyImpl(this.#tableName, this.#table, newAlias, this.ctx, this.#namespaceId);
|
|
895
|
+
}
|
|
896
|
+
select(...args) {
|
|
897
|
+
return new SelectQueryImpl(emptyState(this.#fromSource, this.#scope), this.ctx).select(...args);
|
|
898
|
+
}
|
|
899
|
+
innerJoin(other, on) {
|
|
900
|
+
return this.#toJoined().innerJoin(other, on);
|
|
901
|
+
}
|
|
902
|
+
outerLeftJoin(other, on) {
|
|
903
|
+
return this.#toJoined().outerLeftJoin(other, on);
|
|
904
|
+
}
|
|
905
|
+
outerRightJoin(other, on) {
|
|
906
|
+
return this.#toJoined().outerRightJoin(other, on);
|
|
907
|
+
}
|
|
908
|
+
outerFullJoin(other, on) {
|
|
909
|
+
return this.#toJoined().outerFullJoin(other, on);
|
|
910
|
+
}
|
|
911
|
+
insert(rows) {
|
|
912
|
+
return new InsertQueryImpl(this.#fromSource, this.#namespaceId, this.#table, this.#scope, rows, this.ctx);
|
|
913
|
+
}
|
|
914
|
+
update(setOrCallback) {
|
|
915
|
+
if (typeof setOrCallback === "function") {
|
|
916
|
+
const setExpressions = buildSetExpressions(evaluateUpdateCallback(setOrCallback, this.#scope, this.ctx.queryOperationTypes, this.ctx.rawCodecInferer), this.#namespaceId, this.#table, this.#tableName, "update", this.ctx);
|
|
917
|
+
return new UpdateQueryImpl(this.#fromSource, this.#scope, setExpressions, this.ctx);
|
|
918
|
+
}
|
|
919
|
+
const setExpressions = buildParamValues(setOrCallback, this.#namespaceId, this.#table, this.#tableName, "update", this.ctx);
|
|
920
|
+
return new UpdateQueryImpl(this.#fromSource, this.#scope, setExpressions, this.ctx);
|
|
921
|
+
}
|
|
922
|
+
delete() {
|
|
923
|
+
return new DeleteQueryImpl(this.#fromSource, this.#scope, this.ctx);
|
|
924
|
+
}
|
|
925
|
+
#toJoined() {
|
|
926
|
+
return new JoinedTablesImpl(emptyState(this.#fromSource, this.#scope), this.ctx);
|
|
927
|
+
}
|
|
928
|
+
};
|
|
929
|
+
function sql(options) {
|
|
930
|
+
const { context, rawCodecInferer } = options;
|
|
931
|
+
const ctx = {
|
|
932
|
+
capabilities: context.contract.capabilities,
|
|
933
|
+
queryOperationTypes: context.queryOperations.entries(),
|
|
934
|
+
target: context.contract.target ?? "unknown",
|
|
935
|
+
storageHash: context.contract.storage.storageHash ?? "unknown",
|
|
936
|
+
storage: context.contract.storage,
|
|
937
|
+
applyMutationDefaults: (options) => context.applyMutationDefaults(options),
|
|
938
|
+
rawCodecInferer
|
|
939
|
+
};
|
|
940
|
+
const { storage } = context.contract;
|
|
941
|
+
return new Proxy({}, { get(_target, prop) {
|
|
942
|
+
if (typeof prop !== "string") return;
|
|
943
|
+
if (!Object.hasOwn(storage.namespaces, prop)) return;
|
|
944
|
+
const namespaceId = prop;
|
|
945
|
+
return new Proxy({}, { get(_facetTarget, tableName) {
|
|
946
|
+
if (typeof tableName !== "string") return;
|
|
947
|
+
const table = resolveTableInNamespace(storage, namespaceId, tableName);
|
|
948
|
+
if (table) return new TableProxyImpl(tableName, table, tableName, ctx, namespaceId);
|
|
949
|
+
} });
|
|
950
|
+
} });
|
|
951
|
+
}
|
|
952
|
+
//#endregion
|
|
953
|
+
export { ExpressionImpl, createAggregateFunctions, createFieldProxy, createFunctions, sql };
|
|
954
|
+
|
|
955
|
+
//# sourceMappingURL=builder__runtime.mjs.map
|