Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type-safe seed file generation #42

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

igalklebanov
Copy link
Member

@igalklebanov igalklebanov commented Jun 19, 2024

closes #40.

Hey 馃憢

Seed files, unlike migrations, need to evolve with the codebase and the database schema. Thus, they have to be type-safe and bound to the latest Database interface. But, kysely seed make uses Kysely<any>.

This PR adds the ability to generate type-safe seed files.
Adds a databaseInterfacePath prop @ config.seeds, with the given structure:

<file-path-relative-to-seed-folder-or-node-modules>[#<database-type-name>]

If kysely-codegen is detected, it defaults to using DB from kysely-codegen

import type { DB } from 'kysely-codegen'
import type { Kysely } from 'kysely'

export async function seed(db: Kysely<DB>): Promise<void> {
	// seed code goes here...
	// note: this function is mandatory. you must implement this function.
}

, otherwise uses a non-type-safe template with Kysely<any>.

import type { Kysely } from "kysely";

export async function seed(db: Kysely<any>): Promise<void> {
  // seed code goes here...
  // note: this function is mandatory. you must implement this function.
}

If a path is provided without a database name prefix (#DBName), and prisma-kysely is installed, Database interface name defaults to DB.

import type { DB } from 'path/to/prisma/types'
import type { Kysely } from 'kysely'

export async function seed(db: Kysely<DB>): Promise<void> {
	// seed code goes here...
	// note: this function is mandatory. you must implement this function.
}

@igalklebanov igalklebanov added enhancement New feature or request seed Seed module related labels Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request seed Seed module related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Kysely<Database> @ seed make.
1 participant