Skip to content

Simple key/value datastore methods #256

Description

@ryanseys

Pardon my noob-ish request. As far as I understand, I cannot currently do simple key / value store functionality like due to the inherent complexity of the datastore key objects:

var key = "hello"; // string or number
var value = "world"; // string or number

datastore.set(key, value, function(err) {
  if(!err) {
    // success;
  } 
});

datastore.get("hello", function(err, val) {
  val; // "world"
});

Activity

  1. ryanseys commented on Oct 13, 2014

    @ryanseys
    ContributorAuthor
  2. beriberikix commented on Oct 13, 2014

    @beriberikix
    Contributor

    @pcostell @jgeewax is this why an NDB layer would be useful?

  3. pcostell commented on Oct 13, 2014

    @pcostell
    Contributor

    It's definitely possible to build a simple key/value store on top of the Datastore. NDB however is not this. Even then you'd have to define a model with different properties.

    Datastore is just a more complicated system than a key/value store. It support querying, consistency guarantees, and a large amount of unstructured data.

    If you wanted to build your own wrapper, you could do something very simple like:

    function KeyValueDatastore(dataset) {
      this.dataset = dataset;
    }
    
    // key is a string or int.
    KeyValueDatastore.get = function(key, value, callback) {
      this.dataset.get(this.dataset.key(['KeyValue', key]), function(err, entity) {
        callback(err, 'value' in entity ? entity['value'] : None);
      }
    }
    
    // key is a string or int.
    KeyValueDatastore.set = function(key, value, callback) {
      this.dataset.save({
        key : this.dataset.key(['KeyValue', key]),
        data :  {
          'value' : value
        },
        function(err, keys) {
          callback(err);
        }
      });
    }
    

    In terms of handling the key, I just chose a kind to put things into, then using the provided key as either the name or id.

  4. stephenplusplus commented on Oct 14, 2014

    @stephenplusplus
    Contributor

    The above could be a good case for a gcloud extension from userland.

    var gcloud = require('gcloud')({/*...*/});
    var keystore = require('gcloud-keystore');
    
    var dataset = keystore(gcloud.dataset());
    
    dataset.set('company', 'yay');
    dataset.get('company', function(err, value) {});
  5. stephenplusplus commented on Oct 14, 2014

    @stephenplusplus
    Contributor
  6. stephenplusplus commented on Oct 16, 2014

    @stephenplusplus
    Contributor

    Going to close -- to anyone interested, try out gcloud-keystore!

  7. added this to the Datastore Future milestone on Feb 2, 2015
  8. 28 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

🚨This issue needs some love.api: datastoreIssues related to the Datastore API.triage meI really want to be triaged.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions