npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kysely-codegen

v0.15.0

Published

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

Downloads

551,316

Readme

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:

# PostgreSQL
npm install kysely pg

# MySQL
npm install kysely mysql2

# SQLite
npm install kysely better-sqlite3

# MSSQL
npm install kysely tedious tarn @tediousjs/connection-string

# LibSQL
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

# MSSQL
DATABASE_URL=Server=mssql;Database=database;User Id=user;Password=password

# 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;
}

Include/exclude patterns

You can choose which tables should be included during code generation by providing a glob pattern to the --include-pattern and --exclude-pattern flags. We use micromatch under the hood which provides advanced glob support. For instance, if you only want to include your public tables:

kysely-codegen --include-pattern="public.*"

You can also include only certain tables within a schema:

kysely-codegen --include-pattern="public.+(user|post)"

Or exclude an entire class of tables:

kysely-codegen --exclude-pattern="documents.*"

Help

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; ... }[]

Issue funding

We use Polar.sh to upvote and promote specific features that you would like to see implemented. Check the backlog and help out: