-
Notifications
You must be signed in to change notification settings - Fork 592
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
Use parent from options when constructing a Datastore Key #1769
Conversation
Adds parent to options for Key constructor to specify a parent of the provided path.
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed, please reply here (e.g.
|
I signed it! |
CLAs look good, thanks! |
Thank you for this! I like the goal, but how about instead of supplying a parent directly, we just had a var company = datastore.key(['Company', 123]) // the parent
var department = company.key(['Department', 'Engineering']) // the child
// or...
var department = company.child(['Department', 'Engineering']) |
Sure that would work. For a little context, I currently have this method on an object wrapping Datastore
With the original change that would be
With your suggestion I would have
Personal taste, I prefer the middle one. |
I suppose the biggest potential downside of this change as is, is that currently I think this works
But with the current patch I believe it would break as
No idea if this is intended functionality. |
Thank you for sharing the context, that always helps. I can see the concern for the new method as there is more complexity. I tried to re-work your function to see "what I would do" (of course, this isn't what you would do), but let me know if I've maintained functionality while getting it to a less intimidating size, in a way that you might use: function key(path, parent) {
if (typeof path === 'object') {
// Key provided is already a Key object.
return path;
}
path = Array.isArray(path) ? path : [path];
return (parent || datastore).key({
namespace: 'test',
path: [this.kind].concat(path)
});
} Small win by using function key(path, parent) {
if (typeof path === 'object') {
// Key provided is already a Key object.
return path;
}
return (parent || datastore).key({
namespace: 'test',
path: [this.kind].concat(arrify(path)) // with npm module arrify
});
} On the duplicate key note, any behavior, working or not, would be incidental. I don't believe we've considered the use case of passing a Key object into key. Let me know if you've seen that in the docs or mentioned elsewhere. At first glance, I'm not too concerned about supporting that use, but what do you think? |
Quick follow up. My previous key normalizing function ended up biting me when I started using parent child relationships with
// Normalize key
// Takes an int, string, array, or key object and returns a key object
key(key, {parent} = {}) {
switch (typeof(key)) {
case 'object':
return key;
case 'undefined':
key = [];
break;
case 'string':
if (key.match(/^\d+$/)) {
key = datastore.int(key);
}
key = [key];
break;
case 'number':
key = [key];
break;
default:
throw 'Unhandled key type ' + typeof(key);
}
key = [this.kind].concat(key);
key = datastore.key({
namespace: DATASTORE_NAMESPACE,
path: key
});
if(parent) {
key.parent = parent;
}
return key;
} |
Due to the age of this request, I'm going to close the PR. If you're still interested in this feature, please create an issue for further discussion or a PR at the new Datastore location: https://github.com/googleapis/nodejs-datastore. Thanks! |
Adds parent to options for Key constructor to specify a parent of the provided path.
Thoughts?