Quotura
Quotura is launching soon. Sign-up opens at launch; until then, these pages describe the service as it will run. Get notified

Facet ORM

Facet is a schema-first TypeScript ORM for PostgreSQL and SQLite. You describe models in schema.facet, run facet generate to get a typed client, and query with findMany, create, nested writes and transactions. facet migrate creates and applies SQL migrations and detects drift. It works with any Postgres, Quotura or not.

Last reviewed

Install

Facet needs Node.js 22.13 or newer and has no dependencies of its own. Add pg for Postgres over TCP.

npm install facet-orm pg
npx facet init                 # writes schema.facet and .env
npx facet migrate dev --name init

Facet is published to npm at launch. After upgrading it, run facet generate again.

The schema

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

generator client {
  output = "./generated"
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  posts     Post[]
  createdAt DateTime @default(now())
}

model Post {
  id       String @id @default(uuid())
  title    String
  author   User   @relation(fields: [authorId], references: [id], onDelete: Cascade)
  authorId Int

  @@index([authorId])
}
  • Types: String Int BigInt Float Decimal Boolean DateTime Json and enums; ? makes a field optional.
  • Attributes: @id @unique @default(...) @updatedAt @map @db @relation and @@id @@unique @@index @@map.
  • Relations: one-to-one, one-to-many, many-to-many through an explicit join model, and self-relations.

The client

import pg from 'pg';
import { FacetClient, postgres } from './generated/index.js';

const db = new FacetClient({ adapter: postgres(new pg.Pool({ connectionString: process.env.DATABASE_URL })) });

const user = await db.user.create({ data: { email: 'ada@example.com', posts: { create: [{ title: 'Hello' }] } }, include: { posts: true } });
const feed = await db.post.findMany({ where: { title: { contains: 'hello', mode: 'insensitive' } }, orderBy: { title: 'asc' }, take: 20 });

await db.$transaction(async (tx) => {
  await tx.user.update({ where: { id: user.id }, data: { name: 'Ada' } });
}, { isolationLevel: 'Serializable' });
  • Methods: findMany findFirst findUnique (and OrThrow), create createMany update updateMany upsert delete deleteMany count aggregate groupBy.
  • Filters: equals not in notIn lt lte gt gte contains startsWith endsWith, mode: 'insensitive', AND OR NOT, and some every none is isNot on relations.
  • Relations load with one batched query per level, so there is no N+1 problem. Cursor pagination and distinct are supported.
  • Transactions: interactive or as an array of queries; nested transactions become savepoints; isolationLevel on Postgres.
  • Raw SQL: $queryRaw and $executeRaw as tagged templates, always parameterized.

Migrations

CommandWhat it does
facet migrate dev --name xCreates a migration from schema changes, applies it and regenerates the client
facet migrate deployApplies pending migrations (CI and production), under an advisory lock
facet migrate statusShows applied and pending migrations, and drift from them
facet db pullWrites schema.facet from an existing database, to adopt it
facet migrate resolve --applied xMarks a migration as applied without running it (baselining)

Renaming a field or model migrates as drop and add, with a warning, so edit the SQL by hand for a rename.

Compared with Prisma

Facet covers the core workflow and most of the query API. It does not have scalar lists, implicit many-to-many relations, JSON path filters, full-text search, or MySQL, SQL Server and MongoDB support. Decimal values come back as strings and BigInt as bigint. See Prisma vs Facet and moving from Prisma.