Skip to content

Commit

Permalink
add sample for read and write transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
alkatrivedi committed Apr 5, 2024
1 parent b901522 commit c8d7f40
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 53 deletions.
53 changes: 0 additions & 53 deletions samples/dml.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,59 +67,6 @@ function insertUsingDml(instanceId, databaseId, projectId) {
// [END spanner_dml_standard_insert]
}

async function insertUsingGetTransaction(instanceId, databaseId, projectId) {
// [START spanner_dml_standard_insert_using_getTransaction]
// Imports the Google Cloud client library
const {Spanner} = 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,
});

const options = {
optimisticLock: true,
};

// Gets a reference to a Cloud Spanner instance and database
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);
database.getTransaction(options, async (err, transaction) => {
if (err) {
console.error(err);
return;
}
try {
const [rowCount] = await transaction.runUpdate({
sql: 'INSERT Singers (SingerId, FirstName, LastName) VALUES (10, @firstName, @lastName)',
params: {
firstName: 'Virginia',
lastName: 'Watson',
},
});

console.log(
`Successfully inserted ${rowCount} record into the Singers table.`
);

await transaction.commit();
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
database.close();
}
});
// [END spanner_dml_standard_insert_using_getTransaction]
}

function updateUsingDml(instanceId, databaseId, projectId) {
// [START spanner_dml_standard_update]
// Imports the Google Cloud client library
Expand Down
73 changes: 73 additions & 0 deletions samples/insert-query-with-get-trasanction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2024 Google LLC
//
// 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: Calls a server side function on a Spanner PostgreSQL database.
// usage: node insert-query-with-get-transaction.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

async function main (instanceId, databaseId, projectId) {

Check failure on line 19 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Delete `·`
// [START spanner_insert_query_with_get_transaction]

Check failure on line 20 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
// Imports the Google Cloud client library.

Check failure on line 21 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
const {Spanner} = require('../build/src');

Check failure on line 22 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`

Check failure on line 22 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

"../build/src" is not published

/**

Check failure on line 24 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
* TODO(developer): Uncomment the following lines before running the sample.

Check failure on line 25 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Replace `·····` with `···`
*/

Check failure on line 26 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
// const projectId = 'my-project-id';

Check failure on line 27 in samples/insert-query-with-get-trasanction.js

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
// const instanceId = 'my-instance';
// const databaseId = 'my-database';

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

async function writeTransaction() {

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

const options = {
optimisticLock: true,
};

let promise = await database.getTransaction(options).then();
let transaction = promise[0];

try {
const [rowCount] = await transaction.runUpdate({
sql: `INSERT Singers (SingerId, FirstName, LastName) VALUES
(1, 'Melissa', 'Garcia'),
(2, 'Russell', 'Morales'),
(3, 'Jacqueline', 'Long'),
(4, 'Dylan', 'Shaw')`,
});
console.log(`${rowCount} records inserted into the table.`);
} catch (err) {
console.error('ERROR:', err);
}

await transaction.commit();

transaction.end();
}
writeTransaction();
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions samples/read-query-with-get-transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2024 Google LLC
//
// 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: Calls a server side function on a Spanner PostgreSQL database.
// usage: node insert-query-with-get-transaction.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

async function main (instanceId, databaseId, projectId) {
// [START spanner_insert_query_with_get_transaction]
// Imports the Google Cloud client library.
const {Spanner} = require('../build/src');

/**
* 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 readTransaction() {

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

const options = {
optimisticLock: true,
};

let promise = await database.getTransaction(options).then();
let transaction = promise[0];

try {
await transaction.run("SELECT * FROM Singers").then(results => {

Check warning on line 50 in samples/read-query-with-get-transaction.js

View workflow job for this annotation

GitHub Actions / lint

Strings must use singlequote
const rows = results[0].map(row => row.toJSON());
console.log(rows);
});
} catch (err) {
console.error('ERROR:', err);
}

await transaction.commit();

transaction.end();
}
readTransaction();
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));

0 comments on commit c8d7f40

Please sign in to comment.