Sponsored by
A better way to understand your production bugs.
Prisma like ORM built on top of Deno KV. Allows you to write your database schemas and relations using Zod schemas, and run queries using familiar syntax from Prisma.
include
select
import { z } from "https://deno.land/x/zod@v3.21.4/mod.ts";
import { createPentagon } from "https://deno.land/x/pentagon/mod.ts";
const kv = await Deno.openKv();
export const User = z.object({
id: z.string().uuid().describe("primary"),
createdAt: z.date(),
name: z.string(),
});
export const Order = z.object({
id: z.string().uuid().describe("primary"),
createdAt: z.date(),
name: z.string(),
userId: z.string().uuid(),
});
const db = createPentagon(kv, {
users: {
schema: User,
relations: {
myOrders: ["orders", [Order], "id", "userId"],
},
},
orders: {
schema: Order,
relations: {
user: ["users", User, "userId", "id"],
},
},
});
// Now we have unlocked the magic of Pentagon
const user = await db.users.findFirst({
where: { name: "John Doe" },
select: { name: true, id: true },
});
// We can also do `include` queries, fully typed!
const userWithOrders = await db.users.findFirst({
where: { name: "John Doe" },
include: {
myOrders: true, // 👈 if we want the whole object
/* myOrders: { name: true }, 👈 if we want just some parts to be included */
},
});
Defining relations works by defining the relations
key in the table
definition. This allows us to include
the values of relations, the same way as
we are familiar with from Prisma.
The type signature RelationDefinition explains what each of the array values represent.
Basically, we have [relation name, schema, local key, foreign key]
.
For instance, a many-to-one relation could look like this:
users: {
schema: User,
relations: {
myOrders: ["orders", [Order], undefined, "userId"],
},
},
orders: {
schema: Order,
relations: {
user: ["users", User, "userId", "id"],
},
},
Help is always appreciated, especially with getting the types right! Here's how you can contribute:
deno test --unstable
$ deno test --unstable
Made with ❤️ in Helsinki, Finland.
Published under MIT License.