adamjkb / bark

Materialized path extension for Prisma
https://prisma-extension-bark.gitbook.io
MIT License
33 stars 5 forks source link

Using Bark with multi-organisation database schemas #89

Closed AmruthPillai closed 6 days ago

AmruthPillai commented 6 days ago

Hey there, I faced an issue when implementing the Bark extension with our app.

I see that path is required to be a unique field across the database for Bark to work as expected, but the thing is, we have multiple organisations under the same database (not so multi-tenant, but just B2B). So because of this, I had to remove the unique constraint on path and set a @@unique([path, locationId]) constraint instead, but this seems to break the implementation of createChild as it tries to insert a row using findUnique:

Argument `where` of type WorkOrderWhereUniqueInput needs at least one of `id`, `id_locationId` or `path_deleted_locationId` arguments. Available options are marked with ?.
PrismaClientValidationError: 

Is there a work around to this?

AmruthPillai commented 6 days ago

It's specifically because of this line: https://github.com/adamjkb/bark/blob/eb664d11c833719a5f63ea7a9c73fb236d15f57d/packages/prisma-extension-bark/src/create/create-child.js#L68C4-L77C6

Where there is a query on just path. Instead, if it would be possible to reuse the same query that was provided in where or node, that just might work... I'm not too sure if it has other consequences though.

adamjkb commented 6 days ago

From a technical perspective path is required to be unique because we can't have two nodes pointing to same location in the tree. If my assumptions are correct what you're trying to do here is to create multiple roots, each representing an organization. Essentially you would no longer need locationId, at least not on the tree model; you might need to make make note of the root path on related models.

Hopefully that makes sense!

AmruthPillai commented 6 days ago

Ah, I see. That makes sense. I still would have location ID as we use it for other relations, but basically the path field would be the only universal field across the database. And since everyone would be creating multiple "root" nodes or children of them, it should be fine.

Thank you for explaining the construct :) Much appreciated!