Skip to content

Commit

Permalink
Added example region-tag for Family.js
Browse files Browse the repository at this point in the history
  • Loading branch information
vijay-qlogic committed Aug 1, 2018
1 parent 4bfd84e commit ad72fc3
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 68 deletions.
128 changes: 128 additions & 0 deletions samples/document-snippets/family.js
Original file line number Diff line number Diff line change
@@ -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;
93 changes: 93 additions & 0 deletions samples/document-snippets/tests/family.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
80 changes: 12 additions & 68 deletions src/family.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <caption>include:samples/document-snippets/family.js</caption>
* region_tag:bigtable_create_family
*/
create(options, callback) {
if (is.fn(options)) {
Expand All @@ -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 <caption>include:samples/document-snippets/family.js</caption>
* region_tag:bigtable_del_family
*/
delete(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
Expand Down Expand Up @@ -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 <caption>include:samples/document-snippets/family.js</caption>
* region_tag:bigtable_exists_family
*/
exists(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
Expand Down Expand Up @@ -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 <caption>include:samples/document-snippets/family.js</caption>
* region_tag:bigtable_get_family
*/
get(options, callback) {
if (is.fn(options)) {
Expand Down Expand Up @@ -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 <caption>include:samples/document-snippets/family.js</caption>
* region_tag:bigtable_get_family_meta
*/
getMetadata(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
Expand Down Expand Up @@ -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 <caption>include:samples/document-snippets/family.js</caption>
* region_tag:bigtable_set_family_meta
*/
setMetadata(metadata, gaxOptions, callback) {
if (is.fn(gaxOptions)) {
Expand Down

0 comments on commit ad72fc3

Please sign in to comment.