Skip to content

Commit

Permalink
prisma import data to planetscale code
Browse files Browse the repository at this point in the history
  • Loading branch information
scott306lr committed Dec 21, 2022
1 parent 5f6ba5b commit 5a137dd
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 6 deletions.
131 changes: 131 additions & 0 deletions movis/dataset/import_data.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { PrismaClient } from '@prisma/client'
// import myPerson from './person.json' assert { type: "json" };
import myCountry from './country.json' assert { type: "json" };
import myKeyword from './keyword.json' assert { type: "json" };
import myCompany from './company.json' assert { type: "json" };
import myGenre from './genre.json' assert { type: "json" };
import myLanguage from './language.json' assert { type: "json" };
import myUser from './user.json' assert { type: "json" };
import myRating from './rating.json' assert { type: "json" };
import myMovie from './movie.json' assert { type: "json" };


//node import_data.ts
// const myPerson_data = myPerson.map((val) => {
// return {
// id: val.id,
// name: val.name,
// gender: val.gender,
// popularity: val.popularity
// }
// })

const myCountry_data = myCountry.map((val) => {
return {
id: val.id,
iso_3166_1: val.iso_3166_1,
name: val.name
}
})

const myKeyword_data = myKeyword.map((val) => {
return {
id: val.id,
name: val.name
}
})

const myCompany_data = myCompany.map((val) => {
return {
id: val.id,
name: val.name
}
})

const myGenre_data = myGenre.map((val) => {
return {
id: val.id,
name: val.name
}
})

const myLanguage_data = myLanguage.map((val) => {
return {
id: val.id,
iso_639_1: val.iso_639_1,
name: val.name,
}
})

const myUser_data = myUser.map((val) => {
return {
id: val.id,
}
})

const myRating_data = myRating.map((val) => {
return {
id: val.id,
movieId: val.movieId,
userId: val.userId,
rating: val.rating,
createdAt: val.createdAt,
updatedAt: val.updatedAt
}
}).slice(0, 5000);

const myMovie_data = myMovie.map((val) => {
return {
id: val.id,
title: val.title,
overview: val.overview,
runtime: val.runtime,
poster_url: val.poster_url,
release_date: val.release_date,
budget: val.budget,
revenue: val.revenue,
original_language_id: val.original_language_id,
// spoken_languages: val.spoken_languages,
// keywords: val.keywords,
// crew: val.crew,
// genres: val.genres,
// countries: val.countries,
// companies: val.companies,
popularity: val.popularity,
vote_average: val.vote_average,
vote_count: val.vote_count,
// ratings: val.ratings
}
}).slice(0, 5000);

const prisma = new PrismaClient()

async function main() {
// ... you will write your Prisma Client queries here

const toCreate = []
// toCreate.push(prisma.person.createMany({data: myPerson_data, skipDuplicates: true}))
// toCreate.push(prisma.country.createMany({data: myCountry_data, skipDuplicates: true}))
// toCreate.push(prisma.keyword.createMany({data: myKeyword_data, skipDuplicates: true}))
// toCreate.push(prisma.company.createMany({data: myCompany_data, skipDuplicates: true}))
// toCreate.push(prisma.genre.createMany({data: myGenre_data, skipDuplicates: true}))
toCreate.push(prisma.language.createMany({data: myLanguage_data, skipDuplicates: true}))
// toCreate.push(prisma.user.createMany({data: myUser_data, skipDuplicates: true}))
// toCreate.push(prisma.rating.createMany({data: myRating_data, skipDuplicates: true}))
// toCreate.push(prisma.movie.createMany({data: myMovie_data, skipDuplicates: true}))
await prisma.$transaction(toCreate)
}

console.log("start")
// console.log([myGenre[0]])
main()
.then(async () => {
await prisma.$disconnect()
console.log('success')
})
.catch(async (e) => {
console.error(e)
await prisma.$disconnect()
process.exit(1)
})

12 changes: 6 additions & 6 deletions movis/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ model Language {
movies Movie[]
}

model User {
id Int @id @default(autoincrement())
ratings Rating[]
}

model Rating {
id Int @id @default(autoincrement())
movieId Int
userId Int
rating Int
rating Float
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand All @@ -88,8 +93,3 @@ model Rating {
@@index([movieId])
@@index([userId])
}

model User {
id Int @id @default(autoincrement())
ratings Rating[]
}

0 comments on commit 5a137dd

Please sign in to comment.