forked from googleapis/nodejs-pubsub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added example region-tags for row object (googleapis#266)
* Added example region-tags for row object * lint update * added code review update * more update to sample code * added code-review updates * removed callbacks * added lint fixes, uniform variable naming
- Loading branch information
1 parent
6fb4ca7
commit 58457b7
Showing
4 changed files
with
411 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ env: | |
mocha: true | ||
rules: | ||
node/no-unpublished-require: off | ||
no-unused-vars: off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,312 @@ | ||
/** | ||
* 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 = { | ||
createRow: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_create_row] | ||
const row = table.row('samplerow'); | ||
|
||
row | ||
.create() | ||
.then(result => { | ||
const apiResponse = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_create_row] | ||
}, | ||
|
||
createRules: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_create_rules] | ||
const row = table.row('samplerow'); | ||
// - | ||
// Add an increment amount to an existing value, if the targeted cell is | ||
// unset, it will be treated as containing a zero. | ||
// | ||
const rules = [ | ||
{ | ||
column: 'follows:gwashington', | ||
increment: 1, | ||
}, | ||
]; | ||
|
||
// - | ||
// You can also create a rule that will append data to an existing value. | ||
// If the targeted cell is unset, it will be treated as a containing an | ||
// empty string. | ||
// | ||
// var rules = [ | ||
// { | ||
// column: 'follows:alincoln', | ||
// append: ' Honest Abe!', | ||
// }, | ||
// ]; | ||
|
||
row | ||
.createRules(rules) | ||
.then(result => { | ||
const apiResponse = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_create_rules] | ||
}, | ||
|
||
deleteAllCells: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_delete_all_cells] | ||
const row = table.row('samplerow'); | ||
row | ||
.delete() | ||
.then(result => { | ||
const apiResponse = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_delete_all_cells] | ||
}, | ||
|
||
deleteCells: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_delete_particular_cells] | ||
const row = table.row('samplerow'); | ||
|
||
// Delete selective cell within a family. | ||
// let cells = [ | ||
// 'follows:gwashington' | ||
// ]; | ||
|
||
// Delete all cells within a family. | ||
const cells = ['follows']; | ||
|
||
row | ||
.deleteCells(cells) | ||
.then(result => { | ||
const apiResponse = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_delete_particular_cells] | ||
}, | ||
|
||
exists: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_row_exists] | ||
const row = table.row('samplerow'); | ||
|
||
row | ||
.exists() | ||
.then(result => { | ||
const exists = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_row_exists] | ||
}, | ||
|
||
filter: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_row_filter] | ||
const row = table.row('samplerow'); | ||
|
||
const filter = [ | ||
{ | ||
family: 'follows', | ||
}, | ||
{ | ||
column: 'alincoln', | ||
}, | ||
{ | ||
value: 1, | ||
}, | ||
]; | ||
|
||
// Optionally, you can pass in an array of entries to be ran in the event | ||
// that a match is not made. | ||
const config = { | ||
onNoMatch: [ | ||
{ | ||
method: 'insert', | ||
data: { | ||
follows: { | ||
jadams: 1, | ||
}, | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
row | ||
.filter(filter, config) | ||
.then(result => { | ||
const matched = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_row_filter] | ||
}, | ||
|
||
get: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_get_row] | ||
const row = table.row('samplerow'); | ||
|
||
row | ||
.get() | ||
.then(result => { | ||
const row = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
|
||
//- | ||
// Or pass in an array of column names to populate specific cells. | ||
// Under the hood this will create an interleave filter. | ||
//- | ||
// row | ||
// .get([ | ||
// 'follows:gwashington', | ||
// 'follows:alincoln' | ||
// ]) | ||
// .then(result => { | ||
// let row = result[0]; | ||
// }) | ||
// .catch(err => { | ||
// // Handle the error. | ||
// }); | ||
|
||
// [END bigtable_get_row] | ||
}, | ||
|
||
getMetadata: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_get_row_meta] | ||
const row = table.row('samplerow'); | ||
|
||
row | ||
.getMetadata() | ||
.then(result => { | ||
const metaData = result[0]; | ||
const apiResponse = result[1]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_get_row_meta] | ||
}, | ||
|
||
increment: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_row_increment] | ||
const row = table.row('samplerow'); | ||
|
||
// Specify a custom amount to increment the column by. | ||
// row | ||
// .increment('follows:gwashington', 2) | ||
// .then(result => { | ||
// let value = result[0]; | ||
// let apiResponse = result[1]; | ||
// }); | ||
|
||
// To decrement a column, simply supply a negative value. | ||
// row | ||
// .increment('follows:gwashington', -1) | ||
// .then(result => { | ||
// let value = result[0]; | ||
// let apiResponse = result[1]; | ||
// }); | ||
row | ||
.increment('follows:gwashington') | ||
.then(result => { | ||
const value = result[0]; | ||
const apiResponse = result[1]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_row_increment] | ||
}, | ||
|
||
save: (instanceId, tableId) => { | ||
const Bigtable = require('@google-cloud/bigtable'); | ||
const bigtable = new Bigtable(); | ||
const instance = bigtable.instance(instanceId); | ||
const table = instance.table(tableId); | ||
|
||
// [START bigtable_row_save] | ||
const row = table.row('samplerow'); | ||
const entry = { | ||
follows: { | ||
jadams: 1, | ||
}, | ||
}; | ||
row | ||
.save(entry) | ||
.then(result => { | ||
const apiResponse = result[0]; | ||
}) | ||
.catch(err => { | ||
// Handle the error. | ||
}); | ||
// [END bigtable_row_save] | ||
}, | ||
}; | ||
|
||
module.exports = snippets; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* 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 rowSnippets = require('../row.js'); | ||
|
||
const instance = bigtable.instance(INSTANCE_ID); | ||
|
||
describe('Row 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 row', () => { | ||
rowSnippets.createRow(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should create a row rules', () => { | ||
rowSnippets.createRules(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should delete all cells', () => { | ||
rowSnippets.deleteAllCells(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should delete selected cells', () => { | ||
rowSnippets.deleteCells(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should check row exists', () => { | ||
rowSnippets.exists(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should mutate row with matched filter', () => { | ||
rowSnippets.filter(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should get row meta-data', () => { | ||
rowSnippets.getMetadata(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should increment row', () => { | ||
rowSnippets.increment(INSTANCE_ID, TABLE_ID); | ||
}); | ||
it('should save row', () => { | ||
rowSnippets.save(INSTANCE_ID, TABLE_ID); | ||
}); | ||
}); |
Oops, something went wrong.