Skip to content

Allow to use UUIDs as primary keys #239

Open

Description

@robiningelbrecht

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?

Activity

  1. added
    question
    on Jul 29, 2022
  2. Timu57 commented on Jul 29, 2022

    @Timu57
    Member

    Hi @robiningelbrecht

    Thank you for your interest and feature request.

    When refactoring the project there were some considerations that lead to that decision.

    1. Backwards compatibility - Version 1 of SleekDB already had that restriction, so changes to that functionality need to be done well and backwards compatible.
    2. Ease of use and implementation - Currently SleekDB internally increase a numeric counter by one for every "storing operation" to generate a unique id.
    3. Consistency and abstraction - A half baked implementation could lead to inconsistent code and behaviour.
    4. 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 😊

    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.

  3. robiningelbrecht commented on Jul 31, 2022

    @robiningelbrecht
    Author

    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 :)

  4. CJS-Design commented on Jan 2, 2025

    @CJS-Design

    Hi. I landed here because of the error I recieved as I tried to use my own primary key:

    Fatal error: Uncaught SleekDB\Exceptions\InvalidArgumentException: **The id of the document has to be numeric** in /SleekDB/Store.php:883 Stack trace: #0 /SleekDB/Store.php(384): SleekDB\Store->checkAndStripId('test1') #1 some.php(102): SleekDB\Store->updateOrInsertMany(Array, false) #2 {main} thrown in /SleekDB/Store.php on line 883

    I configured the database with '"primary_key" => "url"', and tried to use $results = $contentStore->updateOrInsertMany($content, false);.

    I did this because in the documentation under primary key it is written:
    #Set the primary key into any string you want.
    #SleekDB will then use that key instead of the default _id key.

    See: https://sleekdb.github.io/#/configurations#primary_key

    To mee this looks as it is not supported (but I just started using SleekDB, so high probability of me understanding something wrong. Also: Thanks for your work!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionfeaturequestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions