Git Product home page Git Product logo

kysely-codegen's Introduction

kysely-codegen

kysely-codegen generates Kysely type definitions from your database. That's it.

Table of contents

Installation

npm install --save-dev kysely-codegen

# You will also need to install Kysely with your driver of choice:
npm install kysely pg
npm install kysely mysql2
npm install kysely better-sqlite3
npm install @libsql/kysely-libsql

Generating type definitions

The most convenient way to get started is to create an .env file with your database connection string:

# PostgreSQL
DATABASE_URL=postgres://username:[email protected]/database

# MySQL
DATABASE_URL=mysql://username:[email protected]/database

# SQLite
DATABASE_URL=C:/Program Files/sqlite3/db

# LibSQL
DATABASE_URL=libsql://token@host:port/database

If your URL contains a password with special characters, those characters may need to be percent-encoded.

If you are using PlanetScale, make sure your URL contains this SSL query string parameter: ssl={"rejectUnauthorized":true}

Then run:

kysely-codegen

This command will generate a .d.ts file from your database, for example:

import { ColumnType } from 'kysely';

export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
  ? ColumnType<S, I | undefined, U>
  : ColumnType<T, T | undefined, T>;

export type Timestamp = ColumnType<Date, Date | string, Date | string>;

export interface Company {
  id: Generated<number>;
  name: string;
}

export interface User {
  company_id: number | null;
  created_at: Generated<Timestamp>;
  email: string;
  id: Generated<number>;
  is_active: boolean;
  name: string;
  updated_at: Timestamp;
}

export interface DB {
  company: Company;
  user: User;
}

For more options, run kysely-codegen --help.

Using the type definitions

Import DB into new Kysely<DB>, and you're done!

import { Kysely, PostgresDialect } from 'kysely';
import { DB } from 'kysely-codegen';
import { Pool } from 'pg';

const db = new Kysely<DB>({
  dialect: new PostgresDialect({
    pool: new Pool({
      connectionString: process.env.DATABASE_URL,
    }),
  }),
});

const rows = await db.selectFrom('user').selectAll().execute();
//    ^ { company_id: number | null; created_at: Date; email: string; id: number; ... }[]

kysely-codegen's People

Contributors

robinblomberg avatar jacobwgillespie avatar bombillazo avatar igalklebanov avatar woltsu avatar boscodomingo avatar yongtheskill avatar seivan avatar thdxr avatar antoineneff avatar mtt-artis avatar magnusrodseth avatar egust avatar vaaralav avatar valtyr avatar spa5k avatar mathieunls avatar lbguilherme avatar fcamblor avatar diogob avatar devvypaws avatar devunt avatar andrew-w-ross avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.