-
Notifications
You must be signed in to change notification settings - Fork 598
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
instanceof Datastore.Double #2503
Comments
We have those up a little bit in the file. Can you check them out and see if that's what you need? |
Sorry, I think I get it. You need to type check against the raw entity. Off hand, I don't have a solution, but I agree that we need to figure something out. |
Here is code example const nonIndexed = ['field1'];
const doubleFields = ['field2', 'field3'];
function fromDatastore(obj) {
obj.id = obj[ds.KEY].id.toString();
Object.keys(obj).forEach(function(key) {
if (obj[key] && obj[key].value && obj[key] instanceof Datastore.Double){
obj[key] = obj[key].value;
}
});
delete obj[ds.KEY];
return obj;
}
function toDatastore(obj, nonIndexed) {
delete obj.id;
nonIndexed = nonIndexed || [];
var results = [];
Object.keys(obj).forEach(function(k) {
if (obj[k] === undefined) { return; }
results.push({
name: k,
value: obj[k],
excludeFromIndexes: nonIndexed.indexOf(k) !== -1
});
});
return results;
}
function set(data) {
if (!data) {
var error = new Error(`Cannot save empty entity`);
error.code = 400;
throw error;
}
var key;
if (data.id) {
key = ds.key([kind, parseInt(data.id, 10)]);
} else {
key = ds.key(kind);
}
Object.keys(data).forEach(function(key) {
if (doubleFields.indexOf(key) > -1){
data[key] = ds.double(data[key]);
}
});
var entity = {
key: key,
data: toDatastore(data, nonIndexed)
};
data[ds.KEY] = key;
return ds.save(entity)
.then(() => {
return fromDatastore(data);
});
}
function read(id) {
var key = ds.key([kind, parseInt(id, 10)]);
return ds.get(key)
.then(([entity]) => {
if (!entity) {
var error = new Error(`No entity found for key ${key.path.join('/')}.`);
error.code = 404;
throw error;
}
return fromDatastore(entity);
});
} |
or you could add methods to Datastore to check if Double or GeoPoint Datastore.isDouble(obj.doublefield) // returns true or false |
FWIW, I've been kludging around this like so: const DatastoreInteger = datastore.int(0).constructor;
const DatastoreDouble = datastore.double(0).constructor;
const DatastoreGeopoint = datastore.geoPoint({latitude: 0, longitude: 0}).constructor;
const DatastoreKey = datastore.key().constructor;
if (datastore.key(['this_is', 'gross']) instanceof DatastoreKey) {
console.log('yep');
}
if (datastore.double(42.42) instanceof DatastoreDouble) {
console.log('it works');
} |
Haha, that is gross, but reliable in the interim. A PR would be welcome :) |
How to get access to entity.Double or entity.GeoPoint for instanceof ?
https://github.com/GoogleCloudPlatform/google-cloud-node/blob/master/packages/datastore/src/index.js#L549-L551
It would be good to have Datastore.Double = entity.Double; Datastore.GeoPoint = entity.GeoPoint;
Environment details
Steps to reproduce
google-cloud
Making sure to follow these steps will guarantee the quickest resolution possible.
Thanks!
The text was updated successfully, but these errors were encountered: