Skip to content

fbrmedia/nodejs-datastore-kvstore

 
 

Repository files navigation

google-cloud-kvstore

Use Datastore as a Key/Value store.

Install

$ npm install google-cloud-kvstore

Example

const {KVStore} = require('google-cloud-kvstore');
const {Datastore} = require('@google-cloud/datastore');

const datastore = new Datastore();
const store = new KVStore(datastore);

// Set an item.
store.set('todos', ['eat', 'sleep', 'repeat'], (err, key) => {});

// Get an item.
store.get('todos', (err, todos) => {
  // todos:
  //   ['eat', 'sleep', 'repeat']
});

// Delete an item.
store.delete('todos', (err) => {});

How

Google Cloud Datastore is a managed, NoSQL, schemaless database for storing non-relational data. Datastore entities are complex objects. However, we can wrap this complexity to mimic a simple key/value store by storing a numeric or string "key" as the id of an entity.

The example below shows the complexity that is hidden with google-cloud-kvstore.

With @google-cloud/datastore:

const key = datastore.key(['KeyValue', 'key']);

datastore.save({
  key: key,
  value: 'value'
}, () => {});

datastore.get(key, () => {});

datastore.delete(key, () => {});

With @google-cloud/datastore + google-cloud-kvstore:

const {KVStore} = require('google-cloud-kvstore');
const store = new KVStore(datastore);
store.set('key', 'value', () => {});
store.get('key', () => {});
store.delete('key', () => {});

API

kvstore(datastore)

datastore

A @google-cloud/datastore instance.

kvstore#delete(key, callback)

key

Type: String|Number

callback

Type: Function Executed with the same signature as Datastore#delete.

kvstore#get(key, callback)

key

Type: String|Number

callback

Type: Function Executed with (err, value)

kvstore#set(key, value, callback)

key

Type: String|Number

value

Type: *

callback

Type: Function Executed with the same signature as Datastore#save.

Credit

Concept originally created by Patrick Costello: googleapis/google-cloud-node#256 (comment).

License

MIT

About

Use @google-cloud/datastore as a Key/Value store.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 79.8%
  • JavaScript 16.9%
  • Python 3.3%