https://github.com/blackglory/better-sqlite3-migrations
🌲 A utility for database migrations with better-sqlite3
https://github.com/blackglory/better-sqlite3-migrations
library nodejs npm-package typescript
Last synced: 10 months ago
JSON representation
🌲 A utility for database migrations with better-sqlite3
- Host: GitHub
- URL: https://github.com/blackglory/better-sqlite3-migrations
- Owner: BlackGlory
- License: mit
- Created: 2020-10-19T04:37:37.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-07-21T17:16:56.000Z (11 months ago)
- Last Synced: 2025-08-08T14:11:45.873Z (11 months ago)
- Topics: library, nodejs, npm-package, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@blackglory/better-sqlite3-migrations
- Size: 672 KB
- Stars: 16
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# better-sqlite3-migrations
A utility for database migrations with [better-sqlite3].
The module using [user_version] to record the schema version.
[better-sqlite3]: https://www.npmjs.com/package/better-sqlite3
[user_version]: https://www.sqlite.org/pragma.html#pragma_user_version
## Install
```sh
npm install --save @blackglory/better-sqlite3-migrations
# or
yarn add @blackglory/better-sqlite3-migrations
```
## API
```ts
interface IMigration {
version: number
up: string | ((db: Database) => void)
down: string | ((db: Database) => void)
}
```
You may need [migration-files].
[migration-files]: https://github.com/BlackGlory/migration-files
### migrate
```ts
function migrate(db: Database, migrations: IMigration[], targetVersion?: number): void
```
If targetVersion is `undefined`, then use the maximum version of migrations.
## FAQ
### Can multiple instances migrate in parallel?
Yes, the `user_version` update is visible to every database connection.
When the maximum migration version is less than the `user_version` (which means it is an obsolete instance), it will skip the migration.
You may need a proper retry strategy,
because each migration uses `BEGIN IMMEDIATE` to ensure that parallel write transactions fail early.