From 28793774998a982712f2f1afaffa5d695d1cf740 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Thu, 15 Oct 2015 15:04:36 -0400 Subject: [PATCH] add datastore v1beta3 snippets --- datastore/concepts.js | 405 +++++++++++++++++++++++++++++++++ package.json | 1 + test/datastore/testConcepts.js | 107 +++++++++ 3 files changed, 513 insertions(+) create mode 100644 datastore/concepts.js create mode 100644 test/datastore/testConcepts.js diff --git a/datastore/concepts.js b/datastore/concepts.js new file mode 100644 index 00000000000..62ded287b4a --- /dev/null +++ b/datastore/concepts.js @@ -0,0 +1,405 @@ +'use strict'; + +var gcloud = require('gcloud'); + +// This mock is used in the documentation snippets. +var datastore = { + delete: function() {}, + get: function() {}, + insert: function() {}, + update: function() {}, + upsert: function() {}, + save: function() {} +}; + +function Concepts(projectId) { + this.datastore = gcloud.datastore({ + projectId: projectId + }); + + // To create the keys, we have to use this instance of Datastore. + datastore.key = this.datastore.key; + + this.incompleteKey = this.getIncompleteKey(); + this.namedKey = this.getNamedKey(); + this.keyWithParent = this.getKeyWithParent(); + this.keyWithMultiLevelParent = this.getKeyWithMultiLevelParent(); +} + +Concepts.prototype.getIncompleteKey = function() { + // [START incomplete_key] + var taskKey = datastore.key('Task'); + // [END incomplete_key] + + return taskKey; +}; + +Concepts.prototype.getNamedKey = function() { + // [START named_key] + var taskKey = datastore.key([ + 'Task', + 'sampletask' + ]); + // [END named_key] + + return taskKey; +}; + +Concepts.prototype.getKeyWithParent = function() { + // [START key_with_parent] + var taskKey = datastore.key([ + 'TaskList', + 'default', + 'Task', + 'sampleTask' + ]); + // [END key_with_parent] + + return taskKey; +}; + +Concepts.prototype.getKeyWithMultiLevelParent = function() { + // [START key_with_multilevel_parent] + var taskKey = datastore.key([ + 'User', + 'alice', + 'TaskList', + 'default', + 'Task', + 'sampleTask' + ]); + // [END key_with_multilevel_parent] + + return taskKey; +}; + +Concepts.prototype.getTask = function() { + // [START basic_entity] + var task = { + type: 'Personal', + done: false, + priority: 4, + description: 'Learn Cloud Datastore' + }; + // [END basic_entity] + + return task; +}; + +Concepts.prototype.testIncompleteKey = function(callback) { + this.datastore.save({ + key: this.incompleteKey, + data: {} + }, callback); +}; + +Concepts.prototype.testNamedKey = function(callback) { + this.datastore.save({ + key: this.namedKey, + data: {} + }, callback); +}; + +Concepts.prototype.testKeyWithParent = function(callback) { + this.datastore.save({ + key: this.keyWithParent, + data: {} + }, callback); +}; + +Concepts.prototype.testKeyWithMultiLevelParent = function(callback) { + this.datastore.save({ + key: this.keyWithMultiLevelParent, + data: {} + }, callback); +}; + +Concepts.prototype.testEntityWithParent = function(callback) { + var taskKey = this.keyWithParent; + + // [START entity_with_parent] + var task = { + key: taskKey, + data: { + type: 'Personal', + done: false, + priority: 4, + description: 'Learn Cloud Datastore' + } + }; + // [END entity_with_parent] + + this.datastore.save(task, callback); +}; + +Concepts.prototype.testProperties = function(callback) { + // [START properties] + var task = { + type: 'Personal', + done: false, + priority: 4, + percent_complete: 10.0, + description: 'Learn Cloud Datastore' + }; + // [END properties] + + this.datastore.save({ + key: this.incompleteKey, + data: task + }, callback); +}; + +Concepts.prototype.testArrayValue = function(callback) { + // [START array_value] + var task = { + tags: [ + 'fun', + 'programming' + ], + collaborators: [ + 'alice', + 'bob' + ] + }; + // [END array_value] + + this.datastore.save({ + key: this.incompleteKey, + data: task + }, callback); +}; + +Concepts.prototype.testBasicEntity = function(callback) { + this.datastore.save({ + key: this.getIncompleteKey(), + data: this.getTask() + }, callback); +}; + +Concepts.prototype.testUpsert = function(callback) { + var taskKey = this.getIncompleteKey(); + var task = this.getTask(); + + // [START upsert] + datastore.upsert({ + key: taskKey, + data: task + }, function(err) { + if (!err) { + // Task inserted successfully. + } + }); + // [END upsert] + + this.datastore.upsert({ + key: taskKey, + data: task + }, callback); +}; + +Concepts.prototype.testInsert = function(callback) { + var taskKey = this.getIncompleteKey(); + var task = this.getTask(); + + // [START insert] + datastore.insert({ + key: taskKey, + data: task + }, function(err) { + if (!err) { + // Task inserted successfully. + } + }); + // [END insert] + + this.datastore.insert({ + key: taskKey, + data: task + }, callback); +}; + +Concepts.prototype.testLookup = function(callback) { + var self = this; + var taskKey = this.getIncompleteKey(); + + // [START lookup] + datastore.get(taskKey, function(err, entity) { + if (!err) { + // Task found. + // entity.data = {/*...*/}; + } + }); + // [End lookup] + + this.datastore.insert({ + key: taskKey, + data: {} + }, function(err) { + if (err) { + callback(err); + return; + } + + self.datastore.get(taskKey, callback); + }); +}; + +Concepts.prototype.testUpdate = function(callback) { + var self = this; + var taskKey = this.getIncompleteKey(); + var task = this.getTask(); + + // [START update] + datastore.update({ + key: taskKey, + data: task + }, function(err) { + if (!err) { + // Task updated successfully. + } + }); + // [END update] + + this.datastore.insert({ + key: taskKey, + data: {} + }, function(err) { + if (err) { + callback(err); + return; + } + + self.datastore.update({ + key: taskKey, + data: task + }, callback); + }); +}; + +Concepts.prototype.testDelete = function(callback) { + var self = this; + var taskKey = this.getIncompleteKey(); + + // [START delete] + datastore.delete(taskKey, function(err) { + if (!err) { + // Task deleted successfully. + } + }); + // [END delete] + + this.datastore.insert({ + key: taskKey, + data: {} + }, function(err) { + if (err) { + callback(err); + return; + } + + self.datastore.delete(taskKey, callback); + }); +}; + +Concepts.prototype.testBatchUpsert = function(callback) { + datastore.key = this.datastore.key; + + // [START batch_upsert] + var taskKey1 = datastore.key(['Task', 1]); + var taskKey2 = datastore.key(['Task', 2]); + + var task1 = { + type: 'Personal', + done: false, + priority: 4, + description: 'Learn Cloud Datastore' + }; + + var task2 = { + type: 'Work', + done: false, + priority: 8, + description: 'Integrate Cloud Datastore' + }; + + datastore.upsert([ + { + key: taskKey1, + data: task1 + }, + { + key: taskKey2, + data: task2 + } + ], function(err) { + if (!err) { + // Tasks inserted successfully. + } + }); + // [END batch_upsert] + + this.datastore.upsert([ + { + key: taskKey1, + data: task1 + }, + { + key: taskKey2, + data: task2 + } + ], callback); +}; + +Concepts.prototype.testBatchLookup = function(callback) { + var taskKey1 = this.datastore.key(['Task', 1]); + var taskKey2 = this.datastore.key(['Task', 2]); + + // [START batch_lookup] + datastore.get([ + taskKey1, + taskKey2 + ], function(err, entities) { + if (!err) { + // entities[0].data = { + // type: 'Personal', + // done: false, + // priority: 4, + // description: 'Learn Cloud Datastore' + // }; + + // entities[1].data = { + // type: 'Work', + // // ... + // }; + } + }); + // [END batch_lookup] + + this.datastore.get([ + taskKey1, + taskKey2 + ], callback); +}; + +Concepts.prototype.testBatchDelete = function(callback) { + var taskKey1 = this.datastore.key(['Task', 1]); + var taskKey2 = this.datastore.key(['Task', 2]); + + // [START batch_delete] + datastore.delete([ + taskKey1, + taskKey2 + ], function(err) { + if (!err) { + // Tasks deleted successfully. + } + }); + // [END batch_delete] + + this.datastore.delete([ + taskKey1, + taskKey2 + ], callback); +}; + +module.exports = Concepts; diff --git a/package.json b/package.json index 1b8c0f1b05f..daee0902b5d 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "license": "Apache 2", "private": true, "dependencies": { + "gcloud": "stephenplusplus/gcloud-node#spp--datastore-v1beta3", "googleapis": "~2.1.3" }, "devDependencies": { diff --git a/test/datastore/testConcepts.js b/test/datastore/testConcepts.js new file mode 100644 index 00000000000..52c8d5da859 --- /dev/null +++ b/test/datastore/testConcepts.js @@ -0,0 +1,107 @@ +'use strict'; + +var assert = require('assert'); +var _ = require('lodash'); + +var Concepts = require('../../datastore/concepts'); +var concept; + +before(function() { + concept = new Concepts(process.env.TEST_PROJECT_ID); +}); + +describe('incomplete key', function() { + it('saves with an incomplete key', function(done) { + concept.testIncompleteKey(done); + }); +}); + +describe('testNamedKey', function() { + it('saves with a named key', function(done) { + concept.testNamedKey(done); + }); +}); + +describe('testKeyWithParent', function() { + it('saves a key with a parent', function(done) { + concept.testKeyWithParent(done); + }); +}); + +describe('testKeyWithMultiLevelParent', function() { + it('saves a key with multiple parents', function(done) { + concept.testKeyWithMultiLevelParent(done); + }); +}); + +describe('testEntityWithParent', function() { + it('saves an entity with a parent', function(done) { + concept.testEntityWithParent(done); + }); +}); + +describe('testProperties', function() { + it('saves an entity with properties', function(done) { + concept.testProperties(done); + }); +}); + +describe('testArrayValue', function() { + it('saves an entity with arrays', function(done) { + concept.testArrayValue(done); + }); +}); + +describe('testBasicEntity', function() { + it('saves a basic entity', function(done) { + concept.testBasicEntity(done); + }); +}); + +describe('testUpsert', function() { + it('saves with an upsert', function(done) { + concept.testUpsert(done); + }); +}); + +describe('testInsert', function() { + it('saves with an insert', function(done) { + concept.testInsert(done); + }); +}); + +describe('testLookup', function() { + it('performs a lookup', function(done) { + concept.testLookup(done); + }); +}); + +describe('testUpdate', function() { + it('saves with an update', function(done) { + concept.testUpdate(done); + }); +}); + +describe('testDelete', function() { + it('deletes an entity', function(done) { + concept.testDelete(done); + }); +}); + +describe('testBatchUpsert', function() { + it('performs a batch upsert', function(done) { + concept.testBatchUpsert(done); + }); +}); + +describe('testBatchLookup', function() { + it('performs a batch lookup', function(done) { + concept.testBatchLookup(done); + }); +}); + +describe('testBatchDelete', function() { + it('performs a batch delete', function(done) { + concept.testBatchDelete(done); + }); +});