better-supabase 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/dist/{access-sql-B0dF-SoF.js → access-sql-CrSbK1q-.js} +1 -1
- package/dist/{adapter-B-tL8hqg.js → adapter-CN45ibbV.js} +663 -157
- package/dist/{agents-CVHGd7It.d.ts → agents-BY_LQXtQ.d.ts} +4 -4
- package/dist/{ai-cache-CBWs1W0k.js → ai-cache-Dfgk8nBT.js} +2 -1
- package/dist/{ai-cache-DaDu_G5L.d.ts → ai-cache-DrUMMgp6.d.ts} +4 -4
- package/dist/{ai-chat-B24UI962.js → ai-chat-CVLRWbtS.js} +4 -3
- package/dist/{ai-chat-7amvXjmQ.d.ts → ai-chat-DXkMy74O.d.ts} +5 -5
- package/dist/{ai-files-CSPNswUY.js → ai-files-CioLu5du.js} +5 -4
- package/dist/ai-message-DfaP8sis.js +14 -0
- package/dist/ai-sdk/agents/index.d.ts +4 -4
- package/dist/ai-sdk/batches/index.d.ts +4 -4
- package/dist/ai-sdk/batches/index.js +4 -4
- package/dist/ai-sdk/cache/index.d.ts +2 -2
- package/dist/ai-sdk/cache/index.js +2 -2
- package/dist/ai-sdk/chat/index.d.ts +1 -1
- package/dist/ai-sdk/chat/index.js +8 -20
- package/dist/ai-sdk/embeddings/index.d.ts +2 -2
- package/dist/ai-sdk/files/index.d.ts +3 -3
- package/dist/ai-sdk/files/index.js +4 -4
- package/dist/ai-sdk/index.d.ts +37 -9
- package/dist/ai-sdk/index.js +3 -2
- package/dist/ai-sdk/mcp/index.d.ts +43 -6
- package/dist/ai-sdk/mcp/index.js +36 -7
- package/dist/ai-sdk/memory/index.d.ts +2 -2
- package/dist/ai-sdk/memory/index.js +1 -1
- package/dist/ai-sdk/workflow/index.d.ts +6 -6
- package/dist/ai-sdk/workflow/index.js +13 -24
- package/dist/{ai-tasks-Cd1HMFvB.d.ts → ai-tasks-BRQ6iQLc.d.ts} +4 -4
- package/dist/{announcements-ZRGnScWI.d.ts → announcements-Bouy0Ski.d.ts} +3 -3
- package/dist/{announcements-CiyOs-xP.js → announcements-p_BFofiR.js} +1 -1
- package/dist/api-Dnf2q-Ht.d.ts +370 -0
- package/dist/api-DtHFC037.js +676 -0
- package/dist/{api-keys-CIjghYjV.d.ts → api-keys-C8BlF3L2.d.ts} +4 -4
- package/dist/arazzo/index.d.ts +255 -0
- package/dist/arazzo/index.js +622 -0
- package/dist/{assistant-DwdpDH1q.d.ts → assistant-DBlFDZsi.d.ts} +5 -5
- package/dist/astro/index.d.ts +7 -7
- package/dist/astro/index.js +7 -7
- package/dist/asyncapi/index.d.ts +103 -0
- package/dist/asyncapi/index.js +569 -0
- package/dist/authorizer-ayxUDXXP.js +126 -0
- package/dist/authorizer-eiOjo-3l.d.ts +73 -0
- package/dist/better-result/index.d.ts +118 -0
- package/dist/better-result/index.js +64 -0
- package/dist/{bind-l4kAvYSv.js → bind-ButSzrHe.js} +1 -1
- package/dist/{bind-Bu_ZNa3W.d.ts → bind-CQ5mPyX1.d.ts} +3 -3
- package/dist/{block-events-CrT8r7Fe.js → block-events-DTxU5tfr.js} +18 -3
- package/dist/{block-fields-GFkxl4dP.js → block-fields-CroxUuF2.js} +3 -3
- package/dist/{block-fields-SJ6fEf3o.d.ts → block-fields-D1pfrcIY.d.ts} +2 -2
- package/dist/{block-helpers-CKtbFcRv.d.ts → block-helpers-C0Xj8War.d.ts} +2 -2
- package/dist/{block-helpers-DWHxHsEQ.js → block-helpers-Cy3pB96I.js} +41 -9
- package/dist/{block-hooks-BmjMyXTa.js → block-hooks-BPNMIlLl.js} +3 -3
- package/dist/{block-hooks-plbZ0AbJ.d.ts → block-hooks-DaOVC7SC.d.ts} +3 -3
- package/dist/{block-transport-BLr-ZgLc.js → block-transport-bAKh-dGc.js} +28 -2
- package/dist/{block-transport-DvaoPuZ9.d.ts → block-transport-inD4fFq-.d.ts} +24 -2
- package/dist/blocks/agents/index.d.ts +3 -3
- package/dist/blocks/agents/index.js +5 -4
- package/dist/blocks/ai-cache/index.d.ts +3 -3
- package/dist/blocks/ai-cache/index.js +3 -3
- package/dist/blocks/ai-chat/index.d.ts +9 -4
- package/dist/blocks/ai-chat/index.js +5 -5
- package/dist/blocks/ai-chat/react/index.d.ts +3 -3
- package/dist/blocks/ai-chat/react/index.js +5 -5
- package/dist/blocks/ai-files/index.d.ts +3 -3
- package/dist/blocks/ai-files/index.js +3 -3
- package/dist/blocks/ai-providers/index.d.ts +3 -3
- package/dist/blocks/ai-providers/index.js +8 -7
- package/dist/blocks/ai-tasks/index.d.ts +3 -3
- package/dist/blocks/ai-tasks/index.js +7 -6
- package/dist/blocks/announcements/index.d.ts +3 -3
- package/dist/blocks/announcements/index.js +3 -3
- package/dist/blocks/announcements/react/index.d.ts +3 -3
- package/dist/blocks/announcements/react/index.js +3 -3
- package/dist/blocks/api-keys/index.d.ts +5 -5
- package/dist/blocks/api-keys/index.js +8 -8
- package/dist/blocks/attachments/index.d.ts +12 -8
- package/dist/blocks/attachments/index.js +22 -13
- package/dist/blocks/audit/index.d.ts +21 -10
- package/dist/blocks/audit/index.js +30 -11
- package/dist/blocks/billing/index.d.ts +11 -9
- package/dist/blocks/billing/index.js +15 -15
- package/dist/blocks/comments/index.d.ts +8 -8
- package/dist/blocks/comments/index.js +8 -7
- package/dist/blocks/connectors/index.d.ts +3 -3
- package/dist/blocks/connectors/index.js +7 -6
- package/dist/blocks/data-lifecycle/index.d.ts +12 -8
- package/dist/blocks/data-lifecycle/index.js +13 -8
- package/dist/blocks/entitlements/index.d.ts +3 -3
- package/dist/blocks/entitlements/index.js +1 -1
- package/dist/blocks/flags/index.d.ts +4 -4
- package/dist/blocks/flags/index.js +4 -3
- package/dist/blocks/inbox/index.d.ts +3 -3
- package/dist/blocks/inbox/index.js +4 -5
- package/dist/blocks/inbox/react/index.d.ts +1 -1
- package/dist/blocks/inbox/react/index.js +1 -1
- package/dist/blocks/inbox/react/server.d.ts +1 -1
- package/dist/blocks/index.d.ts +10 -10
- package/dist/blocks/index.js +6 -6
- package/dist/blocks/jobs/index.d.ts +48 -32
- package/dist/blocks/jobs/index.js +44 -287
- package/dist/blocks/knowledge/index.d.ts +3 -3
- package/dist/blocks/knowledge/index.js +3 -3
- package/dist/blocks/memory/index.d.ts +4 -4
- package/dist/blocks/memory/index.js +7 -6
- package/dist/blocks/notifications/index.d.ts +11 -9
- package/dist/blocks/notifications/index.js +20 -25
- package/dist/blocks/notifications/react/index.d.ts +1 -1
- package/dist/blocks/notifications/react/index.js +1 -1
- package/dist/blocks/onboarding/index.d.ts +3 -3
- package/dist/blocks/onboarding/index.js +3 -3
- package/dist/blocks/onboarding/react/index.d.ts +2 -2
- package/dist/blocks/onboarding/react/index.js +2 -2
- package/dist/blocks/onboarding/react/server.d.ts +1 -1
- package/dist/blocks/organizations/index.d.ts +9 -9
- package/dist/blocks/organizations/index.js +11 -18
- package/dist/blocks/outbox/index.d.ts +2 -2
- package/dist/blocks/outbox/index.js +16 -25
- package/dist/blocks/profiles/index.d.ts +7 -7
- package/dist/blocks/profiles/index.js +7 -6
- package/dist/blocks/push/index.d.ts +4 -4
- package/dist/blocks/push/index.js +3 -3
- package/dist/blocks/settings/index.d.ts +4 -4
- package/dist/blocks/settings/index.js +7 -6
- package/dist/blocks/sso/index.d.ts +5 -5
- package/dist/blocks/sso/index.js +6 -5
- package/dist/blocks/usage/index.d.ts +3 -3
- package/dist/blocks/usage/index.js +5 -4
- package/dist/blocks/waitlist/index.d.ts +5 -5
- package/dist/blocks/waitlist/index.js +5 -4
- package/dist/blocks/webhooks/index.d.ts +22 -13
- package/dist/blocks/webhooks/index.js +41 -70
- package/dist/blocks/workflow-builder/index.d.ts +4 -4
- package/dist/blocks/workflow-builder/index.js +3 -3
- package/dist/blocks/workflow-builder/react/index.d.ts +4 -4
- package/dist/blocks/workflow-builder/react/index.js +4 -4
- package/dist/blocks/workflows/index.d.ts +3 -3
- package/dist/blocks/workflows/index.js +3 -3
- package/dist/blocks/workflows/react/index.d.ts +3 -3
- package/dist/blocks/workflows/react/index.js +3 -3
- package/dist/build-hPwk3_X5.js +1296 -0
- package/dist/{build-BXIzE6Xv.js → build-meLuYGri.js} +3 -3
- package/dist/casing-DdFWEtqr.js +16 -0
- package/dist/chat-sdk/index.d.ts +6 -6
- package/dist/chat-sdk/index.js +5 -4
- package/dist/chat-sdk/react/index.d.ts +1 -1
- package/dist/chat-sdk/react/server.d.ts +1 -1
- package/dist/{claims-DDBQT6BK.js → claims-0KZVT804.js} +1 -1
- package/dist/cli/add-1dc5wM5k.js +69 -0
- package/dist/cli/arazzo-1.0-BalU61-_.js +532 -0
- package/dist/cli/arazzo-1.1-Bnfasbun.js +705 -0
- package/dist/cli/asyncapi-3.0.0-C78dBjR6.js +5593 -0
- package/dist/cli/asyncapi-3.1.0-g-7aIfV1.js +5833 -0
- package/dist/cli/bin.js +4 -4
- package/dist/cli/cache-CIfA-jBV.js +483 -0
- package/dist/cli/{codemod-Bl_tQpHn.js → codemod-JVlTQQG2.js} +4 -4
- package/dist/cli/{command-CK14fP91.js → command-DV-StG1x.js} +5 -0
- package/dist/cli/{config-DU7EJImt.js → config-C2Pz2_4A.js} +7 -4
- package/dist/cli/config-DpbF8ML3.js +2 -0
- package/dist/cli/{config-NiPxAmUF.js → config-M-WzL9O2.js} +116 -18
- package/dist/cli/{db-DOBPt660.js → db-BkcfOlyB.js} +2 -1
- package/dist/cli/{diff-SjkO5rNr.js → diff-D1jGdb3Y.js} +1 -1
- package/dist/cli/{doctor-CVdnLahA.js → doctor-VcDtuusV.js} +317 -222
- package/dist/cli/emit-BHHfwUoT.js +1136 -0
- package/dist/cli/{env-qXgyMV6r.js → env-B--LMhEe.js} +13 -10
- package/dist/cli/env-vzBG90Um.js +2 -0
- package/dist/cli/{env-CJLbUVPB.js → env-zHJtMIBo.js} +11 -3
- package/dist/cli/{errors-4v4DTtda.js → errors-CqTLXe2M.js} +18 -2
- package/dist/cli/{fastest-levenshtein-CFN-lNDv.js → fastest-levenshtein-CF7BCi9l.js} +1 -1
- package/dist/cli/files-BVupXPIC.js +34 -0
- package/dist/cli/gen-DmVj3u4L.js +322 -0
- package/dist/cli/{init-Cr6arjXU.js → glue-C1UE8gQN.js} +16 -313
- package/dist/cli/index.d.ts +59 -31
- package/dist/cli/index.js +8 -6
- package/dist/cli/init-CVZ3s5kl.js +258 -0
- package/dist/cli/{introspect-Bzuv1HHj.js → introspect-BToGNfm-.js} +14 -14
- package/dist/cli/{io-DI1mTdSA.js → io-CFzJVJ68.js} +5 -1
- package/dist/cli/{keys-BGCnnb-s.js → keys-CJhtIxJ3.js} +4 -4
- package/dist/cli/metadata-DSRKCaf-.js +133 -0
- package/dist/cli/names-D6LrQnOv.js +6 -0
- package/dist/cli/openapi-3.0-t7xwkuRH.js +928 -0
- package/dist/cli/openapi-3.1-CVze7tBS.js +870 -0
- package/dist/cli/openapi-3.2-CFoMYM01.js +1031 -0
- package/dist/cli/{openapi-BlRJ18s3.js → openapi-DWa4Ir1q.js} +8 -14
- package/dist/cli/prompts-BdeVmVpg.js +2 -0
- package/dist/cli/{prompts-BNcNfAve.js → prompts-DZJB1nTw.js} +6 -6
- package/dist/cli/{rolldown-runtime-BPOCksWG.js → rolldown-runtime-CMFfr-1z.js} +3 -1
- package/dist/cli/run-DA6_lg3w.js +389 -0
- package/dist/cli/run-DuN1MX-7.js +705 -0
- package/dist/cli/scaffold-5DenHSxF.js +292 -0
- package/dist/cli/scaffold-CwQUMFRb.js +2 -0
- package/dist/cli/{seed-DQzqT7LP.js → seed-CzBmrGON.js} +14 -18
- package/dist/cli/seed-DOmit9Hq.js +2 -0
- package/dist/cli/{skills-QQs4q68J.js → skills-CnO8QFS7.js} +7 -6
- package/dist/cli/snapshot-Bstk6ulj.js +287 -0
- package/dist/cli/source-args-BYfq6imV.js +31 -0
- package/dist/cli/spec-CzosTYm3.js +7438 -0
- package/dist/cli/spec-T_0Bcyw7.js +2 -0
- package/dist/cli/{sql-B8csQPyn.js → sql-BKOOQAX3.js} +240 -218
- package/dist/cli/sql-DJrNDxQm.js +3 -0
- package/dist/cli/{style-C0NFdJRL.js → style-CB3vxgh0.js} +3 -3
- package/dist/cli/typegen-Demi5-VQ.js +486 -0
- package/dist/cli/validate-Ca13ktgz.js +7522 -0
- package/dist/cli/{version-Bp4fyubI.js → version-B4k0aUxS.js} +1 -1
- package/dist/client/index.d.ts +4 -3
- package/dist/client/index.js +60 -3
- package/dist/client/native/index.d.ts +3 -3
- package/dist/client/native/index.js +3 -3
- package/dist/{client-CI0Z2zWX.js → client-wxeTQFtB.js} +36 -3
- package/dist/{clone-BfgmLU90.js → clone-dN9XOL1K.js} +9 -1
- package/dist/{cloud-event-BnmD5fb9.js → cloud-event-CTh9Sr5w.js} +16 -2
- package/dist/{cloud-event-C1JCc1bH.d.ts → cloud-event-Dune0KI7.d.ts} +16 -2
- package/dist/{compiler-DY_wkj4R.d.ts → compiler-V1qvcST1.d.ts} +2 -2
- package/dist/{compose-BIBNgAnq.js → compose-BHej1acw.js} +3 -3
- package/dist/{composite-CpHLEo-G.d.ts → composite-CWAJTRBF.d.ts} +6 -6
- package/dist/{composite-C6I8ZyaF.js → composite-zTe0TCqj.js} +4 -4
- package/dist/config/index.d.ts +3 -4
- package/dist/config/index.js +4 -4
- package/dist/config-DlKvtt95.js +540 -0
- package/dist/{connector-Dv8kK0MO.d.ts → connector-CySQEFcW.d.ts} +1 -1
- package/dist/{connectors-CrpknU0P.d.ts → connectors--PGhmiu0.d.ts} +4 -4
- package/dist/{context-BCrWe7ow.d.ts → context-DsV2SueA.d.ts} +11 -2
- package/dist/context-nPDvQ-Zl.js +41 -0
- package/dist/credentials/index.d.ts +5 -5
- package/dist/credentials/index.js +9 -17
- package/dist/{cron-DVPUg5k3.js → cron-D25Ukpxy.js} +1 -1
- package/dist/cron-route-D2AN9SA1.js +24 -0
- package/dist/{csv-DCK-PT1n.js → csv-BZnHA1Py.js} +1 -1
- package/dist/{define-B-TVjC25.js → define-CFQcvM20.js} +263 -229
- package/dist/{define-CJJkkr1-.d.ts → define-DmcfoNXB.d.ts} +136 -111
- package/dist/edge/index.d.ts +66 -13
- package/dist/edge/index.js +65 -39
- package/dist/elysia/index.d.ts +1 -1
- package/dist/elysia/index.js +1 -1
- package/dist/{embeddings-C9KGQnXt.d.ts → embeddings-B4vhS-dY.d.ts} +1 -1
- package/dist/{ended-XjeGQ5XW.d.ts → ended-CcvcRqEd.d.ts} +7 -1
- package/dist/{entitlements-DRl6ZJUm.js → entitlements-BYFf0Cdh.js} +2 -2
- package/dist/{entitlements-BKSVGtrG.d.ts → entitlements-Ds8qoYZ-.d.ts} +1 -1
- package/dist/env/index.d.ts +1 -1
- package/dist/env/index.js +9 -2
- package/dist/{errors-B11Duf73.js → errors-2M6CKMP4.js} +5 -0
- package/dist/{errors-DLjJHPLK.js → errors-rZ7VIqp6.js} +2 -2
- package/dist/{escape-like-ImTK0ps_.d.ts → escape-like-DDWoHzh_.d.ts} +1 -1
- package/dist/eve/index.d.ts +5 -5
- package/dist/eve/index.js +5 -11
- package/dist/{eve-gT2Fpjut.d.ts → eve-CoAIcCkq.d.ts} +2 -2
- package/dist/events/index.d.ts +4 -4
- package/dist/events/index.js +7 -2
- package/dist/{events-DWDhx77e.d.ts → events-D1BOCll8.d.ts} +6 -4
- package/dist/{executor-Bq28caBo.js → executor-B-fD7DLt.js} +52 -35
- package/dist/executor-B_Hfobee.js +162 -0
- package/dist/{executor-BVBFM86G.d.ts → executor-CpxLPCiz.d.ts} +1 -1
- package/dist/{executor-CaWigTyR.d.ts → executor-DfEtf0cq.d.ts} +43 -27
- package/dist/{executor-DvsBbNBS.d.ts → executor-Drzu3dBK.d.ts} +2 -2
- package/dist/{executor-DbvExCFC.d.ts → executor-ngjKL_Pe.d.ts} +2 -2
- package/dist/expo/index.d.ts +4 -4
- package/dist/expo/index.js +6 -6
- package/dist/expo-sqlite/index.d.ts +3 -3
- package/dist/expo-sqlite/index.js +2 -2
- package/dist/extra-CGQ2_q3S.js +10 -0
- package/dist/{field-errors-BFOa7WhB.d.ts → field-errors-DIe9XYWL.d.ts} +17 -7
- package/dist/{field-errors-VF1yb8yI.js → field-errors-DZVRWRkf.js} +1 -1
- package/dist/format-DTXdu8ya.d.ts +70 -0
- package/dist/{framework-KIPvRQJU.d.ts → framework-148t1m3C.d.ts} +5 -5
- package/dist/{framework-BDCpJI7o.js → framework-3ey3QmrO.js} +2 -2
- package/dist/function-contract-BcZenXMO.d.ts +8 -0
- package/dist/{gateway-C1ssRHnf.d.ts → gateway-S9vicgeo.d.ts} +4 -4
- package/dist/{guards-BnzDvFrz.js → guards-6F2bCtQR.js} +3 -1
- package/dist/h3/index.d.ts +1 -1
- package/dist/h3/index.js +1 -1
- package/dist/h3/v1/index.d.ts +1 -1
- package/dist/h3/v1/index.js +1 -1
- package/dist/{message-CaKlqdsX.js → history-GH1HJWQV.js} +65 -13
- package/dist/hmac-DCud0myt.js +22 -0
- package/dist/hono/index.d.ts +19 -14
- package/dist/hono/index.js +21 -16
- package/dist/{hooks-1BTCJsiw.js → hooks-Bhza9Hnr.js} +68 -100
- package/dist/http-sbmBe5d2.js +25 -0
- package/dist/{inbox-i7E1ppo6.d.ts → inbox-Cd1KCEBL.d.ts} +3 -3
- package/dist/index-B72Rvv_j.d.ts +89 -0
- package/dist/{index-DunNRdnT.d.ts → index-BCHR4Ucl.d.ts} +4 -4
- package/dist/{index-CLg87OBW.d.ts → index-BUK7jUzR.d.ts} +6 -0
- package/dist/{config-D44Y7PAv.d.ts → index-Ba1SI-uO.d.ts} +625 -278
- package/dist/{index-D48jVS-N.d.ts → index-BfxOaOYs.d.ts} +4 -5
- package/dist/{index-ClmjK_o-.d.ts → index-CicIyqOe.d.ts} +4 -4
- package/dist/{index-ACiNPN4Z.d.ts → index-D69h4QqD.d.ts} +1 -1
- package/dist/index-DG2SUXDQ.d.ts +58 -0
- package/dist/{index-Bepi-Hgg.d.ts → index-DoNVLHOU.d.ts} +3 -3
- package/dist/{index-CdZKQF98.d.ts → index-R274XFlg.d.ts} +1 -1
- package/dist/{index-CewnYSpv.d.ts → index-_4R4b01v.d.ts} +8 -18
- package/dist/index.d.ts +89 -14
- package/dist/index.js +12 -11
- package/dist/{json-schema-CvER4kgT.js → json-schema-CT4tvrkA.js} +148 -102
- package/dist/{kit-Fm-Fv7q5.js → kit-BhHr9T9v.js} +36 -10
- package/dist/{kit-CkNzZUqv.d.ts → kit-W99LCPBS.d.ts} +13 -7
- package/dist/{knowledge-BXBbT0Tr.d.ts → knowledge-CAReFpRA.d.ts} +5 -5
- package/dist/{knowledge-CDmiMLew.js → knowledge-DnHtb64A.js} +4 -3
- package/dist/list/index.d.ts +1 -2
- package/dist/{list-query-BRt-zDF7.d.ts → list-query-D3JQeEL7.d.ts} +4 -4
- package/dist/mcp/index.d.ts +49 -19
- package/dist/mcp/index.js +158 -94
- package/dist/mcp/sdk/index.d.ts +38 -6
- package/dist/mcp/sdk/index.js +88 -6
- package/dist/{message-CfDoR0Y6.d.ts → message-B0fNJgq8.d.ts} +1 -2
- package/dist/{messages-B9-5887d.js → messages-CJoHHCWT.js} +4 -205
- package/dist/{mfa-Daak8ukG.d.ts → mfa-SZc9YYyb.d.ts} +1 -1
- package/dist/model-OgBxvk3N.d.ts +300 -0
- package/dist/model-messages-Cxw_Up4f.js +206 -0
- package/dist/model-messages-D4GZllOD.d.ts +34 -0
- package/dist/modules-DksjZyMW.js +77 -0
- package/dist/nestjs/index.d.ts +2 -2
- package/dist/nestjs/index.js +1 -1
- package/dist/next/index.d.ts +105 -55
- package/dist/next/index.js +204 -170
- package/dist/node/index.d.ts +2 -2
- package/dist/node/index.js +1 -1
- package/dist/{onboarding-BZGHYJks.d.ts → onboarding-LZvlK1P4.d.ts} +3 -3
- package/dist/openapi/index.d.ts +4 -2
- package/dist/openapi/index.js +21 -343
- package/dist/optional-peer-D1pi-skw.js +24 -0
- package/dist/orpc/index.d.ts +8 -8
- package/dist/orpc/index.js +21 -20
- package/dist/otel/index.d.ts +3 -3
- package/dist/otel/index.js +7 -7
- package/dist/{outbox-Cq7pPYvl.d.ts → outbox-DZfAvfzC.d.ts} +6 -6
- package/dist/overlay/index.d.ts +233 -0
- package/dist/overlay/index.js +554 -0
- package/dist/{plugin-FGY8UhYm.d.ts → plugin-CPK2HsbY.d.ts} +42 -3
- package/dist/plugins/actor/index.d.ts +1 -1
- package/dist/plugins/actor/index.js +1 -1
- package/dist/plugins/rules/index.d.ts +2 -2
- package/dist/plugins/rules/index.js +1 -1
- package/dist/plugins/soft-delete/index.d.ts +3 -3
- package/dist/plugins/soft-delete/index.js +9 -2
- package/dist/plugins/tenant/index.d.ts +7 -2
- package/dist/plugins/tenant/index.js +16 -2
- package/dist/plugins/timestamps/index.d.ts +1 -1
- package/dist/plugins/timestamps/index.js +8 -1
- package/dist/plugins/validation/index.d.ts +2 -2
- package/dist/plugins/validation/index.js +4 -4
- package/dist/{pool-CAu2nLrL.d.ts → pool-gkhbGp2E.d.ts} +2 -2
- package/dist/postgres/index.d.ts +4 -4
- package/dist/postgres/index.js +2 -1
- package/dist/{postgrest-BLVdEnfe.d.ts → postgrest-BUqLag2Q.d.ts} +1 -1
- package/dist/powersync/index.d.ts +5 -5
- package/dist/powersync/index.js +4 -4
- package/dist/powersync/react/index.d.ts +3 -3
- package/dist/powersync/react/index.js +1 -1
- package/dist/{problem-BreOl8b8.js → problem-CJvFeGu7.js} +13 -2
- package/dist/{problem-DD8LZFjc.d.ts → problem-TvGdxO1t.d.ts} +7 -1
- package/dist/{provider-Di3sRutW.js → provider-DxRTex4x.js} +1 -1
- package/dist/{provider-D-UEMvSD.d.ts → provider-ib9wzUDz.d.ts} +2 -2
- package/dist/query/index.d.ts +2 -2
- package/dist/query/index.js +1 -1
- package/dist/{query-BQ-NaZon.js → query-Ctz-qKRx.js} +1 -1
- package/dist/rate-limit-Bw7HgmFj.js +260 -0
- package/dist/react/index.d.ts +9 -9
- package/dist/react/index.js +4 -4
- package/dist/react/native/index.d.ts +1 -1
- package/dist/react/native/index.js +2 -2
- package/dist/react/server.d.ts +10 -10
- package/dist/react/server.js +3 -3
- package/dist/react/session.d.ts +2 -2
- package/dist/{react-F80mF4-t.js → react-Bmpo59D7.js} +4 -4
- package/dist/react-router/index.d.ts +6 -6
- package/dist/react-router/index.js +9 -11
- package/dist/{read-set-DuKWC9Mj.js → read-set-DAeWhqPf.js} +2 -2
- package/dist/realtime/index.d.ts +3 -3
- package/dist/realtime/index.js +30 -36
- package/dist/{refusal-DhSU2Vvr.d.ts → refusal-CFI8-5nf.d.ts} +4 -1
- package/dist/{refusal-CeJWxmkH.js → refusal-K78A1Vmq.js} +5 -5
- package/dist/{resolve-tJZY3FEa.js → resolve-Bvgf3ut_.js} +131 -42
- package/dist/{resolve-ChTYeTEl.d.ts → resolve-MxXVWyhd.d.ts} +12 -8
- package/dist/resource--vTrBe6I.d.ts +103 -0
- package/dist/resource-BqNqlYg9.js +91 -0
- package/dist/resource-DA811u_4.js +463 -0
- package/dist/resource-execute-rbKvuCsY.d.ts +12 -0
- package/dist/resource-schemas-BJ518zFH.js +79 -0
- package/dist/resource-types-CqB6GWGT.d.ts +141 -0
- package/dist/resources-BVYPnuLo.js +56 -0
- package/dist/resources-DZSk8tXJ.d.ts +7 -0
- package/dist/{respond-DprrgyXX.d.ts → respond-Cw-zanCm.d.ts} +3 -3
- package/dist/{respond-BUgsXNgk.js → respond-volM3ByS.js} +4 -4
- package/dist/{result-D7y2fT87.d.ts → result-BchM1XZn.d.ts} +7 -1
- package/dist/{result-DzUbqgdW.js → result-DEqHCMKP.js} +1 -1
- package/dist/server/index.d.ts +81 -19
- package/dist/server/index.js +16 -14
- package/dist/{server-ZSJ9rCcV.d.ts → server-types-CrAmZ8jv.d.ts} +110 -36
- package/dist/{shared-CxWmcyqB.js → shared-DHjhfRdV.js} +2 -15
- package/dist/shared-DTneRoxi.js +14 -0
- package/dist/{sign-in-Cb1mwv6X.js → sign-in-D_yoP0PF.js} +1 -1
- package/dist/signatures-BRS_p3WD.d.ts +34 -0
- package/dist/signatures-DsWv_bYR.js +103 -0
- package/dist/{signers-4y62_VIi.d.ts → signers-cpdmZYrv.d.ts} +1 -1
- package/dist/solid/index.d.ts +9 -18
- package/dist/solid/index.js +2 -2
- package/dist/solid-start/index.d.ts +6 -6
- package/dist/solid-start/index.js +7 -7
- package/dist/spec/index.d.ts +134 -0
- package/dist/spec/index.js +353 -0
- package/dist/{spec-pins-C89s6DyW.d.ts → spec-pins-CwzDY25w.d.ts} +18 -1
- package/dist/{spec-pins--j6mTok4.js → spec-pins-DsLfUH7l.js} +14 -1
- package/dist/sql/index.d.ts +145 -119
- package/dist/sql/index.js +19921 -19798
- package/dist/{executor-De1Of3m3.js → sql-CwuqaQ4Y.js} +34 -169
- package/dist/ssr/index.d.ts +4 -4
- package/dist/ssr/index.js +3 -2
- package/dist/{standard-BK0cqJ7y.js → standard-DkzZUneg.js} +2 -2
- package/dist/{standard-CYsm7xpT.d.ts → standard-lmkbOtPB.d.ts} +1 -1
- package/dist/{stats-D_AD9GGu.js → stats-DhU78Ctf.js} +8 -4
- package/dist/storage/index.d.ts +4 -6
- package/dist/storage/index.js +16 -17
- package/dist/{bucket-Cl46E3jS.d.ts → storage-api-CB7Zl1Ci.d.ts} +243 -8
- package/dist/{store-CGjgWu5n.d.ts → store-DsIYEBnv.d.ts} +1 -1
- package/dist/streams/index.d.ts +4 -4
- package/dist/streams/index.js +5 -4
- package/dist/streams/redis/index.d.ts +1 -1
- package/dist/streams/redis/index.js +5 -11
- package/dist/{stripe-BMj5LtsT.js → stripe-v-Y2lmop.js} +2 -8
- package/dist/supabase-mcp-C6tMoEjn.d.ts +43 -0
- package/dist/supabase-mcp-Cpn5A4i-.js +28 -0
- package/dist/{support-DWwzVf-O.js → support-D1pFFouF.js} +71 -35
- package/dist/{support-view-CVOJJLpc.d.ts → support-view-scqMgtOo.d.ts} +1 -1
- package/dist/svelte/index.d.ts +8 -17
- package/dist/svelte/index.js +2 -2
- package/dist/sveltekit/index.d.ts +7 -7
- package/dist/sveltekit/index.js +10 -12
- package/dist/table-schema-htM_SbCW.js +286 -0
- package/dist/{tags-Cx-LnIQP.d.ts → tags-BO9LjtWG.d.ts} +1 -1
- package/dist/tanstack-db/index.d.ts +2 -2
- package/dist/tanstack-db/supabase/index.d.ts +52 -0
- package/dist/tanstack-db/supabase/index.js +97 -0
- package/dist/tanstack-start/index.d.ts +6 -6
- package/dist/tanstack-start/index.js +8 -12
- package/dist/{tee-CoEppCER.js → tee-Cb98Vn3b.js} +1 -1
- package/dist/{template--jDkl3UP.js → template-lPVh-EwW.js} +11 -2
- package/dist/{temporal-required-BVvW7SYJ.js → temporal-required-BtynQtM-.js} +1 -1
- package/dist/{tenant-BxNFk9bx.d.ts → tenant-CM368LXb.d.ts} +2 -2
- package/dist/testing/index.d.ts +249 -118
- package/dist/testing/index.js +1418 -870
- package/dist/ttl-cache-XzZ-Q2eX.js +29 -0
- package/dist/types-BRTSpOIh.js +5 -0
- package/dist/{types-C05bxW-W.d.ts → types-Bb_4-vdW.d.ts} +4 -4
- package/dist/{types-AYOvcyep.d.ts → types-CD6yCDx3.d.ts} +5 -5
- package/dist/{types-6rocye-y.d.ts → types-CbOUc5IH.d.ts} +4 -4
- package/dist/{types-6BdhkmZe.d.ts → types-CoE5Zwy4.d.ts} +8 -0
- package/dist/{types-DGs5L0VN.d.ts → types-DSJ3-m9X.d.ts} +4 -4
- package/dist/{queue-CLJurhdy.d.ts → types-D__3EN0q.d.ts} +17 -19
- package/dist/{usage-CrvrL2vw.d.ts → usage-D0NNUFfr.d.ts} +3 -3
- package/dist/{vault-wlbBhqm7.d.ts → vault-BpmApxxW.d.ts} +3 -3
- package/dist/vercel-connect/index.d.ts +1 -1
- package/dist/vercel-connect/index.js +5 -11
- package/dist/{verify-DPuWngD4.d.ts → verify-C91Wyp40.d.ts} +5 -35
- package/dist/verify-CJbYfADA.js +124 -0
- package/dist/{version--AolB5ej.js → version-Cq8gfDbd.js} +1 -1
- package/dist/{view-BeFcRV6G.js → view-D5ryq3gz.js} +1 -1
- package/dist/{view-CF7ihnkg.d.ts → view-TpXEUcVN.d.ts} +3 -3
- package/dist/vue/index.d.ts +9 -18
- package/dist/vue/index.js +2 -2
- package/dist/{watch-Xuxrvxq-.js → watch-BcvtAHOd.js} +2 -2
- package/dist/{watch-DI34G86j.d.ts → watch-COzRWvVN.d.ts} +23 -9
- package/dist/{wire-DtZQ4sdE.js → wire-Df3EWkG8.js} +2 -2
- package/dist/{workflow-builder-DJ1oHn8g.js → workflow-builder-CHqHpAVt.js} +6 -5
- package/dist/workflow-sdk/builder/index.d.ts +3 -3
- package/dist/workflow-sdk/index.d.ts +1 -1
- package/dist/workflow-sdk/world/index.d.ts +23 -17
- package/dist/workflow-sdk/world/index.js +59 -76
- package/dist/{workflow-sdk-B3yLD6Br.d.ts → workflow-sdk-DTZyrvNo.d.ts} +3 -3
- package/dist/{workflow-sdk-world-D-prir6O.js → workflow-sdk-world-CMV1ahFW.js} +528 -526
- package/dist/{workflows-B0j9WKPu.d.ts → workflows-CcpCBfIe.d.ts} +3 -3
- package/dist/{workflows-wu1WWBn7.js → workflows-IvxzG2cl.js} +5 -4
- package/package.json +54 -3
- package/schemas/config-v1.json +243 -0
- package/schemas/doctor-report-v1.json +4 -1
- package/schemas/spec-manifest-v1.json +95 -0
- package/skills/better-supabase/SKILL.md +8 -4
- package/skills/better-supabase/references/troubleshooting.md +20 -20
- package/skills/better-supabase-api/references/expo.md +6 -3
- package/skills/better-supabase-api/references/powersync.md +2 -1
- package/skills/better-supabase-auth/SKILL.md +8 -4
- package/dist/cli/config-B9HkL8nF.js +0 -2
- package/dist/cli/gen-AIf4MREI.js +0 -3100
- package/dist/cli/gen-DSrahEdX.js +0 -2
- package/dist/cli/prompts-BTYQGunc.js +0 -2
- package/dist/cli/run-BO052mki.js +0 -361
- package/dist/config-Dn7F5k9A.js +0 -202
- package/dist/history-BZih6SFP.js +0 -64
- package/dist/index-BkWT_uBO.d.ts +0 -51
- package/dist/index-Bxih3l0u.d.ts +0 -81
- package/dist/index-CaxrMD1A.d.ts +0 -1
- package/dist/index-DQh4VCc9.d.ts +0 -98
- package/dist/keys-4MnkWTTh.js +0 -35
- package/dist/messages-DYP0sUTe.d.ts +0 -59
- package/dist/modules-dxrVK3ID.js +0 -228
- package/dist/path-Cv__-OTj.d.ts +0 -17
- package/dist/resource-BsvzCMlX.js +0 -212
- package/dist/resource-IPS5xi9q.d.ts +0 -74
- package/dist/template-CwO38Do8.d.ts +0 -6
- package/dist/verify-DL41hbvY.js +0 -232
- package/dist/cli/{read-sets-CETOSFx4.js → read-sets-DjJrm8Wj.js} +1 -1
|
@@ -0,0 +1,1136 @@
|
|
|
1
|
+
import { t as byCodePoint } from "./compare-BmIWVEui.js";
|
|
2
|
+
import { s as restrictSchemas, u as tsTypeOf } from "./typegen-Demi5-VQ.js";
|
|
3
|
+
import { DEFAULT_CLAIMS, tenantClaimPaths } from "better-supabase/config";
|
|
4
|
+
import { toCamel } from "better-supabase";
|
|
5
|
+
import { resolveProviderSql } from "better-supabase/sql";
|
|
6
|
+
//#region src/cli/introspect/catalog.ts
|
|
7
|
+
const byName = (a, b) => a.schema === b.schema ? a.name < b.name ? -1 : a.name > b.name ? 1 : 0 : a.schema < b.schema ? -1 : 1;
|
|
8
|
+
function toColumn(column) {
|
|
9
|
+
const isArray = column.data_type === "ARRAY";
|
|
10
|
+
const udt = isArray ? column.format.replace(/^_/, "") : column.format;
|
|
11
|
+
const defaultValue = column.default_value === null || column.default_value === void 0 ? null : String(column.default_value);
|
|
12
|
+
return {
|
|
13
|
+
name: column.name,
|
|
14
|
+
udt,
|
|
15
|
+
format: column.format,
|
|
16
|
+
typeSchema: column.type_schema,
|
|
17
|
+
isArray,
|
|
18
|
+
isEnum: column.enums.length > 0,
|
|
19
|
+
nullable: column.is_nullable,
|
|
20
|
+
hasDefault: defaultValue !== null || column.identity_generation !== null || column.is_generated,
|
|
21
|
+
default: defaultValue,
|
|
22
|
+
identity: column.identity_generation === "ALWAYS" ? "always" : column.identity_generation === "BY DEFAULT" ? "by default" : null,
|
|
23
|
+
generated: column.is_generated,
|
|
24
|
+
updatable: column.is_updatable,
|
|
25
|
+
comment: column.comment
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function toFunction(fn, typeOf, relationName) {
|
|
29
|
+
const args = [];
|
|
30
|
+
const table = [];
|
|
31
|
+
fn.args.forEach((arg, index) => {
|
|
32
|
+
const type = typeOf(arg.type_id);
|
|
33
|
+
const raw = type?.name ?? "unknown";
|
|
34
|
+
const typeSchema = type ? { typeSchema: type.schema } : {};
|
|
35
|
+
const name = arg.name || `arg${index + 1}`;
|
|
36
|
+
if (arg.mode === "in" || arg.mode === "inout" || arg.mode === "variadic") {
|
|
37
|
+
const isArray = raw.startsWith("_");
|
|
38
|
+
args.push({
|
|
39
|
+
name,
|
|
40
|
+
udt: isArray ? raw.slice(1) : raw,
|
|
41
|
+
...typeSchema,
|
|
42
|
+
isArray,
|
|
43
|
+
hasDefault: arg.has_default === true
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
if (arg.mode === "table" || arg.mode === "out" || arg.mode === "inout") table.push({
|
|
47
|
+
name,
|
|
48
|
+
udt: raw,
|
|
49
|
+
...typeSchema
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
const returnType = typeOf(fn.return_type_id);
|
|
53
|
+
const searchPath = fn.config_params?.["search_path"];
|
|
54
|
+
return {
|
|
55
|
+
schema: fn.schema,
|
|
56
|
+
name: fn.name,
|
|
57
|
+
signature: fn.identity_argument_types,
|
|
58
|
+
args,
|
|
59
|
+
returnsTable: fn.args.some((arg) => arg.mode === "table") ? table : null,
|
|
60
|
+
returns: returnType?.name ?? fn.return_type,
|
|
61
|
+
...returnType ? { returnsSchema: returnType.schema } : {},
|
|
62
|
+
returnsRelation: relationName(fn.return_type_relation_id),
|
|
63
|
+
returnsSet: fn.is_set_returning_function,
|
|
64
|
+
volatility: fn.behavior.toLowerCase(),
|
|
65
|
+
securityDefiner: fn.security_definer,
|
|
66
|
+
language: fn.language,
|
|
67
|
+
searchPath: searchPath === void 0 ? null : searchPath.replace(/^""$/, "")
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** Joins the typegen metadata and the extras into one record per object. */
|
|
71
|
+
function toCatalog(snapshot) {
|
|
72
|
+
const meta = snapshot.generator;
|
|
73
|
+
const extras = new Map(snapshot.extras.tables.map((table) => [table.id, table]));
|
|
74
|
+
const columns = /* @__PURE__ */ new Map();
|
|
75
|
+
for (const column of meta.columns) {
|
|
76
|
+
const list = columns.get(column.table_id);
|
|
77
|
+
if (list) list.push(column);
|
|
78
|
+
else columns.set(column.table_id, [column]);
|
|
79
|
+
}
|
|
80
|
+
const primaryKeys = /* @__PURE__ */ new Map();
|
|
81
|
+
for (const key of meta.primaryKeys) {
|
|
82
|
+
const list = primaryKeys.get(key.table_id);
|
|
83
|
+
if (list) list.push(key.name);
|
|
84
|
+
else primaryKeys.set(key.table_id, [key.name]);
|
|
85
|
+
}
|
|
86
|
+
const relationNames = /* @__PURE__ */ new Map();
|
|
87
|
+
for (const list of [
|
|
88
|
+
meta.tables,
|
|
89
|
+
meta.foreignTables,
|
|
90
|
+
meta.views,
|
|
91
|
+
meta.materializedViews
|
|
92
|
+
]) for (const entry of list) relationNames.set(entry.id, `${entry.schema}.${entry.name}`);
|
|
93
|
+
const viewNames = new Set([...meta.views, ...meta.materializedViews].map((view) => `${view.schema}.${view.name}`));
|
|
94
|
+
const foreignKeysOf = (schema, name, extra) => {
|
|
95
|
+
const actions = new Map((extra?.foreignKeys ?? []).map((fk) => [fk.name, fk]));
|
|
96
|
+
return meta.relationships.filter((rel) => rel.schema === schema && rel.relation === name && actions.has(rel.foreign_key_name) && !viewNames.has(`${rel.referenced_schema}.${rel.referenced_relation}`)).map((rel) => {
|
|
97
|
+
const action = actions.get(rel.foreign_key_name);
|
|
98
|
+
return {
|
|
99
|
+
name: rel.foreign_key_name,
|
|
100
|
+
columns: rel.columns,
|
|
101
|
+
refSchema: rel.referenced_schema,
|
|
102
|
+
refTable: rel.referenced_relation,
|
|
103
|
+
refColumns: rel.referenced_columns,
|
|
104
|
+
oneToOne: rel.is_one_to_one,
|
|
105
|
+
onDelete: action?.onDelete ?? "no action",
|
|
106
|
+
onUpdate: action?.onUpdate ?? "no action"
|
|
107
|
+
};
|
|
108
|
+
}).sort((a, b) => byCodePoint(a.name, b.name));
|
|
109
|
+
};
|
|
110
|
+
const build = (entry, flags) => {
|
|
111
|
+
const extra = extras.get(entry.id);
|
|
112
|
+
const own = [...columns.get(entry.id) ?? []].sort((a, b) => a.ordinal_position - b.ordinal_position);
|
|
113
|
+
return {
|
|
114
|
+
id: entry.id,
|
|
115
|
+
schema: entry.schema,
|
|
116
|
+
name: entry.name,
|
|
117
|
+
comment: entry.comment,
|
|
118
|
+
...flags,
|
|
119
|
+
columns: own.map(toColumn),
|
|
120
|
+
primaryKey: extra && extra.primaryKey.length > 0 ? extra.primaryKey : primaryKeys.get(entry.id) ?? [],
|
|
121
|
+
uniques: extra?.uniques ?? [],
|
|
122
|
+
foreignKeys: foreignKeysOf(entry.schema, entry.name, extra),
|
|
123
|
+
checks: extra?.checks ?? [],
|
|
124
|
+
indexes: extra?.indexes ?? [],
|
|
125
|
+
policies: extra?.policies ?? [],
|
|
126
|
+
triggers: extra?.triggers ?? [],
|
|
127
|
+
grants: extra?.grants ?? [],
|
|
128
|
+
...extra?.columnGrants ? { columnGrants: extra.columnGrants } : {}
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
const tables = [
|
|
132
|
+
...meta.tables.map((table) => build(table, {
|
|
133
|
+
kind: "table",
|
|
134
|
+
rls: table.rls_enabled,
|
|
135
|
+
forceRls: table.rls_forced,
|
|
136
|
+
replicaIdentity: table.replica_identity,
|
|
137
|
+
insertable: true,
|
|
138
|
+
updatable: true
|
|
139
|
+
})),
|
|
140
|
+
...meta.foreignTables.map((table) => build(table, {
|
|
141
|
+
kind: "table",
|
|
142
|
+
rls: false,
|
|
143
|
+
forceRls: false,
|
|
144
|
+
replicaIdentity: null,
|
|
145
|
+
insertable: true,
|
|
146
|
+
updatable: true
|
|
147
|
+
})),
|
|
148
|
+
...meta.views.map((view) => build(view, {
|
|
149
|
+
kind: "view",
|
|
150
|
+
rls: false,
|
|
151
|
+
forceRls: false,
|
|
152
|
+
replicaIdentity: null,
|
|
153
|
+
insertable: view.is_insert_enabled ?? view.is_updatable,
|
|
154
|
+
updatable: view.is_update_enabled ?? view.is_updatable
|
|
155
|
+
})),
|
|
156
|
+
...meta.materializedViews.map((view) => build(view, {
|
|
157
|
+
kind: "view",
|
|
158
|
+
rls: false,
|
|
159
|
+
forceRls: false,
|
|
160
|
+
replicaIdentity: null,
|
|
161
|
+
insertable: false,
|
|
162
|
+
updatable: false
|
|
163
|
+
}))
|
|
164
|
+
].sort(byName);
|
|
165
|
+
const included = new Set(snapshot.schemas);
|
|
166
|
+
const enums = meta.types.filter((type) => type.enums.length > 0 && included.has(type.schema)).map((type) => ({
|
|
167
|
+
schema: type.schema,
|
|
168
|
+
name: type.name,
|
|
169
|
+
values: type.enums
|
|
170
|
+
})).sort(byName);
|
|
171
|
+
const types = new Map(meta.types.map((type) => [type.id, type]));
|
|
172
|
+
const functions = meta.functions.map((fn) => toFunction(fn, (id) => types.get(id), (id) => id === null ? null : relationNames.get(id) ?? null)).sort((a, b) => byName(a, b) || byCodePoint(a.signature, b.signature));
|
|
173
|
+
return {
|
|
174
|
+
schemas: snapshot.schemas,
|
|
175
|
+
tables,
|
|
176
|
+
enums,
|
|
177
|
+
functions,
|
|
178
|
+
buckets: snapshot.extras.buckets,
|
|
179
|
+
realtime: snapshot.extras.realtime
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/cli/gen/comment.ts
|
|
184
|
+
/**
|
|
185
|
+
* The metadata a `COMMENT ON` adds to a table or column: its description,
|
|
186
|
+
* without the `@example` lines the validator generators read, and
|
|
187
|
+
* `deprecated` when a line starts with `@deprecated`. The library's
|
|
188
|
+
* `parseComment` (`src/generators/shared.ts`) reads comments the same way.
|
|
189
|
+
*/
|
|
190
|
+
function commentMeta(comment) {
|
|
191
|
+
if (!comment) return {};
|
|
192
|
+
const deprecated = /^\s*@deprecated\b/m.test(comment);
|
|
193
|
+
const description = comment.replaceAll(/^\s*@example\s+(.+?)\s*$/gm, "").replaceAll(/^\s*@deprecated\b.*$/gm, "").trim();
|
|
194
|
+
return {
|
|
195
|
+
...description ? { description } : {},
|
|
196
|
+
...deprecated ? { deprecated: true } : {}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/cli/gen/shared.ts
|
|
201
|
+
const q = (value) => JSON.stringify(value);
|
|
202
|
+
/** A property key: bare when it is a valid identifier, quoted otherwise. */
|
|
203
|
+
function prop(name) {
|
|
204
|
+
return /^[A-Za-z_$][\w$]*$/.test(name) ? name : q(name);
|
|
205
|
+
}
|
|
206
|
+
function nullable(type, isNullable) {
|
|
207
|
+
return isNullable ? `${type} | null` : type;
|
|
208
|
+
}
|
|
209
|
+
/** `T[]`, parenthesized when `T` is a union or other compound type. */
|
|
210
|
+
function arrayOf(type) {
|
|
211
|
+
return /^[\w.$]+$/.test(type) ? `${type}[]` : `(${type})[]`;
|
|
212
|
+
}
|
|
213
|
+
function indent(lines, depth) {
|
|
214
|
+
const pad = " ".repeat(depth);
|
|
215
|
+
return lines.flatMap((line) => line.split("\n")).map((line) => line === "" ? line : pad + line);
|
|
216
|
+
}
|
|
217
|
+
/** `name: {` + indented children + `};`, or an inline empty type. */
|
|
218
|
+
function block(name, children, empty = "{ [_ in never]: never }") {
|
|
219
|
+
if (children.length === 0) return [`${name}: ${empty};`];
|
|
220
|
+
return [
|
|
221
|
+
`${name}: {`,
|
|
222
|
+
...indent(children, 1),
|
|
223
|
+
"};"
|
|
224
|
+
];
|
|
225
|
+
}
|
|
226
|
+
function sameColumns(left, right) {
|
|
227
|
+
return left.length === right.length && left.every((column) => right.includes(column));
|
|
228
|
+
}
|
|
229
|
+
function singular(name) {
|
|
230
|
+
if (name.endsWith("ies")) return `${name.slice(0, -3)}y`;
|
|
231
|
+
if (name.endsWith("sses")) return name.slice(0, -2);
|
|
232
|
+
if (name.endsWith("s") && !name.endsWith("ss")) return name.slice(0, -1);
|
|
233
|
+
return name;
|
|
234
|
+
}
|
|
235
|
+
function pascal(name) {
|
|
236
|
+
return name.replaceAll(/(^|[_-]+)([a-z0-9])/g, (_, __, char) => char.toUpperCase()).replace(/^[a-z]/, (char) => char.toUpperCase());
|
|
237
|
+
}
|
|
238
|
+
/** A valid TypeScript identifier: other characters become `_`, a leading digit gets one. */
|
|
239
|
+
function identifier(name) {
|
|
240
|
+
const safe = name.replaceAll(/[^\w$]/g, "_");
|
|
241
|
+
return /^\d/.test(safe) ? `_${safe}` : safe;
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* The config entry for a database object, keyed `schema.name` or `name`.
|
|
245
|
+
* The qualified key wins.
|
|
246
|
+
*/
|
|
247
|
+
function configFor(entries, schema, name) {
|
|
248
|
+
return entries[`${schema}.${name}`] ?? entries[name];
|
|
249
|
+
}
|
|
250
|
+
function isJsonUdt(udt) {
|
|
251
|
+
return udt === "json" || udt === "jsonb";
|
|
252
|
+
}
|
|
253
|
+
const CHECK_ANY = /^CHECK \(\(\(?"?(\w+)"?\)?(?:::\w+)? = ANY \(\(?ARRAY\[(.+)\]\)?(?:::\w+\[\])?\)\)\)$/;
|
|
254
|
+
const CHECK_OR = /^CHECK \(\((.+)\)\)$/;
|
|
255
|
+
const OR_TERM = /^\(?"?(\w+)"?\)?(?:::\w+)? = '((?:[^']|'')*)'(?:::\w+)?\)?$/;
|
|
256
|
+
const LITERAL = /'((?:[^']|'')*)'(?:::[\w ]+)?/g;
|
|
257
|
+
/**
|
|
258
|
+
* Extracts `column in ('a', 'b')` CHECK constraints, which Postgres stores as
|
|
259
|
+
* `CHECK ((col = ANY (ARRAY['a'::text, 'b'::text])))` or an OR chain.
|
|
260
|
+
*/
|
|
261
|
+
function parseCheckUnion(definition) {
|
|
262
|
+
const any = CHECK_ANY.exec(definition);
|
|
263
|
+
if (any?.[1] && any[2]) {
|
|
264
|
+
const values = [...any[2].matchAll(LITERAL)].map((match) => (match[1] ?? "").replaceAll("''", "'"));
|
|
265
|
+
return values.length > 0 ? {
|
|
266
|
+
column: any[1],
|
|
267
|
+
values
|
|
268
|
+
} : void 0;
|
|
269
|
+
}
|
|
270
|
+
const chain = CHECK_OR.exec(definition);
|
|
271
|
+
if (chain?.[1]) {
|
|
272
|
+
const terms = chain[1].split(/\) OR \(|\s+OR\s+/);
|
|
273
|
+
let column;
|
|
274
|
+
const values = [];
|
|
275
|
+
for (const term of terms) {
|
|
276
|
+
const match = OR_TERM.exec(term.trim());
|
|
277
|
+
if (!match?.[1]) return void 0;
|
|
278
|
+
if (column && column !== match[1]) return void 0;
|
|
279
|
+
column = match[1];
|
|
280
|
+
values.push((match[2] ?? "").replaceAll("''", "'"));
|
|
281
|
+
}
|
|
282
|
+
return column && values.length > 1 ? {
|
|
283
|
+
column,
|
|
284
|
+
values
|
|
285
|
+
} : void 0;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** The text inside one pair of parentheses that wraps all of `text`, if any. */
|
|
289
|
+
function unwrap(text) {
|
|
290
|
+
let current = text.trim();
|
|
291
|
+
while (current.startsWith("(") && current.endsWith(")")) {
|
|
292
|
+
let depth = 0;
|
|
293
|
+
let wraps = true;
|
|
294
|
+
for (let index = 0; index < current.length; index++) {
|
|
295
|
+
const char = current[index];
|
|
296
|
+
if (char === "(") depth++;
|
|
297
|
+
else if (char === ")") depth--;
|
|
298
|
+
if (depth === 0 && index < current.length - 1) {
|
|
299
|
+
wraps = false;
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (!wraps) break;
|
|
304
|
+
current = current.slice(1, -1).trim();
|
|
305
|
+
}
|
|
306
|
+
return current;
|
|
307
|
+
}
|
|
308
|
+
/** Splits `text` on a keyword outside parentheses and quotes. */
|
|
309
|
+
function splitTopLevel(text, keyword) {
|
|
310
|
+
const parts = [];
|
|
311
|
+
let depth = 0;
|
|
312
|
+
let quote;
|
|
313
|
+
let start = 0;
|
|
314
|
+
const separator = ` ${keyword} `;
|
|
315
|
+
for (let index = 0; index < text.length; index++) {
|
|
316
|
+
const char = text[index];
|
|
317
|
+
if (quote) {
|
|
318
|
+
if (char === quote) quote = void 0;
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
if (char === "'" || char === "\"") quote = char;
|
|
322
|
+
else if (char === "(") depth++;
|
|
323
|
+
else if (char === ")") depth--;
|
|
324
|
+
else if (depth === 0 && text.slice(index, index + separator.length).toUpperCase() === separator) {
|
|
325
|
+
parts.push(text.slice(start, index));
|
|
326
|
+
start = index + separator.length;
|
|
327
|
+
index += separator.length - 1;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
parts.push(text.slice(start));
|
|
331
|
+
return parts.map((part) => part.trim());
|
|
332
|
+
}
|
|
333
|
+
const IS_NOT_NULL = /^(?:"((?:[^"]|"")+)"|([\w$]+)) IS NOT NULL$/i;
|
|
334
|
+
/**
|
|
335
|
+
* Columns a CHECK constraint makes not null: `CHECK ((slug IS NOT NULL))` and
|
|
336
|
+
* the `IS NOT NULL` terms of a top-level `AND`. A term under `OR`, `NOT` or
|
|
337
|
+
* any other expression narrows nothing, and neither does a `NOT VALID` one.
|
|
338
|
+
*/
|
|
339
|
+
function parseCheckNotNull(definition) {
|
|
340
|
+
const text = definition.trim();
|
|
341
|
+
if (/\bNOT VALID$/i.test(text)) return [];
|
|
342
|
+
const body = /^CHECK\s*(.*)$/is.exec(text)?.[1];
|
|
343
|
+
if (!body) return [];
|
|
344
|
+
const expression = unwrap(body);
|
|
345
|
+
if (splitTopLevel(expression, "OR").length > 1) return [];
|
|
346
|
+
return splitTopLevel(expression, "AND").flatMap((term) => {
|
|
347
|
+
const match = IS_NOT_NULL.exec(unwrap(term));
|
|
348
|
+
if (!match) return [];
|
|
349
|
+
return [match[1]?.replaceAll("\"\"", "\"") ?? match[2] ?? ""];
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
//#endregion
|
|
353
|
+
//#region src/cli/gen/model.ts
|
|
354
|
+
const SOFT_DELETE_UDTS = /* @__PURE__ */ new Set(["timestamptz", "timestamp"]);
|
|
355
|
+
const applyCasing = (name, casing) => casing === "camel" ? toCamel(name) : name;
|
|
356
|
+
/** The overloads of each function name, in model order. */
|
|
357
|
+
function functionGroups(functions) {
|
|
358
|
+
const groups = /* @__PURE__ */ new Map();
|
|
359
|
+
for (const fn of functions) {
|
|
360
|
+
const group = groups.get(fn.key);
|
|
361
|
+
if (group) group.push(fn);
|
|
362
|
+
else groups.set(fn.key, [fn]);
|
|
363
|
+
}
|
|
364
|
+
return groups;
|
|
365
|
+
}
|
|
366
|
+
function signatureOf(fn) {
|
|
367
|
+
const { name: _name, schema: _schema, ...signature } = fn.meta;
|
|
368
|
+
return {
|
|
369
|
+
...signature,
|
|
370
|
+
args: signature.args.map((arg, index) => fn.args[index]?.optional ? {
|
|
371
|
+
...arg,
|
|
372
|
+
optional: true
|
|
373
|
+
} : arg)
|
|
374
|
+
};
|
|
375
|
+
}
|
|
376
|
+
function tableKey(table, casing, schemas) {
|
|
377
|
+
const base = applyCasing(table.name, casing);
|
|
378
|
+
if (schemas.length === 1 || table.schema === "public") return base;
|
|
379
|
+
return applyCasing(`${table.schema}_${table.name}`, casing);
|
|
380
|
+
}
|
|
381
|
+
const CODEC_TYPE = {
|
|
382
|
+
instant: "Temporal.Instant",
|
|
383
|
+
plainDateTime: "Temporal.PlainDateTime",
|
|
384
|
+
bigint: "bigint",
|
|
385
|
+
string: "string"
|
|
386
|
+
};
|
|
387
|
+
function codecFor(udt, config) {
|
|
388
|
+
const { codecs } = config;
|
|
389
|
+
if (codecs.timestamptz === "instant") {
|
|
390
|
+
if (udt === "timestamptz") return "instant";
|
|
391
|
+
if (udt === "timestamp") return "plainDateTime";
|
|
392
|
+
}
|
|
393
|
+
if (udt === "int8" && codecs.int8 !== "number") return codecs.int8;
|
|
394
|
+
if (udt === "numeric" && codecs.numeric === "string") return "string";
|
|
395
|
+
}
|
|
396
|
+
const TEXT_UDTS = /* @__PURE__ */ new Set([
|
|
397
|
+
"text",
|
|
398
|
+
"varchar",
|
|
399
|
+
"bpchar"
|
|
400
|
+
]);
|
|
401
|
+
/** `config.storagePaths` resolved to bucket ids, with a check that every key is used. */
|
|
402
|
+
function storagePathsOf(config) {
|
|
403
|
+
const bucketId = (name) => {
|
|
404
|
+
const bucket = config.buckets[name];
|
|
405
|
+
if (!bucket) return name;
|
|
406
|
+
return bucket.id ?? name.replaceAll(/[A-Z]/g, (char) => `-${char.toLowerCase()}`);
|
|
407
|
+
};
|
|
408
|
+
const unused = new Set(Object.keys(config.storagePaths));
|
|
409
|
+
return {
|
|
410
|
+
lookup(schema, table, column, udt) {
|
|
411
|
+
const key = [`${schema}.${table}.${column}`, `${table}.${column}`].find((candidate) => candidate in config.storagePaths);
|
|
412
|
+
if (key === void 0) return;
|
|
413
|
+
unused.delete(key);
|
|
414
|
+
if (!TEXT_UDTS.has(udt)) throw new TypeError(`storagePaths["${key}"]: ${schema}.${table}.${column} is ${udt}, not a text column`);
|
|
415
|
+
return bucketId(config.storagePaths[key]);
|
|
416
|
+
},
|
|
417
|
+
assertUsed() {
|
|
418
|
+
const [first] = unused;
|
|
419
|
+
if (first !== void 0) throw new TypeError(`storagePaths["${first}"]: no such column. Use \`table.column\` or \`schema.table.column\` (database names) in \`schemas\`.`);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
function enumType(values) {
|
|
424
|
+
return values.map((value) => JSON.stringify(value)).join(" | ");
|
|
425
|
+
}
|
|
426
|
+
/** Builds the codegen model from a snapshot and config. */
|
|
427
|
+
function buildModel(snapshot, config) {
|
|
428
|
+
const catalog = toCatalog(snapshot);
|
|
429
|
+
const introspection = restrictSchemas(snapshot.generator, config.schemas);
|
|
430
|
+
const tsType = (schema, format, typeSchema) => tsTypeOf(introspection, schema, format, typeSchema).replaceAll(/\(([\w.$]+)\)\[\]/g, "$1[]");
|
|
431
|
+
const enumsByName = /* @__PURE__ */ new Map();
|
|
432
|
+
for (const entry of catalog.enums) enumsByName.set(`${entry.schema}.${entry.name}`, entry.values);
|
|
433
|
+
const jsonImports = /* @__PURE__ */ new Map();
|
|
434
|
+
const unusedJson = new Set(Object.keys(config.json));
|
|
435
|
+
const jsonTypeFor = (schema, table, column) => {
|
|
436
|
+
const key = [`${schema}.${table}.${column}`, `${table}.${column}`].find((candidate) => candidate in config.json);
|
|
437
|
+
const override = key === void 0 ? void 0 : config.json[key];
|
|
438
|
+
if (key === void 0 || !override) return void 0;
|
|
439
|
+
unusedJson.delete(key);
|
|
440
|
+
if ("type" in override) return override.type;
|
|
441
|
+
const [from, name] = override.import.split("#");
|
|
442
|
+
if (!from || !name) throw new TypeError(`json["${key}"].import must look like "./path.ts#TypeName"`);
|
|
443
|
+
jsonImports.set(name, {
|
|
444
|
+
name,
|
|
445
|
+
from
|
|
446
|
+
});
|
|
447
|
+
return name;
|
|
448
|
+
};
|
|
449
|
+
const optionsOf = (table) => configFor(config.tables, table.schema, table.name);
|
|
450
|
+
const storagePaths = storagePathsOf(config);
|
|
451
|
+
const included = catalog.tables.filter((table) => config.schemas.includes(table.schema) && !optionsOf(table)?.exclude);
|
|
452
|
+
const keyOf = /* @__PURE__ */ new Map();
|
|
453
|
+
const ownerOf = /* @__PURE__ */ new Map();
|
|
454
|
+
for (const table of included) {
|
|
455
|
+
const id = `${table.schema}.${table.name}`;
|
|
456
|
+
const key = tableKey(table, config.casing, config.schemas);
|
|
457
|
+
const other = ownerOf.get(key);
|
|
458
|
+
if (other !== void 0) throw new TypeError(`${other} and ${id} both generate the model key "${key}". Exclude one with \`tables["${id}"].exclude\`, or rename one of the tables.`);
|
|
459
|
+
ownerOf.set(key, id);
|
|
460
|
+
keyOf.set(id, key);
|
|
461
|
+
}
|
|
462
|
+
const tables = included.map((table) => {
|
|
463
|
+
const casing = optionsOf(table)?.casing ?? config.casing;
|
|
464
|
+
const checks = /* @__PURE__ */ new Map();
|
|
465
|
+
const checkedNotNull = /* @__PURE__ */ new Set();
|
|
466
|
+
for (const check of table.checks) {
|
|
467
|
+
const union = parseCheckUnion(check.definition);
|
|
468
|
+
if (union) checks.set(union.column, union.values);
|
|
469
|
+
for (const column of parseCheckNotNull(check.definition)) checkedNotNull.add(column);
|
|
470
|
+
}
|
|
471
|
+
const beforeInsert = table.triggers.some((trigger) => trigger.timing === "before" && trigger.level === "row" && trigger.events.includes("insert"));
|
|
472
|
+
const insertOptional = new Set(optionsOf(table)?.insertOptional ?? []);
|
|
473
|
+
for (const name of insertOptional) if (!table.columns.some((column) => column.name === name)) throw new TypeError(`tables.${table.name}.insertOptional: "${name}" is not a column of ${table.schema}.${table.name}`);
|
|
474
|
+
const columns = table.columns.map((column) => {
|
|
475
|
+
const values = (column.isEnum ? enumsByName.get(`${column.typeSchema}.${column.udt}`) : void 0) ?? checks.get(column.name);
|
|
476
|
+
const json = isJsonUdt(column.udt);
|
|
477
|
+
const override = json ? jsonTypeFor(table.schema, table.name, column.name) : void 0;
|
|
478
|
+
const storage = storagePaths.lookup(table.schema, table.name, column.name, column.udt);
|
|
479
|
+
const codec = override || values || storage ? void 0 : codecFor(column.udt, config);
|
|
480
|
+
const base = override ?? (values ? enumType(values) : storage ? `StoragePath<${JSON.stringify(storage)}>` : codec ? CODEC_TYPE[codec] : void 0);
|
|
481
|
+
const columnType = base === void 0 ? tsType(table.schema, column.format, column.typeSchema) : column.isArray ? arrayOf(base) : base;
|
|
482
|
+
const readonly = column.generated || column.identity === "always";
|
|
483
|
+
const narrowed = column.nullable && checkedNotNull.has(column.name);
|
|
484
|
+
const nullable = column.nullable && !narrowed;
|
|
485
|
+
const filled = column.hasDefault || insertOptional.has(column.name) || narrowed && beforeInsert;
|
|
486
|
+
const insertable = !readonly && table.insertable && (table.kind !== "view" || column.updatable);
|
|
487
|
+
const updatable = !readonly && table.updatable && column.updatable;
|
|
488
|
+
return {
|
|
489
|
+
app: applyCasing(column.name, casing),
|
|
490
|
+
db: column.name,
|
|
491
|
+
snapshot: column,
|
|
492
|
+
tsType: columnType,
|
|
493
|
+
nullable,
|
|
494
|
+
filled,
|
|
495
|
+
optional: nullable || filled || readonly,
|
|
496
|
+
readonly,
|
|
497
|
+
values,
|
|
498
|
+
json,
|
|
499
|
+
codec,
|
|
500
|
+
storage,
|
|
501
|
+
insertable,
|
|
502
|
+
updatable
|
|
503
|
+
};
|
|
504
|
+
});
|
|
505
|
+
const byApp = /* @__PURE__ */ new Map();
|
|
506
|
+
for (const column of columns) {
|
|
507
|
+
const other = byApp.get(column.app);
|
|
508
|
+
if (other !== void 0) throw new TypeError(`${table.schema}.${table.name}: columns "${other}" and "${column.db}" both become "${column.app}". Rename one, or set \`tables["${table.schema}.${table.name}"].casing: "snake"\`.`);
|
|
509
|
+
byApp.set(column.app, column.db);
|
|
510
|
+
}
|
|
511
|
+
return {
|
|
512
|
+
table,
|
|
513
|
+
casing,
|
|
514
|
+
columns,
|
|
515
|
+
byDb: new Map(columns.map((column) => [column.db, column]))
|
|
516
|
+
};
|
|
517
|
+
});
|
|
518
|
+
storagePaths.assertUsed();
|
|
519
|
+
const schemasOf = /* @__PURE__ */ new Map();
|
|
520
|
+
for (const table of catalog.tables) {
|
|
521
|
+
if (!config.schemas.includes(table.schema)) continue;
|
|
522
|
+
schemasOf.set(table.name, [...schemasOf.get(table.name) ?? [], table.schema]);
|
|
523
|
+
}
|
|
524
|
+
const known = (key) => {
|
|
525
|
+
const dot = key.indexOf(".");
|
|
526
|
+
if (dot === -1) return schemasOf.has(key);
|
|
527
|
+
return schemasOf.get(key.slice(dot + 1))?.includes(key.slice(0, dot)) ?? false;
|
|
528
|
+
};
|
|
529
|
+
const warnings = [
|
|
530
|
+
...Object.keys(config.tables).filter((name) => !known(name)).map((name) => `tables["${name}"]: no table by that name in ${config.schemas.join(", ")}, so the entry does nothing.`),
|
|
531
|
+
...Object.keys(config.tables).flatMap((name) => {
|
|
532
|
+
const shared = (schemasOf.get(name) ?? []).filter((schema) => !(`${schema}.${name}` in config.tables)).map((schema) => `${schema}.${name}`);
|
|
533
|
+
return shared.length > 1 ? [`tables["${name}"]: ${shared.join(" and ")} share the name, so the entry applies to each. Key it \`schema.table\` to pick one.`] : [];
|
|
534
|
+
}),
|
|
535
|
+
...[...unusedJson].map((key) => `json["${key}"]: no json or jsonb column by that name, so the type is not used. Keys are \`table.column\` or \`schema.table.column\` with database names.`)
|
|
536
|
+
];
|
|
537
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
538
|
+
for (const entry of tables) byKey.set(`${entry.table.schema}.${entry.table.name}`, entry);
|
|
539
|
+
const appName = (schema, table, column) => {
|
|
540
|
+
return byKey.get(`${schema}.${table}`)?.byDb.get(column)?.app ?? column;
|
|
541
|
+
};
|
|
542
|
+
const relations = /* @__PURE__ */ new Map();
|
|
543
|
+
const push = (tableId, relation) => {
|
|
544
|
+
const list = relations.get(tableId);
|
|
545
|
+
if (list) list.push(relation);
|
|
546
|
+
else relations.set(tableId, [relation]);
|
|
547
|
+
};
|
|
548
|
+
for (const { table, casing, byDb: own } of tables) {
|
|
549
|
+
const sourceId = `${table.schema}.${table.name}`;
|
|
550
|
+
for (const fk of table.foreignKeys) {
|
|
551
|
+
const targetId = `${fk.refSchema}.${fk.refTable}`;
|
|
552
|
+
const target = byKey.get(targetId);
|
|
553
|
+
if (!target) continue;
|
|
554
|
+
const targetKey = keyOf.get(targetId) ?? fk.refTable;
|
|
555
|
+
const sourceColumns = fk.columns.map((column) => appName(table.schema, table.name, column));
|
|
556
|
+
const targetColumns = fk.refColumns.map((column) => appName(fk.refSchema, fk.refTable, column));
|
|
557
|
+
const nullable = config.relations.nullableUnderRls && target.table.rls || fk.columns.some((column) => own.get(column)?.nullable ?? true);
|
|
558
|
+
const onDelete = fk.onDelete === "cascade" || fk.onDelete === "set null" || fk.onDelete === "set default" ? { onDelete: fk.onDelete } : {};
|
|
559
|
+
const naming = namingColumns(fk.columns, fk.refColumns);
|
|
560
|
+
const [only] = naming;
|
|
561
|
+
const forwardBase = naming.length === 1 && only?.endsWith("_id") ? only.slice(0, -3) : singular(fk.refTable);
|
|
562
|
+
push(sourceId, {
|
|
563
|
+
name: applyCasing(forwardBase, casing),
|
|
564
|
+
base: forwardBase,
|
|
565
|
+
fkColumns: fk.columns,
|
|
566
|
+
refColumns: fk.refColumns,
|
|
567
|
+
meta: {
|
|
568
|
+
table: targetKey,
|
|
569
|
+
kind: "one",
|
|
570
|
+
nullable,
|
|
571
|
+
foreignKey: fk.name,
|
|
572
|
+
columns: sourceColumns,
|
|
573
|
+
references: targetColumns,
|
|
574
|
+
direction: "forward",
|
|
575
|
+
...onDelete
|
|
576
|
+
}
|
|
577
|
+
});
|
|
578
|
+
const unique = fk.oneToOne || sameColumns(table.primaryKey, fk.columns) || table.uniques.some((key) => sameColumns(key.columns, fk.columns));
|
|
579
|
+
const reverseBase = unique ? singular(table.name) : table.name;
|
|
580
|
+
push(targetId, {
|
|
581
|
+
name: applyCasing(reverseBase, target.casing),
|
|
582
|
+
base: reverseBase,
|
|
583
|
+
fkColumns: fk.columns,
|
|
584
|
+
refColumns: fk.refColumns,
|
|
585
|
+
meta: {
|
|
586
|
+
table: keyOf.get(sourceId) ?? table.name,
|
|
587
|
+
kind: unique ? "one" : "many",
|
|
588
|
+
nullable: true,
|
|
589
|
+
foreignKey: fk.name,
|
|
590
|
+
columns: targetColumns,
|
|
591
|
+
references: sourceColumns,
|
|
592
|
+
direction: "reverse",
|
|
593
|
+
...onDelete
|
|
594
|
+
}
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
const flagsConfig = config.plugins;
|
|
599
|
+
const tableModels = tables.map(({ table, casing, columns, byDb }) => {
|
|
600
|
+
const id = `${table.schema}.${table.name}`;
|
|
601
|
+
const renames = optionsOf(table)?.relations ?? {};
|
|
602
|
+
const columnNames = new Set(columns.map((column) => column.app));
|
|
603
|
+
const list = dedupeRelations(relations.get(id) ?? [], columnNames, casing).map((relation) => ({
|
|
604
|
+
...relation,
|
|
605
|
+
name: renames[relation.name] ?? relation.name
|
|
606
|
+
}));
|
|
607
|
+
const has = (db) => byDb.get(db)?.app;
|
|
608
|
+
const flags = {};
|
|
609
|
+
if (flagsConfig.softDelete) {
|
|
610
|
+
const db = flagsConfig.softDelete.column;
|
|
611
|
+
const column = byDb.get(db);
|
|
612
|
+
if (column) {
|
|
613
|
+
const udt = column.snapshot.udt;
|
|
614
|
+
if (column.snapshot.isArray || !SOFT_DELETE_UDTS.has(udt)) throw new TypeError(`plugins.softDelete.column: ${table.schema}.${table.name}.${db} is ${udt}, but softDelete() writes a timestamp. Use timestamptz or timestamp.`);
|
|
615
|
+
flags.softDelete = column.app;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
if (flagsConfig.timestamps) {
|
|
619
|
+
const createdAt = has(flagsConfig.timestamps.createdAt);
|
|
620
|
+
const updatedAt = has(flagsConfig.timestamps.updatedAt);
|
|
621
|
+
if (createdAt || updatedAt) flags.timestamps = {
|
|
622
|
+
...createdAt ? { createdAt } : {},
|
|
623
|
+
...updatedAt ? { updatedAt } : {}
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
if (flagsConfig.tenant) {
|
|
627
|
+
const column = has(flagsConfig.tenant.column);
|
|
628
|
+
if (column) flags.tenant = column;
|
|
629
|
+
}
|
|
630
|
+
if (flagsConfig.actor) {
|
|
631
|
+
const createdBy = has(flagsConfig.actor.createdBy);
|
|
632
|
+
const updatedBy = has(flagsConfig.actor.updatedBy);
|
|
633
|
+
const impersonatedBy = has(flagsConfig.actor.impersonatedBy);
|
|
634
|
+
if (createdBy || updatedBy || impersonatedBy) flags.actor = {
|
|
635
|
+
...createdBy ? { createdBy } : {},
|
|
636
|
+
...updatedBy ? { updatedBy } : {},
|
|
637
|
+
...impersonatedBy ? { impersonatedBy } : {}
|
|
638
|
+
};
|
|
639
|
+
}
|
|
640
|
+
const key = keyOf.get(id) ?? table.name;
|
|
641
|
+
const columnMeta = {};
|
|
642
|
+
for (const column of columns) {
|
|
643
|
+
const meta = {
|
|
644
|
+
db: column.db,
|
|
645
|
+
type: column.snapshot.udt,
|
|
646
|
+
nullable: column.nullable,
|
|
647
|
+
hasDefault: column.filled
|
|
648
|
+
};
|
|
649
|
+
if (column.readonly) meta.generated = true;
|
|
650
|
+
if (column.snapshot.identity) meta.identity = column.snapshot.identity;
|
|
651
|
+
if (!column.readonly && !column.insertable) meta.insertable = false;
|
|
652
|
+
if (!column.readonly && !column.updatable) meta.updatable = false;
|
|
653
|
+
if (column.snapshot.isArray) meta.array = true;
|
|
654
|
+
if (column.json) meta.json = true;
|
|
655
|
+
if (column.values) meta.enum = column.values;
|
|
656
|
+
if (column.codec) meta.codec = column.codec;
|
|
657
|
+
if (column.storage) meta.storage = column.storage;
|
|
658
|
+
if (config.sensitive.includes(`${table.name}.${column.db}`) || config.sensitive.includes(`${table.schema}.${table.name}.${column.db}`)) meta.sensitive = true;
|
|
659
|
+
Object.assign(meta, commentMeta(column.snapshot.comment));
|
|
660
|
+
columnMeta[column.app] = meta;
|
|
661
|
+
}
|
|
662
|
+
const app = (db) => byDb.get(db)?.app ?? db;
|
|
663
|
+
const uniqueKeys = {};
|
|
664
|
+
for (const unique of table.uniques) uniqueKeys[unique.name] = unique.columns.map(app);
|
|
665
|
+
const relationMeta = {};
|
|
666
|
+
for (const relation of list) relationMeta[relation.name] = relation.meta;
|
|
667
|
+
return {
|
|
668
|
+
key,
|
|
669
|
+
snapshot: table,
|
|
670
|
+
casing,
|
|
671
|
+
columns,
|
|
672
|
+
relations: list,
|
|
673
|
+
meta: {
|
|
674
|
+
key,
|
|
675
|
+
name: table.name,
|
|
676
|
+
schema: table.schema,
|
|
677
|
+
kind: table.kind,
|
|
678
|
+
columns: columnMeta,
|
|
679
|
+
primaryKey: table.primaryKey.map(app),
|
|
680
|
+
uniqueKeys,
|
|
681
|
+
relations: relationMeta,
|
|
682
|
+
flags,
|
|
683
|
+
...commentMeta(table.comment)
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
});
|
|
687
|
+
const includedEnums = catalog.enums.filter((entry) => config.schemas.includes(entry.schema));
|
|
688
|
+
const enumNameCount = /* @__PURE__ */ new Map();
|
|
689
|
+
for (const entry of includedEnums) enumNameCount.set(entry.name, (enumNameCount.get(entry.name) ?? 0) + 1);
|
|
690
|
+
const enums = includedEnums.map((entry) => {
|
|
691
|
+
const key = entry.schema !== "public" && (enumNameCount.get(entry.name) ?? 0) > 1 ? `${entry.schema}_${entry.name}` : entry.name;
|
|
692
|
+
return {
|
|
693
|
+
schema: entry.schema,
|
|
694
|
+
name: entry.name,
|
|
695
|
+
key,
|
|
696
|
+
constant: identifier(`${toCamel(key)}Values`),
|
|
697
|
+
typeName: identifier(pascal(key)),
|
|
698
|
+
values: entry.values
|
|
699
|
+
};
|
|
700
|
+
});
|
|
701
|
+
const tablesByName = new Map(tableModels.map((table) => [`${table.snapshot.schema}.${table.snapshot.name}`, table]));
|
|
702
|
+
const callable = catalog.functions.filter((fn) => config.schemas.includes(fn.schema)).filter((fn) => fn.returns !== "trigger" && fn.returns !== "event_trigger").filter((fn) => !fn.args.some((arg) => arg.udt === "internal"));
|
|
703
|
+
const ownerSchema = /* @__PURE__ */ new Map();
|
|
704
|
+
for (const schema of config.schemas) for (const fn of callable) if (fn.schema === schema && !ownerSchema.has(fn.name)) ownerSchema.set(fn.name, schema);
|
|
705
|
+
const shadowed = /* @__PURE__ */ new Set();
|
|
706
|
+
const functions = callable.filter((fn) => {
|
|
707
|
+
if (ownerSchema.get(fn.name) === fn.schema) return true;
|
|
708
|
+
shadowed.add(`${fn.schema}.${fn.name}`);
|
|
709
|
+
return false;
|
|
710
|
+
}).map((fn) => {
|
|
711
|
+
const typeOf = (format, typeSchema) => {
|
|
712
|
+
const isArray = format.startsWith("_");
|
|
713
|
+
const values = enumsByName.get(`${typeSchema ?? fn.schema}.${isArray ? format.slice(1) : format}`);
|
|
714
|
+
if (values) return isArray ? arrayOf(enumType(values)) : enumType(values);
|
|
715
|
+
return tsType(fn.schema, format, typeSchema);
|
|
716
|
+
};
|
|
717
|
+
const notNull = configFor(config.functions, fn.schema, fn.name)?.notNull;
|
|
718
|
+
const notNullColumns = new Set(Array.isArray(notNull) ? notNull : []);
|
|
719
|
+
if (Array.isArray(notNull)) {
|
|
720
|
+
for (const name of notNullColumns) if (!fn.returnsTable?.some((column) => column.name === name)) throw new TypeError(`functions.${fn.name}.notNull: "${name}" is not a column the function returns`);
|
|
721
|
+
}
|
|
722
|
+
const orNull = (type, column) => notNull === true || column !== void 0 && notNullColumns.has(column) || type === "undefined" || type === "Json" || type === "unknown" ? type : `${type} | null`;
|
|
723
|
+
let returns;
|
|
724
|
+
let result;
|
|
725
|
+
const rowTable = fn.returnsRelation ? tablesByName.get(fn.returnsRelation) : void 0;
|
|
726
|
+
if (fn.returnsTable) {
|
|
727
|
+
const columns = fn.returnsTable.map((column) => {
|
|
728
|
+
const codec = codecFor(column.udt, config);
|
|
729
|
+
return {
|
|
730
|
+
db: column.name,
|
|
731
|
+
app: applyCasing(column.name, config.casing),
|
|
732
|
+
codec,
|
|
733
|
+
tsType: codec ? CODEC_TYPE[codec] : typeOf(column.udt, column.typeSchema)
|
|
734
|
+
};
|
|
735
|
+
});
|
|
736
|
+
returns = `{ ${columns.map((column) => `${JSON.stringify(column.app)}: ${orNull(column.tsType, column.db)}`).join("; ")} }[]`;
|
|
737
|
+
if (columns.some((column) => column.codec !== void 0 || column.app !== column.db)) result = { columns: columns.map(({ db, app, codec }) => ({
|
|
738
|
+
db,
|
|
739
|
+
...app === db ? {} : { name: app },
|
|
740
|
+
...codec ? { codec } : {}
|
|
741
|
+
})) };
|
|
742
|
+
} else if (rowTable) {
|
|
743
|
+
returns = `Models[${JSON.stringify(rowTable.key)}]['Row']`;
|
|
744
|
+
returns = fn.returnsSet ? arrayOf(returns) : orNull(returns);
|
|
745
|
+
if (rowTable.columns.some((column) => column.codec !== void 0 || column.app !== column.db)) result = { table: rowTable.key };
|
|
746
|
+
} else {
|
|
747
|
+
returns = orNull(typeOf(fn.returns, fn.returnsSchema));
|
|
748
|
+
if (fn.returnsSet) returns = arrayOf(returns);
|
|
749
|
+
}
|
|
750
|
+
return {
|
|
751
|
+
key: fn.name,
|
|
752
|
+
meta: {
|
|
753
|
+
name: fn.name,
|
|
754
|
+
schema: fn.schema,
|
|
755
|
+
args: fn.args.map((arg) => ({
|
|
756
|
+
name: arg.name,
|
|
757
|
+
type: arg.udt
|
|
758
|
+
})),
|
|
759
|
+
returns: fn.returns,
|
|
760
|
+
returnsSet: fn.returnsSet,
|
|
761
|
+
volatility: fn.volatility,
|
|
762
|
+
...result ? { result } : {}
|
|
763
|
+
},
|
|
764
|
+
args: fn.args.map((arg) => ({
|
|
765
|
+
name: arg.name,
|
|
766
|
+
tsType: typeOf(arg.isArray ? `_${arg.udt}` : arg.udt, arg.typeSchema),
|
|
767
|
+
optional: arg.hasDefault
|
|
768
|
+
})),
|
|
769
|
+
returns
|
|
770
|
+
};
|
|
771
|
+
});
|
|
772
|
+
for (const name of Object.keys(config.functions)) if (!functions.some((fn) => fn.key === name || `${fn.meta.schema}.${fn.key}` === name)) throw new TypeError(`functions.${name}: no function "${name}" in ${config.schemas.join(", ")}`);
|
|
773
|
+
for (const id of shadowed) {
|
|
774
|
+
const name = id.slice(id.indexOf(".") + 1);
|
|
775
|
+
warnings.push(`${id}: ${ownerSchema.get(name) ?? ""}.${name} has the same name and comes first in \`schemas\`, so ${id} gets no \`$rpc\` types.`);
|
|
776
|
+
}
|
|
777
|
+
const tablesMeta = {};
|
|
778
|
+
for (const table of tableModels) tablesMeta[table.key] = table.meta;
|
|
779
|
+
const enumsMeta = {};
|
|
780
|
+
for (const entry of enums) enumsMeta[entry.key] = entry.values;
|
|
781
|
+
const functionsMeta = {};
|
|
782
|
+
for (const [key, group] of functionGroups(functions)) {
|
|
783
|
+
const [first] = group;
|
|
784
|
+
if (!first) continue;
|
|
785
|
+
functionsMeta[key] = group.length === 1 ? first.meta : {
|
|
786
|
+
...first.meta,
|
|
787
|
+
overloads: group.map(signatureOf)
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
const claimOverrides = {};
|
|
791
|
+
for (const key of Object.keys(DEFAULT_CLAIMS)) if (config.claims[key] !== DEFAULT_CLAIMS[key]) claimOverrides[key] = config.claims[key];
|
|
792
|
+
const tenantClaim = config.claims.tenant === DEFAULT_CLAIMS.tenant ? void 0 : tenantClaimPaths(config.claims.tenant);
|
|
793
|
+
const meta = {
|
|
794
|
+
version: 1,
|
|
795
|
+
casing: config.casing,
|
|
796
|
+
tables: tablesMeta,
|
|
797
|
+
enums: enumsMeta,
|
|
798
|
+
functions: functionsMeta,
|
|
799
|
+
...Object.keys(config.buckets).length > 0 ? { buckets: Object.fromEntries(Object.entries(config.buckets).map(([name, bucket]) => [name, {
|
|
800
|
+
id: bucket.id ?? name.replaceAll(/[A-Z]/g, (char) => `-${char.toLowerCase()}`),
|
|
801
|
+
public: bucket.public ?? false,
|
|
802
|
+
path: bucket.path,
|
|
803
|
+
...bucket.policy ? { policy: typeof bucket.policy === "string" ? bucket.policy : resolveProviderSql(`buckets.${name}`, bucket.policy, config.authorization?.functions) } : {},
|
|
804
|
+
...bucket.policy === "tenant" && tenantClaim ? { tenant: { claim: tenantClaim } } : {},
|
|
805
|
+
...bucket.fileSizeLimit ? { fileSizeLimit: bucket.fileSizeLimit } : {},
|
|
806
|
+
...bucket.allowedMimeTypes ? { allowedMimeTypes: bucket.allowedMimeTypes } : {},
|
|
807
|
+
...bucket.versioning ? { versioning: true } : {},
|
|
808
|
+
...bucket.lifecycle ? { lifecycle: bucket.lifecycle } : {}
|
|
809
|
+
}])) } : {},
|
|
810
|
+
...Object.keys(config.topics).length > 0 ? { topics: config.topics } : {},
|
|
811
|
+
...config.realtime.tables.length > 0 ? { realtime: realtimeMeta(config.realtime.tables, config.realtime.users, tableModels) } : {},
|
|
812
|
+
...Object.keys(claimOverrides).length > 0 ? { claims: claimOverrides } : {}
|
|
813
|
+
};
|
|
814
|
+
return {
|
|
815
|
+
tables: tableModels,
|
|
816
|
+
enums,
|
|
817
|
+
functions,
|
|
818
|
+
jsonImports: [...jsonImports.values()],
|
|
819
|
+
meta,
|
|
820
|
+
config,
|
|
821
|
+
catalog,
|
|
822
|
+
introspection,
|
|
823
|
+
warnings
|
|
824
|
+
};
|
|
825
|
+
}
|
|
826
|
+
function deepFreeze(value) {
|
|
827
|
+
if (typeof value === "object" && value !== null && !Object.isFrozen(value)) {
|
|
828
|
+
Object.freeze(value);
|
|
829
|
+
for (const entry of Object.values(value)) deepFreeze(entry);
|
|
830
|
+
}
|
|
831
|
+
return value;
|
|
832
|
+
}
|
|
833
|
+
/** The public, frozen view of the model that generators receive. */
|
|
834
|
+
function generatorModel(model) {
|
|
835
|
+
return deepFreeze({
|
|
836
|
+
tables: model.tables.map((table) => ({
|
|
837
|
+
key: table.key,
|
|
838
|
+
schema: table.snapshot.schema,
|
|
839
|
+
name: table.snapshot.name,
|
|
840
|
+
casing: table.casing,
|
|
841
|
+
columns: table.columns.map((column) => ({
|
|
842
|
+
app: column.app,
|
|
843
|
+
db: column.db,
|
|
844
|
+
tsType: column.tsType,
|
|
845
|
+
nullable: column.nullable,
|
|
846
|
+
optional: column.optional,
|
|
847
|
+
readonly: column.readonly,
|
|
848
|
+
values: column.values === void 0 ? void 0 : [...column.values],
|
|
849
|
+
json: column.json
|
|
850
|
+
}))
|
|
851
|
+
})),
|
|
852
|
+
enums: model.enums.map((entry) => ({
|
|
853
|
+
schema: entry.schema,
|
|
854
|
+
name: entry.name,
|
|
855
|
+
values: [...entry.values]
|
|
856
|
+
}))
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
/**
|
|
860
|
+
* The columns that name a relation. A composite key that repeats a column on
|
|
861
|
+
* both sides, such as `(customer_id, organization_id)` referencing
|
|
862
|
+
* `(id, organization_id)` to keep rows in one tenant, is named by the rest.
|
|
863
|
+
*/
|
|
864
|
+
function namingColumns(columns, references) {
|
|
865
|
+
const own = columns.filter((column, index) => column !== references[index]);
|
|
866
|
+
return own.length > 0 ? own : columns;
|
|
867
|
+
}
|
|
868
|
+
/**
|
|
869
|
+
* Makes relation names unique per table and distinct from column names:
|
|
870
|
+
* two FKs to the same table become `customersByPrimaryContact`-style names.
|
|
871
|
+
* When those still collide, the names use every column of the key
|
|
872
|
+
* (`locationsByCustomerOrganization`), then the constraint name, and only
|
|
873
|
+
* then a trailing `_`.
|
|
874
|
+
*/
|
|
875
|
+
function dedupeRelations(list, columns, casing) {
|
|
876
|
+
const suffixed = (relation, suffix) => applyCasing(`${relation.base}_by_${suffix}`, casing);
|
|
877
|
+
const keyColumns = (relation, all) => (all ? relation.fkColumns : namingColumns(relation.fkColumns, relation.refColumns)).map((column) => column.replace(/_?[iI]d$/, "")).join("_");
|
|
878
|
+
const repeated = (names) => {
|
|
879
|
+
const seen = /* @__PURE__ */ new Set();
|
|
880
|
+
const twice = /* @__PURE__ */ new Set();
|
|
881
|
+
for (const name of names) (seen.has(name) ? twice : seen).add(name);
|
|
882
|
+
return twice;
|
|
883
|
+
};
|
|
884
|
+
const plain = repeated(list.map((relation) => relation.name));
|
|
885
|
+
const names = [(relation) => suffixed(relation, keyColumns(relation, true)), (relation) => suffixed(relation, relation.meta.foreignKey)].reduce((current, step) => {
|
|
886
|
+
const clash = repeated(current);
|
|
887
|
+
return list.map((relation, index) => {
|
|
888
|
+
const name = current[index] ?? relation.name;
|
|
889
|
+
return clash.has(name) || columns.has(name) ? step(relation) : name;
|
|
890
|
+
});
|
|
891
|
+
}, list.map((relation) => plain.has(relation.name) || columns.has(relation.name) ? suffixed(relation, keyColumns(relation, false)) : relation.name));
|
|
892
|
+
const used = /* @__PURE__ */ new Set();
|
|
893
|
+
return list.map((relation, index) => {
|
|
894
|
+
let name = names[index] ?? relation.name;
|
|
895
|
+
while (used.has(name) || columns.has(name)) name = `${name}_`;
|
|
896
|
+
used.add(name);
|
|
897
|
+
return {
|
|
898
|
+
...relation,
|
|
899
|
+
name
|
|
900
|
+
};
|
|
901
|
+
});
|
|
902
|
+
}
|
|
903
|
+
function realtimeMeta(names, users, tables) {
|
|
904
|
+
const out = {};
|
|
905
|
+
for (const name of names) {
|
|
906
|
+
const table = tables.find((candidate) => candidate.meta.name === name || `${candidate.meta.schema}.${candidate.meta.name}` === name);
|
|
907
|
+
if (!table) throw new Error(`realtime.tables: unknown table "${name}". Check the name and \`schemas\`.`);
|
|
908
|
+
const owner = users[name];
|
|
909
|
+
if (owner !== void 0) {
|
|
910
|
+
const user = Object.entries(table.meta.columns).find(([, column]) => column.db === owner)?.[0];
|
|
911
|
+
if (user === void 0) throw new Error(`realtime.users: "${name}" has no column "${owner}".`);
|
|
912
|
+
out[table.key] = { user };
|
|
913
|
+
continue;
|
|
914
|
+
}
|
|
915
|
+
const tenant = table.meta.flags.tenant;
|
|
916
|
+
out[table.key] = tenant ? { tenant } : {};
|
|
917
|
+
}
|
|
918
|
+
return out;
|
|
919
|
+
}
|
|
920
|
+
//#endregion
|
|
921
|
+
//#region src/cli/gen/emit.ts
|
|
922
|
+
const GENERATED_HEADER = "// @generated by better-supabase gen. Do not edit; run `better-supabase gen` instead.";
|
|
923
|
+
function rowEntries(columns, name) {
|
|
924
|
+
return columns.map((column) => `${prop(column[name])}: ${nullable(column.tsType, column.nullable)};`);
|
|
925
|
+
}
|
|
926
|
+
function insertEntries(columns, name) {
|
|
927
|
+
return columns.map((column) => {
|
|
928
|
+
if (!column.insertable) return `${prop(column[name])}?: never;`;
|
|
929
|
+
const optional = column.optional ? "?" : "";
|
|
930
|
+
return `${prop(column[name])}${optional}: ${nullable(column.tsType, column.nullable)};`;
|
|
931
|
+
});
|
|
932
|
+
}
|
|
933
|
+
function updateEntries(columns, name) {
|
|
934
|
+
return columns.map((column) => {
|
|
935
|
+
if (!column.updatable) return `${prop(column[name])}?: never;`;
|
|
936
|
+
return `${prop(column[name])}?: ${nullable(column.tsType, column.nullable)};`;
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
/** Postgres passes `null` to any argument, so every argument accepts it. */
|
|
940
|
+
function argEntries(fn) {
|
|
941
|
+
return fn.args.map((arg) => `${prop(arg.name)}${arg.optional ? "?" : ""}: ${nullable(arg.tsType, true)};`);
|
|
942
|
+
}
|
|
943
|
+
function enrichedDatabase(model) {
|
|
944
|
+
const schemas = model.config.schemas.flatMap((schema) => {
|
|
945
|
+
const tables = model.tables.filter((t) => t.snapshot.schema === schema).flatMap((table) => {
|
|
946
|
+
const overrides = table.columns.filter((column) => column.values !== void 0 && !column.snapshot.isEnum || column.json && column.tsType !== "Json" || column.storage !== void 0).map((column) => `${prop(column.db)}: ${column.tsType};`);
|
|
947
|
+
return overrides.length > 0 ? block(prop(table.snapshot.name), overrides) : [];
|
|
948
|
+
});
|
|
949
|
+
return tables.length > 0 ? block(prop(schema), tables) : [];
|
|
950
|
+
});
|
|
951
|
+
if (schemas.length === 0) return ["export type Database = SupabaseDatabase;"];
|
|
952
|
+
return [
|
|
953
|
+
"export type Database = EnrichDatabase<",
|
|
954
|
+
" SupabaseDatabase,",
|
|
955
|
+
" {",
|
|
956
|
+
...indent(schemas, 2),
|
|
957
|
+
" }",
|
|
958
|
+
">;"
|
|
959
|
+
];
|
|
960
|
+
}
|
|
961
|
+
function modelsType(model) {
|
|
962
|
+
return [
|
|
963
|
+
"export type Models = {",
|
|
964
|
+
...indent(model.tables.flatMap((table) => {
|
|
965
|
+
const relations = table.relations.map((relation) => `${prop(relation.name)}: { table: ${q(relation.meta.table)}; kind: ${q(relation.meta.kind)}; nullable: ${String(relation.meta.nullable)} };`);
|
|
966
|
+
const pk = table.meta.primaryKey;
|
|
967
|
+
const uniques = Object.entries(table.meta.uniqueKeys).map(([name, columns]) => `${prop(name)}: readonly [${columns.map(q).join(", ")}];`);
|
|
968
|
+
const flags = [];
|
|
969
|
+
const tableFlags = table.meta.flags;
|
|
970
|
+
if (tableFlags.softDelete) flags.push(`softDelete: ${q(tableFlags.softDelete)};`);
|
|
971
|
+
if (tableFlags.timestamps) flags.push("timestamps: true;");
|
|
972
|
+
if (tableFlags.tenant) flags.push(`tenant: ${q(tableFlags.tenant)};`);
|
|
973
|
+
if (tableFlags.actor) flags.push("actor: true;");
|
|
974
|
+
const none = "Record<never, never>";
|
|
975
|
+
const union = (names) => names.length === 0 ? "never" : names.map(q).join(" | ");
|
|
976
|
+
return block(prop(table.key), [
|
|
977
|
+
...block("Row", rowEntries(table.columns, "app"), none),
|
|
978
|
+
...table.snapshot.insertable ? block("Insert", insertEntries(table.columns, "app"), none) : ["Insert: never;"],
|
|
979
|
+
...table.snapshot.updatable ? block("Update", updateEntries(table.columns, "app"), none) : ["Update: never;"],
|
|
980
|
+
...block("Relations", relations, none),
|
|
981
|
+
`PrimaryKey: ${union(pk)};`,
|
|
982
|
+
...block("UniqueKeys", uniques, none),
|
|
983
|
+
`Checks: ${union(table.snapshot.checks.map((check) => check.name))};`,
|
|
984
|
+
`ForeignKeys: ${union(table.snapshot.foreignKeys.map((fk) => fk.name))};`,
|
|
985
|
+
...block("Flags", flags, none)
|
|
986
|
+
]);
|
|
987
|
+
}), 1),
|
|
988
|
+
"};"
|
|
989
|
+
];
|
|
990
|
+
}
|
|
991
|
+
function functionsType(model) {
|
|
992
|
+
if (model.functions.length === 0) return ["export type Functions = Record<never, never>;"];
|
|
993
|
+
return [
|
|
994
|
+
"export type Functions = {",
|
|
995
|
+
...indent([...functionGroups(model.functions)].flatMap(([key, group]) => {
|
|
996
|
+
const [first] = group;
|
|
997
|
+
if (group.length === 1 && first) return block(prop(key), [...block("Args", argEntries(first), "Record<never, never>"), `Returns: ${first.returns};`]);
|
|
998
|
+
return [`${prop(key)}:`, ...indent(group.flatMap((fn, index) => {
|
|
999
|
+
const lines = [...block("Args", argEntries(fn), "Record<PropertyKey, never>"), `Returns: ${fn.returns};`];
|
|
1000
|
+
const end = index === group.length - 1 ? "};" : "}";
|
|
1001
|
+
return [
|
|
1002
|
+
"| {",
|
|
1003
|
+
...indent(lines, 1),
|
|
1004
|
+
end
|
|
1005
|
+
];
|
|
1006
|
+
}), 1)];
|
|
1007
|
+
}), 1),
|
|
1008
|
+
"};"
|
|
1009
|
+
];
|
|
1010
|
+
}
|
|
1011
|
+
/** The metadata module next to `generated.ts`: `generated.meta.js` and its `.d.ts`. */
|
|
1012
|
+
function metaPaths(output) {
|
|
1013
|
+
const base = output.replace(/\.[cm]?[jt]sx?$/, "");
|
|
1014
|
+
return {
|
|
1015
|
+
js: `${base}.meta.js`,
|
|
1016
|
+
dts: `${base}.meta.d.ts`
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* JSON with one line per table, function and other named entry, so the
|
|
1021
|
+
* module stays small to load and a schema change still diffs per entry.
|
|
1022
|
+
*/
|
|
1023
|
+
function compactJson(meta) {
|
|
1024
|
+
return `{\n${Object.entries(meta).map(([key, value]) => {
|
|
1025
|
+
const name = JSON.stringify(key);
|
|
1026
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) return ` ${name}: ${JSON.stringify(value)}`;
|
|
1027
|
+
const inner = Object.entries(value).map(([entry, item]) => ` ${JSON.stringify(entry)}: ${JSON.stringify(item)}`);
|
|
1028
|
+
return inner.length === 0 ? ` ${name}: {}` : ` ${name}: {\n${inner.join(",\n")}\n }`;
|
|
1029
|
+
}).join(",\n")}\n}`;
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* The schema metadata as plain JavaScript plus a one-line declaration, so
|
|
1033
|
+
* TypeScript reads `SchemaMeta` instead of checking thousands of lines of
|
|
1034
|
+
* object literal in every program that imports the generated module.
|
|
1035
|
+
*/
|
|
1036
|
+
function emitMeta(model, options = {}) {
|
|
1037
|
+
const runtime = options.runtimeImport ?? "better-supabase";
|
|
1038
|
+
return {
|
|
1039
|
+
js: [
|
|
1040
|
+
GENERATED_HEADER,
|
|
1041
|
+
`// @ts-self-types="${options.types ?? "./generated.meta.d.ts"}"`,
|
|
1042
|
+
"/* oxlint-disable */",
|
|
1043
|
+
"/* eslint-disable */",
|
|
1044
|
+
"",
|
|
1045
|
+
`export default ${compactJson({ ...model.meta })};`,
|
|
1046
|
+
""
|
|
1047
|
+
].join("\n"),
|
|
1048
|
+
dts: [
|
|
1049
|
+
GENERATED_HEADER,
|
|
1050
|
+
"/* oxlint-disable */",
|
|
1051
|
+
"/* eslint-disable */",
|
|
1052
|
+
"",
|
|
1053
|
+
`import type { SchemaMeta } from ${q(runtime)};`,
|
|
1054
|
+
"",
|
|
1055
|
+
"declare const meta: SchemaMeta;",
|
|
1056
|
+
"export default meta;",
|
|
1057
|
+
""
|
|
1058
|
+
].join("\n")
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
/** Names the generated module declares or imports itself. */
|
|
1062
|
+
const RESERVED_EXPORTS = [
|
|
1063
|
+
"defineSchema",
|
|
1064
|
+
"Schema",
|
|
1065
|
+
"SchemaMeta",
|
|
1066
|
+
"EnrichDatabase",
|
|
1067
|
+
"MutableWhere",
|
|
1068
|
+
"OrderByArg",
|
|
1069
|
+
"OrderByInput",
|
|
1070
|
+
"StoragePath",
|
|
1071
|
+
"SupabaseDatabase",
|
|
1072
|
+
"meta",
|
|
1073
|
+
"Json",
|
|
1074
|
+
"Database",
|
|
1075
|
+
"Models",
|
|
1076
|
+
"Functions",
|
|
1077
|
+
"TableName",
|
|
1078
|
+
"UniqueConstraint",
|
|
1079
|
+
"CheckConstraint",
|
|
1080
|
+
"ForeignKeyConstraint",
|
|
1081
|
+
"RowOf",
|
|
1082
|
+
"InsertOf",
|
|
1083
|
+
"UpdateOf",
|
|
1084
|
+
"WhereOf",
|
|
1085
|
+
"OrderByOf",
|
|
1086
|
+
"OrderTermOf",
|
|
1087
|
+
"buckets",
|
|
1088
|
+
"topics",
|
|
1089
|
+
"schema"
|
|
1090
|
+
];
|
|
1091
|
+
/** Renders the generated module. */
|
|
1092
|
+
function emitModule(model, options) {
|
|
1093
|
+
const runtime = options.runtimeImport ?? "better-supabase";
|
|
1094
|
+
const lines = [
|
|
1095
|
+
GENERATED_HEADER,
|
|
1096
|
+
"/* oxlint-disable */",
|
|
1097
|
+
"/* eslint-disable */",
|
|
1098
|
+
"",
|
|
1099
|
+
`import { defineSchema, type Schema${options.inlineMeta ? ", type SchemaMeta" : ""} } from ${q(runtime)};`
|
|
1100
|
+
];
|
|
1101
|
+
const storagePaths = model.tables.some((table) => table.columns.some((column) => column.storage !== void 0));
|
|
1102
|
+
lines.push(`import type { EnrichDatabase, MutableWhere, OrderByArg, OrderByInput${storagePaths ? ", StoragePath" : ""} } from ${q(runtime)};`, `import type { Database as SupabaseDatabase } from ${q(options.databaseTypesImport ?? "./database.types.ts")};`);
|
|
1103
|
+
const byFile = /* @__PURE__ */ new Map();
|
|
1104
|
+
for (const entry of model.jsonImports) {
|
|
1105
|
+
const list = byFile.get(entry.from) ?? [];
|
|
1106
|
+
list.push(entry.name);
|
|
1107
|
+
byFile.set(entry.from, list);
|
|
1108
|
+
}
|
|
1109
|
+
for (const [from, names] of [...byFile.entries()].sort(([a], [b]) => byCodePoint(a, b))) lines.push(`import type { ${names.sort().join(", ")} } from ${q(options.importPathFor(from))};`);
|
|
1110
|
+
lines.push(options.inlineMeta ? `\nconst meta: SchemaMeta = ${compactJson({ ...model.meta })};` : `import meta from ${q(options.metaImport ?? "./generated.meta.js")};`, "", "export type Json = string | number | boolean | null | { [key: string]: Json | undefined } | Json[];", "", ...enrichedDatabase(model), "");
|
|
1111
|
+
const exported = new Map([...RESERVED_EXPORTS, ...model.jsonImports.map((entry) => entry.name)].map((name) => [name, "the generated module"]));
|
|
1112
|
+
const claim = (name, source) => {
|
|
1113
|
+
const other = exported.get(name);
|
|
1114
|
+
if (other !== void 0) throw new TypeError(`${source} and ${other} both export "${name}" from the generated module. Rename one of them in the database.`);
|
|
1115
|
+
exported.set(name, source);
|
|
1116
|
+
return name;
|
|
1117
|
+
};
|
|
1118
|
+
for (const entry of model.enums) {
|
|
1119
|
+
const source = `enum ${entry.schema}.${entry.name}`;
|
|
1120
|
+
lines.push(`export const ${claim(entry.constant, source)} = [${entry.values.map(q).join(", ")}] as const;`, `export type ${claim(entry.typeName, source)} = (typeof ${entry.constant})[number];`);
|
|
1121
|
+
}
|
|
1122
|
+
for (const table of model.tables) for (const column of table.columns) if (column.values && !column.snapshot.isEnum) {
|
|
1123
|
+
const source = `the CHECK on ${table.snapshot.schema}.${table.snapshot.name}.${column.db}`;
|
|
1124
|
+
const constant = identifier(`${table.key}${pascal(column.app)}Values`.replace(/^[A-Z]/, (c) => c.toLowerCase()));
|
|
1125
|
+
const typeName = identifier(`${pascal(table.key)}${pascal(column.app)}`);
|
|
1126
|
+
lines.push(`export const ${claim(constant, source)} = [${column.values.map(q).join(", ")}] as const;`, `export type ${claim(typeName, source)} = (typeof ${constant})[number];`);
|
|
1127
|
+
}
|
|
1128
|
+
if (model.enums.length > 0) lines.push("");
|
|
1129
|
+
lines.push(...modelsType(model), "", ...functionsType(model), "", "export type TableName = keyof Models;", "/** Constraint names for `isConflict`, `isCheck` and `isForeignKey`. */", "export type UniqueConstraint = { [T in TableName]: Extract<keyof Models[T]['UniqueKeys'], string> }[TableName];", "export type CheckConstraint = Models[TableName]['Checks'];", "export type ForeignKeyConstraint = Models[TableName]['ForeignKeys'];", "export type RowOf<T extends TableName> = Models[T]['Row'];", "export type InsertOf<T extends TableName> = Models[T]['Insert'];", "export type UpdateOf<T extends TableName> = Models[T]['Update'];", "/** A `where` for `T` you can assign to one key at a time. */", "export type WhereOf<T extends TableName> = MutableWhere<Models, T>;", "/** An `orderBy` for `T`: one sort or a list, by column or to-one relation. */", "export type OrderByOf<T extends TableName> = OrderByArg<Models, T>;", "/** One sort term of `T`'s `orderBy`, by column or to-one relation. */", "export type OrderTermOf<T extends TableName> = OrderByInput<Models, T>;", "");
|
|
1130
|
+
if (model.meta.buckets) lines.push("/** Bucket configs for `defineBucket` from `better-supabase/storage`. */", `export const buckets = ${JSON.stringify(model.meta.buckets, null, 2)} as const;`, "");
|
|
1131
|
+
if (model.meta.topics) lines.push("/** Topic templates for `defineTopic` from `better-supabase/realtime`. */", `export const topics = ${JSON.stringify(model.meta.topics, null, 2)} as const;`, "");
|
|
1132
|
+
lines.push("export const schema: Schema<Models, Database, Functions> = defineSchema(meta);", "");
|
|
1133
|
+
return lines.join("\n");
|
|
1134
|
+
}
|
|
1135
|
+
//#endregion
|
|
1136
|
+
export { buildModel as a, toCatalog as c, metaPaths as i, emitMeta as n, generatorModel as o, emitModule as r, configFor as s, GENERATED_HEADER as t };
|