-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseed.ts
36 lines (29 loc) · 917 Bytes
/
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
import { db } from "./src/app/db"
import { Elements } from "./src/app/db/schema"
type ElementData = {
elements: {
name: string
number: number
symbol: string
}[]
}
async function main () {
console.log('Downloading seed data...')
const res = await fetch('https://raw.githubusercontent.com/Bowserinator/Periodic-Table-JSON/master/PeriodicTableJSON.json')
if (res.status !== 200) {
throw new Error(`received status code ${res.status} when fetching elements JSON`)
}
console.log('Parsing seed data...')
const elementJson = await res.json() as ElementData
const values = elementJson.elements.map(el => {
const { symbol, name, number } = el
return {
symbol, name, atomicNumber: number
}
})
console.log('Delete existing element data...')
await db.delete(Elements)
console.log('Insert new element data...')
await db.insert(Elements).values(values)
}
main()