Skip to content

About

Generate Effect schemas compatible with Kysely from Prisma models

Topics

Resources

Contributing

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

264 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prisma-effect-kysely

Prisma 8 PostgreSQL contract generator for stable Effect 4 database codecs and native Kysely table contracts.

Install

Generated files import both required peers directly: effect@^4.0.3 and kysely@^0.29.6.

bun add prisma-effect-kysely effect@^4.0.3 kysely@^0.29.6

Setup

Generate from Prisma 8's emitted PostgreSQL contract:

bunx prisma contract emit
bunx prisma-effect-kysely contract \
  --contract ./generated/prisma/contract.json \
  --schema ./prisma/contract.prisma \
  --output ./generated/effect

The schema path preserves /// @customType(...) expressions because Prisma 8 does not include documentation comments in contract.json.

The output directory contains enums.ts, types.ts, and index.ts. Generated relative imports and re-exports use .js specifiers so TypeScript can resolve the sources with module and moduleResolution set to NodeNext, and Node can load the compiled ESM without a bundler. In an ESM package ("type": "module"), import the public entrypoint as ./generated/effect/index.js; it exports the model codecs, enums, and Kysely DB and table types.

Pass --multi-domain to write those three files for each Prisma namespace at <output>/<namespace>/src/generated. Every domain writes enums.ts, and each types.ts declares every ID brand it references. It does not scaffold libraries or projects.

Each Prisma enum emits a native TypeScript enum for named application values and a PascalCase Effect codec. If both names would collide, the TypeScript enum gets an Enum suffix, such as StatusEnum.ACTIVE, while Status remains the codec.

Generated output

Each model has select, insert, and update codecs backed by one private VariantSchema field definition. Codec Type values use Prisma's semantic field names with driver-native leaves; codec Encoded values use physical database keys with those same leaves. Kysely table interfaces emit the physical keys explicitly while indexing the semantic Type leaves so ID and custom brands survive.

import { Schema } from 'effect';
import { VariantSchema } from 'effect/schema';
import type { ColumnType } from 'kysely';

const DatabaseSchema = VariantSchema.make({
  variants: ['select', 'insert', 'update'],
  defaultVariant: 'select',
});

export const UserId = Schema.String.check(Schema.isUUID()).pipe(Schema.brand('UserId'));
export type UserId = typeof UserId.Type;

const UserFields = DatabaseSchema.Struct({
  id: DatabaseSchema.Field({
    select: UserId,
    insert: Schema.optionalKey(UserId),
  }),
  email: DatabaseSchema.Field({
    select: Schema.String,
    insert: Schema.String,
    update: Schema.optionalKey(Schema.String),
  }),
});

export const User = DatabaseSchema.extract(UserFields, 'select');
export type User = typeof User.Type;
export const UserInsert = DatabaseSchema.extract(UserFields, 'insert');
export type UserInsert = typeof UserInsert.Type;
export const UserUpdate = DatabaseSchema.extract(UserFields, 'update');
export type UserUpdate = typeof UserUpdate.Type;

export interface UserTable {
  id: ColumnType<(typeof User.Type)['id'], (typeof UserInsert.Type)['id'], never>;
  email: ColumnType<
    (typeof User.Type)['email'],
    (typeof UserInsert.Type)['email'],
    Exclude<(typeof UserUpdate.Type)['email'], undefined>
  >;
}

export interface DB {
  User: UserTable;
}

For @map, codec Type values keep Prisma's semantic field name while codec Encoded values and Kysely interfaces use the physical column name. @@map likewise controls the DB table key.

If multiple namespaces use the same physical table name, single-output mode qualifies only those colliding DB keys as <namespace>.<table>. Multi-domain output keeps bare table names because each namespace has its own DB.

Consumer usage

import { Kysely, type Insertable, type Selectable, type Updateable } from 'kysely';
import type { DB } from './generated/effect/index.js';

const db = new Kysely<DB>({ dialect });

type UserRow = Selectable<DB['User']>;
type NewUserRow = Insertable<DB['User']>;
type UserPatch = Updateable<DB['User']>;

const row: UserRow = await db.selectFrom('User').selectAll().executeTakeFirstOrThrow();
const insert: NewUserRow = { email: 'user@example.com' };
const update: UserPatch = { email: 'next@example.com' };

Kysely rows retain physical database keys, driver-native leaves, and generated ID/custom brands. PostgreSQL BigInt is native bigint in Kysely and generated codec Type/Encoded values. Generated codecs validate values and map @map keys, but they do not coerce scalar values before or after a query.

await db.insertInto('User').values(insert).execute();

Field and default ownership

  • Nullable fields and database-owned defaults are optional on insert.
  • Database-owned defaults are scalar/array literals or autoincrement(), dbgenerated(), and now().
  • Prisma Client defaults (uuid(), cuid(), ulid(), and nanoid()), missing DMMF default payloads, unknown functions, and bare @updatedAt remain required on insert. Kysely does not run Prisma Client defaults.
  • A field with @updatedAt and a recognized database default is optional on insert and remains updateable.
  • Every primary-key component, including every field in a composite @@id, is absent from update codecs and has Kysely update type never.
  • Optional values use Schema.NullOr; omission and explicit null are distinct, and explicit undefined is rejected.
  • Relations and unsupported fields are excluded. Foreign-key scalars use the target model's branded ID schema.

Type mappings

Prisma / PostgreSQL codec Generated codec and Kysely leaf
String Schema.String / string
UUID string Schema.String.check(Schema.isUUID()) / string
Int Schema.Int / number
Float Schema.Number / number
BigInt Schema.BigInt / bigint
Decimal Schema.String / string
PostgreSQL date Schema.String / string
PostgreSQL time Schema.String / string
PostgreSQL timestamp native Date
PostgreSQL timestamptz native Date
PostgreSQL interval { months, days, micros }
Json Schema.Json / JSON value
Bytes Schema.Uint8Array / Uint8Array
Enum Schema.Enum(...) / named stored enum member

Incoming Prisma *-temporal contract identifiers map directly to PostgreSQL driver-native values: date/time are strings, timestamp/timestamptz are Date, and interval is { months, days, micros }. Generated codecs do not perform value coercion.

UUID detection

A string column is a UUID only when Prisma's DMMF reports @db.Uuid, either through field.nativeType or the /// @db.Uuid documentation marker. Field-name inference is intentionally unsupported because external text IDs such as Stripe acct_… and cus_… values are not UUIDs.

Custom type overrides

@customType(...) defines only the scalar refinement and must be an Effect 4 expression already in generated scope. The generator applies Prisma list and nullability metadata around it:

model User {
  /// @customType(Schema.String.pipe(Schema.brand('EmailAddress')))
  email String @unique

  /// @customType(Schema.Number.pipe(Schema.brand('PositiveInt')))
  age Int

  /// @customType(Schema.Number.pipe(Schema.brand('Coordinate')))
  coordinates Int[]
}

For example, String? @customType(Schema.String.pipe(Schema.brand('EmailAddress'))) emits Schema.NullOr(Schema.String.pipe(Schema.brand('EmailAddress'))), while Int[] @customType(Schema.Number.pipe(Schema.brand('Coordinate'))) emits Schema.Array(Schema.Number.pipe(Schema.brand('Coordinate'))). Do not put Schema.NullOr or Schema.Array in the annotation.

Join models

Prisma 8 contract models are emitted as ordinary models, including explicit join models whose physical table names begin with _. They receive the standard select, insert, update, table-interface, and DB entries; table names alone never cause model removal or relation synthesis.

Package exports

The package exposes the prisma-effect-kysely contract generator executable. Generated application code never imports prisma-effect-kysely at runtime.

Development

bun install
bun run test
bun run typecheck
bun run build
bun run prepublishOnly

Type checking and builds run TypeScript 7.0.2 through @typescript/native. The unscoped typescript 6 dependency supplies the compiler API required by typescript-eslint because TypeScript 7.0 intentionally ships no programmatic API; it is not the active compiler.

Releasing

Uses Changesets:

bun changeset
git add .changeset/ && git commit -m "docs: changeset"
git push

CI opens a Version Packages PR. Merging it runs prepublishOnly, packs once, and publishes the checked tarball with npm trusted publishing and provenance. CI compares the published version and integrity with that tarball before creating the git tag and GitHub release. Stable releases use latest; Changesets pre mode uses its configured tag.

Configure npm's trusted publisher for this repository and .github/workflows/release.yml (and the environment, if one is configured). The workflow requires no npm token. A publish that succeeds but fails the registry verification needs manual investigation before retrying or creating its tag.

Registry verification allows up to 120 checks, ten seconds apart, for npm processing. Publishing is not retried; an integrity mismatch still fails the release.

License

MIT

About

Generate Effect schemas compatible with Kysely from Prisma models

Topics

Resources

Contributing

Stars

3 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages