forked from daedalus-developers/auth-sveltekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.ts
83 lines (71 loc) · 2.15 KB
/
seed.ts
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// import fs from 'fs/promises';
import path from 'path';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import * as schema from './src/lib/server/schemas/';
import { users, type UserInsertSchema } from './src/lib/server/schemas/';
import { drizzle } from 'drizzle-orm/postgres-js';
import { generateId } from 'lucia';
import { Argon2id } from 'oslo/password';
import postgres from 'postgres';
import 'dotenv/config';
import { exit } from 'process';
import { slugifyString } from './src/lib/utils';
(async () => {
// const DIR_PATH = path.dirname(process.env.DATABASE_URL!);
//
// try {
// await fs.access(process.env.DATABASE_URL!, fs.constants.F_OK);
// console.log('Database found!');
// } catch (error) {
// console.log('Database not found. Creating a new one...');
// await fs.mkdir(DIR_PATH, { recursive: true });
// await fs.writeFile(process.env.DATABASE_URL!, '');
// }
const client = postgres(process.env.DATABASE_URL!);
const db = drizzle(client, {
schema
});
try {
migrate(db, { migrationsFolder: path.resolve('./migrations') });
} catch (error) {
console.log('Database in sync with migrations. Starting server...');
} finally {
console.log('Seeding super user');
const superPassword = await new Argon2id().hash('superuser');
const adminPassword = await new Argon2id().hash('admin123');
const superuser: UserInsertSchema = {
id: generateId(5),
username: 'superuser',
password: superPassword,
email: '[email protected]',
role: 'super'
};
const admin: UserInsertSchema = {
id: generateId(5),
username: 'admin',
password: adminPassword,
email: '[email protected]',
role: 'admin'
};
const initialUsers = [
{ ...admin, id: generateId(5) },
{ ...superuser, id: generateId(5) }
];
await db.insert(users).values(initialUsers).onConflictDoNothing();
const categories = [
'Smartphone',
'Food',
'Clothing',
'Furniture',
'Awesome Product',
'Laptop & Computer',
'Random Category'
];
await db
.insert(schema.categories)
.values(categories.map((name) => ({ name: slugifyString(name) })))
.onConflictDoNothing();
console.log('Database Migrated.');
exit(0);
}
})();