I am unable to save customers, purchased items to my database, I am using sqlite prisma, belwo is my customers.json
[
{
"id": 1,
"firstName": "Emily",
"lastName": "Davis",
"username": "emilydavisxyz",
"password": "password123",
"role": "Customer",
"balance": 1000,
"street": "123 Main St",
"city": "New York City",
"country": "United States",
"zipCode": "10001"
},
{
"id": 2,
"firstName": "Jane",
"lastName": "Doe",
"username": "janedoexyz",
"password": "password123",
"role": "Customer",
"balance": 750,
"street": "456 Elm St",
"city": "London",
"country": "United Kingdom",
"zipCode": "32493"
},
{
"id": 3,
"firstName": "David",
"lastName": "Brown",
"username": "davidbrownxyz",
"password": "password123",
"role": "Customer",
"balance": 500,
"street": "789 Oak St",
"city": "Sydney",
"country": "Australia",
"zipCode": "20200"
},
{
"id": 4,
"firstName": "Noah",
"lastName": "Thomas",
"username": "noahthomasxyz",
"password": "password123",
"role": "Customer",
"balance": 1200,
"street": "101 Pine St",
"city": "Paris",
"country": "France",
"zipCode": "75001"
},
{
"id": 5,
"firstName": "Ava",
"lastName": "Lee",
"username": "avaleexyz",
"password": "password123",
"role": "Customer",
"balance": 850,
"street": "111 Cedar St",
"city": "Tokyo",
"country": "Japan",
"zipCode": "45739"
}
]
now this is my purchased-items.json
[
{
"purchaseId": 1,
"userId": 1,
"itemsPurchased": [
{
"isbn": "9780316769488",
"quantity": 1,
"imageUrl": "to_kill_a_mockingbird.jpg",
"title": "To Kill a Mockingbird",
"price": 10.99
},
{
"isbn": "9781984818653",
"quantity": 1,
"imageUrl": "the_silent_patient.jpg",
"title": "The Silent Patient",
"price": 12.99
}
]
},
{
"purchaseId": 2,
"userId": 1,
"itemsPurchased": [
{
"isbn": "9784012345678",
"quantity": 3,
"imageUrl": "whispers_in_the_attic.jpg",
"title": "Whispers in the Attic",
"price": 14.25
}
]
},
{
"purchaseId": 3,
"userId": 2,
"itemsPurchased": [
{
"isbn": "9786021978321",
"quantity": 2,
"imageUrl": "atomic_habits.jpg",
"title": "Atomic Habits",
"price": 14.99
},
{
"isbn": "9782753981642",
"quantity": 1,
"imageUrl": "the_power_of_meaning.jpg",
"title": "The Power of Meaning",
"price": 13.75
},
{
"isbn": "9781982117801",
"quantity": 1,
"imageUrl": "educated.jpg",
"title": "Educated",
"price": 15.99
}
]
},
{
"purchaseId": 4,
"userId": 4,
"itemsPurchased": [
{
"isbn": "9786021978321",
"quantity": 3,
"imageUrl": "atomic_habits.jpg",
"title": "Atomic Habits",
"price": 14.99
},
{
"isbn": "9781984818653",
"quantity": 2,
"imageUrl": "the_silent_patient.jpg",
"title": "The Silent Patient",
"price": 12.99
}
]
}
]
This is the prisma schema I made: in schema.prisma client version
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model Books {
id Int @id @default(autoincrement())
isbn String @unique
title String
author String
description String
genres String
publishedDate String
pageCount Int
imageUrl String
price Float
sales Int
availableQuantity Int
sellerID Int
Sellers Sellers @relation(fields: [sellerID], references: [id], onDelete: Cascade, onUpdate: Cascade)
}
model Sellers {
id Int @id @default(autoincrement())
firstName String
lastName String
username String
password String
role String
bankAccount String
companyName String
books Books[] //books the seller is selling
}
model Customers {
id Int @id @default(autoincrement())
firstName String
lastName String
username String
password String
role String
balance Int
street String
city String
country String
zipCode String
purchaseditem purchasedItem[]
}
model purchasedItem {
id Int @id @default(autoincrement())
userId Int
itemsPurchased Item[]
Customers Customers? @relation(fields: [userId], references: [id])
}
model Item {
isbn String @unique
quantity Int
imageUrl String
title String
price Float
purchasedItemId Int @unique
purchasedItem purchasedItem @relation(fields: [purchasedItemId], references: [id], onDelete: Cascade, onUpdate: Cascade)
}
now finally this is how I am trying to seed in seed.js, but it is not working, what am i doing wrong how to fix?? please helpp
import fs from 'fs-extra'
import path from 'path'
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
const bookspath=path.join(process.cwd(),'app/data/books.json')
const sellerpath=path.join(process.cwd(),'app/data/sellers.json')
const customerpath=path.join(process.cwd(),'app/data/customers.json')
const purchasepath=path.join(process.cwd(),'app/data/purchased-items.json')
async function addBooks(b) {
b.genres=b.genres.join(',');
await prisma.books.create({data:b})
//for retreiving as array use b.genres=b.genres.split(',')
}
async function savedata(itemsPurchased){
try{
const s=JSON.stringify(itemsPurchased);
await prisma.purchasedItem.create({data:s});
console.log("Data saved successfully")
} catch(error){
console.error('error saving data',error)
}
}
async function main(){
try{
await prisma.books.deleteMany({});
await prisma.sellers.deleteMany({});
await prisma.customers.deleteMany({});
await prisma.purchasedItem.deleteMany({});
await prisma.item.deleteMany({});
const seller=await fs.readJSON(sellerpath)
for(const s of seller)
await prisma.sellers.create({data:s})
const book=await fs.readJSON(bookspath)
for(const b of book)
await addBooks(b) //this method will read according to data type
const customer=await fs.readJSON(customerpath)
for(const c of customer){
await prisma.customers.create({data:c})
const purchases=await fs.readJSON(purchasepath)
for(let p of purchases){
if(p.itemsPurchased){
savedata(p.itemsPurchased)
}
else{
}
}
}
console.log("successfully seeded")
}
catch(error){
console.log(error);
}
}
await main()
I already added everything above, I want to save this data, so when i type npx prisma studio it shows me there properly, I don’t know how to use postgressql thats why I want to use sqlite to implement this
kashaf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.