|
| 1 | +// Copyright 2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +var gcloud = require('gcloud'); |
| 17 | + |
| 18 | +// Create a datastore client. |
| 19 | +var datastore = gcloud.datastore(); |
| 20 | + |
| 21 | +/** |
| 22 | + * Gets a Datastore key from the kind/key pair in the request. |
| 23 | + * |
| 24 | + * @param {Object} requestData Cloud Function request data. |
| 25 | + * @param {string} requestData.key Datastore key string. |
| 26 | + * @param {string} requestData.kind Datastore kind. |
| 27 | + * @returns {Object} Datastore key object. |
| 28 | + */ |
| 29 | +function getKeyFromRequestData (requestData) { |
| 30 | + if (!requestData.key) { |
| 31 | + throw new Error('Key not provided. Make sure you have a "key" property ' + |
| 32 | + 'in your request'); |
| 33 | + } |
| 34 | + |
| 35 | + if (!requestData.kind) { |
| 36 | + throw new Error('Kind not provided. Make sure you have a "kind" property ' + |
| 37 | + 'in your request'); |
| 38 | + } |
| 39 | + |
| 40 | + return datastore.key([requestData.kind, requestData.key]); |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Creates and/or updates a record. |
| 45 | + * |
| 46 | + * @example |
| 47 | + * gcloud alpha functions call ds-set --data '{"kind":"gcf-test","key":"foobar","value":{"message": "Hello World!"}}' |
| 48 | + * |
| 49 | + * @param {Object} context Cloud Function context. |
| 50 | + * @param {Function} context.success Success callback. |
| 51 | + * @param {Function} context.failure Failure callback. |
| 52 | + * @param {Object} data Request data, in this case an object provided by the user. |
| 53 | + * @param {string} data.kind The Datastore kind of the data to save, e.g. "user". |
| 54 | + * @param {string} data.key Key at which to save the data, e.g. 5075192766267392. |
| 55 | + * @param {Object} data.value Value to save to Cloud Datastore, e.g. {"name":"John"} |
| 56 | + */ |
| 57 | +function set (context, data) { |
| 58 | + try { |
| 59 | + // The value contains a JSON document representing the entity we want to save |
| 60 | + if (!data.value) { |
| 61 | + throw new Error('Value not provided. Make sure you have a "value" ' + |
| 62 | + 'property in your request'); |
| 63 | + } |
| 64 | + |
| 65 | + var key = getKeyFromRequestData(data); |
| 66 | + |
| 67 | + return datastore.save({ |
| 68 | + key: key, |
| 69 | + data: data.value |
| 70 | + }, function (err) { |
| 71 | + if (err) { |
| 72 | + console.error(err); |
| 73 | + return context.failure(err); |
| 74 | + } |
| 75 | + |
| 76 | + return context.success('Entity saved'); |
| 77 | + }); |
| 78 | + } catch (err) { |
| 79 | + console.error(err); |
| 80 | + return context.failure(err.message); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * Retrieves a record. |
| 86 | + * |
| 87 | + * @example |
| 88 | + * gcloud alpha functions call ds-get --data '{"kind":"gcf-test","key":"foobar"}' |
| 89 | + * |
| 90 | + * @param {Object} context Cloud Function context. |
| 91 | + * @param {Function} context.success Success callback. |
| 92 | + * @param {Function} context.failure Failure callback. |
| 93 | + * @param {Object} data Request data, in this case an object provided by the user. |
| 94 | + * @param {string} data.kind The Datastore kind of the data to retrieve, e.g. "user". |
| 95 | + * @param {string} data.key Key at which to retrieve the data, e.g. 5075192766267392. |
| 96 | + */ |
| 97 | +function get (context, data) { |
| 98 | + try { |
| 99 | + var key = getKeyFromRequestData(data); |
| 100 | + |
| 101 | + return datastore.get(key, function (err, entity) { |
| 102 | + if (err) { |
| 103 | + console.error(err); |
| 104 | + return context.failure(err); |
| 105 | + } |
| 106 | + |
| 107 | + // The get operation will not fail for a non-existent entity, it just |
| 108 | + // returns null. |
| 109 | + if (!entity) { |
| 110 | + return context.failure('No entity found for key ' + key.path); |
| 111 | + } |
| 112 | + |
| 113 | + return context.success(entity); |
| 114 | + }); |
| 115 | + } catch (err) { |
| 116 | + console.error(err); |
| 117 | + return context.failure(err.message); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Deletes a record. |
| 123 | + * |
| 124 | + * @example |
| 125 | + * gcloud alpha functions call ds-del --data '{"kind":"gcf-test","key":"foobar"}' |
| 126 | + * |
| 127 | + * @param {Object} context Cloud Function context. |
| 128 | + * @param {Function} context.success Success callback. |
| 129 | + * @param {Function} context.failure Failure callback. |
| 130 | + * @param {Object} data Request data, in this case an object provided by the user. |
| 131 | + * @param {string} data.kind The Datastore kind of the data to delete, e.g. "user". |
| 132 | + * @param {string} data.key Key at which to delete data, e.g. 5075192766267392. |
| 133 | + */ |
| 134 | +function del (context, data) { |
| 135 | + try { |
| 136 | + var key = getKeyFromRequestData(data); |
| 137 | + |
| 138 | + return datastore.delete(key, function (err) { |
| 139 | + if (err) { |
| 140 | + console.error(err); |
| 141 | + return context.failure(err); |
| 142 | + } |
| 143 | + |
| 144 | + return context.success('Entity deleted'); |
| 145 | + }); |
| 146 | + } catch (err) { |
| 147 | + console.error(err); |
| 148 | + return context.failure(err.message); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +exports.set = set; |
| 153 | +exports.get = get; |
| 154 | +exports.del = del; |
0 commit comments