Allow to use UUIDs as primary keys
At this moment in time primary keys have to ne numeric:
The id of the document has to be numeric
I like to use https://github.com/ramsey/uuid to generate UUIDs for my entities, but I'm not able to use them as my primary key because they are not numeric. Is there a reason why there's such a restriction?
Hi @robiningelbrecht
Thank you for your interest and feature request.
When refactoring the project there were some considerations that lead to that decision.
- Backwards compatibility - Version 1 of SleekDB already had that restriction, so changes to that functionality need to be done well and backwards compatible.
- Ease of use and implementation - Currently SleekDB internally increase a numeric counter by one for every "storing operation" to generate a unique id.
- Consistency and abstraction - A half baked implementation could lead to inconsistent code and behaviour.
-
Additional Dependency - We try to avoid any kind of dependency. That's why we could offer a kind of API to hook into the standard behaviour but can't provide additional methods like with implementing
ramsey/uuid
We thought about implementing a "string id" feature but didn't have the time to do so.
That said, If I remember correctly you can use the updateOrInsert() and updateOrInsertMany() methods to insert documents with your own ID.
https://sleekdb.github.io/#/insert-data#updateOrInsert
If you want to implement that feature you are welcome to do so :blush:
The _id generation is already encapsulated into a single method.
https://github.com/rakibtg/SleekDB/blob/ecbf9224d7dd6214bce76da1dae8bcbd583e464e/src/Store.php#L837
You could extract that into it's own class and allow the user to implement their own functionality by providing a closure, custom class using an interface or something like that as a configuration option when creating a Store-Object.
Feel free to join our discord server if you need help or want to discuss details.
Hi @Timu57,
I certainly don't wat to introduce ramsey/uuid as a dependency in the codebase. I was just wondering why strings are not supported as primary keys. I'll join the Discord server to discuss this PR :)