chrisdhanaraj
03/28/2021, 2:10 AMconnect
is just an abstraction? (https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries/#connect-an-existing-record)?
i.e., I let's say I have
modal Author {
id
posts. Post[]
}
modal Post {
id
authorId. Int
author. Author. @relation(fields: whatever)
}
Let's say I create a new Author and want to swap some posts to them. Is there a functional difference between
prisma.author.create({
data: {},
posts: connect{}
}
vs
prisma.author.create({ data}
prisma.posts.updateMany({
where: {
authorId
},
data: {}
}
Mykyta Machekhin
03/28/2021, 11:23 PMIan Ray
03/29/2021, 1:30 AMprisma introspect
. I wanted to double check this doesnāt already exist.
raw introspect output might yield
...
model table_name {
field_name Int @annotations(for()) @days
}
...
prettified output (specifically with snake_case
table/field names improved)
...
model TableName {
fieldName Int @annotations(for()) @days @map("field_name")
@@map("table_name")
}
...
Before I go any further, does this already exist in the ecosystem? I couldnāt find anything in @prisma/sdk
Natalia
Nick
03/29/2021, 11:00 PMmodel User {
id Int @id @default(autoincrement())
name String
email String @unique
friendedBy User[] @relation("Friends", references: [id])
friended User[] @relation("Friends", references: [id])
}
And the generated postgres tables look like -
CREATE TABLE public."User" (
email text NOT NULL,
name text NOT NULL,
id integer NOT NULL
);
ALTER TABLE
public."User"
ADD
CONSTRAINT "User_pkey" PRIMARY KEY (id)
CREATE TABLE public."_Friends" ("B" integer NOT NULL, "A" integer NOT NULL);
CREATE UNIQUE INDEX "_Friends_AB_unique" ON "_Friends"("A", "B");
CREATE INDEX "_Friends_B_index" ON "_Friends"("B");
ALTER TABLE "_Friends" ADD FOREIGN KEY ("A") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "_Friends" ADD FOREIGN KEY ("B") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
The trouble I'm having is adding friends to a user. This is what I'm trying -
await prisma.user.update({
where: {
id: input.id,
},
data: {
friends: {
create: { A: input.id, B: input.friendId },
},
},
})
It doesn't work and I suspect I might need a connect in there. Any suggestions?Nick
03/29/2021, 11:19 PM_friends
-
await prisma.friend.create({
data: {
connect: [
{
id: input.id,
},
{
id: input.friendId,
},
],
},
})
Nick
03/29/2021, 11:37 PMawait prisma.user.update({
where: {
id: input.id,
},
data: {
friended: {
connect: {
id: input.friendId,
},
},
},
})
Now I just need to figure out how to query on the friends for a user. I thought that I could just use a query like this -
type User {
id: Int!
name: String!
email: String!
friended: [Int]
friendedBy: [Int]
}
type Query {
users: [User!]!
}
But I just get nulls back for friended
and friendedBy
Nick
03/30/2021, 3:55 AMmodel User {
id Int @id @default(autoincrement())
name String
email String @unique
games UserGame[]
friends User[] @relation("Friends")
friendOf User[] @relation("Friends")
}
type User {
id: Int!
name: String!
email: String!
games: [UserGame]!
friends: [User]
friendOf: [User]
}
type Query {
user(email: String): User
users: [User!]!
}
input CreateUserInput {
name: String!
email: String!
}
input UpdateUserInput {
name: String
email: String
}
input FriendInput {
id: Int!
friendId: Int!
}
type Mutation {
createUser(input: CreateUserInput!): User
addFriend(input: FriendInput!): User
removeFriend(input: FriendInput!): User
}
export const user = ({ email }) => {
return db.user.findUnique({
where: { email },
include: { friends: true, friendOf: true },
})
}
export const users = () => {
return db.user.findMany({ include: { friends: true, friendOf: true } })
}
export const User = {
games: (_obj, { root }) =>
db.user.findUnique({ where: { id: root.id } }).games(),
}
export const addFriend = ({ input }) => {
return db.$transaction([
db.user.update({
where: {
id: input.id,
},
data: {
friends: {
connect: {
id: input.friendId,
},
},
},
}),
db.user.update({
where: {
id: input.friendId,
},
data: {
friends: {
connect: {
id: input.id,
},
},
},
}),
])
}
export const removeFriend = ({ input }) => {
return db.$transaction([
db.user.update({
where: {
id: input.id,
},
data: {
friends: {
disconnect: {
id: input.friendId,
},
},
},
}),
db.user.update({
where: {
id: input.friendId,
},
data: {
friends: {
disconnect: {
id: input.id,
},
},
},
}),
])
}
mutation {
addFriend(input: { id: 4, friendId: 3 }) {
id
}
}
mutation {
removeFriend(input: { id: 4, friendId: 3 }) {
id
}
}
query {
users {
id,
name,
email
friends {
id
name
}
friendOf {
id,
name
}
}
}
cfree
03/30/2021, 6:24 AMthe info object can't be used to resolve relations automatically any more, instead you'll need to implement your type resolvers to ensure that relations get resolved properly.Are there any examples of this anywhere? Ref: https://www.prisma.io/docs/guides/upgrade-guides/upgrade-from-prisma-1/upgrading-prisma-binding-to-sdl-first/
Marko
03/30/2021, 12:03 PMError in PostgreSQL connection: Error { kind: Closed, cause: None }
(going to attach log in the thread). I am using a single prisma client to access everything and am using version 2.19.0
and node 12. Can't find any issues, so any help would be appreciated.Natalia
Tomas Diaz
03/30/2021, 8:57 PMTomas Diaz
03/30/2021, 8:59 PMTomas Diaz
03/30/2021, 9:16 PMEthan Zoller
03/31/2021, 12:58 AMTorrino
03/31/2021, 9:04 AMTorrino
03/31/2021, 9:27 AMAlistair Smith
03/31/2021, 2:29 PMJohn Cantrell
03/31/2021, 7:37 PMMartĆÆn
03/31/2021, 9:07 PMGabriel Chase
04/01/2021, 2:19 AM*import* { User } *from* 'prisma/generated/type-graphql/models'
*import* { UserCreateInput } *from* 'prisma/generated/type-graphql/resolvers/inputs'
There are times i see when people use @generated
which links to /node_modules/@generated
I'm just wondering which one is standard and why is the User
from prisma/generated.../models
different from @generated/.../models
?Etel
James Lee
04/01/2021, 7:13 PMjest-mock-extended
and using mockDeep
to create typed mocks. However, after upgrading to latest 2.20.1 from 2.19.0, I'm receiving a typescript error.
Type of property 'OR' circularly references itself in mapped type '{ [K in keyof { AND?: Enumerable<WorkplaceScalarWhereWithAggregatesInput>; OR?: Enumerable<WorkplaceScalarWhereWithAggregatesInput>; ... 12 more ...; updatedAt?: string | ... 1 more ... | DateTimeWithAggregatesFilter; }]: Or<...> extends 1 ? { ...; }[K] extends infer TK ? GetHavingFields<...> : never : {} extends Fi...'.
El
04/01/2021, 10:27 PMMessageRead
to `Message`:
model Message {
id String @id @default(uuid())
content String
createdAt DateTime @default(now()) @map("created_at")
sender User @relation(fields: [senderId], references: [id])
senderId Int @map("sender_id")
read MessageRead?
@@map("message")
}
model MessageRead {
id String @id @default(uuid())
seenAt DateTime @default(now()) @map("seen_at")
seenMessage Message @relation(fields: [seenMessageId], references: [id])
seenMessageId String @map("seen_message_id")
user User @relation(fields: [userId], references: [id])
userId Int @map("user_id")
@@map("message_read")
}
MessageRead
keeps track of the User
latest read message.
I'm currently trying to figure out why this doesn't update the connection of MessageRead
`seenMessage`:
await this.prisma.messageRead.upsert({
where: {
id
},
update: {
seenAt: new Date(),
seenMessage: {
connect: {
id: message.id,
},
},
},
create: {
user: {
connect: {
id: user.id,
},
},
seenMessage: {
connect: {
id: message.id,
},
},
},
});
Imad at
04/02/2021, 5:03 PMImad at
04/02/2021, 5:09 PMImad at
04/02/2021, 5:09 PMPrismaClientValidationError:
`Invalid prisma.post.create()
invocation:`
{
data: {
title: 'test',
content: 'test',
author: {
connect: {
email: null
~~~~
}
}
}
}
Isidoro Ferreiro
04/02/2021, 9:24 PM