| 15 | 'use strict'; |
| 16 | |
| 17 | async function createTableWithTimestamp(instanceId, databaseId, projectId) { |
| 18 | // [START spanner_create_table_with_timestamp_column] |
| 19 | // Imports the Google Cloud client library |
| 20 | const {Spanner} = require('@google-cloud/spanner'); |
| 21 | |
| 22 | /** |
| 23 | * TODO(developer): Uncomment the following lines before running the sample. |
| 24 | */ |
| 25 | // const projectId = 'my-project-id'; |
| 26 | // const instanceId = 'my-instance'; |
| 27 | // const databaseId = 'my-database'; |
| 28 | |
| 29 | // Creates a client |
| 30 | const spanner = new Spanner({ |
| 31 | projectId: projectId, |
| 32 | }); |
| 33 | |
| 34 | // Gets a reference to a Cloud Spanner Database Admin Client object |
| 35 | const databaseAdminClient = spanner.getDatabaseAdminClient(); |
| 36 | |
| 37 | // Note: Cloud Spanner interprets Node.js numbers as FLOAT64s, so they |
| 38 | // must be converted to strings before being inserted as INT64s |
| 39 | const request = [ |
| 40 | `CREATE TABLE Performances ( |
| 41 | SingerId INT64 NOT NULL, |
| 42 | VenueId INT64 NOT NULL, |
| 43 | EventDate DATE, |
| 44 | Revenue INT64, |
| 45 | LastUpdateTime TIMESTAMP NOT NULL OPTIONS (allow_commit_timestamp=true) |
| 46 | ) PRIMARY KEY (SingerId, VenueId, EventDate), |
| 47 | INTERLEAVE IN PARENT Singers ON DELETE CASCADE`, |
| 48 | ]; |
| 49 | try { |
| 50 | // Creates a table in an existing database |
| 51 | const [operation] = await databaseAdminClient.updateDatabaseDdl({ |
| 52 | database: databaseAdminClient.databasePath( |
| 53 | projectId, |
| 54 | instanceId, |
| 55 | databaseId |
| 56 | ), |
| 57 | statements: request, |
| 58 | }); |
| 59 | |
| 60 | console.log(`Waiting for operation on ${databaseId} to complete...`); |
| 61 | |
| 62 | await operation.promise(); |
| 63 | |
| 64 | console.log(`Created table Performances in database ${databaseId}.`); |
| 65 | } catch (err) { |
| 66 | console.error('ERROR:', err); |
| 67 | } |
| 68 | // [END spanner_create_table_with_timestamp_column] |
| 69 | } |
| 70 | |
| 71 | async function insertWithTimestamp(instanceId, databaseId, projectId) { |
| 72 | // [START spanner_insert_data_with_timestamp_column] |