| 1 | import { defineContract } from "@/Prisma/ORM/index.ts"; |
| 2 | |
| 3 | export const contract = defineContract( |
| 4 | { extensions: {}, foreignKeyDefaults: { constraint: true, index: true } }, |
| 5 | ({ field, model, rel }) => { |
| 6 | const User = model("User", { |
| 7 | fields: { |
| 8 | id: field |
| 9 | .int() |
| 10 | .id() |
| 11 | .default({ kind: "function", expression: "autoincrement()" }), |
| 12 | email: field.text().unique(), |
| 13 | name: field.text().optional(), |
| 14 | }, |
| 15 | }).sql({ table: "user" }); |
| 16 | const Post = model("Post", { |
| 17 | fields: { |
| 18 | id: field |
| 19 | .int() |
| 20 | .id() |
| 21 | .default({ kind: "function", expression: "autoincrement()" }), |
| 22 | title: field.text(), |
| 23 | authorId: field.int(), |
| 24 | }, |
| 25 | relations: { |
| 26 | author: rel.belongsTo(User, { from: "authorId", to: "id" }).sql({ |
| 27 | fk: { constraint: true, index: true }, |
| 28 | }), |
| 29 | }, |
| 30 | }).sql({ table: "post" }); |
| 31 | return { |
| 32 | models: { |
| 33 | User: User.relations({ posts: rel.hasMany(Post, { by: "authorId" }) }), |
| 34 | Post, |
| 35 | }, |
| 36 | }; |
| 37 | }, |
| 38 | ); |