-
Notifications
You must be signed in to change notification settings - Fork 17
/
seed-tags.js
46 lines (40 loc) · 1.26 KB
/
seed-tags.js
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
const {PrismaClient} = require('@prisma/client')
const fs = require('node:fs');
const readline = require('node:readline');
const prisma = new PrismaClient()
async function main(filepath) {
const fileStream = fs.createReadStream(filepath);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
// Note: we use the crlfDelay option to recognize all instances of CR LF
// ('\r\n') in input.txt as a single line break.
for await (const line of rl) {
// Each line in input.txt will be successively available here as `line`.
prisma.tag.upsert({
where: {
name: line
},
update: {
name: line,
aliases: []
},
create: {
name: line,
aliases: []
}
}).then((tag) => {
console.log(`Created tag ${tag.name} with id: ${tag.id}`)
}).catch((error) => {
console.error(`Error creating tag ${line}: ${error}`)
})
}
}
main(process.argv[2]).then(() => {
console.log('Seed complete')
}).catch((error) => {
console.error(`Error seeding tags: ${error}`)
}).finally(async () => {
await prisma.$disconnect()
})