Skip to content

Commit

Permalink
Merge branch 'main' into string_array_untyped
Browse files Browse the repository at this point in the history
  • Loading branch information
surbhigarg92 authored Mar 4, 2024
2 parents 7856cde + f12e295 commit 8da320c
Show file tree
Hide file tree
Showing 109 changed files with 8,640 additions and 2,495 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"concat-stream": "^2.0.0",
"dedent": "^1.0.0",
"execa": "^5.0.0",
"gapic-tools": "^0.3.0",
"gapic-tools": "^0.4.0",
"gts": "^5.0.0",
"jsdoc": "^4.0.0",
"jsdoc-fresh": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/sa
__Usage:__


`node get-instance-config.js <PROJECT_ID>`
`node get-instance-config.js <PROJECT_ID> <INSTANCE_CONFIG_ID>`


-----
Expand Down
36 changes: 25 additions & 11 deletions samples/add-and-drop-new-database-role.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 Google LLC
// 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.
Expand Down Expand Up @@ -30,19 +30,18 @@ function main(
// const instanceId = 'my-instance';
// const databaseId = 'my-database';
// const projectId = 'my-project-id';
// Imports the Google Cloud Spanner client library

// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');

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

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

async function addAndDropNewDatabaseRole() {
// Creates a new user defined role and grant permissions
try {
const request = [
Expand All @@ -51,7 +50,14 @@ function main(
'CREATE ROLE child',
'GRANT ROLE parent TO ROLE child',
];
const [operation] = await database.updateSchema(request);
const [operation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: request,
});

console.log('Waiting for operation to complete...');
await operation.promise();
Expand All @@ -65,7 +71,14 @@ function main(
// A role can't be dropped until all its permissions are revoked.
try {
const request = ['REVOKE ROLE parent FROM ROLE child', 'DROP ROLE child'];
const [operation] = await database.updateSchema(request);
const [operation] = await databaseAdminClient.updateDatabaseDdl({
database: databaseAdminClient.databasePath(
projectId,
instanceId,
databaseId
),
statements: request,
});

console.log('Waiting for operation to complete...');
await operation.promise();
Expand All @@ -74,8 +87,9 @@ function main(
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
// Close the spanner client when finished.
// The databaseAdminClient does not require explicit closure. The closure of the Spanner client will automatically close the databaseAdminClient.
spanner.close();
}
}
addAndDropNewDatabaseRole();
Expand Down
89 changes: 89 additions & 0 deletions samples/archived/add-and-drop-new-database-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2022 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: Add and drop new database role
// usage: node add-and-drop-new-database-role.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>

'use strict';

function main(
instanceId = 'my-instance',
databaseId = 'my-database',
projectId = 'my-project-id'
) {
// [START spanner_add_and_drop_database_role]
/**
* 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 addAndDropNewDatabaseRole() {
// Gets a reference to a Cloud Spanner instance and database.
const instance = spanner.instance(instanceId);
const database = instance.database(databaseId);

// Creates a new user defined role and grant permissions
try {
const request = [
'CREATE ROLE parent',
'GRANT SELECT ON TABLE Singers TO ROLE parent',
'CREATE ROLE child',
'GRANT ROLE parent TO ROLE child',
];
const [operation] = await database.updateSchema(request);

console.log('Waiting for operation to complete...');
await operation.promise();

console.log('Created roles child and parent and granted privileges');
} catch (err) {
console.error('ERROR:', err);
}

// Revoke permissions and drop child role.
// A role can't be dropped until all its permissions are revoked.
try {
const request = ['REVOKE ROLE parent FROM ROLE child', 'DROP ROLE child'];
const [operation] = await database.updateSchema(request);

console.log('Waiting for operation to complete...');
await operation.promise();

console.log('Revoked privileges and dropped role child');
} catch (err) {
console.error('ERROR:', err);
} finally {
// Close the database when finished.
await database.close();
}
}
addAndDropNewDatabaseRole();
// [END spanner_add_and_drop_database_role]
}

process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
69 changes: 69 additions & 0 deletions samples/archived/backups-cancel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Copyright 2020 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.
*/

'use strict';

async function cancelBackup(instanceId, databaseId, backupId, projectId) {
// [START spanner_cancel_backup_create]
// Imports the Google Cloud client library and precise date 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';
// const backupId = 'my-backup';

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

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

const backup = instance.backup(backupId);

// Creates a new backup of the database
try {
console.log(`Creating backup of database ${database.formattedName_}.`);
const databasePath = database.formattedName_;
// Expire backup one day in the future
const expireTime = Date.now() + 1000 * 60 * 60 * 24;
const [, operation] = await backup.create({
databasePath: databasePath,
expireTime: expireTime,
});

// Cancel the backup
await operation.cancel();

console.log('Backup cancelled.');
} catch (err) {
console.error('ERROR:', err);
} finally {
// Delete backup in case it got created before the cancel operation
await backup.delete();

// Close the database when finished.
await database.close();
}
// [END spanner_cancel_backup_create]
}

module.exports.cancelBackup = cancelBackup;
97 changes: 97 additions & 0 deletions samples/archived/backups-copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright 2022 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: Copies a source backup
// usage: node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>

'use strict';

function main(
instanceId = 'my-instance',
backupId = 'my-backup',
sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
projectId = 'my-project-id'
) {
// [START spanner_copy_backup]
/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const instanceId = 'my-instance';
// const backupId = 'my-backup',
// const sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
// const projectId = 'my-project-id';

// Imports the Google Cloud Spanner client library
const {Spanner} = require('@google-cloud/spanner');
const {PreciseDate} = require('@google-cloud/precise-date');

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

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

// Expire copy backup 14 days in the future
const expireTime = Spanner.timestamp(
Date.now() + 1000 * 60 * 60 * 24 * 14
).toStruct();

// Copy the source backup
try {
console.log(`Creating copy of the source backup ${sourceBackupPath}.`);
const [, operation] = await instance.copyBackup(
sourceBackupPath,
backupId,
{
expireTime: expireTime,
}
);

console.log(
`Waiting for backup copy ${
instance.backup(backupId).formattedName_
} to complete...`
);
await operation.promise();

// Verify the copy backup is ready
const copyBackup = instance.backup(backupId);
const [copyBackupInfo] = await copyBackup.getMetadata();
if (copyBackupInfo.state === 'READY') {
console.log(
`Backup copy ${copyBackupInfo.name} of size ` +
`${copyBackupInfo.sizeBytes} bytes was created at ` +
`${new PreciseDate(copyBackupInfo.createTime).toISOString()} ` +
'with version time ' +
`${new PreciseDate(copyBackupInfo.versionTime).toISOString()}`
);
} else {
console.error('ERROR: Copy of backup is not ready.');
}
} catch (err) {
console.error('ERROR:', err);
}
}
spannerCopyBackup();
// [END spanner_copy_backup]
}
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
main(...process.argv.slice(2));
Loading

0 comments on commit 8da320c

Please sign in to comment.