forked from ChocoLZS/live-player
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed-admins.ts
More file actions
68 lines (59 loc) · 2.35 KB
/
seed-admins.ts
File metadata and controls
68 lines (59 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { getDb, admins } from './src/lib/db';
import * as schema from './src/lib/db/schema';
import { drizzle } from 'drizzle-orm/d1';
import { eq } from 'drizzle-orm';
import bcrypt from 'bcryptjs';
async function seedAdmins() {
let db;
try {
db = getDb();
// Test access
try { await db.select().from(admins).limit(1); } catch { throw new Error('Context missing'); }
} catch (e) {
console.log("⚠️ Standard getDb failed (likely local script), trying Wrangler Platform Proxy...");
const { getPlatformProxy } = await import('wrangler');
const { env } = await getPlatformProxy();
if (!env.DB) throw new Error('DB binding not found in Wrangler proxy');
db = drizzle(env.DB as any, { schema });
}
console.log('🌱 Seeding admins table...');
console.log('🌱 Seeding admins table...');
const accounts = [
// 1. Environment / Default account (Legacy)
{
username: process.env.ADMIN_ACCOUNT || 'sumie',
password: process.env.ADMIN_PASSWORD || '12qwaszx34ER$'
},
// 2. New specific account
{
username: 'kysadmins',
password: 'ky$nananiji'
}
];
for (const acc of accounts) {
if (!acc.password) {
console.warn(`⚠️ Password not set for ${acc.username}, skipping...`);
continue;
}
const existingAdmin = await db.select().from(admins).where(eq(admins.username, acc.username)).limit(1);
const passwordHash = await bcrypt.hash(acc.password, 10);
if (existingAdmin.length === 0) {
await db.insert(admins).values({
username: acc.username,
passwordHash: passwordHash,
role: 'admin',
isActive: true
});
console.log(`✅ Admin "${acc.username}" created`);
} else {
// Optional: Update password if exists, or just leave it?
// Usually seed scripts ensure state matches code, so let's update.
await db.update(admins)
.set({ passwordHash: passwordHash, isActive: true })
.where(eq(admins.username, acc.username));
console.log(`ℹ️ Admin "${acc.username}" updated`);
}
}
console.log('✅ Seeding complete!');
}
seedAdmins().catch(console.error);