Skip to content

Commit

Permalink
Merge pull request #342 from anchan828/add-kysely-migration-package
Browse files Browse the repository at this point in the history
feat: add migration packages
  • Loading branch information
anchan828 authored Sep 7, 2024
2 parents 924916e + 6bfd4fb commit 99c9bc6
Show file tree
Hide file tree
Showing 32 changed files with 539 additions and 70 deletions.
152 changes: 152 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/kysely-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"dependencies": {
"@anchan828/nest-kysely": "^0.6.5",
"@anchan828/kysely-migration": "^0.6.5",
"@nestjs/common": "10.4.1",
"@nestjs/core": "10.4.1",
"@nestjs/platform-express": "10.4.1",
Expand Down Expand Up @@ -61,4 +62,4 @@
"access": "public"
},
"packageManager": "npm@10.8.3"
}
}
3 changes: 2 additions & 1 deletion packages/kysely-example/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { KyselyMigrationClassProvider, KyselyModule } from "@anchan828/nest-kysely";
import { KyselyMigrationClassProvider } from "@anchan828/kysely-migration";
import { KyselyModule } from "@anchan828/nest-kysely";
import { Module } from "@nestjs/common";
import { MysqlDialect } from "kysely";
import { createPool } from "mysql2";
Expand Down
4 changes: 4 additions & 0 deletions packages/kysely-migration/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!dist/**/*
dist/**/*.tsbuildinfo
!cli.js
5 changes: 5 additions & 0 deletions packages/kysely-migration/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const basePrettierConfig = require("../../.prettierrc");

module.exports = {
...basePrettierConfig,
};
21 changes: 21 additions & 0 deletions packages/kysely-migration/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 anchan828

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
106 changes: 106 additions & 0 deletions packages/kysely-migration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# @anchan828/kysely-migration

![npm](https://img.shields.io/npm/v/@anchan828/kysely-migration.svg)
![NPM](https://img.shields.io/npm/l/@anchan828/kysely-migration.svg)

Module for using [kysely](https://www.npmjs.com/package/kysely) with nestjs.

## Installation

```bash
$ npm i --save kysely @anchan828/kysely-migration
```

## Quick Start

### KyselyMigrationClassProvider

This provider can perform migration by passing the Migration class.

```ts
import { Kysely, Migration } from "kysely";
import { KyselyMigrationClassProvider } from "@anchan828/kysely-migration";

class CreateUserTable implements Migration {
public async up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable("user")
.addColumn("id", "integer", (cb) => cb.primaryKey().autoIncrement().notNull())
.addColumn("name", "varchar(255)", (cb) => cb.notNull())
.execute();
}
}

const provider = new KyselyMigrationClassProvider([CreateUserTable]);

await new Migrator({ db, provider }).migrateToLatest();
```

### KyselyMigrationFileProvider

This provider can perform migration by passing the migrations directory path. This provider wraps the FileMigrationProvider provided by kysely and supports SQL files.

```shell
migrations
├── 1715003546247-CreateUserTable.ts
├── 1715003558664-CreateUserInsertTrigger.sql
├── 1715003568628-UpdateUserTable.sql
└── 1715003583015-CreateUserTableIndex.js
```

```ts
const provider = new KyselyMigrationFileProvider({
fs: require("fs"),
path: require("path"),
migrationFolder: path.join(__dirname, "migrations"),
});

await new Migrator({ db, provider }).migrateToLatest();
```

### KyselyMigrationMergeProvider

This provider can perform migration by merging multiple providers.

```ts
const provider = new KyselyMigrationMergeProvider({
providers: [
new KyselyMigrationFileProvider({
fs: require("fs"),
path: require("path"),
migrationFolder: path.join(__dirname, "migrations"),
}),
new KyselyMigrationClassProvider([CreateUserTable]),
],
});

await new Migrator({ db, provider }).migrateToLatest();
```

## Repeatable Migrations

This is a feature to support migrations that need to be regenerated multiple times, such as views/functions/triggers/etc. Unlike migrations that are executed only once, it compares the checksum of the SQL to be executed to determine the need for migration.

```ts
import { KyselyRepeatableMigrationSqlFileProvider, RepeatableMigrator } from "@anchan828/kysely-migration";
const provider = new KyselyRepeatableMigrationSqlFileProvider({
sqlFiles: [resolve(__dirname, "user-view.sql")],
sqlTexts: [{ name: "test", sql: "SELECT 1;" }],
});

await new RepeatableMigrator({ db, provider }).migrateToLatest();
```

| name | hash | timestamp |
| --------- | -------------------------------- | ------------------------ |
| user-view | 6c7e36422f79696602e19079534b4076 | 2024-05-11T17:04:20.211Z |
| test | e7d6c7d4d9b1b0b4c7f5d7b3d5e9d4d4 | 2024-05-11T17:04:20.211Z |

### Note

- Once the file is renamed, the migration is executed even if the contents have not changed.
- Views/Functions/Triggers created by Repeatable Migration are not automatically deleted (or update/rename) by simply deleting the corresponding SQL, so please delete them using the normal migration function.

## License

[MIT](LICENSE)
Loading

0 comments on commit 99c9bc6

Please sign in to comment.