Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(spanner): add a sample for max commit delays #1993

Merged
merged 17 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions samples/max-commit-delay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright 2024 Google LLC
surbhigarg92 marked this conversation as resolved.
Show resolved Hide resolved
//
// 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.
// sample-metadata:
// title: Executes request with max commit delay
// usage: node max-commit-delay.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

surbhigarg92 marked this conversation as resolved.
Show resolved Hide resolved
'use strict';

function main(
instanceId = 'my-instance',
databaseId = 'my-database',
projectId = 'my-project-id'
) {
// [START spanner_set_max_commit_delay]
// Imports the Google Cloud client library.
const {Spanner, protos} = require('@google-cloud/spanner');

/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const projectId = 'my-project-id';
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

// Creates a client.
const spanner = new Spanner({
projectId: projectId,
});

async function spannerSetMaxCommitDelay() {
// Gets a reference to a Cloud Spanner instance and database.
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

// Instantiate Spanner table objects.
const albumsTable = database.table('Albums');

// Updates rows in the Venues table.
try {
const [response] = await albumsTable.upsert(
nginsberg-google marked this conversation as resolved.
Show resolved Hide resolved
[
{SingerId: '1', AlbumId: '1', MarketingBudget: '200000'},
{SingerId: '2', AlbumId: '2', MarketingBudget: '400000'},
],
{
maxCommitDelay: protos.google.protobuf.Duration({
nginsberg-google marked this conversation as resolved.
Show resolved Hide resolved
seconds: 0, // 0 seconds
nanos: 100000000, // 100,000,000 nanoseconds = 100 milliseconds
}),
}
);
console.log(
`Updated data with ${response.commitStats.mutationCount} mutations.`
);
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
database.close();
}
}
spannerSetMaxCommitDelay();
// [END spanner_set_max_commit_delay]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
8 changes: 8 additions & 0 deletions samples/system-test/spanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,14 @@ describe('Spanner', () => {
assert.match(output, new RegExp('Updated data with (\\d+) mutations'));
});

// set_max_commit_delay
it('should update rows in Albums example table and set max commit delay', async () => {
const output = execSync(
`${crudCmd} setMaxCommitDelay ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}`
);
assert.match(output, new RegExp('Updated data with (\\d+) mutations'));
});

// create_database_with_version_retention_period
it('should create a database with a version retention period', async () => {
const output = execSync(
Expand Down
Loading