GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • ZenStack
  • Get Started
  • Reference
  • Blog
  • Star on Github
  • Discord
  • 3.x
  • 2.x
  • Prerequisite
  • Quick Start
  • Data Modeling
  • ORM
  • Using the CLI
  • Query API

This view

  • Original page
  • New address

GeezerVison is showing zenstack.dev — Database Client | ZenStack

Version: 3.x

Database Client

🔋 ZenStack vs Prisma

Unlike Prisma, ZenStack doesn't bundle any database driver. You're responsible for installing a compatible one. Also it doesn't read database connection string from the schema. Instead, you pass in the connection information when creating the client.

The zen generate command compiles the ZModel schema into TypeScript code, which we can in turn use to initialize a type-safe database client. ZenStack uses Kysely to handle the low-level database operations, so the client is initialize with a Kysely dialect - an object that encapsulates database details. For convenience, a few commonly used Kysely dialects (e.g., SqliteDialect and PostgresDialect) are reexported from @zenstackhq/orm/dialects.

The samples below show creating a client using SQLite (via better-sqlite3) and PostgreSQL (via node-postgres). You can also use any other Kysely dialects for these two types of databases.

  • SQLite
  • PostgreSQL
  • MySQL
  • npm
  • pnpm
  • bun
  • yarn
npm install better-sqlite3
npm install --save-dev @types/better-sqlite3
pnpm add better-sqlite3
pnpm add --save-dev @types/better-sqlite3
bun add better-sqlite3
bun add --dev @types/better-sqlite3
yarn add better-sqlite3
yarn add --dev @types/better-sqlite3
db.ts
import { ZenStackClient } from '@zenstackhq/orm';
import { SqliteDialect } from '@zenstackhq/orm/dialects/sqlite';
import SQLite from 'better-sqlite3';
import { schema } from './zenstack/schema';

export const db = new ZenStackClient(schema, {
dialect: new SqliteDialect({
database: new SQLite(':memory:'),
}),
});
  • npm
  • pnpm
  • bun
  • yarn
npm install pg
npm install --save-dev @types/pg
pnpm add pg
pnpm add --save-dev @types/pg
bun add pg
bun add --dev @types/pg
yarn add pg
yarn add --dev @types/pg
db.ts
import { ZenStackClient } from '@zenstackhq/orm';
import { PostgresDialect } from '@zenstackhq/orm/dialects/postgres';
import { schema } from './zenstack/schema';
import { Pool } from 'pg';

export const db = new ZenStackClient(schema, {
dialect: new PostgresDialect({
pool: new Pool({
connectionString: process.env.DATABASE_URL,
}),
}),
});
  • npm
  • pnpm
  • bun
  • yarn
npm install mysql2
pnpm add mysql2
bun add mysql2
yarn add mysql2
db.ts
import { ZenStackClient } from '@zenstackhq/orm';
import { MysqlDialect } from '@zenstackhq/orm/dialects/mysql';
import { schema } from './zenstack/schema';
import { createPool } from 'mysql2';

export const db = new ZenStackClient(schema, {
dialect: new MysqlDialect({
pool: createPool(process.env.DATABASE_URL),
}),
});

The created db object has the full ORM API inferred from the type of the schema parameter. When necessary, you can also explicitly get the inferred client type like:

import type { ClientContract } from '@zenstackhq/orm';
import type { SchemaType } from '@/zenstack/schema';

export type DbClient = ClientContract<SchemaType>;
Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.