GeezerVison is showing github.com — Expose something finer than just `.save()` through the datastore abstraction · Issue #384 · googleapis/google-cloud-node
Expose something finer than just .save() through the datastore abstraction #384DescriptionOne thing I'd like is more features than just .save(). At the moment, I'm inserting a kind=User with name=chilts. However if I were to have another user ask for the name 'chilts', then .save() will say "Oh, the key is complete, so I'll do an update". I would like to make it try an insert and fail because that name already exists (ie. the primary key) ... is that currently possible via your api (from what I can tell it isn't)?
I love how all of the thorny details are abstracted away, but possibly exposing more of the API through here would be awesome. i.e. .insert(), .update() as well as .save(). Sometimes the key logic in .save() isn't needed.
Thanks again for a great library. Activitydhermes commented on Feb 6, 2015 @chilts The relevant verbs are:
update - Must exist
insert - Must not exist
upsert - Can be brand new or exist already
insert_auto_id - Incomplete path
I am also interested in
- The demand for this from end users
- The solution to this demand
dhermes commented on Feb 6, 2015 ISTM after a quick scan that the relevant bits of code only use upsert and insert_auto_id (as is done in gcloud-python).
It just build an accumulator which starts out as
{
upsert: [],
insert_auto_id: []
}
and is translated to the relevant protobuf via DatastoreRequest.prototype.makeReq_.
You could build your own accumulator with update and insert keys and then execute
DatastoreRequest().makeReq_('commit', req, onCommitCallback);
ryanseys commented on Feb 8, 2015 Agreed, we should support insert functionality on its own as it's pretty standard for the "attempting to add new users" use-case that @chilts described above. ryanseys commented on Feb 8, 2015 @chilts How does #387 fill this need? Basically you'll explicitly set a method for your entity like so:
var gcloud = require('gcloud');
var datastore = gcloud.datastore;
var dataset = datastore.dataset();
dataset.save([
{
key: dataset.key({ path: ['User', 'chilts'] }),
method: 'insert', // force an insert
data: {
updateTime: (new Date()).toString()
}
}
], function(err, result) {
console.log(err, result);
});chilts commented on Feb 8, 2015 Yeah, that would probably work.
For now (for my current project) I've moved to a different datastore so I'll just keep my eye on how you guys progress over here. Cheers. ryanseys commented on Feb 8, 2015 Cool please let us know if you have any more ideas how we can make the library better, your feedback is very important :) stephenplusplus commented on Feb 16, 2015 This is now merged in and will make it for 0.13.0.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|