skoshx / pentagon

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.
https://dash.deno.com/playground/pentagon-demo
MIT License
155 stars 5 forks source link
deno denokv orm prisma typescript

Sponsored by
Flytrap
A better way to understand your production bugs.

See all the data flowing through your code leading up to bugs. Flytrap allows you to fix bugs in production in a matter of minutes, instead of days.

pentagon

Test Github Action Lint Github Action

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.

Features

💻 Example usage

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 */
  },
});

Relations

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"],
  },
},

💻 Development

Help is always appreciated, especially with getting the types right! Here's how you can contribute:

Running tests

$ deno test --unstable

License

Made with ❤️ in Helsinki, Finland.

Published under MIT License.