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

56 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-09-04 23:04:41 +00:00
import 'reflect-metadata'
import { DrizzleService } from '$lib/server/api/services/drizzle.service'
import { type Table, getTableName, sql } from 'drizzle-orm'
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
import env from '../common/env'
import * as seeds from './seeds'
import * as schema from './tables'
2024-09-04 23:04:41 +00:00
const drizzleService = new DrizzleService()
if (!env.DB_SEEDING) {
throw new Error('You must set DB_SEEDING to "true" when running seeds')
}
2024-09-04 23:04:41 +00:00
async function resetTable(db: NodePgDatabase<typeof schema>, 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,
2024-09-04 23:04:41 +00:00
schema.expansionsTable,
schema.externalIdsTable,
2024-08-08 03:59:30 +00:00
schema.federatedIdentityTable,
2024-09-04 23:04:41 +00:00
schema.gamesTable,
schema.gamesToExternalIdsTable,
schema.mechanicsTable,
schema.mechanicsToExternalIdsTable,
schema.mechanics_to_games,
schema.password_reset_tokens,
2024-09-04 23:04:41 +00:00
schema.publishersTable,
schema.publishersToExternalIdsTable,
schema.publishers_to_games,
schema.recoveryCodesTable,
2024-09-04 23:04:41 +00:00
schema.rolesTable,
schema.sessionsTable,
2024-08-08 03:59:30 +00:00
schema.twoFactorTable,
schema.user_roles,
schema.usersTable,
schema.wishlist_items,
2024-09-04 23:04:41 +00:00
schema.wishlistsTable,
]) {
// await db.delete(table); // clear tables without truncating / resetting ids
2024-09-04 23:04:41 +00:00
await resetTable(drizzleService.db, table)
}
2024-09-04 23:04:41 +00:00
await seeds.roles(drizzleService.db)
await seeds.users(drizzleService.db)
2024-09-04 23:04:41 +00:00
await drizzleService.dispose()
process.exit()