boredgame/src/lib/server/api/databases/seed.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Table, getTableName, sql } from 'drizzle-orm'
import env from '../../../../env'
import { db, pool } from '../packages/drizzle'
import * as seeds from './seeds'
import * as schema from './tables'
if (!env.DB_SEEDING) {
throw new Error('You must set DB_SEEDING to "true" when running seeds')
}
async function resetTable(db: db, table: Table) {
return db.execute(sql.raw(`TRUNCATE TABLE ${getTableName(table)} RESTART IDENTITY CASCADE`))
}
for (const table of [
2024-08-08 03:59:30 +00:00
schema.categoriesTable,
schema.categoriesToExternalIdsTable,
2024-08-08 03:59:30 +00:00
schema.categories_to_games_table,
schema.collection_items,
schema.collections,
2024-08-08 03:59:30 +00:00
schema.credentialsTable,
schema.expansions,
schema.externalIds,
2024-08-08 03:59:30 +00:00
schema.federatedIdentityTable,
schema.games,
schema.gamesToExternalIds,
schema.mechanics,
schema.mechanicsToExternalIds,
schema.mechanics_to_games,
schema.password_reset_tokens,
schema.publishers,
schema.publishersToExternalIds,
schema.publishers_to_games,
schema.recoveryCodesTable,
schema.roles,
schema.sessionsTable,
2024-08-08 03:59:30 +00:00
schema.twoFactorTable,
schema.user_roles,
schema.usersTable,
schema.wishlist_items,
2024-08-08 03:59:30 +00:00
schema.wishlists,
]) {
// await db.delete(table); // clear tables without truncating / resetting ids
await resetTable(db, table)
}
await seeds.roles(db)
await seeds.users(db)
await pool.end()
process.exit()