Skip to content

What is monolayer?

monolayer-pg is a PostgreSQL database schema manager for TypeScript projects built on top of kysely with:


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!