Skip to content

Commit 6ba6ac2

Browse files
committed
refactor(update-classroom-data): use upsert to update classroom data
1 parent f9b4994 commit 6ba6ac2

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

data/classroom/write.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"preview": "NODE_OPTIONS='--import ./public/instrument.server.mjs' nuxt preview",
1111
"postinstall": "nuxt prepare && prisma generate",
1212
"update-club-info": "tsx utils/update-club-info.ts",
13-
"upload-classroom-info": "tsx data/classroom/write.ts",
13+
"update-classroom-data": "tsx utils/update-classroom-data.ts",
1414
"lint": "eslint .",
1515
"lint:fix": "eslint . --fix",
1616
"typecheck": "vue-tsc --noEmit --skipLibCheck"

utils/update-classroom-data.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { PrismaClient } from '@prisma/client'
2+
import data from '~/data/classroom/classroom.json'
3+
4+
const prisma = new PrismaClient()
5+
6+
const runSequence: any = []
7+
8+
data.forEach((e) => {
9+
runSequence.push(
10+
prisma.classroomData.upsert({
11+
where: {
12+
name: e.name,
13+
},
14+
update: {
15+
name: e.name,
16+
alias: e.alias,
17+
size: e.size,
18+
},
19+
create: {
20+
name: e.name,
21+
alias: e.alias,
22+
size: e.size,
23+
},
24+
}),
25+
)
26+
})
27+
28+
// eslint-disable-next-line no-console
29+
console.log(`start transaction with length ${runSequence.length}`)
30+
await prisma.$transaction(runSequence)

0 commit comments

Comments
 (0)