Skip to content

How to get auto assigned id #410

Description

@Solido

Using gcloud datastore save leave no indication about the generated ID for the cell.
Nothing in the call back or the key or the data.

Activity

  1. ryanseys commented on Feb 24, 2015

    @ryanseys
    Contributor

    The key will be automatically populated with the id in its path after the save call. See the docs here for more information.

    Excerpt:

    If a key is incomplete, its associated object is inserted and the original Key object is updated to contain the generated ID.

  2. stephenplusplus commented on Feb 24, 2015

    @stephenplusplus
    Contributor

    What version of gcloud-node are you using, and can you paste what your call to .save looks like and the returned values to the callback supplied?

  3. Solido commented on Feb 25, 2015

    @Solido
    Author

    Thanks for the replies !

    Based on the examples of Todo (source, best doc ever) i've used the pop() method on the path to get the ID and now it's working.

    Coming for Angular and others JS api i'm quite disturbed with this API paradigm.
    I was expecting something like promise or a success and error callback method that send the new ID.

    Something like save ( ..., onSuccess(id), onFail(err)) and not save (..., cb(err)) if (!err) path.pop().

    Or save(...).then(dataFilledWithId)

    Have to said this API is quite powerful but sparse on docs. I got my main API running on a java GAE and using GCloud to control it from a nodejs front, it's really cool but it leaves me with a lot of questions on how to use it correctly !

    Thanks for the job done :)

  4. ryanseys commented on Mar 1, 2015

    @ryanseys
    Contributor

    The idea is that you create a key object and then you use it to save an object, and later you can use that exact same key object to reference that specific object for future operations.

    Perhaps we should add some helper methods to the Key object such as getId() ? Would that help?

  5. stephenplusplus commented on Mar 2, 2015

    @stephenplusplus
    Contributor

    From an old discussion: #175 (comment)

  6. ryanseys commented on Apr 29, 2015

    @ryanseys
    Contributor

    Is this resolved? I believe it's relatively straightforward to get the auto-assigned id from the key but let me know if that's not the case.

  7. byrneciaran commented on May 8, 2015

    @byrneciaran

    Is this a bug in the demo ? ... looking at the Todo app sample code the response doesn't return a key. It returns a mutation_result so key.path.pop(); doesn't work. The key should be initialised outside the save function and then the id can be retrieved.

    ds.save({
          key: ds.key(['TodoList', LIST_NAME, 'Todo']),
          data: data
        }, function(err, key) {
          if (err) {
            callback(err);
            return;
          }
          data.id = key.path.pop();
          callback(null, data);
        });
  8. stephenplusplus commented on Nov 23, 2015

    @stephenplusplus
    Contributor

    I ran the example and key.path was correct: [ 'TodoList', 'default-list', 'Todo', 5720605454237696 ]

    We do need a better way to get the key and various other helpers. We'll use #574 to track that.

  9. added a commit that references this issue on Sep 27, 2022
  10. 26 remaining items

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

Metadata

Metadata

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions