| 1 | import { defineContract, model } from "@/Prisma/ORM/index.ts"; |
| 2 | import { |
| 3 | defineContract as nativeDefineContract, |
| 4 | model as nativeModel, |
| 5 | } from "@prisma/orm-postgres/contract-builder"; |
| 6 | import { expect, it } from "alchemy-test"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import { contract } from "./fixtures/client/contract.ts"; |
| 9 | |
| 10 | it.effect( |
| 11 | "uses native Prisma functions and runtime metadata unchanged", |
| 12 | () => |
| 13 | Effect.sync(() => { |
| 14 | expect(defineContract).toBe(nativeDefineContract); |
| 15 | expect(model).toBe(nativeModel); |
| 16 | expect( |
| 17 | contract.domain.namespaces.public.models.Post.relations.author, |
| 18 | ).toMatchObject({ |
| 19 | cardinality: "N:1", |
| 20 | nullable: false, |
| 21 | to: { namespace: "public", model: "User" }, |
| 22 | }); |
| 23 | expect( |
| 24 | contract.domain.namespaces.public.models.User.relations.posts, |
| 25 | ).toMatchObject({ |
| 26 | cardinality: "1:N", |
| 27 | to: { namespace: "public", model: "Post" }, |
| 28 | }); |
| 29 | expect( |
| 30 | contract.storage.namespaces.public.entries.table.user.columns.id, |
| 31 | ).toMatchObject({ |
| 32 | codecId: "pg/int4@1", |
| 33 | default: { kind: "function", expression: "autoincrement()" }, |
| 34 | }); |
| 35 | expect( |
| 36 | contract.storage.namespaces.public.entries.table.user.columns.email, |
| 37 | ).not.toHaveProperty("many"); |
| 38 | expect( |
| 39 | contract.storage.namespaces.public.entries.table.user.uniques, |
| 40 | ).toContainEqual(expect.objectContaining({ columns: ["email"] })); |
| 41 | }), |
| 42 | { tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"] }, |
| 43 | ); |
| 44 | |
| 45 | it.effect( |
| 46 | "preserves namespaces, defaults, and scalar lists", |
| 47 | () => |
| 48 | Effect.sync(() => { |
| 49 | const authored = defineContract( |
| 50 | { namespaces: ["auth"] }, |
| 51 | ({ field, model }) => ({ |
| 52 | models: { |
| 53 | Account: model("Account", { |
| 54 | namespace: "auth", |
| 55 | fields: { |
| 56 | id: field.int().id().defaultSql("1"), |
| 57 | tags: field.text().many(), |
| 58 | name: field.text().optional(), |
| 59 | }, |
| 60 | }).sql({ table: "account" }), |
| 61 | }, |
| 62 | }), |
| 63 | ); |
| 64 | expect( |
| 65 | authored.domain.namespaces.auth.models.Account.storage, |
| 66 | ).toMatchObject({ namespaceId: "auth", table: "account" }); |
| 67 | expect( |
| 68 | authored.storage.namespaces.auth.entries.table.account.columns.tags, |
| 69 | ).toMatchObject({ codecId: "pg/text@1", many: true }); |
| 70 | expect( |
| 71 | authored.storage.namespaces.auth.entries.table.account.columns.name, |
| 72 | ).toMatchObject({ nullable: true }); |
| 73 | }), |
| 74 | { tags: ["unit", "provider:prisma", "provider:prisma:orm", "local"] }, |
| 75 | ); |