TinyJoin logoTinyJoin

migrate

The migrate function applies the migrations that drizzle-kit generate wrote, which the database has not yet recorded, in the journal's order.

migrate<TSchema>(
  db: TinyJoinDatabase<TSchema>,
  config: MigrationConfig,
): Promise<void>
TypeDescription
dbTinyJoinDatabase<TSchema>

The Drizzle database, from drizzle.

configMigrationConfig

The journal and the SQL text of each migration.

returnsPromise<void>

This has no return value.

Each migration runs as one script together with the row that records it, so it commits whole or not at all, and a migration that fails leaves the database as the previous one left it, and rejects with its error. A migration another Client of the same database applied first is skipped, so every tab of an application can call migrate as it starts.

A migration is recorded by its tag and applied once: a file changed after it was applied is not applied again, and a migration that joins the journal before others already applied is still applied. The migrations need no file system, so a browser application bundles them.

Example

import {create} from 'tinyjoin';
import {drizzle, migrate} from 'tinyjoin/drizzle';
import journal from './drizzle/meta/_journal.json';

const db = drizzle(await create('opfs://my-app-v1'));
await migrate(db, {
  journal,
  migrations: import.meta.glob<string>('./drizzle/*.sql', {
    query: '?raw',
    import: 'default',
    eager: true,
  }),
});

Since

v0.5.0