Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving multiple Entities #29

Closed
lostpebble opened this issue Dec 6, 2016 · 6 comments
Closed

Saving multiple Entities #29

lostpebble opened this issue Dec 6, 2016 · 6 comments

Comments

@lostpebble
Copy link

Hi there,

I see you've got the equivalent of the datastore.save( entity ) method, which we can access in this library the other way round with entity.save(); - returning a promise or passing in a callback if we like to save a single element.

I'm wondering if there is anything which would be the equivalent of datastore.save( entities[] )

For example, in my code:

async function resaveEntities(model, cursor = null) {
  const query = model.query().limit(50);

  if (cursor) {
    query.start(cursor);
  }

  const result = await query.run();
  const response = result[0];

  log.info(`fetched ${response.entities.length} entities of ${model.entityKind}`);

  // CODE HERE TO RE-SAVE ALL THE FETCHED ENTITIES

  if (response.nextPageCursor) {
    resaveEntities(model, response.nextPageCursor);
  } else {
    log.info(`END of re-saving of [${model.entityKind}]`);
  }
}

The reason I ask is because if I'm not mistaken, batch saving large groups of entities (as I'm trying to do here, basically running through all the entities of a kind and re-saving them- in order to re-index their properties if I changed the schema) can be optimised when its done in groups instead of individually every time.

I suppose I could use the google-cloud-node library to re-save them, but would I not lose the schema then? When I look at the data that is being returned in the query, it appears that it's just a bunch of plain data- no extra schema information or even functions so I'm unsure if entity.save() would actually even work either.

I'm going to play around with it for a bit. But let me know if there is a solution I may be overlooking.

@lostpebble
Copy link
Author

I realise this is actually rather related to #22 , and more complicated than I thought because the query is not returning a group of Entities we can easily update / save with all the relevant information.

@sebelga
Copy link
Owner

sebelga commented Dec 6, 2016

Hi, that's an interesting thing to add. Yes it is related to #22 to be able to query and receive entities instances (not just data + id). I could then add a new global method to save an array of entities.
You are right it is much more effective to save several entities in 1 query. I am going to work on this for the next release.

@lostpebble
Copy link
Author

Great, thanks! Really appreciate all the work you've put into this extension of the Datastore @sebelga. Best solution for NodeJS by far 😄

Also, it may help to take a look at Objectify to get ideas of what's been done well before in other languages. I used it a lot before moving over to Node and was quite impressed with it too.

@sebelga
Copy link
Owner

sebelga commented Dec 7, 2016

Thanks for the kind words! :) I will have a look at Objectify, seems very interesting! Any feature you found specially useful that we could implement here?

sebelga added a commit that referenced this issue Dec 7, 2016
sebelga added a commit that referenced this issue Dec 7, 2016
sebelga added a commit that referenced this issue Dec 7, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
sebelga added a commit that referenced this issue Dec 8, 2016
Added #29 Saving multiple Entities
@sebelga
Copy link
Owner

sebelga commented Dec 8, 2016

Hi, just release v0.9.0 with this feature. Could you please try to see if it works for you? thansk!

@sebelga sebelga closed this as completed Dec 10, 2016
@lostpebble
Copy link
Author

Happy to confirm it is working as intended! As well as validating geoPoints when the structure is correct. Thanks for the quick release!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants