Skip to content

A super modular DO piece that simply adds ability to run migrations on DOs using an added function _migrate.

Notifications You must be signed in to change notification settings

janwilmake/migratable-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Problem: when you have a DO with production data and you want to alter the schema, you can't just do this in the constructor as it will be ran every time.

Solution: migratable-object runs each new version just once upon construction such that your schemas can always be up-to-date.

npm i migratable-object
import { Migratable, MigratableObject } from "migratable-object";

@Migratable({
  migrations: {
    "1": ["CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)"],
    "2": ["ALTER TABLE users ADD COLUMN email TEXT"],
  },
})
class MyMigratableObject extends DurableObject {
  async fetch(request: Request): Promise<Response> {
    // Your regular request can assume the migrations were ran.
    return new Response("Hello World!");
  }
}

Or this is the other way to use it without the decorator:

import { MigratableObject } from "migratable-object";

export class ItemsStore extends MigratableObject {
  constructor(state, env) {
    super(state, env, {
      migrations: {
        "1": ["CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)"],
        "2": ["ALTER TABLE users ADD COLUMN email TEXT"],
      },
    });
  }
}

Changelog

About

A super modular DO piece that simply adds ability to run migrations on DOs using an added function _migrate.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published