Jonathan Gotti
05/12/2022, 1:44 PMJonathan Selander
05/12/2022, 1:47 PMChris Bitoy
05/12/2022, 2:39 PMPrismaClient
. Although I didn’t quite agree because I have seen online people (including folks at Prisma) set it up the same way I did on multiple tutorial videos. I usually set it up like this in every single file that I needed to use Prisma:
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
However, he insisted that "the best practice is to create the prisma client once and import it - this way you're not recreating database connections if multiple files use prisma, etc"
Can you advice on how best to set it up - still learningYaakov
05/12/2022, 3:08 PMCROSS JOIN
in Prisma?shahrukh ahmed
05/12/2022, 4:23 PMChris Bitoy
05/12/2022, 9:13 PM<http://railway.app|railway.app>
- which is working out well for me. However, I set up a Prisma data proxy
on my app with the AWS connection string, and now that I don’t seem to want/ need it anymore I removed it but getting an error:
error - InvalidDatasourceError: Datasource URL should use Prisma:// protocol.
If you are not using the Data Proxy, remove the data proxy from the preview features in your
schema and ensure that PRISMA_CLIENT_ENGINE_TYPE environment variable is not set to data proxy.
Since getting the error I have removed previewFeatures = ["dataProxy"]
from the prisma.schema
file to make it look like this (back to what it was before configuring with dataproxy):
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url= env("DATABASE_URL")
}
but the error still persists, how do I fix this?Tricked dev
05/12/2022, 10:04 PMKIM SEI HOON
05/13/2022, 4:00 AMWiput Pootong
05/13/2022, 10:22 AMThom
05/13/2022, 12:32 PMChris Bitoy
05/13/2022, 1:03 PMprisma.findMany
and got this error:
error - Error: Error serializing `.categories[0].createdAt` returned from `getServerSideProps` in "/".
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.
How do I fix this?Martin Pinnau
05/13/2022, 1:05 PMyarn add @graphql-yoga/node
. If I migrate to graphql-yoga v2 I get the error that nexus is needed, but this breaks nexus-plugin-prisma. Could someone please help me ? 😉
This is my index.ts:
require("dotenv").config();
import { PrismaClient } from "@prisma/client";
import * as path from "path";
import { nexusSchemaPrisma } from "nexus-plugin-prisma/schema";
import { makeSchema } from "@nexus/schema";
import { createServer } from "@graphql-yoga/node";
import { checkEnv } from "./src/utils";
import { getClaims } from "./src/auth";
import config from "./config";
import { Query, Mutation } from "./src/types";
checkEnv(config.requieredEnvs);
const schema = makeSchema({
types: [Query, Mutation],
plugins: [
nexusSchemaPrisma({
experimentalCRUD: true,
}),
],
outputs: {
schema: path.join(__dirname, "./src/generated/schema.graphql"),
typegen: path.join(__dirname, "./src/generated/nexus.ts"),
},
});
const serverOptions = {
port: process.env.MAIL_PORT || 4000,
};
const server = createServer({
schema,
context: async (req) => {
return {
...req,
PrismaClient,
claims: await getClaims(req),
options: serverOptions,
};
},
});
server.start();
console.log(`🚀 Server is running on <http://localhost>:${serverOptions.port}`);
Chris Bitoy
05/13/2022, 4:00 PMUser model
with enum roles:
1. Admin
2. siteUser
3. Moderator
If I want one siteUser
to be able to select and add (multiple) Moderators
as their favs, I could do a one-many relationship, but because they are both in the ‘User model’ it’s a little challenging - How can I implement this logic in my prisma.schema
file?Jason Kleinberg
05/13/2022, 7:11 PMShahid Mawji
05/13/2022, 7:21 PMimport { prisma } from "../prisma";
await prisma.$transaction(async (prisma) => {
const booking = await prisma.bookingGroup.create(..)
EmailService.sendEmail(booking) //If this function also has a prisma call, the transaction fails
});
I'm unsure if theres a best practice we should be following here (we're looking to keep our code concise but this seems to be a limitation of abstracting out logic if it involves the database)Jason Kleinberg
05/13/2022, 7:53 PMJason Kleinberg
05/13/2022, 7:54 PMAngel C
05/14/2022, 1:50 AMOmar
05/14/2022, 2:38 AMAaron Waller
05/14/2022, 10:51 AMreturn prisma.user.findUnique({
where: {
id: args.id,
},
select: {
posts: true
},
})
The return type of the query is [Posts!]
and I get this error
Expected Iterable, but did not find one for field \"Query.testQuery\"
chrisdrackett
05/14/2022, 8:07 PMchrisdrackett
05/14/2022, 8:07 PMSlackbot
05/15/2022, 2:37 PMAlexis
05/15/2022, 2:40 PMconst user = {
id: 0102932023202,
profile: {
// This would be JSONB
},
inventory: [] // This is also JSONB
badges: [] // This is also JSONB
}
Oleg Komarov
05/15/2022, 2:58 PMAaron Waller
05/15/2022, 3:45 PMmodel User {
uid String @id @default(uuid())
username String? @db.VarChar(24) @unique
favorites Post[]
model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
favoritedBy User[]
}
How does the mutation look like for adding a favorite to the user ?Alisher BugDeveloper
05/15/2022, 4:26 PMAaron Waller
05/15/2022, 6:20 PMconnect
keyword only used for explicit m-n relations or also implicit ones?Motdde
05/15/2022, 11:29 PMasync function handler(req: NextApiRequest, res: NextApiResponse<Response>) {
await verifyUserToken(req, res)
const automobileQuery = req.query.automobile.toString().split(',')
if (automobileQuery.length > 2) {
throw new CustomError(
400,
'automobile URL parameter expects a maximum of two values'
)
}
let values: VehicleType[] | undefined
if (automobileQuery.includes('BIKE')) {
if (values === undefined) values = []
values.push(VehicleType.BIKE)
}
if (automobileQuery.includes('CAR')) {
if (values === undefined) values = []
values.push(VehicleType.CAR)
}
let vehicleType = undefined
let orders = await prisma.order.findMany({
where: {
vendorId: null,
trackingStatus: 'READY_FOR_PICKUP',
vehicleType: { in: values },
},
})
return res.json({
status: true,
data: { orders },
})
}
Jacob Lloyd
05/16/2022, 4:26 AM