import { createPentagon } from "https://deno.land/x/pentagon@v0.1.3/mod.ts";
import { z } from "zod";
import { kv } from "$utils/db/kv.ts";
const WithDefautTimestamps = z.object({
createdAt: z
.string()
.datetime()
.default(() => new Date().toISOString()),
updatedAt: z
.string()
.datetime()
.default(() => new Date().toISOString())
.nullable()
.nullable(),
});
const WithDefaultId = z.object({
id: z
.string()
.uuid()
.default(() => crypto.randomUUID())
.describe("primary"),
});
const UserModel = z
.object({
id: z.number().describe("primary"),
firstName: z.string(),
lastName: z.string().optional(),
username: z.string().describe("unique").optional(),
role: z.enum(["admin", "user", "root"]).default("user"),
})
.merge(WithDefautTimestamps);
const ProjectModel = WithDefaultId.extend({
name: z.string(),
description: z.string().optional(),
plan: z.string().default("free"),
}).merge(WithDefautTimestamps);
const ProjectUserModel = WithDefaultId.extend({
role: z.enum(["owner", "member"]).default("member"),
// relations
projectId: z.string().uuid().describe("unique"),
userId: z.number().describe("unique"),
}).merge(WithDefautTimestamps);
export const pentagon = createPentagon(kv, {
users: {
schema: UserModel,
relations: {
projects: ["projectUsers", [ProjectUsersModel], "??", "??"], // how to fix "?"
},
},
projects: {
schema: ProjectModel,
relations: {
// name: [relation name, schema, local key, foreign key]
users: ["projectUsers", [ProjectUsersModel], "??", "??"], // how to fix "?"
},
},
projectUsers: {
schema: ProjectUsersModel,
relations: {
project: ["project", ProjectModel, "projectId", "id"],
user: ["user", UserModel, "userId", "id"],
},
},
});
Let's say i have the following prisma schema and queries, how do i achieve it with pentagon at this current stage?
Prisma schema
Queries
So here are model definitions so far:
Pentagon model definitions
So looking at the prisma schema definition and the model definitions using
zod, how do i properly define the relations betweenusers,projectsandprojectUsersand easily replicate the queries?