drizzle-kit
Advanced tools
Comparing version 0.12.2 to 0.12.3
{ | ||
"name": "drizzle-kit", | ||
"version": "0.12.2", | ||
"version": "0.12.3", | ||
"repository": "https://github.com/lambda-direct/drizzle-kit", | ||
@@ -5,0 +5,0 @@ "author": "Alex Blokh <aleksandrblokh@gmail.com>", |
105
readme.md
## Drizzle Kit | ||
DrizzleKit - is a CLI migrator tool for DrizzleORM. It is probably one and only tool that lets you completely automatically generate SQL migrations and covers ~95% of the common cases like delitions and renames by prompting user input. | ||
DrizzleKit - is a CLI migrator tool for DrizzleORM. It is probably one and only tool that lets you completely automatically generate SQL migrations and covers ~95% of the common cases like delitions and renames by prompting user input.\ | ||
https://github.com/drizzle-team/drizzle-kit-mirror - is a mirror repository for issues. | ||
### How it works | ||
`drizzle-kit` will traverse `data folder` from configuration file, find all schema .ts files. Generate schema snapshot and compare it to the previous version(if there's one). Based on the difference it will generate all needed SQL migrations and if there're any `automatically unresolvable` cases like `renames` it will prompt user for input. | ||
`drizzle-kit` will traverse `schema folder` or `schema file`, generate schema snapshot and compare it to the previous version(if there's one).\ | ||
Based on the difference it will generate all needed SQL migrations and if there're any `automatically unresolvable` cases like `renames` it will prompt user for input. | ||
For schema file: | ||
```typescript | ||
import { AbstractTable } from "drizzle-orm"; | ||
// ./src/db/schema.ts | ||
export class UsersTable extends AbstractTable<UsersTable> { | ||
id = this.serial("id").primaryKey(); | ||
fullName = this.varchar("full_name", { size: 256 }); | ||
import { integer, pgTable, serial, text, varchar } from "drizzle-orm-pg"; | ||
fullNameIndex = this.index(this.fullName); | ||
const users = pgTable("users", { | ||
id: serial("id").primaryKey(), | ||
fullName: varchar("full_name", { length: 256 }), | ||
}, (table) => ({ | ||
nameIdx: index("name_idx", table.fullName), | ||
}) | ||
); | ||
public tableName(): string { | ||
return "users"; | ||
} | ||
} | ||
export class AuthOtpTable extends AbstractTable<AuthOtpTable> { | ||
id = this.serial("id").primaryKey(); | ||
phone = this.varchar("phone", { size: 256 }); | ||
userId = this.int("user_id").foreignKey(UsersTable, (t) => t.id); | ||
public tableName(): string { | ||
return "auth_otp"; | ||
} | ||
} | ||
export const authOtp = pgTable("auth_otp", { | ||
id: serial("id").primaryKey(), | ||
phone: varchar("phone", { length: 256 }), | ||
userId: integer("user_id").references(() => users.id), | ||
}); | ||
``` | ||
@@ -55,30 +52,50 @@ It will generate: | ||
### Installation & configuration | ||
```bash | ||
npm install -g drizzle-kit | ||
```shell | ||
$ npm install -D drizzle-kit | ||
``` | ||
Create a `drizzle.config.yml` configuration file: | ||
```yaml | ||
migrationRootFolder: drizzle ## all migrations will live here | ||
dataFolder: './src/data' ## where are all schema .ts files | ||
Running with CLI options | ||
```shell | ||
$ npm exec drizzle-kit generate --out migrations-folder --dialect pg --schema src/db/schema.ts | ||
``` | ||
\ | ||
That's it, you're ready to go 🚀 | ||
``` | ||
> drizzle-kit migrate | ||
``` | ||
\ | ||
You can also run migrations in project scope | ||
```js | ||
// package.json | ||
Or put your file to `drizzle.config.json` configuration file: | ||
```json | ||
{ | ||
... | ||
scripts: { | ||
... | ||
migrate: "drizzle-kit migrate" | ||
} | ||
"dialect": "pg", | ||
"out": "./migrations-folder", | ||
"schema": "./src/db" | ||
} | ||
> npm run migrate | ||
``` | ||
--- | ||
### List of commands | ||
**`$ drizzle-kit generate`** - generates SQL migrations based on .ts schema\ | ||
`--config` [optional defalut=drizzle.config.json] path to an optional config file\ | ||
`--dialect` [optional default=pg] database dialect, one of -> pg, mysql, sqlite\ | ||
`--schema` path to a schema file or folder with multiple schema files\ | ||
`--out` [optional default=drizzle/] place where to store migration files\ | ||
```shell | ||
$ drizzle-kit generate | ||
## runs generate command with drizzle.config.json | ||
$ drizzle-kit generate --config custom.config.json | ||
## runs generate command with custom.config.json | ||
$ drizzle-kit generate --dialect pg --schema src/schema.ts | ||
## runs generate command and outputs results to ./drizzle | ||
$ drizzle-kit generate --dialect .. --schema .. --out ./migration-folder | ||
## runs generate command and outputs results to ./migration-folder | ||
``` | ||
**`$ drizzle-kit up`** - updates stale snapshots | ||
`--config` [optional defalut=drizzle.config.json] path to an optional config file\ | ||
`--dialect` [optional default=pg] database dialect, one of -> pg, mysql, sqlite\ | ||
`--schema` path to a schema file or folder with multiple schema files\ | ||
**`$ drizzle-kit check`** - checks for collisions | ||
`--config` [optional defalut=drizzle.config.json] path to an optional config file\ | ||
`--dialect` [optional default=pg] database dialect, one of -> pg, mysql, sqlite\ | ||
`--schema` path to a schema file or folder with multiple schema files\ | ||
729799
101