smykes
smykes
PPrisma
Created by smykes on 12/26/2024 in #help-and-questions
Programatic where clause based off query parameters.
I'm using fastify although I doubt that matters. Attached is pseudoish code. I am looking for a way to programmatically add to the where clause based off query parameters. If there are no query parameters, just do a select * from {table} I have searched the documentation pretty well, and searched blog entries etc, couldn't find an answer: Here is my code with comments in the broken where https://gist.github.com/smykes/deeffc91002340057e107803aa9e58f8 Thank you in advance.
11 replies
PPrisma
Created by smykes on 12/19/2024 in #help-and-questions
Strange type error in seeding file
I have a strange issue with my seeding file.
import { PrismaClient } from "@prisma/client";
import { v4 as uuidv4 } from "uuid";
import BOOK_DATA from "@/data/data.json";
const prisma = new PrismaClient();

async function main() {
BOOK_DATA.forEach((book) => {
if (book["Date Read"] !== "" && book["Date Read"] !== null) {
prisma.book.upsert({
create: {
id: uuidv4(),
author: book["Author"],
avg_rating: book["Average Rating"],
book_id: book["Book Id"],
date_read: book["Date Read"].split("/").join("-"),
isbn: book["ISBN"],
isbn_13: book["ISBN13"],
month_read: parseInt(book["Date Read"].split("/")[1], 10),
number_of_pages: book["Number of Pages"],
publisher: book["Publisher"],
shelf: book["Exclusive Shelf"],
title: book["Title"],
user_rating: book["My Rating"],
year_read: parseInt(book["Date Read"].split("/")[0], 10),
},
});
}
});
}

main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e: Error) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
import { PrismaClient } from "@prisma/client";
import { v4 as uuidv4 } from "uuid";
import BOOK_DATA from "@/data/data.json";
const prisma = new PrismaClient();

async function main() {
BOOK_DATA.forEach((book) => {
if (book["Date Read"] !== "" && book["Date Read"] !== null) {
prisma.book.upsert({
create: {
id: uuidv4(),
author: book["Author"],
avg_rating: book["Average Rating"],
book_id: book["Book Id"],
date_read: book["Date Read"].split("/").join("-"),
isbn: book["ISBN"],
isbn_13: book["ISBN13"],
month_read: parseInt(book["Date Read"].split("/")[1], 10),
number_of_pages: book["Number of Pages"],
publisher: book["Publisher"],
shelf: book["Exclusive Shelf"],
title: book["Title"],
user_rating: book["My Rating"],
year_read: parseInt(book["Date Read"].split("/")[0], 10),
},
});
}
});
}

main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e: Error) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});
title is insisting Type 'string | number' is not assignable to type 'string'. but my schema and migration files clearly state title is a string. Where is this coming from? It's really strange.
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Book {
id String @id
author String
avg_rating Int
book_id Int
createdAt DateTime @default(now())
date_read String
isbn String
isbn_13 String
month_read Int
number_of_pages Int
publisher String
shelf String
title String
user_rating Int
year_read Int
}
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Book {
id String @id
author String
avg_rating Int
book_id Int
createdAt DateTime @default(now())
date_read String
isbn String
isbn_13 String
month_read Int
number_of_pages Int
publisher String
shelf String
title String
user_rating Int
year_read Int
}
11 replies