rooteco / tweetscape

the supercharged twitter feed
https://prototype.tweetscape.co
GNU Affero General Public License v3.0
18 stars 2 forks source link

Use Prisma nested writes instead of directly inserting into relation tables #318

Closed nicholaschiang closed 2 years ago

nicholaschiang commented 2 years ago

Imported from @nicholaschiang's original Linear issue TS-87.

nicholaschiang commented 2 years ago

In app/routes/index.ts, I tried doing something like:

      await db.influencers.upsert({
        create: { 
          ...influencer,
          tokens: {
            connectOrCreate: {
              create: token,
              where: { influencer_id: token.influencer_id },
            },
          },
        },
        update: influencer,
        where: { id: influencer.id },
      });

But that won't upsert the token row if it already exists, which is what needs to happen. Currently, Prisma doesn't support a nested upsert within an upsert (you can only nest upserts within an update I think): https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#upsert-1

nicholaschiang commented 2 years ago

Prisma also doesn't seem to support many-to-many nested relation creation.