import superjson from 'superjson'; import { db } from '@iridiumcms/db'; import { users, medias, contents, webhooks, apiTokens, schedules, } from '@iridiumcms/db/tables'; export type Table = typeof medias | typeof users | typeof contents | typeof webhooks | typeof apiTokens | typeof schedules; export const tables = [ ['users.json', users], ['medias.json', medias], ['contents.json', contents], ['webhooks.json', webhooks], ['apiTokens.json', apiTokens], ['schedules.json', schedules], // intentionally omit sessions, // we dont want users from one backup // to be logged into another environment // (especially since those sessions are likely // expired and it would kick whoever is logged in) ] as ([string, Table])[]; export const backupTable = async function(table: Table) { const records = await db.select().from(table); return new Blob([superjson.stringify(records)], { type: 'application/json', }); }; export const restoreTable = async function(table: Table, file: ArrayBuffer) { const records = superjson.parse(new TextDecoder().decode(file)) as any[]; await db.delete(table); if (records.length) { await db.insert(table).values(records); } return records; };