prisma-crystal

Prisma Crystal is a lightweight ORM for crystal-lang 🕶️

prisma-crystal 🕶️

Prisma Crystal is a lightweight ORM for crystal-lang.

It allows to make us of types from a prisma.schema in Crystal powered app/server.

Features

  • Use Prisma as a Crystal ORM
  • Write regular schema.prisma and enjoy!
  • Auto-generated crystal shard prisma_client
  • Auto-generated crystal models from schema
  • Auto-generated crystal client
  • Fluent query builder (Prisma-like)
  • Type-safety for free
  • Simple and easy to use
  • Tested using regular crystal spec tests
  • Usage of Prisma Studio to see/edit database in a GUI
  • Tooling like Prisma Migrate
  • Fairly transparent and fast, does not goes into your way

Installation

Git clone this repo while it's a prototype and not published.

... document install process ...
... document install process ...

Usage

  1. Set DATABASE_URL in .env file
  2. Run database initial migrations (Prisma migrate / micrate / etc)
  3. Run prisma generate to parse schema
  4. This generates shard prisma_client (using prisma generate)
  5. Enjoy!

Example

Take a look at src/server.cr for more examples!

// schema.prisma

generator client {
  provider = "prisma-client-crystal"
  output   = "./lib/prisma_client"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model User {
  id         Int       @id @default(autoincrement())
  name       String
  email      String    @unique
  role       String
  createdAt  DateTime  @default(now())
}

# server.cr
require "prisma_client" # generated earlier

client = PrismaClient::Client.new

user1 = client.users
  .where(["id = 1"])
  .find_one

user_unique = client.users
  .find_unique_by_id(1)

users = client.users
  .select(["id", "name", "role"])
  .limit(20)
  .order_by("DESC")
  .find_many

new_user = client.users
  .returning(["id", "name", "role"])
  .create({
    name: "New User",
    email: "newuser@email.com",
    role: "user",
  })

# or with upsert (update or insert)

new_user = client.users
  .returning(["id", "name", "role"])
  .upsert({
    id: 1,
    name: "NewUs3r8B",
    email: "newusee@email.com",
    role: "user",
  })

deleted = client.users
  .where(["id = 42"])
  .delete

Development

See the dev.sh shortcut which automates this:

  1. Edit example/prisma/.env to set DATABASE_URL (Postgres).
  2. Create DB and tables (See SQL below).
  3. Install generator deps: cd generator && yarn build; cd ..
  4. From repo root, run npx prisma generate --schema=example/prisma/schema.prisma if you have Prisma CLI and point the generator to local package (or manually run the generator: node generator/dist/index.js with proper env). For prototype, lib/ already contains generated files.
  5. Install Crystal shards: shards install
  6. Run Crystal server: crystal run src/server.cr

Create SQL tables

Example SQL (Postgres):

CREATE DATABASE prisma_crystal;
\c prisma_crystal
CREATE TABLE users (
  id serial primary key,
  email text UNIQUE NOT NULL,
  name text,
  "createdAt" timestamptz DEFAULT now()
);
CREATE TABLE posts (
  id serial primary key,
  title text NOT NULL,
  content text,
  published boolean default false,
  "authorId" int not null references users(id),
  "createdAt" timestamptz DEFAULT now()
);

Limitations

  • Prototype only; simple mapping and SQL generation.
  • Minimal typing.
  • Use carefully in production.

Author

License

This project is released under the MIT License.

Repository

prisma-crystal

Owner
Statistic
  • 1
  • 0
  • 0
  • 3
  • about 3 hours ago
  • April 24, 2026
License

Links
Synced at

Sun, 26 Apr 2026 06:16:22 GMT

Languages