From 3765edc6b3e20a32004efd6dfe2c69f64fbc0086 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya <lalji.kanjareeya@qlogic.io> Date: Thu, 15 Apr 2021 17:14:01 +0530 Subject: [PATCH 1/4] sample: dml samples conform to rubric --- samples/delete-using-dml.js | 70 +++++++++ samples/delete-using-partitioned-dml.js | 62 ++++++++ samples/insert-using-dml.js | 78 ++++++++++ ...-with-custom-timeout-and-retry-settings.js | 80 ++++++++++ samples/query-data-with-parameter.js | 75 ++++++++++ samples/system-test/spanner.test.js | 27 ++-- samples/update-using-batch-dml.js | 78 ++++++++++ samples/update-using-dml-with-struct.js | 79 ++++++++++ samples/update-using-dml-with-timestamp.js | 72 +++++++++ samples/update-using-dml.js | 71 +++++++++ samples/update-using-partitioned-dml.js | 62 ++++++++ samples/write-and-read-using-dml.js | 78 ++++++++++ samples/write-using-dml.js | 73 +++++++++ samples/write-with-transaction-using-dml.js | 139 ++++++++++++++++++ 14 files changed, 1030 insertions(+), 14 deletions(-) create mode 100644 samples/delete-using-dml.js create mode 100644 samples/delete-using-partitioned-dml.js create mode 100644 samples/insert-using-dml.js create mode 100644 samples/insert-with-custom-timeout-and-retry-settings.js create mode 100644 samples/query-data-with-parameter.js create mode 100644 samples/update-using-batch-dml.js create mode 100644 samples/update-using-dml-with-struct.js create mode 100644 samples/update-using-dml-with-timestamp.js create mode 100644 samples/update-using-dml.js create mode 100644 samples/update-using-partitioned-dml.js create mode 100644 samples/write-and-read-using-dml.js create mode 100644 samples/write-using-dml.js create mode 100644 samples/write-with-transaction-using-dml.js diff --git a/samples/delete-using-dml.js b/samples/delete-using-dml.js new file mode 100644 index 000000000..35f08e0d6 --- /dev/null +++ b/samples/delete-using-dml.js @@ -0,0 +1,70 @@ +// Copyright 2021 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: Deletes record using DML +// usage: node deleteUsingDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_standard_delete] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function deleteUsingDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + database.runTransaction(async (err, transaction) => { + if (err) { + console.error(err); + return; + } + try { + const [rowCount] = await transaction.runUpdate({ + sql: "DELETE FROM Singers WHERE FirstName = 'Alice'", + }); + + console.log(`Successfully deleted ${rowCount} record.`); + await transaction.commit(); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + }); + } + deleteUsingDml().catch(console.error); + // [END spanner_dml_standard_delete] +} +main(...process.argv.slice(2)); diff --git a/samples/delete-using-partitioned-dml.js b/samples/delete-using-partitioned-dml.js new file mode 100644 index 000000000..b946288e2 --- /dev/null +++ b/samples/delete-using-partitioned-dml.js @@ -0,0 +1,62 @@ +// Copyright 2021 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: Deletes multilple records using DML. +// usage: node deleteUsingPartitionedDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_partitioned_delete] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function deleteUsingPartitionedDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + try { + const [rowCount] = await database.runPartitionedUpdate({ + sql: 'DELETE FROM Singers WHERE SingerId > 10', + }); + console.log(`Successfully deleted ${rowCount} records.`); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + } + deleteUsingPartitionedDml().catch(console.error); + // [END spanner_dml_partitioned_delete] +} +main(...process.argv.slice(2)); diff --git a/samples/insert-using-dml.js b/samples/insert-using-dml.js new file mode 100644 index 000000000..03fe953d9 --- /dev/null +++ b/samples/insert-using-dml.js @@ -0,0 +1,78 @@ +// Copyright 2021 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: Inserts record using DML into an example Cloud Spanner table. +// usage: node insertUsingDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_standard_insert] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function insertUsingDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + database.runTransaction(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(); + } + }); + } + insertUsingDml().catch(console.error); + // [END spanner_dml_standard_insert] +} +main(...process.argv.slice(2)); diff --git a/samples/insert-with-custom-timeout-and-retry-settings.js b/samples/insert-with-custom-timeout-and-retry-settings.js new file mode 100644 index 000000000..60fbb3b9a --- /dev/null +++ b/samples/insert-with-custom-timeout-and-retry-settings.js @@ -0,0 +1,80 @@ +// Copyright 2021 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: Insert records using custom timeout and retry settings. +// usage: node insertWithCustomTimeoutAndRetrySettings <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_set_custom_timeout_and_retry] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function insertWithCustomTimeoutAndRetrySettings() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + const table = database.table('Singers'); + + const DEADLINE_EXCEEDED_STATUS_CODE = 4; + const UNAVAILABLE_STATUS_CODE = 14; + const retryAndTimeoutSettings = { + retry: { + retryCodes: [DEADLINE_EXCEEDED_STATUS_CODE, UNAVAILABLE_STATUS_CODE], + backoffSettings: { + // Configure retry delay settings. + initialRetryDelayMillis: 500, + maxRetryDelayMillis: 64000, + retryDelayMultiplier: 1.5, + // Configure RPC and total timeout settings. + initialRpcTimeoutMillis: 60000, + rpcTimeoutMultiplier: 1.0, + maxRpcTimeoutMillis: 60000, + totalTimeoutMillis: 60000, + }, + }, + }; + + const row = { + SingerId: 16, + FirstName: 'Martha', + LastName: 'Waller', + }; + + await table.insert(row, retryAndTimeoutSettings); + + console.log('record inserted.'); + } + insertWithCustomTimeoutAndRetrySettings().catch(console.error); + // [END spanner_set_custom_timeout_and_retry] +} +main(...process.argv.slice(2)); diff --git a/samples/query-data-with-parameter.js b/samples/query-data-with-parameter.js new file mode 100644 index 000000000..f64c07af9 --- /dev/null +++ b/samples/query-data-with-parameter.js @@ -0,0 +1,75 @@ +// Copyright 2021 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: Query record inserted using DML with a query parameter. +// usage: node queryDataWithParameter <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_query_with_parameter] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function queryDataWithParameter() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + const query = { + sql: `SELECT SingerId, FirstName, LastName + FROM Singers WHERE LastName = @lastName`, + params: { + lastName: 'Garcia', + }, + }; + + // Queries rows from the Albums table + try { + const [rows] = await database.run(query); + + rows.forEach(row => { + const json = row.toJSON(); + console.log( + `SingerId: ${json.SingerId}, FirstName: ${json.FirstName}, LastName: ${json.LastName}` + ); + }); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + } + queryDataWithParameter().catch(console.error); + // [END spanner_query_with_parameter] +} +main(...process.argv.slice(2)); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index 84645f206..f7a508670 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -32,7 +32,6 @@ const rpcPriorityCommand = 'node rpc-priority.js'; const transactionCmd = 'node transaction.js'; const timestampCmd = 'node timestamp.js'; const structCmd = 'node struct.js'; -const dmlCmd = 'node dml.js'; const datatypesCmd = 'node datatypes.js'; const backupsCmd = 'node backups.js'; const instanceCmd = 'node instance.js'; @@ -669,7 +668,7 @@ describe('Spanner', () => { // dml_standard_insert it('should insert rows into an example table using a DML statement', async () => { const output = execSync( - `${dmlCmd} insertUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node insert-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match( output, @@ -680,7 +679,7 @@ describe('Spanner', () => { // dml_standard_update it('should update a row in an example table using a DML statement', async () => { const output = execSync( - `${dmlCmd} updateUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node update-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 1 record/); }); @@ -688,7 +687,7 @@ describe('Spanner', () => { // dml_standard_delete it('should delete a row from an example table using a DML statement', async () => { const output = execSync( - `${dmlCmd} deleteUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node delete-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully deleted 1 record\./); }); @@ -696,7 +695,7 @@ describe('Spanner', () => { // dml_standard_update_with_timestamp it('should update the timestamp of multiple records in an example table using a DML statement', async () => { const output = execSync( - `${dmlCmd} updateUsingDmlWithTimestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node update-using-dml-with-timestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 2 records/); }); @@ -704,7 +703,7 @@ describe('Spanner', () => { // dml_write_then_read it('should insert a record in an example table using a DML statement and then query the record', async () => { const output = execSync( - `${dmlCmd} writeAndReadUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node write-and-read-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Timothy Campbell/); }); @@ -712,7 +711,7 @@ describe('Spanner', () => { // dml_structs it('should update a record in an example table using a DML statement along with a struct value', async () => { const output = execSync( - `${dmlCmd} updateUsingDmlWithStruct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node update-using-dml-with-struct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 1 record/); }); @@ -720,7 +719,7 @@ describe('Spanner', () => { // dml_getting_started_insert it('should insert multiple records into an example table using a DML statement', async () => { const output = execSync( - `${dmlCmd} writeUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node write-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /4 records inserted/); }); @@ -728,7 +727,7 @@ describe('Spanner', () => { // dml_query_with_parameter it('should use a parameter query to query record that was inserted using a DML statement', async () => { const output = execSync( - `${dmlCmd} queryWithParameter ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node query-with-parameter ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /SingerId: 12, FirstName: Melissa, LastName: Garcia/); }); @@ -736,7 +735,7 @@ describe('Spanner', () => { // dml_getting_started_update it('should transfer value from one record to another using DML statements within a transaction', async () => { const output = execSync( - `${dmlCmd} writeWithTransactionUsingDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node write-with-transaction-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match( output, @@ -747,7 +746,7 @@ describe('Spanner', () => { // dml_partitioned_update it('should update multiple records using a partitioned DML statement', async () => { const output = execSync( - `${dmlCmd} updateUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node update-using-partitioned-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 3 records/); }); @@ -755,7 +754,7 @@ describe('Spanner', () => { // dml_partitioned_delete it('should delete multiple records using a partitioned DML statement', async () => { const output = execSync( - `${dmlCmd} deleteUsingPartitionedDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node delete-using-partitioned-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully deleted 5 records/); }); @@ -763,7 +762,7 @@ describe('Spanner', () => { // dml_batch_update it('should insert and update records using Batch DML', async () => { const output = execSync( - `${dmlCmd} updateUsingBatchDml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node update-using-batch-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match( output, @@ -1073,7 +1072,7 @@ describe('Spanner', () => { // custom_timeout_and_retry it('should insert with custom timeout and retry settings', async () => { const output = execSync( - `${dmlCmd} insertWithCustomTimeoutAndRetrySettings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node insert-with-custom-timeout-and-retry-settings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /record inserted./); }); diff --git a/samples/update-using-batch-dml.js b/samples/update-using-batch-dml.js new file mode 100644 index 000000000..f19fe3a2c --- /dev/null +++ b/samples/update-using-batch-dml.js @@ -0,0 +1,78 @@ +// Copyright 2021 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: Insert and Update records using Batch DML. +// usage: node updateUsingBatchDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_batch_update] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function updateUsingBatchDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + const insert = { + sql: `INSERT INTO Albums (SingerId, AlbumId, AlbumTitle, MarketingBudget) + VALUES (1, 3, "Test Album Title", 10000)`, + }; + + const update = { + sql: `UPDATE Albums SET MarketingBudget = MarketingBudget * 2 + WHERE SingerId = 1 and AlbumId = 3`, + }; + + const dmlStatements = [insert, update]; + + try { + await database.runTransactionAsync(async transaction => { + const [rowCounts] = await transaction.batchUpdate(dmlStatements); + await transaction.commit(); + console.log( + `Successfully executed ${rowCounts.length} SQL statements using Batch DML.` + ); + }); + } catch (err) { + console.error('ERROR:', err); + throw err; + } finally { + // Close the database when finished. + database.close(); + } + } + updateUsingBatchDml().catch(console.error); + // [END spanner_dml_batch_update] +} +main(...process.argv.slice(2)); diff --git a/samples/update-using-dml-with-struct.js b/samples/update-using-dml-with-struct.js new file mode 100644 index 000000000..5a14a9988 --- /dev/null +++ b/samples/update-using-dml-with-struct.js @@ -0,0 +1,79 @@ +// Copyright 2021 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: Updates record using DML and a struct value. +// usage: node updateUsingDmlWithStruct <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_structs] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function updateUsingDmlWithStruct() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + const nameStruct = Spanner.struct({ + FirstName: 'Timothy', + LastName: 'Campbell', + }); + + database.runTransaction(async (err, transaction) => { + if (err) { + console.error(err); + return; + } + try { + const [rowCount] = await transaction.runUpdate({ + sql: `UPDATE Singers SET LastName = 'Grant' + WHERE STRUCT<FirstName STRING, LastName STRING>(FirstName, LastName) = @name`, + params: { + name: nameStruct, + }, + }); + + console.log(`Successfully updated ${rowCount} record.`); + await transaction.commit(); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + }); + } + updateUsingDmlWithStruct().catch(console.error); + // [END spanner_dml_structs] +} +main(...process.argv.slice(2)); diff --git a/samples/update-using-dml-with-timestamp.js b/samples/update-using-dml-with-timestamp.js new file mode 100644 index 000000000..2edd02909 --- /dev/null +++ b/samples/update-using-dml-with-timestamp.js @@ -0,0 +1,72 @@ +// Copyright 2021 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: Updates records with timestamp using DML +// usage: node updateUsingDmlWithTimestamp <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_standard_update_with_timestamp] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function updateUsingDmlWithTimestamp() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + database.runTransaction(async (err, transaction) => { + if (err) { + console.error(err); + return; + } + try { + const [rowCount] = await transaction.runUpdate({ + sql: `UPDATE Albums + SET LastUpdateTime = PENDING_COMMIT_TIMESTAMP() + WHERE SingerId = 1`, + }); + + console.log(`Successfully updated ${rowCount} records.`); + await transaction.commit(); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + }); + } + updateUsingDmlWithTimestamp().catch(console.error); + // [END spanner_dml_standard_update_with_timestamp] +} +main(...process.argv.slice(2)); diff --git a/samples/update-using-dml.js b/samples/update-using-dml.js new file mode 100644 index 000000000..63c2f9f6c --- /dev/null +++ b/samples/update-using-dml.js @@ -0,0 +1,71 @@ +// Copyright 2021 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: Updates record using DML. +// usage: node updateUsingDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_standard_update] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function updateUsingDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + database.runTransaction(async (err, transaction) => { + if (err) { + console.error(err); + return; + } + try { + const [rowCount] = await transaction.runUpdate({ + sql: `UPDATE Albums SET MarketingBudget = MarketingBudget * 2 + WHERE SingerId = 1 and AlbumId = 1`, + }); + + console.log(`Successfully updated ${rowCount} record.`); + await transaction.commit(); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + }); + } + updateUsingDml().catch(console.error); + // [END spanner_dml_standard_update] +} +main(...process.argv.slice(2)); diff --git a/samples/update-using-partitioned-dml.js b/samples/update-using-partitioned-dml.js new file mode 100644 index 000000000..e66866d32 --- /dev/null +++ b/samples/update-using-partitioned-dml.js @@ -0,0 +1,62 @@ +// Copyright 2021 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: Updates multiple records using DML. +// usage: node updateUsingPartitionedDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_partitioned_update] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function updateUsingPartitionedDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + try { + const [rowCount] = await database.runPartitionedUpdate({ + sql: 'UPDATE Albums SET MarketingBudget = 100000 WHERE SingerId > 1', + }); + console.log(`Successfully updated ${rowCount} records.`); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + } + updateUsingPartitionedDml().catch(console.error); + // [END spanner_dml_partitioned_update] +} +main(...process.argv.slice(2)); diff --git a/samples/write-and-read-using-dml.js b/samples/write-and-read-using-dml.js new file mode 100644 index 000000000..9a00b232b --- /dev/null +++ b/samples/write-and-read-using-dml.js @@ -0,0 +1,78 @@ +// Copyright 2021 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: Inserts and reads record using DML. +// usage: node writeAndReadUsingDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_write_then_read] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function writeAndReadUsingDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + database.runTransaction(async (err, transaction) => { + if (err) { + console.error(err); + return; + } + try { + await transaction.runUpdate({ + sql: `INSERT Singers (SingerId, FirstName, LastName) + VALUES (11, 'Timothy', 'Campbell')`, + }); + + const [rows] = await transaction.run({ + sql: 'SELECT FirstName, LastName FROM Singers', + }); + rows.forEach(row => { + const json = row.toJSON(); + console.log(`${json.FirstName} ${json.LastName}`); + }); + + await transaction.commit(); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + }); + } + writeAndReadUsingDml().catch(console.error); + // [END spanner_dml_write_then_read] +} +main(...process.argv.slice(2)); diff --git a/samples/write-using-dml.js b/samples/write-using-dml.js new file mode 100644 index 000000000..724a808c2 --- /dev/null +++ b/samples/write-using-dml.js @@ -0,0 +1,73 @@ +// Copyright 2021 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: Inserts multiple records using DML. +// usage: node writeUsingDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_getting_started_insert] + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function writeUsingDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + database.runTransaction(async (err, transaction) => { + if (err) { + console.error(err); + return; + } + try { + const [rowCount] = await transaction.runUpdate({ + sql: `INSERT Singers (SingerId, FirstName, LastName) VALUES + (12, 'Melissa', 'Garcia'), + (13, 'Russell', 'Morales'), + (14, 'Jacqueline', 'Long'), + (15, 'Dylan', 'Shaw')`, + }); + console.log(`${rowCount} records inserted.`); + await transaction.commit(); + } catch (err) { + console.error('ERROR:', err); + } finally { + // Close the database when finished. + database.close(); + } + }); + } + writeUsingDml().catch(console.error); + // [END spanner_dml_getting_started_insert] +} +main(...process.argv.slice(2)); diff --git a/samples/write-with-transaction-using-dml.js b/samples/write-with-transaction-using-dml.js new file mode 100644 index 000000000..7f615c3cf --- /dev/null +++ b/samples/write-with-transaction-using-dml.js @@ -0,0 +1,139 @@ +// Copyright 2021 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: Execute a read-write transaction using DML. +// usage: node writeWithTransactionUsingDml <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> + +'use strict'; + +function main( + instanceId = 'my-instance', + databaseId = 'my-database', + projectId = 'my-project-id' +) { + // [START spanner_dml_getting_started_update]\ + + // This sample transfers 200,000 from the MarketingBudget field + // of the second Album to the first Album, as long as the second + // Album has enough money in its budget. Make sure to run the + // addColumn and updateData samples first (in that order). + + /** + * TODO(developer): Uncomment these variables before running the sample. + */ + // const instanceId = 'my-instance'; + // const databaseId = 'my-database'; + // const projectId = 'my-project-id'; + + // Imports the Google Cloud Spanner client library + const {Spanner} = require('@google-cloud/spanner'); + + // Instantiates a client + const spanner = new Spanner({ + projectId: projectId, + }); + + async function writeWithTransactionUsingDml() { + // Gets a reference to a Cloud Spanner instance and database + const instance = spanner.instance(instanceId); + const database = instance.database(databaseId); + + const transferAmount = 200000; + + database.runTransaction((err, transaction) => { + if (err) { + console.error(err); + return; + } + let firstBudget, secondBudget; + const queryOne = `SELECT MarketingBudget FROM Albums + WHERE SingerId = 2 AND AlbumId = 2`; + + const queryTwo = `SELECT MarketingBudget FROM Albums + WHERE SingerId = 1 AND AlbumId = 1`; + + Promise.all([ + // Reads the second album's budget + transaction.run(queryOne).then(results => { + // Gets second album's budget + const rows = results[0].map(row => row.toJSON()); + secondBudget = rows[0].MarketingBudget; + console.log(`The second album's marketing budget: ${secondBudget}`); + + // Makes sure the second album's budget is large enough + if (secondBudget < transferAmount) { + throw new Error( + `The second album's budget (${secondBudget}) is less than the transfer amount (${transferAmount}).` + ); + } + }), + + // Reads the first album's budget + transaction.run(queryTwo).then(results => { + // Gets first album's budget + const rows = results[0].map(row => row.toJSON()); + firstBudget = rows[0].MarketingBudget; + console.log(`The first album's marketing budget: ${firstBudget}`); + }), + ]) + .then(() => { + // Transfers the budgets between the albums + console.log(firstBudget, secondBudget); + firstBudget += transferAmount; + secondBudget -= transferAmount; + + console.log(firstBudget, secondBudget); + + // Updates the database + // Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they + // must be converted (back) to strings before being inserted as INT64s. + + return transaction + .runUpdate({ + sql: `UPDATE Albums SET MarketingBudget = @Budget + WHERE SingerId = 1 and AlbumId = 1`, + params: { + Budget: firstBudget, + }, + }) + .then(() => + transaction.runUpdate({ + sql: `UPDATE Albums SET MarketingBudget = @Budget + WHERE SingerId = 2 and AlbumId = 2`, + params: { + Budget: secondBudget, + }, + }) + ); + }) + .then(() => { + // Commits the transaction and send the changes to the database + return transaction.commit(); + }) + .then(() => { + console.log( + `Successfully executed read-write transaction using DML to transfer ${transferAmount} from Album 2 to Album 1.` + ); + }) + .then(() => { + // Closes the database when finished + database.close(); + }); + }); + } + writeWithTransactionUsingDml().catch(console.error); + // [END spanner_dml_getting_started_update] +} +main(...process.argv.slice(2)); From cd8089c6c4b03fe37bc3185d9d554a55230be389 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya <lalji.kanjareeya@qlogic.io> Date: Thu, 15 Apr 2021 17:18:55 +0530 Subject: [PATCH 2/4] fix: fix test issue --- samples/system-test/spanner.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index f7a508670..bb55e4b96 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -727,7 +727,7 @@ describe('Spanner', () => { // dml_query_with_parameter it('should use a parameter query to query record that was inserted using a DML statement', async () => { const output = execSync( - `node query-with-parameter ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node query-data-with-parameter ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /SingerId: 12, FirstName: Melissa, LastName: Garcia/); }); From ddf48f411b03607fc497de171cd2ee58072a6647 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya <lalji.kanjareeya@qlogic.io> Date: Thu, 22 Apr 2021 20:12:55 +0530 Subject: [PATCH 3/4] samples: rename the file with prefix and added unhandled promise rejection --- ...dml.js => dml-delete-using-partitioned.js} | 4 +++ .../{delete-using-dml.js => dml-delete.js} | 4 +++ ...with-custom-timeout-and-retry-settings.js} | 4 +++ .../{insert-using-dml.js => dml-insert.js} | 4 +++ ...er.js => dml-query-data-with-parameter.js} | 4 +++ ...batch-dml.js => dml-update-using-batch.js} | 4 +++ ...dml.js => dml-update-using-partitioned.js} | 4 +++ ...th-struct.js => dml-update-with-struct.js} | 4 +++ ...estamp.js => dml-update-with-timestamp.js} | 4 +++ .../{update-using-dml.js => dml-update.js} | 4 +++ ...ead-using-dml.js => dml-write-and-read.js} | 4 +++ ...g-dml.js => dml-write-with-transaction.js} | 4 +++ samples/{write-using-dml.js => dml-write.js} | 4 +++ samples/system-test/spanner.test.js | 26 +++++++++---------- 14 files changed, 65 insertions(+), 13 deletions(-) rename samples/{delete-using-partitioned-dml.js => dml-delete-using-partitioned.js} (95%) rename samples/{delete-using-dml.js => dml-delete.js} (95%) rename samples/{insert-with-custom-timeout-and-retry-settings.js => dml-insert-with-custom-timeout-and-retry-settings.js} (96%) rename samples/{insert-using-dml.js => dml-insert.js} (95%) rename samples/{query-data-with-parameter.js => dml-query-data-with-parameter.js} (95%) rename samples/{update-using-batch-dml.js => dml-update-using-batch.js} (96%) rename samples/{update-using-partitioned-dml.js => dml-update-using-partitioned.js} (95%) rename samples/{update-using-dml-with-struct.js => dml-update-with-struct.js} (96%) rename samples/{update-using-dml-with-timestamp.js => dml-update-with-timestamp.js} (95%) rename samples/{update-using-dml.js => dml-update.js} (95%) rename samples/{write-and-read-using-dml.js => dml-write-and-read.js} (96%) rename samples/{write-with-transaction-using-dml.js => dml-write-with-transaction.js} (97%) rename samples/{write-using-dml.js => dml-write.js} (95%) diff --git a/samples/delete-using-partitioned-dml.js b/samples/dml-delete-using-partitioned.js similarity index 95% rename from samples/delete-using-partitioned-dml.js rename to samples/dml-delete-using-partitioned.js index b946288e2..68b0c9100 100644 --- a/samples/delete-using-partitioned-dml.js +++ b/samples/dml-delete-using-partitioned.js @@ -59,4 +59,8 @@ function main( deleteUsingPartitionedDml().catch(console.error); // [END spanner_dml_partitioned_delete] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/delete-using-dml.js b/samples/dml-delete.js similarity index 95% rename from samples/delete-using-dml.js rename to samples/dml-delete.js index 35f08e0d6..91f446bb2 100644 --- a/samples/delete-using-dml.js +++ b/samples/dml-delete.js @@ -67,4 +67,8 @@ function main( deleteUsingDml().catch(console.error); // [END spanner_dml_standard_delete] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/insert-with-custom-timeout-and-retry-settings.js b/samples/dml-insert-with-custom-timeout-and-retry-settings.js similarity index 96% rename from samples/insert-with-custom-timeout-and-retry-settings.js rename to samples/dml-insert-with-custom-timeout-and-retry-settings.js index 60fbb3b9a..84a8eec7a 100644 --- a/samples/insert-with-custom-timeout-and-retry-settings.js +++ b/samples/dml-insert-with-custom-timeout-and-retry-settings.js @@ -77,4 +77,8 @@ function main( insertWithCustomTimeoutAndRetrySettings().catch(console.error); // [END spanner_set_custom_timeout_and_retry] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/insert-using-dml.js b/samples/dml-insert.js similarity index 95% rename from samples/insert-using-dml.js rename to samples/dml-insert.js index 03fe953d9..b9b84ad85 100644 --- a/samples/insert-using-dml.js +++ b/samples/dml-insert.js @@ -75,4 +75,8 @@ function main( insertUsingDml().catch(console.error); // [END spanner_dml_standard_insert] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/query-data-with-parameter.js b/samples/dml-query-data-with-parameter.js similarity index 95% rename from samples/query-data-with-parameter.js rename to samples/dml-query-data-with-parameter.js index f64c07af9..849fed98b 100644 --- a/samples/query-data-with-parameter.js +++ b/samples/dml-query-data-with-parameter.js @@ -72,4 +72,8 @@ function main( queryDataWithParameter().catch(console.error); // [END spanner_query_with_parameter] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/update-using-batch-dml.js b/samples/dml-update-using-batch.js similarity index 96% rename from samples/update-using-batch-dml.js rename to samples/dml-update-using-batch.js index f19fe3a2c..1ce8da5d5 100644 --- a/samples/update-using-batch-dml.js +++ b/samples/dml-update-using-batch.js @@ -75,4 +75,8 @@ function main( updateUsingBatchDml().catch(console.error); // [END spanner_dml_batch_update] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/update-using-partitioned-dml.js b/samples/dml-update-using-partitioned.js similarity index 95% rename from samples/update-using-partitioned-dml.js rename to samples/dml-update-using-partitioned.js index e66866d32..3ea9f372a 100644 --- a/samples/update-using-partitioned-dml.js +++ b/samples/dml-update-using-partitioned.js @@ -59,4 +59,8 @@ function main( updateUsingPartitionedDml().catch(console.error); // [END spanner_dml_partitioned_update] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/update-using-dml-with-struct.js b/samples/dml-update-with-struct.js similarity index 96% rename from samples/update-using-dml-with-struct.js rename to samples/dml-update-with-struct.js index 5a14a9988..a23da2fda 100644 --- a/samples/update-using-dml-with-struct.js +++ b/samples/dml-update-with-struct.js @@ -76,4 +76,8 @@ function main( updateUsingDmlWithStruct().catch(console.error); // [END spanner_dml_structs] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/update-using-dml-with-timestamp.js b/samples/dml-update-with-timestamp.js similarity index 95% rename from samples/update-using-dml-with-timestamp.js rename to samples/dml-update-with-timestamp.js index 2edd02909..1836564c3 100644 --- a/samples/update-using-dml-with-timestamp.js +++ b/samples/dml-update-with-timestamp.js @@ -69,4 +69,8 @@ function main( updateUsingDmlWithTimestamp().catch(console.error); // [END spanner_dml_standard_update_with_timestamp] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/update-using-dml.js b/samples/dml-update.js similarity index 95% rename from samples/update-using-dml.js rename to samples/dml-update.js index 63c2f9f6c..2c4d0fa0d 100644 --- a/samples/update-using-dml.js +++ b/samples/dml-update.js @@ -68,4 +68,8 @@ function main( updateUsingDml().catch(console.error); // [END spanner_dml_standard_update] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/write-and-read-using-dml.js b/samples/dml-write-and-read.js similarity index 96% rename from samples/write-and-read-using-dml.js rename to samples/dml-write-and-read.js index 9a00b232b..0144c86f6 100644 --- a/samples/write-and-read-using-dml.js +++ b/samples/dml-write-and-read.js @@ -75,4 +75,8 @@ function main( writeAndReadUsingDml().catch(console.error); // [END spanner_dml_write_then_read] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/write-with-transaction-using-dml.js b/samples/dml-write-with-transaction.js similarity index 97% rename from samples/write-with-transaction-using-dml.js rename to samples/dml-write-with-transaction.js index 7f615c3cf..9f7735c47 100644 --- a/samples/write-with-transaction-using-dml.js +++ b/samples/dml-write-with-transaction.js @@ -136,4 +136,8 @@ function main( writeWithTransactionUsingDml().catch(console.error); // [END spanner_dml_getting_started_update] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/write-using-dml.js b/samples/dml-write.js similarity index 95% rename from samples/write-using-dml.js rename to samples/dml-write.js index 724a808c2..08d2f0b83 100644 --- a/samples/write-using-dml.js +++ b/samples/dml-write.js @@ -70,4 +70,8 @@ function main( writeUsingDml().catch(console.error); // [END spanner_dml_getting_started_insert] } +process.on('unhandledRejection', err => { + console.error(err.message); + process.exitCode = 1; +}); main(...process.argv.slice(2)); diff --git a/samples/system-test/spanner.test.js b/samples/system-test/spanner.test.js index bb55e4b96..45f1aab45 100644 --- a/samples/system-test/spanner.test.js +++ b/samples/system-test/spanner.test.js @@ -668,7 +668,7 @@ describe('Spanner', () => { // dml_standard_insert it('should insert rows into an example table using a DML statement', async () => { const output = execSync( - `node insert-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-insert ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match( output, @@ -679,7 +679,7 @@ describe('Spanner', () => { // dml_standard_update it('should update a row in an example table using a DML statement', async () => { const output = execSync( - `node update-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-update ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 1 record/); }); @@ -687,7 +687,7 @@ describe('Spanner', () => { // dml_standard_delete it('should delete a row from an example table using a DML statement', async () => { const output = execSync( - `node delete-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-delete ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully deleted 1 record\./); }); @@ -695,7 +695,7 @@ describe('Spanner', () => { // dml_standard_update_with_timestamp it('should update the timestamp of multiple records in an example table using a DML statement', async () => { const output = execSync( - `node update-using-dml-with-timestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-update-with-timestamp ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 2 records/); }); @@ -703,7 +703,7 @@ describe('Spanner', () => { // dml_write_then_read it('should insert a record in an example table using a DML statement and then query the record', async () => { const output = execSync( - `node write-and-read-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-write-and-read ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Timothy Campbell/); }); @@ -711,7 +711,7 @@ describe('Spanner', () => { // dml_structs it('should update a record in an example table using a DML statement along with a struct value', async () => { const output = execSync( - `node update-using-dml-with-struct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-update-with-struct ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 1 record/); }); @@ -719,7 +719,7 @@ describe('Spanner', () => { // dml_getting_started_insert it('should insert multiple records into an example table using a DML statement', async () => { const output = execSync( - `node write-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-write ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /4 records inserted/); }); @@ -727,7 +727,7 @@ describe('Spanner', () => { // dml_query_with_parameter it('should use a parameter query to query record that was inserted using a DML statement', async () => { const output = execSync( - `node query-data-with-parameter ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-query-data-with-parameter ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /SingerId: 12, FirstName: Melissa, LastName: Garcia/); }); @@ -735,7 +735,7 @@ describe('Spanner', () => { // dml_getting_started_update it('should transfer value from one record to another using DML statements within a transaction', async () => { const output = execSync( - `node write-with-transaction-using-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-write-with-transaction ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match( output, @@ -746,7 +746,7 @@ describe('Spanner', () => { // dml_partitioned_update it('should update multiple records using a partitioned DML statement', async () => { const output = execSync( - `node update-using-partitioned-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-update-using-partitioned ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully updated 3 records/); }); @@ -754,7 +754,7 @@ describe('Spanner', () => { // dml_partitioned_delete it('should delete multiple records using a partitioned DML statement', async () => { const output = execSync( - `node delete-using-partitioned-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-delete-using-partitioned ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /Successfully deleted 5 records/); }); @@ -762,7 +762,7 @@ describe('Spanner', () => { // dml_batch_update it('should insert and update records using Batch DML', async () => { const output = execSync( - `node update-using-batch-dml ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-update-using-batch ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match( output, @@ -1072,7 +1072,7 @@ describe('Spanner', () => { // custom_timeout_and_retry it('should insert with custom timeout and retry settings', async () => { const output = execSync( - `node insert-with-custom-timeout-and-retry-settings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` + `node dml-insert-with-custom-timeout-and-retry-settings ${INSTANCE_ID} ${DATABASE_ID} ${PROJECT_ID}` ); assert.match(output, /record inserted./); }); From 4d7e150da58e0c63416b50338d585eeac8e316e7 Mon Sep 17 00:00:00 2001 From: Lalji Kanjareeya <lalji.kanjareeya@qlogic.io> Date: Thu, 27 May 2021 19:02:22 +0530 Subject: [PATCH 4/4] fix: fix lint --- samples/dml-insert.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/samples/dml-insert.js b/samples/dml-insert.js index b9b84ad85..ff4786359 100644 --- a/samples/dml-insert.js +++ b/samples/dml-insert.js @@ -51,8 +51,7 @@ function main( } try { const [rowCount] = await transaction.runUpdate({ - sql: - 'INSERT Singers (SingerId, FirstName, LastName) VALUES (10, @firstName, @lastName)', + sql: 'INSERT Singers (SingerId, FirstName, LastName) VALUES (10, @firstName, @lastName)', params: { firstName: 'Virginia', lastName: 'Watson',