const migrations: MigrationList = {
2: async (migration) => {
class Test extends Model {
id!: number
name!: string
}
await Test.forEach(async (instance, tx) => {
instance.name = `${instance.id} name`
await instance.save(tx)
}, migration.tx)
const test = await Test.get(69, migration.tx)
await test?.delete(migration.tx)
},
}
A list of migrations to be executed. The key is the target version number, and the value is the migration callback. Eg.
{ 2: (migration) => { ... } }will execute the migration callback when the current database version is smaller than 2.Because of this a key of
1will only be executed on the first migration, this can be used to populate the database with initial data.A migration callback receives a W-ORM.MigrationContext object as its only argument. This object contains a transaction to be used for the migration.
It is expected for the callback to create Model classes that represent the table's state in between these two versions. The fields aren't actually used by W-ORM in this scenario, and only serve to improve the typing within the migration. The W-ORM.Model methods can be then used to manipulate the data. It is very important to use the transaction provided by the migration context, otherwise the migration will hang forever.