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

instanceof Datastore.Double #2503

Closed
pashaseliverstov opened this issue Aug 2, 2017 · 6 comments
Closed

instanceof Datastore.Double #2503

pashaseliverstov opened this issue Aug 2, 2017 · 6 comments
Assignees
Labels
api: datastore Issues related to the Datastore API. priority: p2 Moderately-important priority. Fix may not be included in next release.

Comments

@pashaseliverstov
Copy link

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

  • OS:
  • Node.js version:
  • npm version:
  • google-cloud-node version:

Steps to reproduce

  1. require google-cloud
  2. ?

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

@stephenplusplus
Copy link
Contributor

We have those up a little bit in the file. Can you check them out and see if that's what you need?

@stephenplusplus
Copy link
Contributor

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.

@pashaseliverstov
Copy link
Author

pashaseliverstov commented Aug 2, 2017

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);
      });
  }

@pashaseliverstov
Copy link
Author

or you could add methods to Datastore to check if Double or GeoPoint

Datastore.isDouble(obj.doublefield) // returns true or false
Datastore.isGeoPoint(obj.geopointfield) // returns true or false

@pashaseliverstov pashaseliverstov changed the title Datastore.Double instanceof Datastore.Double Aug 3, 2017
@stephenplusplus stephenplusplus added api: datastore Issues related to the Datastore API. type: enhancement labels Aug 3, 2017
@landrito landrito added priority: p2 Moderately-important priority. Fix may not be included in next release. status: acknowledged labels Aug 7, 2017
@beaulac
Copy link
Contributor

beaulac commented Sep 21, 2017

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');
}

@stephenplusplus
Copy link
Contributor

Haha, that is gross, but reliable in the interim. A PR would be welcome :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the Datastore API. priority: p2 Moderately-important priority. Fix may not be included in next release.
Projects
None yet
Development

No branches or pull requests

4 participants