Support
Quality
Security
License
Reuse
Coming Soon for all Libraries!
Currently covering the most popular Java, JavaScript and Python libraries. See a SAMPLE HERE.
kandi's functional review helps you automatically verify the functionalities of the libraries and avoid rework.
If Prisma currently doesn't have a certain feature, be sure to check out the roadmap to see if this is already planned for the future.
The Prisma schema
// Data source
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
// Generator
generator client {
provider = "prisma-client-js"
}
// Data model
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
Accessing your database with Prisma Client
npm install @prisma/client
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types
// your data
let newsLetters = [
{
id: 'your-id',
email: 'email@example.com',
createdAt: new Date()
}
];
// map the array
newsLetters.map(x => {
x.createdAt = Math.floor(x.createdAt / 1000);
return x;
})
// use newsLetters now
console.log(newsLetters);
-----------------------
for (const element of newsLetters) {
element.createdAt = element.createdAt.toString()
}
How to create a custom health check for Prisma with @nestjs/terminus?
prisma.$queryRaw`SELECT 1`
-----------------------
import { Injectable } from "@nestjs/common";
import { HealthCheckError, HealthIndicator, HealthIndicatorResult } from "@nestjs/terminus";
import { PrismaService } from "./prisma.service";
@Injectable()
export class PrismaHealthIndicator extends HealthIndicator {
constructor(private readonly prismaService: PrismaService) {
super();
}
async isHealthy(key: string): Promise<HealthIndicatorResult> {
try {
await this.prismaService.$queryRaw`SELECT 1`;
return this.getStatus(key, true);
} catch (e) {
throw new HealthCheckError("Prisma check failed", e);
}
}
}
Prisma using 1-1 and 1-n relation with same model
model Product {
id String @id @default(uuid()) @db.VarChar(36)
image_id String @unique
// image ProductImage @relation(name: "main_image", fields: [image_id], references: [id])
image ProductImage @relation(name: "main_image", fields: [image_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
images ProductImage[]
slug String @unique(map: "slug") @db.VarChar(255)
title String @db.VarChar(255)
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
@@map("product")
}
model ProductImage {
id String @id @default(uuid()) @db.VarChar(36)
src String @db.VarChar(255)
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
product_id String
product Product @relation(fields: [product_id], references: [id])
product_main_image Product[] @relation("main_image")
@@map("product_image")
}
NX NestJs - Unexpected error: Error: Unable to load hasher for task "api:serve"
"executor": "@nrwl/node:execute" -> "executor": "@nrwl/node:node"
"executor": "@nrwl/node:build" -> "executor": "@nrwl/node:webpack"
How to link many to many items in Prisma without duplicates
model MenuProducts {
menu_id Int
menu Menu @relation(fields: [menu_id], references: [id])
product_id Int
product Product @relation(fields: [product_id], references: [id])
created_at DateTime @default(now())
updated_at DateTime @updatedAt
@@map("menu_products")
@@id([product_id, menu_id])
@@unique([product_id, menu_id])
}
creating objects for 1:n relation with prisma
model Fish {
name String @id
boardFish BoardFish? // removed autogenerated `[]` and made optional
}
model BoardFish {
id Int @id @default(autoincrement())
name String
fishType Fish @relation(fields: [name], references: [name])
}
const fish = await prisma.boardFish.create({
data: {
fishType: {
connect: { name: 'salmon' }, // or `connectOrCreate` if it doesn't exist
},
}
})
-----------------------
model Fish {
name String @id
boardFish BoardFish? // removed autogenerated `[]` and made optional
}
model BoardFish {
id Int @id @default(autoincrement())
name String
fishType Fish @relation(fields: [name], references: [name])
}
const fish = await prisma.boardFish.create({
data: {
fishType: {
connect: { name: 'salmon' }, // or `connectOrCreate` if it doesn't exist
},
}
})
When I run nest.js, I get a Missing "driver" option error
@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloDriver,
}),
],
})
How to resolve 'getUserByAccount is not a function' in next-auth?
npm uninstall next-auth @next-auth/prisma-adapter
npm install @next-auth/prisma-adapter
-----------------------
npm uninstall next-auth @next-auth/prisma-adapter
npm install @next-auth/prisma-adapter
Prisma client not marking property as null
prisma-script> select id, "oauthData" from "User"
+---------+-------------+
| id | oauthData |
|---------+-------------|
| 124124 | null |
| 1241241 | <null> |
+---------+-------------+
Prisma, select row from table 1, depending on the latest foreign key in table 2
let publication = await prisma.publication.findFirst({
where: {
id
},
include: {
publicationStatus: {
orderBy: {
createdAt: 'desc'
},
take: 1
}
}
});
// check publication.publicationStatus[0].status and handle appropriately
QUESTION
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types
Asked 2022-Mar-31 at 18:49I am using prisma and Next.js. When I try to retrieve the content from prisma in getStaticProps
it does fetch the data but I can't pass it on to the main component.
export const getStaticProps = async () => {
const prisma = new PrismaClient();
const newsLetters = await prisma.newsLetters.findMany();
console.log(newsLetters);
return {
props: {
newsLetters: newsLetters,
},
};
};
As you can see in this image it is fetching as well as printing the content.
But when I pass I get the following error for passing it as props
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.
ANSWER
Answered 2021-Dec-22 at 12:43Looks like nextJS doesn't like serializing anything but scalar types for performance reasons. You can read more in this github issue. Best way you can handle this is that you convert your Date objects to UNIX timestamp before returning them.
// your data
let newsLetters = [
{
id: 'your-id',
email: 'email@example.com',
createdAt: new Date()
}
];
// map the array
newsLetters.map(x => {
x.createdAt = Math.floor(x.createdAt / 1000);
return x;
})
// use newsLetters now
console.log(newsLetters);
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit