Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function updateUsingDmlWithStruct(instanceId, databaseId, projectId) {
// [START spanner_dml_structs]
// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');
const nameStruct = Spanner.struct({
FirstName: 'Timothy',
LastName: 'Campbell',
});
/**
* 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,
});
* 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,
});
// 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: 'Elena',
LastName: 'Campbell',
});
const query = {
sql: 'SELECT SingerId FROM Singers WHERE FirstName = @name.FirstName',
params: {
name: nameStruct,
},
};
// Queries rows from the Singers table
try {
const [rows] = await database.run(query);
rows.forEach(row => {
const json = row.toJSON();
async function queryDataWithStruct(instanceId, databaseId, projectId) {
// [START spanner_create_struct_with_data]
// Imports the Google Cloud client library
const {Spanner} = require('@google-cloud/spanner');
const nameStruct = Spanner.struct({
FirstName: 'Elena',
LastName: 'Campbell',
});
// [END spanner_create_struct_with_data]
// [START spanner_query_data_with_struct]
/**
* 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,
{
name: 'LastName',
type: 'string',
},
],
};
// [END spanner_create_user_defined_struct]
// [START spanner_create_array_of_struct_with_data]
const bandMembersType = {
type: 'array',
child: nameType,
};
const bandMembers = [
Spanner.struct({
FirstName: 'Elena',
LastName: 'Campbell',
}),
Spanner.struct({
FirstName: 'Gabriel',
LastName: 'Wright',
}),
Spanner.struct({
FirstName: 'Benjamin',
LastName: 'Martinez',
}),
];
// [END spanner_create_array_of_struct_with_data]
// [START spanner_query_data_with_array_of_struct]
const query = {
const songInfoType = {
type: 'struct',
fields: [
{
name: 'SongName',
type: 'string',
},
{
name: 'ArtistNames',
type: 'array',
child: nameType,
},
],
};
const songInfoStruct = Spanner.struct({
SongName: 'Imagination',
ArtistNames: [
Spanner.struct({FirstName: 'Elena', LastName: 'Campbell'}),
Spanner.struct({FirstName: 'Hannah', LastName: 'Harris'}),
],
});
const query = {
sql:
'SELECT SingerId, @songInfo.SongName FROM Singers ' +
'WHERE STRUCT(FirstName, LastName) ' +
'IN UNNEST(@songInfo.ArtistNames)',
params: {
songInfo: songInfoStruct,
},
types: {
name: 'SongName',
type: 'string',
},
{
name: 'ArtistNames',
type: 'array',
child: nameType,
},
],
};
const songInfoStruct = Spanner.struct({
SongName: 'Imagination',
ArtistNames: [
Spanner.struct({FirstName: 'Elena', LastName: 'Campbell'}),
Spanner.struct({FirstName: 'Hannah', LastName: 'Harris'}),
],
});
const query = {
sql:
'SELECT SingerId, @songInfo.SongName FROM Singers ' +
'WHERE STRUCT(FirstName, LastName) ' +
'IN UNNEST(@songInfo.ArtistNames)',
params: {
songInfo: songInfoStruct,
},
types: {
songInfo: songInfoType,
},
};
{
name: 'SongName',
type: 'string',
},
{
name: 'ArtistNames',
type: 'array',
child: nameType,
},
],
};
const songInfoStruct = Spanner.struct({
SongName: 'Imagination',
ArtistNames: [
Spanner.struct({FirstName: 'Elena', LastName: 'Campbell'}),
Spanner.struct({FirstName: 'Hannah', LastName: 'Harris'}),
],
});
const query = {
sql:
'SELECT SingerId, @songInfo.SongName FROM Singers ' +
'WHERE STRUCT(FirstName, LastName) ' +
'IN UNNEST(@songInfo.ArtistNames)',
params: {
songInfo: songInfoStruct,
},
types: {
songInfo: songInfoType,
},
};