Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andrenarchy/lauf
ðââïļ Migration runner for Typescript
https://github.com/andrenarchy/lauf
migration postgresql typescript
Last synced: 2 months ago
JSON representation
ðââïļ Migration runner for Typescript
- Host: GitHub
- URL: https://github.com/andrenarchy/lauf
- Owner: andrenarchy
- License: mit
- Created: 2022-07-09T19:57:22.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-29T06:15:18.000Z (3 months ago)
- Last Synced: 2024-10-29T07:19:05.538Z (3 months ago)
- Topics: migration, postgresql, typescript
- Language: TypeScript
- Homepage:
- Size: 1.07 MB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ðââïļ lauf
[![CI](https://github.com/andrenarchy/lauf/actions/workflows/ci.yaml/badge.svg)](https://github.com/andrenarchy/lauf/actions/workflows/ci.yaml) [![npm](https://img.shields.io/npm/v/lauf)](https://www.npmjs.com/package/lauf)*lauf* is a lightweight migration runner for Typescript.
ð Uses PostgreSQL for keeping track of migrations.
ð Guaranteed consistency for your PostgreSQL data via transactions.
âïļ Handle arbitrary further databases or file storages in your migrations (e.g., S3 or GCS).
ðĐâðŧ Migration order is defined in code, not implicitly through files in a directory.
ðĶ Use any packages you want in your migrations.
ðŠķ Lightweight: only a single dependency (`pg`).## Documentation
### Example
Migrations can be run like
```typescript
import { runMigrations } from 'lauf'
import pg from 'pg'await runMigrations({
setup: async () => {
const pgClient = new pg.Client({ connectionString: process.env.POSTGRESQL_URL })
await pgClient.connect()
return { pgClient }
},
teardown: ({ pgClient }) => pgClient.end(),
migrations: [
{
id: '2022-07-09-create-users',
description: 'Create users',
up: ({ pgClient }) => pgClient.query(
`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);`
),
down: ({ pgClient }) => pgClient.query(`DROP TABLE users;`),
},
// add further migrations
],
logger: (msg) => console.log(msg)
})
```### Splitting in multiple files
For organizing migrations, each migration can also be kept in a separate file like
```typescript
import { Migration } from 'lauf'const migration: Migration = {
id: '2022-07-09-create-users',
description: 'Create users',
up: ({ pgClient }) => pgClient.query(
`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);`
),
down: ({ pgClient }) => pgClient.query(`DROP TABLE users;`),
}export default migration
```Then the files can be run as follows:
```typescript
await runMigrations({
// see above
migrations: [
import('./2022-07-10-create-users-table.js'),
// add further migrations here
].map(v => v.default),
})
```### Migrate down/up
Setting the `mode` option to `up` or `down` you can migrate step-wise up or down. The default value is `latest` which runs all migrations.
### Further databases or storages
The `setup` function can return arbitrary further properties. All returned properties will be passed to the migrations and the `teardown` function. For example:
```typescript
await runMigrations({
setup: async () => {
const pgClient = new pg.Client({ connectionString: process.env.POSTGRESQL_URL })
await pgClient.connect()
const gcs = new Storage(process.env.GCS_CREDENTIALS)
return { pgClient, gcs }
},
teardown: ({ pgClient, gcs }) => pgClient.end(),
migrations: [
{
id: '2022-07-09-create-users',
description: 'Create users',
up: async ({ pgClient, gcs }) => {
await pgClient.query(
`CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT);`
)
await gcs.upload(...)
},
down: ({ pgClient, gcs }) => pgClient.query(`DROP TABLE users;`),
},
// add further migrations
],
})
```## Tests
```bash
docker run -d --name pg-lauf -e POSTGRES_PASSWORD=test -p 5432:5432 postgres:14
export POSTGRESQL_URL=postgres://postgres:[email protected]:5432/postgres?sslmode=disable
npm run build && npm test
```