diff --git a/samples/document-snippets/instance.js b/samples/document-snippets/instance.js index f5e622b52..d32b6e75b 100644 --- a/samples/document-snippets/instance.js +++ b/samples/document-snippets/instance.js @@ -13,12 +13,11 @@ * limitations under the License. */ -const Bigtable = require('@google-cloud/bigtable'); -const bigtable = new Bigtable(); - const snippets = { - createInstance: (instanceId, clusterId, callback) => { + createInstance: (instanceId, clusterId) => { // [START bigtable_create_instance] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); // options for a PRODUCTION Instance @@ -53,19 +52,17 @@ const snippets = { const newInstance = result[0]; // let operations = result[1]; // let apiResponse = result[2]; - - console.log(`Created Instance: ${newInstance.id}`); - callback(null, newInstance); }) .catch(err => { - console.error('Error creating prod-instance:', err); - callback(err); + // Handle the error. }); // [END bigtable_create_instance] }, - createCluster: (instanceId, clusterId, callback) => { + createCluster: (instanceId, clusterId) => { // [START bigtable_create_cluster] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); // const options = { @@ -73,31 +70,29 @@ const snippets = { // nodes: 3, // storage: 'ssd', // }; - const options = { location: 'us-central1-b', storage: 'hdd', }; - instance .createCluster(clusterId, options) .then(result => { const newCluster = result[0]; // const operations = result[1]; // const apiResponse = result[2]; - console.log(`Cluster created: ${newCluster.id}`); - callback(null, newCluster); }) .catch(err => { - console.error('Error creating cluster: ', err); - callback(err); + // Handle the error. }); // [END bigtable_create_cluster] }, createAppProfile: (instanceId, clusterId, appProfileId, callback) => { // [START bigtable_create_app_profile] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); + const cluster = instance.cluster(clusterId); const options = { @@ -108,18 +103,18 @@ const snippets = { instance.createAppProfile(appProfileId, options, (err, appProfile) => { if (err) { - console.error('Error creating appProfile: ', err); - callback(err); - return; + // Handle the error. + return callback(err); } - console.log(`App-Profile created: ${appProfile.name}`); - callback(appProfile); + return callback(appProfile); }); // [END bigtable_create_app_profile] }, - createTable: (instanceId, tableId, callback) => { + createTable: (instanceId, tableId) => { // [START bigtable_create_table] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); const options = { @@ -151,110 +146,104 @@ const snippets = { .then(result => { const newTable = result[0]; // const apiResponse = result[1]; - console.log(`Table created: ${newTable.name}`); - callback(null, newTable); }) .catch(err => { - console.error('Error creating table: ', err); - callback(err); + // Handle the error. }); // [END bigtable_create_table] }, - existsInstance: (instanceId, callback) => { + existsInstance: instanceId => { + // [START bigtable_exists_instance] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); - // [START bigtable_exists_instance] instance .exists() .then(result => { const exists = result[0]; - console.log(`Instance ${instanceId} Exists: ${exists}`); - callback(null, exists); }) .catch(err => { - console.error('Error in checking Instance exists: ', err); - callback(err); + // Handle the error. }); // [END bigtable_exists_instance] }, - getInstance: (instanceId, callback) => { + getInstance: instanceId => { + // [START bigtable_get_instance] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); - // [START bigtable_get_instance] instance .get() .then(result => { const instance = result[0]; // const apiResponse = result[1]; - console.log(`Instance: \n${instance}`); - callback(null, instance); }) .catch(err => { - console.error('Error geting Instance: ', err); - callback(err); + // Handle the error. }); // [END bigtable_get_instance] }, - getClusters: (instanceId, callback) => { + getClusters: instanceId => { + // [START bigtable_get_clusters] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); - // [START bigtable_get_clusters] instance .getClusters() .then(result => { - console.log(`Clusters: \n${result[0]}`); - callback(null, result); + const clusters = result[0]; }) .catch(err => { - console.error('Error geting Clusters: ', err); - callback(err); + // Handle the error. }); // [END bigtable_get_clusters] }, - getAppProfiles: (instanceId, callback) => { + getAppProfiles: instanceId => { + // [START bigtable_get_app_profiles] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); - // [START bigtable_get_app_profiles] instance .getAppProfiles() .then(result => { - console.log(`AppProfiles: \n${result[0]}`); - callback(null, result); + const appProfiles = result[0]; }) .catch(err => { - console.error('Error geting AppProfiles: ', err); - callback(err); + // Handle the error. }); // [END bigtable_get_app_profiles] }, - getMetaData: (instanceId, callback) => { + getMetadata: instanceId => { + // [START bigtable_get_instance_metadata] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); - // [START bigtable_get_instance_metadata] instance .getMetadata() .then(result => { const metaData = result[0]; - // const apiResponse = result[1]; - console.log('%s %O', 'Instance Metadata:\n', metaData); - callback(null, metaData); }) .catch(err => { - console.error('Error geting Metadata: ', err); - callback(err); + // Handle the error. }); // [END bigtable_get_instance_metadata] }, - getTables: (instanceId, callback) => { - const instance = bigtable.instance(instanceId); - + getTables: instanceId => { // [START bigtable_get_tables] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); // To control how many API requests are made and page through the results // manually, set `autoPaginate` to false. @@ -268,24 +257,20 @@ const snippets = { instance .getTables(options) .then(result => { - console.log(`Tables:`); const tables = result[0]; - tables.forEach(t => { - console.log(t.id); - }); - callback(null, tables); }) .catch(err => { - console.error('Error geting Tables: ', err); - callback(err); + // Handle the error. }); // [END bigtable_get_tables] }, - updateInstance: (instanceId, callback) => { + updateInstance: instanceId => { + // [START bigtable_set_meta_data] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); - // [START bigtable_set_meta_data] const metadata = { displayName: 'updated-name', }; @@ -293,29 +278,27 @@ const snippets = { instance .setMetadata(metadata) .then(result => { - console.log(`API Response: \n${result[0]}`); - callback(null, result); + const apiResponse = result[0]; }) .catch(err => { - console.error('Error in Set MetaData: ', err); - callback(err); + // Handle the error. }); // [END bigtable_set_meta_data] }, - delInstance: (instanceId, callback) => { + delInstance: instanceId => { // [START bigtable_del_instance] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); const instance = bigtable.instance(instanceId); + instance .delete() .then(result => { const apiResponse = result[0]; - console.log(`Instance ${instanceId} deleted: ${apiResponse}`); - callback(null, apiResponse); }) .catch(err => { - console.error('Error deleting instance: ', err); - callback(err); + // Handle the error. }); // [END bigtable_del_instance] }, diff --git a/samples/document-snippets/table.js b/samples/document-snippets/table.js new file mode 100644 index 000000000..6f23c4fff --- /dev/null +++ b/samples/document-snippets/table.js @@ -0,0 +1,332 @@ +/** + * 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 = { + createTable: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_create_table] + table + .create() + .then(result => { + const table = result[0]; + // let apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_create_table] + }, + + existsTable: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_exists_table] + table + .exists() + .then(result => { + const exists = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_exists_table] + }, + + getTable: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_get_table] + table + .get() + .then(result => { + const table = result[0]; + // const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_get_table] + }, + + getMetadata: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_get_table_meta] + table + .getMetadata() + .then(result => { + const metaData = result[0]; + // const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_get_table_meta] + }, + + createFamily: (instanceId, tableId, familyId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_create_family] + const options = {}; + // options.rule = { + // age: { + // seconds: 0, + // nanos: 5000 + // }, + // versions: 3, + // union: true + // }; + + table + .createFamily(familyId, options) + .then(result => { + const family = result[0]; + // const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_create_table] + }, + + getFamilies: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_get_families] + table + .getFamilies() + .then(result => { + const families = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_get_families] + }, + + insertRows: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_insert_rows] + const entries = [ + { + key: 'alincoln', + data: { + follows: { + gwashington: 1, + }, + }, + }, + ]; + + table + .insert(entries) + .then(result => { + const apiResponse = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_insert_rows] + }, + + getRows: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_get_rows] + const options = { + keys: ['alincoln', 'gwashington'], + }; + table + .getRows(options) + .then(result => { + const rows = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_get_rows] + }, + + mutate: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_mutate_rows] + const entries = [ + { + method: 'delete', + key: 'alincoln', + }, + ]; + table + .mutate(entries) + .then(() => { + // handle success + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_mutate_rows] + }, + + createReadStream: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_table_readstream] + table + .createReadStream() + .on('error', err => { + // Handle the error. + }) + .on('data', function(row) { + // `row` is a Row object. + }) + .on('end', function() { + // All rows retrieved. + }); + //- + // If you anticipate many results, you can end a stream early to prevent + // unnecessary processing. + //- + // table + // .createReadStream() + // .on('data', function (row) { + // this.end(); + // }); + + //- + // Specify arbitrary keys for a non-contiguous set of rows. + // The total size of the keys must remain under 1MB, after encoding. + //- + // table.createReadStream({ + // keys: [ + // 'alincoln', + // 'gwashington' + // ] + // }); + + //- + // Scan for row keys that contain a specific prefix. + //- + // table.createReadStream({ + // prefix: 'gwash' + // }); + + //- + // Specify a contiguous range of rows to read by supplying `start` and `end` + // keys. + // + // If the `start` key is omitted, it is interpreted as an empty string. + // If the `end` key is omitted, it is interpreted as infinity. + //- + // table.createReadStream({ + // start: 'alincoln', + // end: 'gwashington' + // }); + + //- + // Specify multiple ranges. + //- + // table.createReadStream({ + // ranges: [{ + // start: 'alincoln', + // end: 'gwashington' + // }, { + // start: 'tjefferson', + // end: 'jadams' + // }] + // }); + + //- + // Apply a {@link Filter} to the contents of the specified rows. + //- + // table.createReadStream({ + // filter: [ + // { + // column: 'gwashington' + // }, { + // value: 1 + // } + // ] + // }); + // + // [END bigtable_table_readstream] + }, + + sampleRowKeys: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_sample_row_keys] + table + .sampleRowKeys() + .then(result => { + const sampleRKeys = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_sample_row_keys] + }, + + delRows: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_del_rows] + table + .deleteRows('alincoln') + .then(result => { + const apiResponse = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [START bigtable_del_rows] + }, + + delTable: (instanceId, tableId) => { + const instance = bigtable.instance(instanceId); + const table = instance.table(tableId); + + // [START bigtable_del_table] + table + .delete() + .then(result => { + const apiResponse = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_del_table] + }, +}; + +module.exports = snippets; diff --git a/samples/document-snippets/tests/instance.js b/samples/document-snippets/tests/instance.js index 5e3cb5fe5..148bc9be5 100644 --- a/samples/document-snippets/tests/instance.js +++ b/samples/document-snippets/tests/instance.js @@ -31,90 +31,60 @@ const instanceSnippets = require('../instance.js'); describe('Instance Snippets', function() { after(function(done) { const instance = bigtable.instance(INSTANCE_ID); - instance.delete(done); - }); - - it('should create an instance', function(done) { - instanceSnippets.createInstance(INSTANCE_ID, CLUSTER_ID, err => { - assert.ifError(err); + instance.exists().then(result => { + const exists = result[0]; + if (exists) { + instance.delete(); + } done(); }); }); - // it('should create cluster', function(done) { - // instanceSnippets.createCluster(INSTANCE_ID, CLUSTER_ID, function(err, instance) { - // assert.ifError(err); - // done(); - // }); + it('should create an instance', () => { + instanceSnippets.createInstance(INSTANCE_ID, CLUSTER_ID); + }); + + // it('should create cluster', () => { + // instanceSnippets.createCluster(INSTANCE_ID, CLUSTER_ID); // }); // it('should create an app-profile', function(done) { - // instanceSnippets.createAppProfile(INSTANCE_ID, APP_PROFILE_ID, function(err, appProfile) { - // assert.ifError(err); - // done(); - // }); + // instanceSnippets.createAppProfile(INSTANCE_ID, APP_PROFILE_ID, done); // }); - it('should create table', function(done) { - instanceSnippets.createTable(INSTANCE_ID, TABLE_ID, err => { - assert.ifError(err); - done(); - }); + it('should create table', () => { + instanceSnippets.createTable(INSTANCE_ID, TABLE_ID); }); - it('should check instance existance', function(done) { - instanceSnippets.existsInstance(INSTANCE_ID, err => { - assert.ifError(err); - done(); - }); + it('should check instance existance', () => { + instanceSnippets.existsInstance(INSTANCE_ID); }); - it('should get instance', function(done) { - instanceSnippets.getInstance(INSTANCE_ID, err => { - assert.ifError(err); - done(); - }); + it('should get instance', () => { + instanceSnippets.getInstance(INSTANCE_ID); }); - it('should get Clusters', function(done) { - instanceSnippets.getClusters(INSTANCE_ID, err => { - assert.ifError(err); - done(); - }); + it('should get Clusters', () => { + instanceSnippets.getClusters(INSTANCE_ID); }); - // it('should get appProfiles', function(done) { - // instanceSnippets.getAppProfiles(INSTANCE_ID, err => { - // assert.ifError(err); - // done(); - // }); + // it('should get appProfiles', () => { + // instanceSnippets.getAppProfiles(INSTANCE_ID); // }); - it('should get MetaData', function(done) { - instanceSnippets.getMetaData(INSTANCE_ID, err => { - assert.ifError(err); - done(); - }); + it('should get metadata', () => { + instanceSnippets.getMetadata(INSTANCE_ID); }); - it('should get tables', function(done) { - instanceSnippets.getTables(INSTANCE_ID, err => { - assert.ifError(err); - done(); - }); + it('should get tables', () => { + instanceSnippets.getTables(INSTANCE_ID); }); - it('should update instance', function(done) { - instanceSnippets.updateInstance(INSTANCE_ID, err => { - assert.ifError(err); - done(); - }); + it('should update instance', () => { + instanceSnippets.updateInstance(INSTANCE_ID); }); - it('should delete instance', function(done) { - instanceSnippets.delInstance(INSTANCE_ID, err => { - assert.ifError(err); - done(); - }); + it('should delete instance', () => { + instanceSnippets.delInstance(INSTANCE_ID); }); }); diff --git a/samples/document-snippets/tests/table.js b/samples/document-snippets/tests/table.js new file mode 100644 index 000000000..30c7e0da4 --- /dev/null +++ b/samples/document-snippets/tests/table.js @@ -0,0 +1,101 @@ +/** + * 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 tableSnippets = require('../table.js'); + +const instance = bigtable.instance(INSTANCE_ID); + +describe('Table Snippets', function() { + before(async () => { + await instance.create({ + clusters: [ + { + name: CLUSTER_ID, + location: 'us-central1-f', + storage: 'hdd', + }, + ], + type: 'DEVELOPMENT', + }); + }); + + after(async () => { + await instance.delete(); + }); + + it('should create a table', () => { + tableSnippets.createTable(INSTANCE_ID, TABLE_ID); + }); + + it('should check table exists', () => { + tableSnippets.existsTable(INSTANCE_ID, TABLE_ID); + }); + + it('should get the table', () => { + tableSnippets.getTable(INSTANCE_ID, TABLE_ID); + }); + + it('should get table metadata', () => { + tableSnippets.getMetadata(INSTANCE_ID, TABLE_ID); + }); + + it('should create family', () => { + tableSnippets.createFamily(INSTANCE_ID, TABLE_ID, 'follows'); + }); + + it('should get families', () => { + tableSnippets.getFamilies(INSTANCE_ID, TABLE_ID); + }); + + it('should insert row', () => { + tableSnippets.insertRows(INSTANCE_ID, TABLE_ID); + }); + + it('should get rows', () => { + tableSnippets.getRows(INSTANCE_ID, TABLE_ID); + }); + + it('should mutate table', () => { + tableSnippets.mutate(INSTANCE_ID, TABLE_ID); + }); + + it('should create a read-stream', () => { + tableSnippets.createReadStream(INSTANCE_ID, TABLE_ID); + }); + + it('should create sample row-keys', () => { + tableSnippets.sampleRowKeys(INSTANCE_ID, TABLE_ID); + }); + + // it('should delete rows', () => { + // tableSnippets.delRows(INSTANCE_ID, TABLE_ID); + // }); + + it('should delete table', () => { + tableSnippets.delTable(INSTANCE_ID, TABLE_ID); + }); +}); diff --git a/src/table.js b/src/table.js index 01facde06..eda4d4a32 100644 --- a/src/table.js +++ b/src/table.js @@ -147,20 +147,9 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.` * @param {?error} callback.err An error returned while making this request. * @param {Table} callback.table The newly created table. * @param {object} callback.apiResponse The full API response. - * @example - * table.create(function(err, table, apiResponse) { - * if (!err) { - * // The table was created successfully. - * } - * }); * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * table.create().then(function(data) { - * var table = data[0]; - * var apiResponse = data[1]; - * }); + * @example