Skip to content

Instantly share code, notes, and snippets.

@npalm
Created September 23, 2019 17:58
Show Gist options
  • Save npalm/90e70ddb3892f88e94f29262d3a56879 to your computer and use it in GitHub Desktop.
Save npalm/90e70ddb3892f88e94f29262d3a56879 to your computer and use it in GitHub Desktop.
seed
const csv = require('csvtojson')
const { prisma } = require('./generated/prisma-client')
async function loadPersons(data) {
const createPersonRequests = data.map(element => prisma.createPerson(element));
await Promise.all(createPersonRequests);
}
async function loadConferences(data) {
const request = data.map(element => prisma.createConference(element));
await Promise.all(request);
}
async function findSpeakers(talk) {
let speakers = talk.speakers.split(',')
const fragment = `
fragment UserIds on Person {
id
}
`
let result = []
for (var i = 0; i < speakers.length; i++) {
let data = await prisma.persons({
where: {
name: speakers[i]
}
}).$fragment(fragment)
result = result.concat(data)
}
return result
}
async function findConferences(talk) {
let conferences = talk.conferences.split(',')
const fragment = `
fragment ConferenceIds on Conference {
id
}
`
let result = []
for (var i = 0; i < conferences.length; i++) {
let data = await prisma.conferences({
where: {
name: conferences[i]
}
}).$fragment(fragment)
result = result.concat(data)
}
return result
}
async function dowork(json) {
let speakersIds = await findSpeakers(json)
json.speakers = {
connect: speakersIds
}
let conferenceIds = await findConferences(json)
json.conferences = {
connect: conferenceIds
}
await prisma.createTalk(json)
}
async function loadTalks(data) {
const request = data.map(element => dowork(element))
await Promise.all(request);
}
async function main() {
let persons = await csv().fromFile('speakers.csv')
await loadPersons(persons)
let conferences = await csv().fromFile('conferences.csv')
await loadConferences(conferences)
let talks = await csv().fromFile('talks.csv')
await loadTalks(talks)
}
main().catch(e => console.error(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment