-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for Proto columns (#1991)
* feat: Support For Proto Column in Spanner (#1829) * feat: Generated Proto Changes Changes need to be reverted. * feat: Implementation for Proto Message & Enum -Adding Logic for Serialization & Deserialization -New Type Codes and utilities * feat: Proto static files and typings and generated descriptors * sample: Adding DML, DQL, DML, table insert & read samples. * style: Lint * test: Adding unit tests * refactor: minor refactoring * refactor: minor refactoring * test: Adding integration tests * docs: Adding docs * test: Adding sample Integration Tests * refactor: Minor refactoring and updating comments/docs. * test: Making test fixes * refactor: Minor refactoring and lint fixes * refactor: Minor refactoring and lint fixes * Updating docs and minor changes. * test: fixing test * refactor: minor changes * refactor: minor refactoring * docs: Updating docs & comments * feat(spanner): fix lint * fix(spanner: lint * fix(spanner): lint * fix(spanner): lint * fix(spanner): lint * feat(spanner): fix db name * feat(spanner): remove it.only * feat(spanner): fix tests lint * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat(spanner): remove samples and sample tests * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat(spanner): update schema to avoid reserved keyword * feat(spanner): add samples and sample tests * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * feat(spanner): code refactoring --------- Co-authored-by: Gaurav Purohit <[email protected]> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
- Loading branch information
1 parent
81fa610
commit ae59c7f
Showing
25 changed files
with
2,724 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// 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. | ||
// 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'; | ||
|
||
// eslint-disable-next-line node/no-unpublished-require | ||
const singer = require('./resource/singer.js'); | ||
const music = singer.examples.spanner.music; | ||
|
||
function main( | ||
instanceId = 'my-instance', | ||
databaseId = 'my-database', | ||
projectId = 'my-project-id' | ||
) { | ||
// [START spanner_query_with_proto_types_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 queryDataWithProtoTypes() { | ||
// 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, | ||
SingerInfo, | ||
SingerInfo.nationality, | ||
SingerInfoArray, | ||
SingerGenre, | ||
SingerGenreArray | ||
FROM Singers | ||
WHERE SingerInfo.nationality = @country | ||
and SingerGenre=@singerGenre`, | ||
params: { | ||
country: 'Country2', | ||
singerGenre: music.Genre.FOLK, | ||
}, | ||
/* `columnsMetadata` is an optional parameter and is used to deserialize the | ||
proto message and enum object back from bytearray and int respectively. | ||
If columnsMetadata is not passed for proto messages and enums, then the data | ||
types for these columns will be bytes and int respectively. */ | ||
columnsMetadata: { | ||
SingerInfo: music.SingerInfo, | ||
SingerInfoArray: music.SingerInfo, | ||
SingerGenre: music.Genre, | ||
SingerGenreArray: music.Genre, | ||
}, | ||
}; | ||
|
||
// Queries rows from the Singers table. | ||
try { | ||
const [rows] = await database.run(query); | ||
|
||
rows.forEach(row => { | ||
const json = row.toJSON(); | ||
console.log( | ||
`SingerId: ${json.SingerId}, SingerInfo: ${json.SingerInfo}, SingerGenre: ${json.SingerGenre}, | ||
SingerInfoArray: ${json.SingerInfoArray}, SingerGenreArray: ${json.SingerGenreArray}` | ||
); | ||
}); | ||
} catch (err) { | ||
console.error('ERROR:', err); | ||
} finally { | ||
// Close the database when finished. | ||
database.close(); | ||
} | ||
} | ||
|
||
queryDataWithProtoTypes(); | ||
// [END spanner_query_with_proto_types_parameter] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
main(...process.argv.slice(2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* 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. | ||
* 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: Creates a new database with a proto column and enum | ||
// usage: node proto-type-add-column.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID> | ||
|
||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
|
||
function main( | ||
instanceId = 'my-instance', | ||
databaseId = 'my-database', | ||
projectId = 'my-project-id' | ||
) { | ||
// [START spanner_add_proto_type_columns] | ||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const projectId = 'my-project-id'; | ||
// const instanceId = 'my-instance-id'; | ||
// const databaseId = 'my-database-id'; | ||
|
||
// Imports the Google Cloud client library | ||
const {Spanner} = require('@google-cloud/spanner'); | ||
|
||
// Creates a client | ||
const spanner = new Spanner({ | ||
projectId: projectId, | ||
}); | ||
|
||
const databaseAdminClient = spanner.getDatabaseAdminClient(); | ||
async function protoTypeAddColumn() { | ||
// Adds a new Proto Message column and Proto Enum column to the Singers table. | ||
|
||
const request = [ | ||
`CREATE PROTO BUNDLE ( | ||
examples.spanner.music.SingerInfo, | ||
examples.spanner.music.Genre, | ||
)`, | ||
'ALTER TABLE Singers ADD COLUMN SingerInfo examples.spanner.music.SingerInfo', | ||
'ALTER TABLE Singers ADD COLUMN SingerInfoArray ARRAY<examples.spanner.music.SingerInfo>', | ||
'ALTER TABLE Singers ADD COLUMN SingerGenre examples.spanner.music.Genre', | ||
'ALTER TABLE Singers ADD COLUMN SingerGenreArray ARRAY<examples.spanner.music.Genre>', | ||
]; | ||
|
||
// Read a proto descriptor file and convert it to a base64 string | ||
const protoDescriptor = fs | ||
.readFileSync('./resource/descriptors.pb') | ||
.toString('base64'); | ||
|
||
// Alter existing table to add a column. | ||
const [operation] = await databaseAdminClient.updateDatabaseDdl({ | ||
database: databaseAdminClient.databasePath( | ||
projectId, | ||
instanceId, | ||
databaseId | ||
), | ||
statements: request, | ||
protoDescriptors: protoDescriptor, | ||
}); | ||
|
||
console.log(`Waiting for operation on ${databaseId} to complete...`); | ||
await operation.promise(); | ||
console.log( | ||
`Altered table "Singers" on database ${databaseId} on instance ${instanceId} with proto descriptors.` | ||
); | ||
} | ||
protoTypeAddColumn(); | ||
// [END spanner_add_proto_type_columns] | ||
} | ||
|
||
process.on('unhandledRejection', err => { | ||
console.error(err.message); | ||
process.exitCode = 1; | ||
}); | ||
main(...process.argv.slice(2)); |
Oops, something went wrong.