2024-06-12 02:12:12 +00:00
|
|
|
import { Table, getTableName, sql } from 'drizzle-orm';
|
2024-05-08 00:19:13 +00:00
|
|
|
import { Argon2id } from 'oslo/password';
|
2024-06-12 02:12:12 +00:00
|
|
|
import { ADMIN_USERNAME, DB_SEEDING } from '$env/static/private';
|
|
|
|
|
import { db } from '../db';
|
|
|
|
|
import * as schema from './schema';
|
|
|
|
|
import * as seeds from './seeds';
|
|
|
|
|
|
|
|
|
|
if (!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 [
|
|
|
|
|
schema.categories,
|
|
|
|
|
schema.categoriesToExternalIds,
|
|
|
|
|
schema.categories_to_games,
|
|
|
|
|
schema.collection_items,
|
|
|
|
|
schema.collections,
|
|
|
|
|
schema.expansions,
|
|
|
|
|
schema.externalIds,
|
|
|
|
|
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.recoveryCodes,
|
|
|
|
|
schema.roles,
|
|
|
|
|
schema.sessions,
|
|
|
|
|
schema.userRoles,
|
|
|
|
|
schema.users,
|
|
|
|
|
schema.wishlists,
|
|
|
|
|
schema.wishlist_items,
|
|
|
|
|
]) {
|
|
|
|
|
// await db.delete(table); // clear tables without truncating / resetting ids
|
|
|
|
|
await resetTable(db, table);
|
|
|
|
|
}
|
2024-02-15 01:48:47 +00:00
|
|
|
|
2024-06-12 02:12:12 +00:00
|
|
|
await seeds.roles(db);
|
2024-02-15 01:48:47 +00:00
|
|
|
|
2024-06-12 02:12:12 +00:00
|
|
|
await connection.end();
|
2024-02-15 01:48:47 +00:00
|
|
|
|
2024-03-15 19:05:47 +00:00
|
|
|
console.log('Creating roles ...');
|
2024-04-04 22:08:18 +00:00
|
|
|
const adminRole = await db
|
2024-03-15 19:05:47 +00:00
|
|
|
.insert(schema.roles)
|
|
|
|
|
.values([{ name: 'admin' }])
|
2024-04-04 22:08:18 +00:00
|
|
|
.onConflictDoNothing()
|
|
|
|
|
.returning();
|
|
|
|
|
const userRole = await db
|
2024-03-15 19:05:47 +00:00
|
|
|
.insert(schema.roles)
|
|
|
|
|
.values([{ name: 'user' }])
|
2024-04-04 22:08:18 +00:00
|
|
|
.onConflictDoNothing()
|
|
|
|
|
.returning();
|
2024-03-15 19:05:47 +00:00
|
|
|
await db
|
|
|
|
|
.insert(schema.roles)
|
|
|
|
|
.values([{ name: 'editor' }])
|
|
|
|
|
.onConflictDoNothing();
|
|
|
|
|
await db
|
|
|
|
|
.insert(schema.roles)
|
|
|
|
|
.values([{ name: 'moderator' }])
|
|
|
|
|
.onConflictDoNothing();
|
|
|
|
|
console.log('Roles created.');
|
2024-02-17 08:10:19 +00:00
|
|
|
|
2024-04-04 22:08:18 +00:00
|
|
|
console.log('Admin Role: ', adminRole);
|
|
|
|
|
|
|
|
|
|
const adminUser = await db
|
2024-05-08 00:19:13 +00:00
|
|
|
.insert(schema.users)
|
|
|
|
|
.values({
|
2024-06-12 02:12:12 +00:00
|
|
|
username: `${ADMIN_USERNAME}`,
|
2024-05-08 00:19:13 +00:00
|
|
|
email: '',
|
|
|
|
|
hashed_password: await new Argon2id().hash(`${process.env.ADMIN_PASSWORD}`),
|
|
|
|
|
first_name: 'Brad',
|
|
|
|
|
last_name: 'S',
|
|
|
|
|
verified: true,
|
|
|
|
|
})
|
|
|
|
|
.returning()
|
|
|
|
|
.onConflictDoNothing();
|
2024-04-04 22:08:18 +00:00
|
|
|
|
|
|
|
|
console.log('Admin user created.', adminUser);
|
|
|
|
|
|
2024-05-08 00:19:13 +00:00
|
|
|
await db
|
|
|
|
|
.insert(schema.user_roles)
|
|
|
|
|
.values({
|
|
|
|
|
user_id: adminUser[0].id,
|
|
|
|
|
role_id: adminRole[0].id,
|
|
|
|
|
})
|
|
|
|
|
.onConflictDoNothing();
|
2024-04-04 22:08:18 +00:00
|
|
|
|
|
|
|
|
console.log('Admin user given admin role.');
|
|
|
|
|
|
2024-05-08 00:19:13 +00:00
|
|
|
await db
|
|
|
|
|
.insert(schema.user_roles)
|
|
|
|
|
.values({
|
|
|
|
|
user_id: adminUser[0].id,
|
|
|
|
|
role_id: userRole[0].id,
|
|
|
|
|
})
|
|
|
|
|
.onConflictDoNothing();
|
2024-04-04 22:08:18 +00:00
|
|
|
|
|
|
|
|
console.log('Admin user given user role.');
|
|
|
|
|
|
2024-02-17 08:10:19 +00:00
|
|
|
await pool.end();
|
|
|
|
|
process.exit();
|