What is monolayer-pg?
monolayer-pg is a PostgreSQL database schema manager for TypeScript projects built on top of kysely
with:
- Declarative, type-safe database schema definition.
- Type-safe database client(s) for
kysely
(no codegen 🎉). - Schema management without migrations.
- Support for multiple databases.
- Support for multiple schemas per database.
- Effortless
Prisma
integration. - Detailed warnings on backwards-incompatible, blocking,unsafe, or destructive changes.
- Data migrations decoupled from your schema management.
- Comprehensive Zod validations for all supported data types (no codegen 🎉) for
kysely
.
Here's a taste of how you define database schemas with monolayer-pg
:
ts
const users = table({
columns: {
id: integer().generatedAlwaysAsIdentity(),
email: text().notNull(),
name: text(),
},
constraints: {
primaryKey: primaryKey(["id"]),
unique: [unique(["email"])],
},
indexes: [index(["email"])],
});
const posts = table({
columns: {
id: integer().generatedAlwaysAsIdentity(),
title: text().notNull(),
content: text(),
published: boolean().default(false),
authorId: integer(),
createdAt: timestampWithTimeZone().notNull().default(sql`now()`),
updatedAt: timestampWithTimeZone().notNull().default(sql`now()`),
},
constraints: {
primaryKey: primaryKey(["id"]),
foreignKeys: [
foreignKey(["authorId"], users, ["id"])
.deleteRule("set null")
.updateRule("cascade"),
],
},
indexes: [index(["authorId"])],
});
export const dbSchema = schema({
tables: {
users,
posts,
},
});
Head over to the Quickstart and begin using monolayer-pg
!