Skip to content

Commit f050354

Browse files
feat: adding support and samples for Jsonb data type in spangres (#1729)
* changes for json b * splitting samples * changes as per review * linting * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * fixing tests * fixing tests * Update pg-jsonb-query-parameter.js Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent eaa445e commit f050354

File tree

11 files changed

+497
-1
lines changed

11 files changed

+497
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
144144
| Calls a server side function on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-functions.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-functions.js,samples/README.md) |
145145
| Creates a new storing index in a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-index-create-storing.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-index-create-storing.js,samples/README.md) |
146146
| Created interleaved table hierarchy using PostgreSQL dialect. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-interleaving.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-interleaving.js,samples/README.md) |
147+
| Showcase how add a jsonb column in a PostgreSQL table. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-jsonb-add-column.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-jsonb-add-column.js,samples/README.md) |
148+
| Showcase how query data to a jsonb column in a PostgreSQL table. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-jsonb-query-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-jsonb-query-parameter.js,samples/README.md) |
149+
| Showcase how update data to a jsonb column in a PostgreSQL table. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-jsonb-update-data.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-jsonb-update-data.js,samples/README.md) |
147150
| Showcase how to work with the PostgreSQL NUMERIC/DECIMAL data type on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-numeric-data-type.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-numeric-data-type.js,samples/README.md) |
148151
| Showcases how a Spanner PostgreSQL database orders null values in a query. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-ordering-nulls.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-ordering-nulls.js,samples/README.md) |
149152
| Execute a query with parameters on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-query-parameter.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-query-parameter.js,samples/README.md) |

samples/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ and automatic, synchronous replication for high availability.
6969
* [Calls a server side function on a Spanner PostgreSQL database.](#calls-a-server-side-function-on-a-spanner-postgresql-database.)
7070
* [Creates a new storing index in a Spanner PostgreSQL database.](#creates-a-new-storing-index-in-a-spanner-postgresql-database.)
7171
* [Created interleaved table hierarchy using PostgreSQL dialect.](#created-interleaved-table-hierarchy-using-postgresql-dialect.)
72+
* [Showcase how add a jsonb column in a PostgreSQL table.](#showcase-how-add-a-jsonb-column-in-a-postgresql-table.)
73+
* [Showcase how query data to a jsonb column in a PostgreSQL table.](#showcase-how-query-data-to-a-jsonb-column-in-a-postgresql-table.)
74+
* [Showcase how update data to a jsonb column in a PostgreSQL table.](#showcase-how-update-data-to-a-jsonb-column-in-a-postgresql-table.)
7275
* [Showcase how to work with the PostgreSQL NUMERIC/DECIMAL data type on a Spanner PostgreSQL database.](#showcase-how-to-work-with-the-postgresql-numeric/decimal-data-type-on-a-spanner-postgresql-database.)
7376
* [Showcases how a Spanner PostgreSQL database orders null values in a query.](#showcases-how-a-spanner-postgresql-database-orders-null-values-in-a-query.)
7477
* [Execute a query with parameters on a Spanner PostgreSQL database.](#execute-a-query-with-parameters-on-a-spanner-postgresql-database.)
@@ -1038,6 +1041,57 @@ __Usage:__
10381041

10391042

10401043

1044+
### Showcase how add a jsonb column in a PostgreSQL table.
1045+
1046+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-jsonb-add-column.js).
1047+
1048+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-jsonb-add-column.js,samples/README.md)
1049+
1050+
__Usage:__
1051+
1052+
1053+
`node pg-jsonb-add-column.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`
1054+
1055+
1056+
-----
1057+
1058+
1059+
1060+
1061+
### Showcase how query data to a jsonb column in a PostgreSQL table.
1062+
1063+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-jsonb-query-parameter.js).
1064+
1065+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-jsonb-query-parameter.js,samples/README.md)
1066+
1067+
__Usage:__
1068+
1069+
1070+
`node pg-jsonb-query-parameter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`
1071+
1072+
1073+
-----
1074+
1075+
1076+
1077+
1078+
### Showcase how update data to a jsonb column in a PostgreSQL table.
1079+
1080+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-jsonb-update-data.js).
1081+
1082+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/pg-jsonb-update-data.js,samples/README.md)
1083+
1084+
__Usage:__
1085+
1086+
1087+
`node pg-jsonb-update-data.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>`
1088+
1089+
1090+
-----
1091+
1092+
1093+
1094+
10411095
### Showcase how to work with the PostgreSQL NUMERIC/DECIMAL data type on a Spanner PostgreSQL database.
10421096

10431097
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-numeric-data-type.js).

samples/pg-jsonb-add-column.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2022 Google LLC
2+
//
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+
// sample-metadata:
16+
// title: Showcase how add a jsonb column in a PostgreSQL table.
17+
// usage: node pg-jsonb-add-column.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
18+
19+
'use strict';
20+
21+
function main(
22+
instanceId = 'my-instance',
23+
databaseId = 'my-database',
24+
projectId = 'my-project-id'
25+
) {
26+
// [START spanner_postgresql_jsonb_add_column]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const instanceId = 'my-instance';
31+
// const databaseId = 'my-database';
32+
// const projectId = 'my-project-id';
33+
34+
// Imports the Google Cloud Spanner client library
35+
const {Spanner} = require('@google-cloud/spanner');
36+
37+
// Instantiates a client
38+
const spanner = new Spanner({
39+
projectId: projectId,
40+
});
41+
42+
async function pgJsonbAddColumn() {
43+
// Gets a reference to a Cloud Spanner instance and database.
44+
const instance = spanner.instance(instanceId);
45+
const database = instance.database(databaseId);
46+
47+
const request = ['ALTER TABLE Venues ADD COLUMN VenueDetails JSONB'];
48+
49+
// Updates schema by adding a new table.
50+
const [operation] = await database.updateSchema(request);
51+
console.log(`Waiting for operation on ${databaseId} to complete...`);
52+
await operation.promise();
53+
console.log(
54+
`Added jsonb column to table venues to database ${databaseId}.`
55+
);
56+
}
57+
pgJsonbAddColumn();
58+
// [END spanner_postgresql_jsonb_add_column]
59+
}
60+
61+
process.on('unhandledRejection', err => {
62+
console.error(err.message);
63+
process.exitCode = 1;
64+
});
65+
main(...process.argv.slice(2));

samples/pg-jsonb-query-parameter.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2022 Google LLC
2+
//
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+
// sample-metadata:
16+
// title: Showcase how query data to a jsonb column in a PostgreSQL table.
17+
// usage: node pg-jsonb-query-parameter.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
18+
19+
'use strict';
20+
21+
function main(
22+
instanceId = 'my-instance',
23+
databaseId = 'my-database',
24+
projectId = 'my-project-id'
25+
) {
26+
// [START spanner_postgresql_jsonb_update_data]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const instanceId = 'my-instance';
31+
// const databaseId = 'my-database';
32+
// const projectId = 'my-project-id';
33+
34+
// Imports the Google Cloud Spanner client library
35+
const {Spanner} = require('@google-cloud/spanner');
36+
37+
// Instantiates a client
38+
const spanner = new Spanner({
39+
projectId: projectId,
40+
});
41+
42+
async function pgJsonbDataType() {
43+
// Gets a reference to a Cloud Spanner instance and database.
44+
const instance = spanner.instance(instanceId);
45+
const database = instance.database(databaseId);
46+
47+
const select_query = {
48+
sql: `SELECT venueid, venuedetails FROM Venues
49+
WHERE CAST(venuedetails ->> 'rating' AS INTEGER) > $1`,
50+
params: {
51+
p1: 2,
52+
},
53+
types: {
54+
p1: 'int64',
55+
},
56+
json: true,
57+
};
58+
59+
// Queries row from the Venues table.
60+
try {
61+
const [rows] = await database.run(select_query);
62+
63+
rows.forEach(row => {
64+
console.log(
65+
`VenueId: ${row.venueid}, Details: ${JSON.stringify(
66+
row.venuedetails
67+
)}`
68+
);
69+
});
70+
} finally {
71+
// Close the database when finished.
72+
await database.close();
73+
}
74+
}
75+
pgJsonbDataType();
76+
// [END spanner_postgresql_jsonb_update_data]
77+
}
78+
79+
process.on('unhandledRejection', err => {
80+
console.error(err.message);
81+
process.exitCode = 1;
82+
});
83+
main(...process.argv.slice(2));

samples/pg-jsonb-update-data.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright 2022 Google LLC
2+
//
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+
// sample-metadata:
16+
// title: Showcase how update data to a jsonb column in a PostgreSQL table.
17+
// usage: node pg-jsonb-update-data.js <INSTANCE_ID> <DATABASE_ID> <PROJECT_ID>
18+
19+
'use strict';
20+
21+
function main(
22+
instanceId = 'my-instance',
23+
databaseId = 'my-database',
24+
projectId = 'my-project-id'
25+
) {
26+
// [START spanner_postgresql_jsonb_update_data]
27+
/**
28+
* TODO(developer): Uncomment these variables before running the sample.
29+
*/
30+
// const instanceId = 'my-instance';
31+
// const databaseId = 'my-database';
32+
// const projectId = 'my-project-id';
33+
34+
// Imports the Google Cloud Spanner client library
35+
const {Spanner} = require('@google-cloud/spanner');
36+
37+
// Instantiates a client
38+
const spanner = new Spanner({
39+
projectId: projectId,
40+
});
41+
42+
async function pgJsonbUpdateData() {
43+
// Gets a reference to a Cloud Spanner instance and database.
44+
const instance = spanner.instance(instanceId);
45+
const database = instance.database(databaseId);
46+
// Instantiate Spanner table objects.
47+
const venuesTable = database.table('venues');
48+
49+
const data = [
50+
{
51+
VenueId: '19',
52+
VenueDetails: {rating: 9, open: true},
53+
},
54+
{
55+
VenueId: '4',
56+
// PG JSONB sorts first by key length and then lexicographically with equivalent key length
57+
// and takes the last value in the case of duplicate keys
58+
VenueDetails: `[
59+
{
60+
"name": null,
61+
"available": true
62+
},
63+
{
64+
"name": "room 2",
65+
"available": false,
66+
"name": "room 3"
67+
},
68+
{
69+
"main hall": {
70+
"description": "this is the biggest space",
71+
"size": 200
72+
}
73+
}
74+
]`,
75+
},
76+
{
77+
VenueId: '42',
78+
VenueDetails: {
79+
name: null,
80+
open: {
81+
Monday: true,
82+
Tuesday: false,
83+
},
84+
tags: ['large', 'airy'],
85+
},
86+
},
87+
];
88+
89+
try {
90+
await venuesTable.update(data);
91+
console.log('Updated data.');
92+
} catch (err) {
93+
console.error('ERROR:', err);
94+
} finally {
95+
// Close the database when finished.
96+
await database.close();
97+
}
98+
}
99+
pgJsonbUpdateData();
100+
// [END spanner_postgresql_jsonb_update_data]
101+
}
102+
103+
process.on('unhandledRejection', err => {
104+
console.error(err.message);
105+
process.exitCode = 1;
106+
});
107+
main(...process.argv.slice(2));

samples/system-test/spanner.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,42 @@ describe('Spanner', () => {
16271627
assert.match(output, new RegExp('VenueId: 398, Revenue: 0.000000123'));
16281628
});
16291629

1630+
// pg_jsonb_add_column
1631+
it('should add a jsonb column to a table', async () => {
1632+
const output = execSync(
1633+
`node pg-jsonb-add-column.js ${SAMPLE_INSTANCE_ID} ${PG_DATABASE_ID} ${PROJECT_ID}`
1634+
);
1635+
assert.match(
1636+
output,
1637+
new RegExp(`Waiting for operation on ${PG_DATABASE_ID} to complete...`)
1638+
);
1639+
assert.match(
1640+
output,
1641+
new RegExp(
1642+
`Added jsonb column to table venues to database ${PG_DATABASE_ID}.`
1643+
)
1644+
);
1645+
});
1646+
1647+
// pg_jsonb_insert_data
1648+
it('should insert pg jsonb data', async () => {
1649+
const output = execSync(
1650+
`node pg-jsonb-update-data.js ${SAMPLE_INSTANCE_ID} ${PG_DATABASE_ID} ${PROJECT_ID}`
1651+
);
1652+
assert.match(output, new RegExp('Updated data.'));
1653+
});
1654+
1655+
// pg_jsonb_query_data
1656+
it('should query pg jsonb data', async () => {
1657+
const output = execSync(
1658+
`node pg-jsonb-query-parameter.js ${SAMPLE_INSTANCE_ID} ${PG_DATABASE_ID} ${PROJECT_ID}`
1659+
);
1660+
assert.match(
1661+
output,
1662+
new RegExp('VenueId: 19, Details: {"value":{"open":true,"rating":9}}')
1663+
);
1664+
});
1665+
16301666
// pg_case_sensitivity
16311667
it('should create case sensitive table and query the information in a Spanner PostgreSQL database', async () => {
16321668
const output = execSync(

0 commit comments

Comments
 (0)