-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseed.ts
More file actions
162 lines (155 loc) · 3.85 KB
/
seed.ts
File metadata and controls
162 lines (155 loc) · 3.85 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
PrismaClient,
Prisma,
Responder,
ItemType,
ResponderItem,
ItemTypeGroup,
} from '@prisma/client'
import { randomUUID } from 'crypto'
import { connect } from 'http2'
const prisma = new PrismaClient()
async function main() {
// First we create some Responders.
// This lets us get their IDs so we can use them to build the relation
// define an empty typed array
const responders: Responder[] = []
// fill the array with the created responders
responders.push(
await prisma.responder.create({
data: {
name: 'Roger Dodger',
callsign: 'GR101',
},
}),
await prisma.responder.create({
data: {
name: 'Simple Simon',
callsign: 'GR102',
},
}),
await prisma.responder.create({
data: {
name: 'Fanny Adams',
callsign: 'GR103',
},
})
)
const itemTypeGroup: ItemTypeGroup[] = []
itemTypeGroup.push(
await prisma.itemTypeGroup.create({
data: {
name: 'Razer Lift',
notes: 'blah blah some notes lorem whatever',
},
}),
await prisma.itemTypeGroup.create({
data: {
name: 'Thingyma Bob',
},
})
)
// Now we create some item types. Again we need to do this so we can get the IDs
//deind an empty typed array
const itemTypes: ItemType[] = []
// fill the array with created item types
itemTypes.push(
await prisma.itemType.create({
data: {
name: 'Big Bandage',
hasBattery: false,
minimum: 1,
hasExpiryDate: true,
hasSerialNumber: false,
infoUrl: 'https://www.google.com',
},
}),
await prisma.itemType.create({
data: {
name: 'Metal Jobbie',
hasBattery: false,
minimum: 1,
hasExpiryDate: false,
hasModel: true,
itemTypeGroupId: itemTypeGroup[0].id,
},
}),
await prisma.itemType.create({
data: {
name: 'electric doodah',
hasBattery: true,
minimum: 2,
hasExpiryDate: false,
infoUrl: 'https://www.facebook.com',
},
}),
await prisma.itemType.create({
data: {
name: 'optional ordonance',
hasBattery: false,
minimum: 0,
hasExpiryDate: true,
hasSwasft: true,
hasModel: true,
itemTypeGroupId: itemTypeGroup[1].id,
},
})
)
/**
* @returns random int beween 0 and 41
*/
function randomQuantity(): number {
return Math.floor(Math.random() * 42)
}
function randomString(has: boolean): string | null {
if (!has) {
return null
}
if (Math.random() < 0.5) {
return null
}
return randomUUID()
}
function randomDate(has: boolean): Date | null {
if (!has) {
return null
}
if (Math.random() < 0.5) {
return null
}
const dayInMilliseconds = 86400000
const nowTime = new Date()
const fromTime = nowTime.getTime() - 150 * dayInMilliseconds
const toTime = nowTime.getTime() + 900 * dayInMilliseconds
return new Date(fromTime + Math.random() * (toTime - fromTime))
}
// Now loop through all the responders and item types
// and create a responderItem for each combination
for (const responder of responders) {
for (const itemType of itemTypes) {
await prisma.responderItem.create({
data: {
quantity: randomQuantity(),
model: randomString(itemType.hasModel),
serial: randomString(itemType.hasSerialNumber),
swasft: randomString(itemType.hasSwasft),
expiry: randomDate(itemType.hasExpiryDate),
responder: {
connect: { id: responder.id },
},
itemType: {
connect: { id: itemType.id },
},
},
})
}
}
}
main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})