Skip to content

Commit

Permalink
chore: readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Oct 25, 2023
1 parent 80033f1 commit 2ffd8b4
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 38 deletions.
116 changes: 95 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,111 @@

```bash
npm i --save-dev kysely-migrate
pnpm add -D kysely-migrate
yarn add -D kysely-migrate
bun add -D kysely-migrate
```

## Usage

Create a `kysely-migrate.config.ts` file and fill it out:

```ts
// kysely-migrate.config.ts
import { Kysely, MysqlDialect } from 'kysely'
import { defineConfig } from 'kysely-migrate'
import { createPool } from 'mysql2'

export default defineConfig({
db: new Kysely({
dialect: new MysqlDialect({ pool: createPool('mysql://') }),
}),
migrationFolder: 'src/db/migrations',
codegen: { dialect: 'mysql', out: 'src/db/types.ts' },
})
```

Run [commands](#commands) to manage migrations and generate types.

```bash
kysely-migrate <command> [options]
```

## Docs
## API

Run `kysely-migrate --help` (output below) or `kysely-migrate <command> --help` to see the list of available commands and options.
### Commands

```bash
Usage:
$ kysely-migrate <command> [options]

Commands:
codegen generate types from database metadata
create create new migration
down migrate one step down
init create configuration file
list list migrations
to migrate to selected migration
up migrate one step up

For more info, run any command with the `--help` flag:
$ kysely-migrate create --help

Options:
-h, --help Display this message
-v, --version Display version number
Run `kysely-migrate --help` or `kysely-migrate <command> --help` to see the list of available commands, options, and examples.

- `codegen` generate types from database metadata
- `create` create new migration
- `down` migrate one step down
- `init` create configuration file
- `list` list migrations
- `to` migrate to selected migration
- `up` migrate one step up

### defineConfig

Creates [`Config`](#config) object.

| Name | Type | Description |
| ------- | ------------------------------------------ | --------------------------------------------------------------------- |
| `config` | `Config \| (() => Config \| Promise<Config>)` | Configuration object or a function that returns a configuration object. |
| returns | [`Config`](#config) | Configuration object. |

### loadEnv

Loads environment variables from `.env` or `.env.*` files.

| Name | Type | Description |
| -------------- | ------------------------- | ------------------------------------------- |
| `config.mode` | `string \| undefined` | `.env` file type (e.g. `` `.env.${mode}` ``) |
| `config.envDir` | `string \| undefined` | Directory to load `.env` file from |
| returns | `Record<string, string>` | Parsed environment variables. |

### Config

`Config` object.

```ts
{
/** Kysely instance used to manipulate migrations and introspect database */
db: Kysely<any>
/** Path to migrations directory */
migrationFolder: string

/** `kysely-migrate codegen` options */
codegen?:
| {
/** Custom definition mappings for database types to TypeScript types */
definitions?: Definitions | undefined
/** Dialect definitions to inherit */
dialect?: 'mysql' | 'postgres' | 'sqlite' | undefined
/** Output file path */
out: string
}
| undefined

/** Used for internal `FileMigrationProvider` instance. Defaults to `node:fs/promises`. */
fs?: FileMigrationProviderFS | undefined
/** Used for internal `FileMigrationProvider` instance. Defaults to `node:path`. */
path?: FileMigrationProviderPath | undefined
/** Defaults to internal `migrator` created with `db` and `migrationFolder`. */
migrator?: Migrator | undefined
}
```

### Dialect Definitions

Dialect definition files map database types to TypeScript types. They are used by the codegen command to generate types from database metadata. The following dialect definitions are available:

```ts
import {
mysqlDefinitions,
postgresDefinitions,
sqliteDefinitions,
} from 'kysely-migrate'
```

## Sponsors
Expand Down
3 changes: 3 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ cli
.option('-R, --reset', '[boolean] reset all migrations')
.option('-r, --root <path>', '[string] root path to resolve config from')
.example((name) => `${name} down`)
.example((name) => `${name} down --reset`)
.action(async (options: DownOptions) => await down(options))

cli
Expand All @@ -55,6 +56,7 @@ cli
.option('-n, --name <name>', '[string] migration name')
.option('-r, --root <path>', '[string] root path to resolve config from')
.example((name) => `${name} to`)
.example((name) => `${name} to --name 0001_every_mangos_carry`)
.action(async (options: ToOptions) => await to(options))

cli
Expand All @@ -63,6 +65,7 @@ cli
.option('-l, --latest', '[boolean] apply all pending migrations')
.option('-r, --root <path>', '[string] root path to resolve config from')
.example((name) => `${name} up`)
.example((name) => `${name} up --latest`)
.action(async (options: UpOptions) => await up(options))

cli.help()
Expand Down
22 changes: 8 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@ import {
import { type CliOptions } from 'kysely-codegen'
import { type Definitions } from './utils/codegen/types.js'

export type Config =
| {
codegen?: Evaluate<Codegen> | undefined
db: Kysely<any>
fs?: FileMigrationProviderFS | undefined
path?: FileMigrationProviderPath | undefined
migrationFolder: string
}
| {
codegen?: Evaluate<Codegen> | undefined
db?: Kysely<any> | undefined
migrationFolder: string
migrator: Migrator
}
export type Config = {
codegen?: Evaluate<Codegen> | undefined
db: Kysely<any>
fs?: FileMigrationProviderFS | undefined
path?: FileMigrationProviderPath | undefined
migrationFolder: string
migrator?: Migrator | undefined
}

type Codegen =
| {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/getMigrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FileMigrationProvider, Migrator } from 'kysely'
import { type Config } from '../config.js'

export function getMigrator(config: Config) {
if ('migrator' in config) return config.migrator
if ('migrator' in config && config.migrator) return config.migrator

const { db, fs = nodeFs, path = nodePath } = config
const migrationFolder = nodePath.resolve(config.migrationFolder)
Expand Down
4 changes: 2 additions & 2 deletions src/utils/loadEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { expand } from 'dotenv-expand'
// https://github.com/vitejs/vite/blob/main/packages/vite/src/node/env.ts#L7
export function loadEnv(
config: {
mode?: string
envDir?: string
mode?: string | undefined
envDir?: string | undefined
} = {},
): Record<string, string> {
const mode = config.mode
Expand Down

0 comments on commit 2ffd8b4

Please sign in to comment.