forked from maybe-finance/maybe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.ts
70 lines (66 loc) · 2.11 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
import { Institution, PrismaClient, Provider } from '@prisma/client'
const prisma = new PrismaClient()
/*
* NOTE: seeding should be idempotent
*/
async function main() {
const institutions: (Pick<Institution, 'id' | 'name'> & {
providers: { provider: Provider; providerId: string; rank?: number }[]
})[] = [
{
id: 1,
name: 'Capital One',
providers: [
{ provider: 'PLAID', providerId: 'ins_9', rank: 1 },
{ provider: 'FINICITY', providerId: '170778' },
],
},
{
id: 2,
name: 'Discover Bank',
providers: [
{ provider: 'PLAID', providerId: 'ins_33' },
{ provider: 'FINICITY', providerId: '13796', rank: 1 },
],
},
]
await prisma.$transaction([
// create institution linked to provider institutions
...institutions.map(({ id, name, providers }) =>
prisma.institution.upsert({
where: { id },
create: {
name,
providers: {
connectOrCreate: providers.map(({ provider, providerId, rank = 0 }) => ({
where: {
provider_providerId: { provider, providerId },
},
create: {
provider,
providerId,
name,
rank,
},
})),
},
},
update: {},
})
),
])
}
// Only run the seed in preview environments, not production
if (process.env.NODE_ENV !== 'production') {
console.log('seeding...')
main()
.catch((e) => {
console.error('prisma seed failed', e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})
} else {
console.warn('seeding skipped', process.env.NODE_ENV)
}