Git Product home page Git Product logo

pentagon's Introduction

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

  • No codegen required, everything is inferred using Zod and TypeScript
  • All same functions as Prisma supported (not all yet implemented)
  • Support for include
  • Support for select
  • Pagination (todo)

๐Ÿ’ป Example usage

import { z } from "https://deno.land/x/[email protected]/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:

  • Clone this repository
  • Fix types / add feature
  • Run the tests using deno test --unstable
  • Open PR

Running tests

$ deno test --unstable

License

Made with โค๏ธ in Helsinki, Finland.

Published under MIT License.

pentagon's People

Contributors

jnssnmrcs avatar lilnasy avatar skoshx avatar waptik avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.