diff --git a/samples/document-snippets/family.js b/samples/document-snippets/family.js new file mode 100644 index 000000000..2e561be75 --- /dev/null +++ b/samples/document-snippets/family.js @@ -0,0 +1,137 @@ +/** + * 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 snippets = { + createColmFamily: (instanceId, tableId, familyId) => { + // [START bigtable_create_family] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + + family + .create() + .then(result => { + const family = result[0]; + // let apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_create_family] + }, + existsFamily: (instanceId, tableId, familyId) => { + // [START bigtable_exists_family] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + + family + .exists() + .then(result => { + const exists = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_exists_family] + }, + getFamily: (instanceId, tableId, familyId) => { + // [START bigtable_get_family] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + + family + .get() + .then(result => { + const family = result[0]; + // const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_get_family] + }, + getMetadata: (instanceId, tableId, familyId) => { + // [START bigtable_get_family_meta] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + + family + .getMetadata() + .then(result => { + const metaData = result[0]; + // const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_get_family_meta] + }, + setMetadata: (instanceId, tableId, familyId) => { + // [START bigtable_set_family_meta] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + + const metadata = { + rule: { + versions: 2, + union: true, + }, + }; + + family + .setMetadata(metadata) + .then(result => { + const apiResponse = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_set_family_meta] + }, + delFamily: (instanceId, tableId, familyId) => { + // [START bigtable_del_family] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + const family = table.family(familyId); + + family + .delete() + .then(result => { + const apiResponse = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [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..a57e96116 --- /dev/null +++ b/samples/document-snippets/tests/family.js @@ -0,0 +1,75 @@ +/** + * Copyright 2018 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 = `sample-family-${uuid.v4()}`.substr(0, 10); // 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', () => { + familySnippets.createColmFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID); + }); + + it('should check family exists', () => { + familySnippets.existsFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID); + }); + + it('should get the family', () => { + familySnippets.getFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID); + }); + + it('should get family metadata', () => { + familySnippets.getMetadata(INSTANCE_ID, TABLE_ID, FAMILY_ID); + }); + + it('should set family metadata', () => { + familySnippets.setMetadata(INSTANCE_ID, TABLE_ID, FAMILY_ID); + }); + + it('should delete family', () => { + familySnippets.delFamily(INSTANCE_ID, TABLE_ID, FAMILY_ID); + }); +}); diff --git a/src/family.js b/src/family.js index 1dea3df00..286267915 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)) {