The following is a suggestion for an improvement to the photon API for relation lists:
Consider a schema like so:
model User {
...other fields
posts Post[]
}
When I want to update a user's posts, I could:
photon.users.update({
where: { id: "U1" },
data: {
posts: {
set: [
{ id: 1 },
{ id: 2 }
]
}
}
})
Note that I'm deliberately using set rather than a combination of connect & disconnect, because I know exactly what I want the posts relation list to be.
If I want to do the same operation while CREATING a user, I would have to do:
photon.users.create({
data: {
posts: {
connect: [
{ id: 1 },
{ id: 2 }
]
}
}
})
Note how I have to rename set to connect here. It would be nice to have a set for relation lists in photon's create operation as well. This would just be an alias for connect, but having it explicitly reduces some level of confusion in the API in my opinion.
Also relevant: scalar & enum lists have a set parameter both during creates and updates, so adding this alias would bring relation lists on par with scalar & enum lists.
I am assuming that it would benefit studio a lot so I am marking this as a candidate.
Wouldn't change a whole lot, but it could potentially make photon query generation a little more straightforward. I mostly pointed this out since I discovered this "gap" fixing a bug yesterday :D
What about the case when I want to connect or create (if it doesn't exist) a nested record on creation?
@vadim-shilov If I understand you correctly, you should be able to do:
photon.users.create({
data: {
posts: {
create: [{
data: {
... post fields that you want to set
}
}]
}
}
})
The create parameter would take the same input as a photon.posts.create call.
More info here: https://github.com/prisma/prisma2/blob/master/docs/photon/api.md#create
@nikolasburk Noticed that there isn't an example to demo nested creates, perhaps we should add one?
For example:
There is relation user:profiles (1:n).
When user authorized via Facebook (or another social network) my app creates a new profile rec (if it doesn't exist) or updates a founded one. If a user rec exists at that moment then the app creates a connection beetwen this two recs. But if it doesn't exist the app creates a new one with connection to profile rec. In your example the 'create' action will throw an error if a user already exists (I use the 'upsert' method to create/update a profile rec).
@vadim-shilov Wouldn't upsert work for you in this case?
I faced this problem:
const profileId = '12345';
const email = '[email protected]';
const avatarUrl = 'https://www.blablabla.com/avatars/user12345';
const nick = 'user12345';
const loggedAt = new Date();
photon.profiles.upsert({
where: {
profileId
},
update: {
avatarUrl,
nick,
loggedAt,
user: {
update: {
loggedAt
}
}
},
create: {
profileId,
email,
avatarUrl,
nick,
user: {
upsert: { // there #upsert is not available (#create and #connect only)!
where: {
email
},
update: {
loggedAt
},
create: {
email,
// other data ...
loggedAt
}
}
}
}
});
Of course, I can make two sequential calls :)
Internal note: dropping because priority
Note: unassigning myself on thing I'm not actively working on. I may pick this up in the future.
This is implemented by now.
Most helpful comment
Wouldn't change a whole lot, but it could potentially make photon query generation a little more straightforward. I mostly pointed this out since I discovered this "gap" fixing a bug yesterday :D