Giter VIP home page Giter VIP logo

Comments (6)

paraseba avatar paraseba commented on June 8, 2024

There is no concept of order (or next) for DDB primary keys. To define an "ID" you would need both the hashkey and the (optional) range key. The range key has an order for a given hash key, and you could be interested in generating the next one, but notice that the range key itself doesn't constitute an ID. Many objects could share the same range key or hashkey but not both.

If what you are trying to do is to generate the next range key for a given hash key, I think you are out of luck. You either need to track state somehow, or make sure there is no race condition between the moment you read the last id and you write the next one. There is no way in DDB to make a write based on a condition for a different key.

I'm probably misunderstanding your question completely, sorry if that's the case.

from faraday.

Engelberg avatar Engelberg commented on June 8, 2024

Sounds like my question was confusing. In the README, you show that it is vital to manually add an :id (primary key) to the map before inserting it into the database. In the example, you simply appear to be counting up from 0. In a real application, how can you guarantee to pick a new :id value for each new map you add to the database? (For example, some databases establish the primary key as an auto-incrementing integer. Mongo automatically creates a UUID to serve as the id. It's not clear if the UUID approach would work with faraday, because the implication is that id's need to be numbers, not a long string.) So in general, what's the recommended process for guaranteeing a unique ID?

from faraday.

paraseba avatar paraseba commented on June 8, 2024

Oh I see. That's just a toy example. Notice that when the table is being created:

 [:id :n]  ; Primary key named "id", (:n => number type)

that :n is saying that the id attribute is a number, but it could also be a string or binary.

I don't think there is a "safe" way to generate these ids in a distributed system, without storing state somewhere. But also, remember that the hash keys (:id here) don't have inherent order, you can not retrieve a sorted list from DynamoDB for instance.

Long enough random strings/numbers are an option. In my view, generating the keys is definitely beyond the project goals. Speaking for myself here, don't know what is @ptaoussanis 's view on the matter.

from faraday.

ptaoussanis avatar ptaoussanis commented on June 8, 2024

Hi Mark, Sebastián's correct. (Thanks @paraseba!)

Since the primary key also acts as an index, you'll often want to provide something that's meaningful to your application like a username, product code, etc. Otherwise a simple UUID would be quite appropriate.

Note that there'd be no real benefit to having incremental primary ids since DDB automatically partitions over the primary key space and so range queries wouldn't be a possibility anyway. That's where the "range" key comes in handy, as it's designed to play well with the DDB key partitioning.

Does that help / make sense?

from faraday.

Engelberg avatar Engelberg commented on June 8, 2024

Yes, thanks.

from faraday.

ptaoussanis avatar ptaoussanis commented on June 8, 2024

BTW feel free to shout if you run into any other problems, the docs are currently serviceable at best.

from faraday.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.