Skip to content

Latest commit

 

History

History
127 lines (103 loc) · 3.77 KB

README.md

File metadata and controls

127 lines (103 loc) · 3.77 KB

Zapatos Interface Generator

This is a fork of Zapatos that makes zapatos generate interfaces instead of a global declared module.

The motivation for this was to be able to use Zapatos's rich generated types in npm modules, and with Kysely.

Installation

npm install --save-dev zapatos-interface-generator

Usage

Use zapatos as normal and generate a schema.d.ts file. When you look at the schema file, it will be a bit different, there aren't any global declarations, so you can generate multiple of these files or bundle them into a package without anything going wrong

Usage with Kysely

Use your generated zapatos bindings to create a Kysely type that works for your database:

import type { ColumnType, Kysely } from "kysely"
import * as schema from "./zapatos/schema"
import { type Primitive } from "type-fest"
import { type JSONValue } from "zapatos/db"

type ZapatosInsertableTypeToPrimitive<T> = Exclude<
  Extract<T, Primitive | Date | JSONValue>,
  symbol
>

type ZapatosTableNameToKyselySchema<T extends schema.Table> = {
  [K in keyof schema.SelectableForTable<T>]: ColumnType<
    ZapatosInsertableTypeToPrimitive<schema.SelectableForTable<T>[K]>,
    K extends keyof schema.InsertableForTable<T>
      ? ZapatosInsertableTypeToPrimitive<schema.InsertableForTable<T>[K]>
      : never,
    K extends keyof schema.UpdatableForTable<T>
      ? ZapatosInsertableTypeToPrimitive<schema.UpdatableForTable<T>[K]>
      : never
  >
}

export type MyKyselySchema = {
  readonly [T in schema.Table]: ZapatosTableNameToKyselySchema<T>
}

export type MyKysely = Kysely<AirbyteGithubSchema>

(maybe we should generate this file too)

Example schema.d.ts file

export interface foo {
  Table: 'foo';
  Selectable: {
    /**
    * **foo.id**
    * - \`int4\` in database
    * - \`NOT NULL\`, default: \`nextval('foo_id_seq'::regclass)\`
    */
    id: number;
    /**
    * **foo.name**
    * - \`text\` in database
    * - \`NOT NULL\`, no default
    */
    name: string;
  };
  // ...
}

export interface Schema_public {  
  Table: bar["Table"] | foo["Table"];
  Selectable: bar["Selectable"] | foo["Selectable"];
  JSONSelectable: bar["JSONSelectable"] | foo["JSONSelectable"];
  Whereable: bar["Whereable"] | foo["Whereable"];
  Insertable: bar["Insertable"] | foo["Insertable"];
  Updatable: bar["Updatable"] | foo["Updatable"];
  UniqueIndex: bar["UniqueIndex"] | foo["UniqueIndex"];
  Column: bar["Column"] | foo["Column"];
  
  AllBaseTables: [bar["Table"], foo["Table"]];
  AllForeignTables: [];
  AllViews: [];
  AllMaterializedViews: [];
  AllTablesAndViews: [bar["Table"], foo["Table"]];
}

export type Schema = 'public';
export type Table = Schema_public["Table"];
export type Selectable = Schema_public["Selectable"];
export type JSONSelectable = Schema_public["JSONSelectable"];
export type Whereable = Schema_public["Whereable"];
export type Insertable = Schema_public["Insertable"];
export type Updatable = Schema_public["Updatable"];
export type UniqueIndex = Schema_public["UniqueIndex"];
export type Column = Schema_public["Column"];

export type AllSchemas = ['public'];
export type AllBaseTables = [...Schema_public["AllBaseTables"]];
export type AllForeignTables = [...Schema_public["AllForeignTables"]];
export type AllViews = [...Schema_public["AllViews"]];
export type AllMaterializedViews = [...Schema_public["AllMaterializedViews"]];
export type AllTablesAndViews = [...Schema_public["AllTablesAndViews"]];

export type SelectableForTable<T extends Table> = {
  "bar": bar["Selectable"];
  "foo": foo["Selectable"];
}[T];

export type JSONSelectableForTable<T extends Table> = {
  "bar": bar["JSONSelectable"];
  "foo": foo["JSONSelectable"];
}[T];

For zapatos details and documentation, see https://jawj.github.io/zapatos/