-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseed.ts
More file actions
63 lines (56 loc) · 1.54 KB
/
seed.ts
File metadata and controls
63 lines (56 loc) · 1.54 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
import path from 'path';
import { migrate } from 'drizzle-orm/postgres-js/migrator';
import * as schema from './src/lib/server/schemas';
import { drizzle } from 'drizzle-orm/postgres-js';
import { Argon2id } from './src/lib/utils/argon2';
import postgres from 'postgres';
import { exit } from 'process';
import 'dotenv/config';
import { generateId } from './src/lib/utils/crypto';
import type { InferInsertModel } from 'drizzle-orm';
type UserInsert = InferInsertModel<typeof schema.users>;
const seedDefaultUsers = async () => {
const users: UserInsert[] = [
{
firstName: 'super',
lastName: 'user',
id: generateId(5),
email: 'super@local.dev',
password: await new Argon2id().hash('superuser')
},
{
id: generateId(5),
firstName: 'admin',
lastName: 'user',
email: 'admin@local.dev',
password: await new Argon2id().hash('admin')
},
{
id: generateId(5),
firstName: 'client',
lastName: 'user',
email: 'client1@local.dev',
password: await new Argon2id().hash('client1')
}
];
return { users };
};
(async () => {
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 Database...');
const { users } = await seedDefaultUsers();
await db.transaction(async (tx) => {
await tx.insert(schema.users).values(users);
});
console.log('Database Migrated.');
exit(0);
}
})();