https://github.com/treeder/migrations
Simple JS database/SQL migration library.
https://github.com/treeder/migrations
Last synced: 4 months ago
JSON representation
Simple JS database/SQL migration library.
- Host: GitHub
- URL: https://github.com/treeder/migrations
- Owner: treeder
- License: mit
- Created: 2024-01-29T21:42:08.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-09-13T21:21:13.000Z (9 months ago)
- Last Synced: 2025-09-13T23:31:05.864Z (9 months ago)
- Language: JavaScript
- Size: 60.5 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# migrations
Simple SQLite migration library.
This works with Cloudflare D1 out of the box.
This will perform the migration and since it's in git, it will also keep a record of all db changes.
## Usage
```sh
npm install treeder/migrations
```
## Using classes
Define a class with properties. Properties are just like Lit component properties so they have a similar feel.
```js
import { ClassMigrations } from 'migrations'
// First define your models as classes:
export class Product {
static properties = {
id: {
type: String,
primaryKey: true,
},
createdAt: {
type: Date,
},
name: {
type: String,
},
price: {
type: Number,
},
data: {
type: Object, // Use Object for JSON fields.
},
}
}
```
Then run the migrations:
```js
// Then use this to create your migrations:
let migrations = new ClassMigrations(env.D1, [Product])
await migrations.run()
```
If you add new properties, the database will automatically update on the next time you run it.
### Ensure you only run it once on startup
Use this once function:
```
```
### Indexes
Add an index property to the field.
```js
{
userId: {
type: String,
index: true,
},
}
```
To make it a unique index:
```js
{
userId: {
type: String,
index: {
unique: true,
},
},
}
```
## Using raw statements
```js
import { Migrations } from 'migrations'
let migrations = new Migrations(db)
// add all your migrations, one statement per add()
// WARNING: DO NOT REMOVE A MIGRATION, EVER! JUST LEAVE THEM AND ADD TO THE LIST
migrations.add(`CREATE TABLE IF NOT EXISTS mytable (id string PRIMARY KEY, createdAt text)`)
migrations.add(`CREATE TABLE IF NOT EXISTS mytable2 (id string PRIMARY KEY, createdAt text)`)
// Then run it. You can run this any number of times, it will only run each migration once.
await migrations.run()
```