google-cloud-node icon indicating copy to clipboard operation
google-cloud-node copied to clipboard

instanceof Datastore.Double

Open pashaseliverstov opened this issue 9 years ago • 6 comments

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!

pashaseliverstov avatar Aug 02 '17 22:08 pashaseliverstov

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

stephenplusplus avatar Aug 02 '17 22:08 stephenplusplus

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.

stephenplusplus avatar Aug 02 '17 22:08 stephenplusplus

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 avatar Aug 02 '17 22:08 pashaseliverstov

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 avatar Aug 02 '17 22:08 pashaseliverstov

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

beaulac avatar Sep 21 '17 05:09 beaulac

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

stephenplusplus avatar Sep 21 '17 14:09 stephenplusplus