Command-line tool for Kysely
npm install kysely-ctl




!GitHub contributors

###### Join the discussion ⠀⠀⠀⠀⠀⠀⠀


kysely-ctl is the official command-line tool for Kysely.
We strive to make it TypeScript-first, cross-platform
(macOS, Linux, and Windows),
cross-runtime (Node.js, Bun, and Deno),
and cross-module system (ESM
and CommonJS) compatible.
We also aim to have feature parity with Knex.js's CLI.
kysely-ctl requires kysely >= 0.18.1 to be installed in your project/s.
When installed globally,
kysely-ctl will be available as kysely in your terminal,
anywhere.
#### Node.js:
``bash`
npm i -g kysely-ctl
or:
`bash`
pnpm add -g kysely-ctl
#### Bun
`bash`
bun add -g kysely-ctl
#### Deno
`bash`
deno install -g -f npm:kysely-ctl@latest
Alternatively, you can install
kysely-ctl in your project as a dev dependency,
which will make it available as kysely in your project's package.json:
#### Node.js:
`bash`
npm i -D kysely-ctl
or:
`bash`
yarn add -D kysely-ctl
or:
`bash`
pnpm add -D kysely-ctl
#### Bun
`bash`
bun add -D kysely-ctl
#### Deno
`bash`
deno add -D npm:kysely-ctl
Currently, a
kysely.config.ts file is required, in the project root OR .config
folder. Run kysely init in your terminal to create one.
`ts
import { defineConfig } from "kysely-ctl";
export default defineConfig({
destroyOnExit, // optional. dictates whether the (resolved) kysely instance should be destroyed when a command is finished executing. default is true.Kysely
dialect, // a dialect instance OR the name of an underlying driver library (e.g. 'pg').dialect
dialectConfig, // optional. when is the name of an underlying driver library, dialectConfig is the options passed to the Kysely dialect that matches that library..js
migrations: { // optional.
allowJS, // optional. controls whether , .cjs or .mjs migrations are allowed. default is false.migrate make
getMigrationPrefix, // optional. a function that returns a migration prefix. affects command. default is () => ${Date.now()}_.Kysely
migrationFolder, // optional. path to where migration files are located. default is a folder named "migrations" next to the config file/folder.
migrator, // optional. a migrator instance factory of shape (db: Kysely. default is Kysely's Migrator.Kysely
provider, // optional. a migration provider instance. default is kysely-ctl's TSFileMigrationProvider.Kysely
},
plugins, // optional. plugins list. default is []..js
seeds: { // optional.
allowJS, // optional. controls whether , .cjs or .mjs seeds are allowed. default is false.seed make
getSeedPrefix, // optional. a function that returns a seed prefix. affects command. default is () => ${Date.now()}_.kysely-ctl
provider, // optional. a seed provider instance. default is 's FileSeedProvider.(db: Kysely
seeder, // optional. a seeder instance factory of shape . default is kysely-ctl's Seeder.`
seedFolder, // optional. path to where seed files are located. default is a folder named "seeds" next to the config file/folder.
}
});
#### Reuse Kysely instance defined elsewhere
You can pass a Kysely instance, instead of dialect, dialectConfig & plugins:
`ts
import { defineConfig } from "kysely-ctl";
import { kysely } from 'path/to/kysely/instance';
export default defineConfig({
destroyOnExit, // optional. dictates whether the kysely instance should be destroyed when a command is finished executing. default is true.`
// ...
kysely,
// ...
});
#### Custom migration/seed file prefixes
To use Knex's timestamp prefixes:
`ts
import { defineConfig, getKnexTimestampPrefix } from "kysely-ctl";
export default defineConfig({
// ...
migrations: {
// ...
getMigrationPrefix: getKnexTimestampPrefix,
// ...
},
// ...
});
`
#### Extending configuration
See c12 docs and the following example.
#### Environment-specific configuration
See c12 docs and the following example.
For more information run kysely -h in your terminal.
#### Migrate
The
migrate module mirrors Knex.js CLI's module of the
same name.
`bash`
knex migrate:
Can now be called as either:
`bash`
kysely migrate:
or
`bash`
kysely migrate
> [!NOTE]
> rollback without --all flag is not supported, as Kysely
doesn't keep track of "migration batches".
#### Seed
The
seed module mirrors Knex.js CLI's module of the same
name.
`bash`
knex seed:
Can now be called as either:
`bash`
kysely seed:
or
`bash`
kysely seed
> [!NOTE]
> We also provide kysely seed list, which is not part of Knex.js
CLI.
#### SQL
The
sql module allows you to run SQL queries directly from the command line using the kysely instance.
Single-query:
`bash`
kysely sql "select 1"
or interactive mode:
`bash
kysely sql -f json
✔ sqlite ❯
select 1;
{
"rows": [
{
"1": 1
}
]
}
❯ sqlite ❯
exit
``
acro5piano who built kysely-migration-cli
and inspired this project.
UnJS's amazing tools that help power this project.
Knex.js team for paving the way.