Browse Source

order safe data structure for migrations

pull/1572/head
Bernd Storath 4 months ago
parent
commit
9f6e9ab3c3
No known key found for this signature in database GPG Key ID: D6C85685A555540F
  1. 1
      src/nuxt.config.ts
  2. 19
      src/services/database/migrations/index.ts

1
src/nuxt.config.ts

@ -27,6 +27,7 @@ export default defineNuxtConfig({
nitro: { nitro: {
esbuild: { esbuild: {
options: { options: {
// to support big int
target: 'es2020', target: 'es2020',
}, },
}, },

19
src/services/database/migrations/index.ts

@ -4,10 +4,10 @@ import { run1 } from './1';
export type MIGRATION_FN = (db: Low<Database>) => Promise<void>; export type MIGRATION_FN = (db: Low<Database>) => Promise<void>;
const MIGRATION_LIST = { const MIGRATION_LIST = [
// Adds Initial Database Structure // Adds Initial Database Structure
'1': run1, { id: '1', fn: run1 },
} satisfies Record<string, MIGRATION_FN>; ] satisfies { id: string; fn: MIGRATION_FN }[];
/** /**
* Runs all migrations * Runs all migrations
@ -15,18 +15,15 @@ const MIGRATION_LIST = {
*/ */
export async function migrationRunner(db: Low<Database>) { export async function migrationRunner(db: Low<Database>) {
const ranMigrations = db.data.migrations; const ranMigrations = db.data.migrations;
const runMigrations = Object.keys( for (const migration of MIGRATION_LIST) {
MIGRATION_LIST if (ranMigrations.includes(migration.id)) {
) as (keyof typeof MIGRATION_LIST)[];
for (const migrationId of runMigrations) {
if (ranMigrations.includes(migrationId)) {
continue; continue;
} }
try { try {
await MIGRATION_LIST[migrationId](db); await migration.fn(db);
db.data.migrations.push(migrationId); db.data.migrations.push(migration.id);
} catch (e) { } catch (e) {
throw new Error(`Failed to run Migration ${migrationId}: ${e}`); throw new Error(`Failed to run Migration ${migration.id}: ${e}`);
} }
} }
await db.write(); await db.write();

Loading…
Cancel
Save