From ad72fc3e4b3137d71cbdc2c9457ddef0bd710b95 Mon Sep 17 00:00:00 2001 From: vijay-qlogic Date: Wed, 1 Aug 2018 14:50:55 +0530 Subject: [PATCH] Added example region-tag for Family.js --- samples/document-snippets/family.js | 128 ++++++++++++++++++++++ samples/document-snippets/tests/family.js | 93 ++++++++++++++++ src/family.js | 80 ++------------ 3 files changed, 233 insertions(+), 68 deletions(-) create mode 100644 samples/document-snippets/family.js create mode 100644 samples/document-snippets/tests/family.js diff --git a/samples/document-snippets/family.js b/samples/document-snippets/family.js new file mode 100644 index 000000000..f7334a3e2 --- /dev/null +++ b/samples/document-snippets/family.js @@ -0,0 +1,128 @@ +/** + * Copyright 2018, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const Bigtable = require('@google-cloud/bigtable'); +const bigtable = new Bigtable(); + +const snippets = { + createColmFamily: (instanceId, tableId, familyId, callback) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + // [START bigtable_create_family] + family + .create() + .then(result => { + const family = result[0]; + // let apiResponse = result[1]; + callback(null, family); + }) + .catch(err => { + callback(err); + }); + // [END bigtable_create_family] + }, + existsFamily: (instanceId, tableId, familyId, callback) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + + // [START bigtable_exists_family] + family + .exists() + .then(result => { + const exists = result[0]; + callback(null, exists); + }) + .catch(err => { + callback(err); + }); + // [END bigtable_exists_family] + }, + getFamily: (instanceId, tableId, familyId, callback) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + // [START bigtable_get_family] + family + .get() + .then(result => { + const family = result[0]; + // const apiResponse = result[1]; + callback(null, family); + }) + .catch(err => { + callback(err); + }); + // [END bigtable_get_family] + }, + getMetaData: (instanceId, tableId, familyId, callback) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + // [START bigtable_get_family_meta] + family + .getMetadata() + .then(result => { + const metaData = result[0]; + // const apiResponse = result[1]; + callback(null, metaData); + }) + .catch(err => { + callback(err); + }); + // [END bigtable_get_family_meta] + }, + setMetaData: (instanceId, tableId, familyId, callback) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + // [START bigtable_set_family_meta] + var metadata = { + rule: { + versions: 2, + union: true, + }, + }; + family + .setMetadata(metadata) + .then(result => { + const apiResponse = result[0]; + callback(null, apiResponse); + }) + .catch(err => { + callback(err); + }); + // [END bigtable_set_family_meta] + }, + delFamily: (instanceId, tableId, familyId, callback) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + // [START bigtable_del_family] + family + .delete() + .then(result => { + const apiResponse = result[0]; + callback(null, apiResponse); + }) + .catch(err => { + callback(err); + }); + // [END bigtable_del_family] + }, +}; + +module.exports = snippets; diff --git a/samples/document-snippets/tests/family.js b/samples/document-snippets/tests/family.js new file mode 100644 index 000000000..e4f3b7832 --- /dev/null +++ b/samples/document-snippets/tests/family.js @@ -0,0 +1,93 @@ +/** + * Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; +const assert = require('assert'); +const uuid = require(`uuid`); + +const Bigtable = require(`@google-cloud/bigtable`); +const bigtable = new Bigtable(); + +const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const TABLE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const FAMILY_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules + +const familySnippets = require('../family.js'); + +const instance = bigtable.instance(INSTANCE_ID); + +describe('Family Snippets', function() { + before(async () => { + await instance.create({ + clusters: [ + { + name: CLUSTER_ID, + location: 'us-central1-f', + storage: 'hdd', + }, + ], + type: 'DEVELOPMENT', + }); + await instance.createTable(TABLE_ID); + }); + + after(async () => { + await instance.delete(); + }); + + it('should create a column family', function(done) { + familySnippets.createColmFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID, err => { + assert.ifError(err); + done(); + }); + }); + + it('should check family exists', function(done) { + familySnippets.existsFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID, err => { + assert.ifError(err); + done(); + }); + }); + + it('should get the family', function(done) { + familySnippets.getFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID, err => { + assert.ifError(err); + done(); + }); + }); + + it('should get family meta-data', function(done) { + familySnippets.getMetaData(INSTANCE_ID, TABLE_ID, FAMILY_ID, err => { + assert.ifError(err); + done(); + }); + }); + + it('should set family meta-data', function(done) { + familySnippets.setMetaData(INSTANCE_ID, TABLE_ID, FAMILY_ID, err => { + assert.ifError(err); + done(); + }); + }); + + it('should delete family', function(done) { + familySnippets.delFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID, err => { + assert.ifError(err); + done(); + }); + }); +}); diff --git a/src/family.js b/src/family.js index 04551f3b3..d7206436c 100644 --- a/src/family.js +++ b/src/family.js @@ -150,18 +150,8 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.` * @param {Family} callback.family The metadata. * @param {object} callback.apiResponse The full API response. * - * @example - * family.create(function(err, family, apiResponse) { - * // The column family was created successfully. - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * family.create().then(function(data) { - * const family = data[0]; - * const apiResponse = data[1]; - * }); + * @example include:samples/document-snippets/family.js + * region_tag:bigtable_create_family */ create(options, callback) { if (is.fn(options)) { @@ -182,15 +172,8 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.` * request. * @param {object} callback.apiResponse The full API response. * - * @example - * family.delete(function(err, apiResponse) {}); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * family.delete().then(function(data) { - * const apiResponse = data[0]; - * }); + * @example include:samples/document-snippets/family.js + * region_tag:bigtable_del_family */ delete(gaxOptions, callback) { if (is.fn(gaxOptions)) { @@ -227,15 +210,8 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.` * request. * @param {boolean} callback.exists Whether the family exists or not. * - * @example - * family.exists(function(err, exists) {}); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * family.exists().then(function(data) { - * const exists = data[0]; - * }); + * @example include:samples/document-snippets/family.js + * region_tag:bigtable_exists_family */ exists(gaxOptions, callback) { if (is.fn(gaxOptions)) { @@ -275,18 +251,8 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.` * @param {Family} callback.family The Family object. * @param {object} callback.apiResponse The resource as it exists in the API. * - * @example - * family.get(function(err, family, apiResponse) { - * // `family.metadata` has been populated. - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * family.get().then(function(data) { - * const family = data[0]; - * const apiResponse = data[1]; - * }); + * @example include:samples/document-snippets/family.js + * region_tag:bigtable_get_family */ get(options, callback) { if (is.fn(options)) { @@ -322,16 +288,8 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.` * request. * @param {object} callback.metadata The metadata. * - * @example - * family.getMetadata(function(err, metadata, apiResponse) {}); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * family.getMetadata().then(function(data) { - * var metadata = data[0]; - * var apiResponse = data[1]; - * }); + * @example include:samples/document-snippets/family.js + * region_tag:bigtable_get_family_meta */ getMetadata(gaxOptions, callback) { if (is.fn(gaxOptions)) { @@ -375,22 +333,8 @@ Please use the format 'follows' or '${table.name}/columnFamilies/my-family'.` * request. * @param {object} callback.apiResponse The full API response. * - * @example - * var metadata = { - * rule: { - * versions: 2, - * union: true - * } - * }; - * - * family.setMetadata(metadata, function(err, apiResponse) {}); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * family.setMetadata(metadata).then(function(data) { - * var apiResponse = data[0]; - * }); + * @example include:samples/document-snippets/family.js + * region_tag:bigtable_set_family_meta */ setMetadata(metadata, gaxOptions, callback) { if (is.fn(gaxOptions)) {