adamjkb / bark

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

Just some thoughts #61

Closed donnguyen closed 10 months ago

donnguyen commented 10 months ago

Hi Adam,

Thank you for your great work. I have some thoughts and proposals to share while testing this out.

Instead of using its own path, why don't we use the record's ID to build the paths? There are some benefits to using this approach:

We wouldn't need to have createRoot. If the path is empty, it is the root. This would save us a query to the DB to find lastRoot. It would be easier to adopt the extension. Say we already have data in the model we want to use this. It is kind of hard to adopt at this stage because we need to rebuild path for all existing records, but if we use the proposed approach, they will all be root records, saving us from having to remove all existing data. It contrasts with what you stated: "it's quite obvious how inefficient it is to use human language to encode something that really, only computers should care about." However, I think it is sometimes really helpful/easier to troubleshoot/understand how things happened. What are your thoughts?

adamjkb commented 10 months ago

Thanks for the feedback!

One of the main benefit of using base36 encoding for the path is that it also encapsulates an custom order within the siblings. An use case I could think of is having a Todo list where each item can sub items and you want people to be able to reorder tasks to their liking. (this would be pretty simple to achieve with the move() operation)

If we were to use IDs we would lose this order unless another field is added to keep track of relations on each level.

But to your point it being easier to understand how things happen: a path and its segments (0001, ...) is more legible than a lot of concatenated IDs but tells you just about the same amount of information because even if you pick out an ID from the path it would require you to query for it.


As for migration to this extension if your dataset is large and it would be too slow to run createRoot over and over in a loop you have the option to manually set the path from the index of an array. You can find a int2str utility in the repo that is used to create these paths.