|
| 1 | +/** |
| 2 | + * Copyright 2017, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +'use strict'; |
| 17 | + |
| 18 | +function updateData (instanceId, databaseId) { |
| 19 | + // [START update_data] |
| 20 | + // Imports the Google Cloud client library |
| 21 | + const Spanner = require('@google-cloud/spanner'); |
| 22 | + |
| 23 | + // Instantiates a client |
| 24 | + const spanner = Spanner(); |
| 25 | + |
| 26 | + // Uncomment these lines to specify the instance and database to use |
| 27 | + // const instanceId = 'my-instance'; |
| 28 | + // const databaseId = 'my-database'; |
| 29 | + |
| 30 | + // Gets a reference to a Cloud Spanner instance and database |
| 31 | + const instance = spanner.instance(instanceId); |
| 32 | + const database = instance.database(databaseId); |
| 33 | + |
| 34 | + // Update a row in the Albums table |
| 35 | + // Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they |
| 36 | + // must be converted to strings before being inserted as INT64s |
| 37 | + const albumsTable = database.table('Albums'); |
| 38 | + |
| 39 | + albumsTable.update([ |
| 40 | + { SingerId: '1', AlbumId: '1', MarketingBudget: '100000' }, |
| 41 | + { SingerId: '2', AlbumId: '2', MarketingBudget: '500000' } |
| 42 | + ]) |
| 43 | + .then(() => { |
| 44 | + console.log('Updated data.'); |
| 45 | + }); |
| 46 | + // [END update_data] |
| 47 | +} |
| 48 | + |
| 49 | +function insertData (instanceId, databaseId) { |
| 50 | + // [START insert_data] |
| 51 | + // Imports the Google Cloud client library |
| 52 | + const Spanner = require('@google-cloud/spanner'); |
| 53 | + |
| 54 | + // Instantiates a client |
| 55 | + const spanner = Spanner(); |
| 56 | + |
| 57 | + // Uncomment these lines to specify the instance and database to use |
| 58 | + // const instanceId = 'my-instance'; |
| 59 | + // const databaseId = 'my-database'; |
| 60 | + |
| 61 | + // Gets a reference to a Spanner instance and database |
| 62 | + const instance = spanner.instance(instanceId); |
| 63 | + const database = instance.database(databaseId); |
| 64 | + |
| 65 | + // Instantiate Spanner table objects |
| 66 | + const singersTable = database.table('Singers'); |
| 67 | + const albumsTable = database.table('Albums'); |
| 68 | + |
| 69 | + Promise.all([ |
| 70 | + // Inserts rows into the Singers table |
| 71 | + // Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so |
| 72 | + // they must be converted to strings before being inserted as INT64s |
| 73 | + singersTable.insert([ |
| 74 | + { SingerId: '1', FirstName: 'Marc', LastName: 'Richards' }, |
| 75 | + { SingerId: '2', FirstName: 'Catalina', LastName: 'Smith' }, |
| 76 | + { SingerId: '3', FirstName: 'Alice', LastName: 'Trentor' }, |
| 77 | + { SingerId: '4', FirstName: 'Lea', LastName: 'Martin' }, |
| 78 | + { SingerId: '5', FirstName: 'David', LastName: 'Lomond' } |
| 79 | + ]), |
| 80 | + |
| 81 | + // Inserts rows into the Albums table |
| 82 | + albumsTable.insert([ |
| 83 | + { SingerId: '1', AlbumId: '1', AlbumTitle: 'Go, Go, Go' }, |
| 84 | + { SingerId: '1', AlbumId: '2', AlbumTitle: 'Total Junk' }, |
| 85 | + { SingerId: '2', AlbumId: '1', AlbumTitle: 'Green' }, |
| 86 | + { SingerId: '2', AlbumId: '2', AlbumTitle: 'Forever Hold your Peace' }, |
| 87 | + { SingerId: '2', AlbumId: '3', AlbumTitle: 'Terrified' } |
| 88 | + ]) |
| 89 | + ]) |
| 90 | + .then(() => { |
| 91 | + console.log('Inserted data.'); |
| 92 | + }); |
| 93 | + // [END insert_data] |
| 94 | +} |
| 95 | + |
| 96 | +function queryData (instanceId, databaseId) { |
| 97 | + // [START query_data] |
| 98 | + // Imports the Google Cloud client library |
| 99 | + const Spanner = require('@google-cloud/spanner'); |
| 100 | + |
| 101 | + // Instantiates a client |
| 102 | + const spanner = Spanner(); |
| 103 | + |
| 104 | + // Uncomment these lines to specify the instance and database to use |
| 105 | + // const instanceId = 'my-instance'; |
| 106 | + // const databaseId = 'my-database'; |
| 107 | + |
| 108 | + // Gets a reference to a Cloud Spanner instance and database |
| 109 | + const instance = spanner.instance(instanceId); |
| 110 | + const database = instance.database(databaseId); |
| 111 | + |
| 112 | + const query = { |
| 113 | + sql: 'SELECT SingerId, AlbumId, AlbumTitle FROM Albums' |
| 114 | + }; |
| 115 | + |
| 116 | + // Queries rows from the Albums table |
| 117 | + database.run(query) |
| 118 | + .then((results) => { |
| 119 | + const rows = results[0]; |
| 120 | + |
| 121 | + rows.forEach((row) => { |
| 122 | + const json = row.toJSON(); |
| 123 | + console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`); |
| 124 | + }); |
| 125 | + }); |
| 126 | + // [END query_data] |
| 127 | +} |
| 128 | + |
| 129 | +function readData (instanceId, databaseId) { |
| 130 | + // [START read_data] |
| 131 | + // Imports the Google Cloud client library |
| 132 | + const Spanner = require('@google-cloud/spanner'); |
| 133 | + |
| 134 | + // Instantiates a client |
| 135 | + const spanner = Spanner(); |
| 136 | + |
| 137 | + // Uncomment these lines to specify the instance and database to use |
| 138 | + // const instanceId = 'my-instance'; |
| 139 | + // const databaseId = 'my-database'; |
| 140 | + |
| 141 | + // Gets a reference to a Cloud Spanner instance and database |
| 142 | + const instance = spanner.instance(instanceId); |
| 143 | + const database = instance.database(databaseId); |
| 144 | + |
| 145 | + // Read rows from the Albums table |
| 146 | + const albumsTable = database.table('Albums'); |
| 147 | + |
| 148 | + const query = { |
| 149 | + columns: ['SingerId', 'AlbumId', 'AlbumTitle'], |
| 150 | + keySet: { |
| 151 | + all: true |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + albumsTable.read(query) |
| 156 | + .then((results) => { |
| 157 | + const rows = results[0]; |
| 158 | + |
| 159 | + rows.forEach((row) => { |
| 160 | + const json = row.toJSON(); |
| 161 | + console.log(`SingerId: ${json.SingerId.value}, AlbumId: ${json.AlbumId.value}, AlbumTitle: ${json.AlbumTitle}`); |
| 162 | + }); |
| 163 | + }); |
| 164 | + // [END read_data] |
| 165 | +} |
| 166 | + |
| 167 | +const cli = require(`yargs`) |
| 168 | + .demand(1) |
| 169 | + .command( |
| 170 | + `update <instanceName> <databaseName>`, |
| 171 | + `Modifies existing rows of data in an example Cloud Spanner table.`, |
| 172 | + {}, |
| 173 | + (opts) => updateData(opts.instanceName, opts.databaseName) |
| 174 | + ) |
| 175 | + .command( |
| 176 | + `query <instanceName> <databaseName>`, |
| 177 | + `Executes a read-only SQL query against an example Cloud Spanner table.`, |
| 178 | + {}, |
| 179 | + (opts) => queryData(opts.instanceName, opts.databaseName) |
| 180 | + ) |
| 181 | + .command( |
| 182 | + `insert <instanceName> <databaseName>`, |
| 183 | + `Inserts new rows of data into an example Cloud Spanner table.`, |
| 184 | + {}, |
| 185 | + (opts) => insertData(opts.instanceName, opts.databaseName) |
| 186 | + ) |
| 187 | + .command( |
| 188 | + `read <instanceName> <databaseName>`, |
| 189 | + `Reads data in an example Cloud Spanner table.`, |
| 190 | + {}, |
| 191 | + (opts) => readData(opts.instanceName, opts.databaseName) |
| 192 | + ) |
| 193 | + .example(`node $0 update "my-instance" "my-database"`) |
| 194 | + .example(`node $0 query "my-instance" "my-database"`) |
| 195 | + .example(`node $0 insert "my-instance" "my-database"`) |
| 196 | + .example(`node $0 read "my-instance" "my-database"`) |
| 197 | + .wrap(120) |
| 198 | + .recommendCommands() |
| 199 | + .epilogue(`For more information, see https://cloud.google.com/spanner/docs`); |
| 200 | + |
| 201 | +if (module === require.main) { |
| 202 | + cli.help().strict().argv; |
| 203 | +} |
0 commit comments