lowdb-api icon indicating copy to clipboard operation
lowdb-api copied to clipboard

Cannot provide my own id

Open alblandino opened this issue 8 years ago • 5 comments

The structure you use to save the resources automatically generates an incremental ID, in which case you want to save this:

{
    id: 100,
    fname: "Joel",
    lname: "Jaime"
}

I simply omit the ID field since I would try to replace the one generated by lowdb-api.

One suggestion would be that instead of an incremental ID you generate a kind of {_id: guid_format} (or any unique data) or have the identifier inside protected object:

{
    core: {
        id : AUTOGENERATED_ID
    },
    fname: "Joel",
    lname: "Jaime"
}

alblandino avatar Oct 31 '17 14:10 alblandino

Related with the PR #3

alblandino avatar Oct 31 '17 15:10 alblandino

@jblandino Do you think it is common to provide an ID on creation? If that's the case I'm open to consider allowing users to provide id on creation.

As you mentioned, I think it would be also good to allow user to provide a function for id generation. That way, user can opt-in for any kind of identity generator.

What are your thoughts?

rmariuzzo avatar Oct 31 '17 15:10 rmariuzzo

Hi @rmariuzzo.

Well if you knew that when I implemented it in a project that I currently have, the first thing I did was send some session data to see how I could store them temporarily but send these fields:

{ id: Number, _token: String, date: Date }

The ID is a value that I generate in my own application and for me that field is mandatory, it happens that when I save it I realize that lowdb replaces it with its own identifier automatically.

This need was that it led me to think that it would be good to leave the resource identifier outside the object where the user's data is stored.

Note: in this issue I put a different structure to the one I have in the PR, this is because I found the PR form at the last moment and I consider it more feasible.

alblandino avatar Oct 31 '17 15:10 alblandino

@jblandino in that case I'm willing to allow user to provide their id. The flow will be:

  1. user post data.
  2. if the posted data contains id use it, otherwise use generator.
  3. persist posted data.

What do you think? I'm open to suggestions.

rmariuzzo avatar Oct 31 '17 16:10 rmariuzzo

Perfect @rmariuzzo , if the user passes his own ID he could use it and in case he does not pass it then the autogenerate is used, I see it well.

alblandino avatar Oct 31 '17 17:10 alblandino