GeezerVison is showing github.com — feat: Postgres Numeric and database support (#1592) · googleapis/google-cloud-node@70d5e5e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/README.md +12Lines changed: 12 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -129,6 +129,18 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre | 129 | 129 | | Numeric-add-column | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-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/numeric-add-column.js,samples/README.md) | | 130 | 130 | | Numeric-query-parameter | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-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/numeric-query-parameter.js,samples/README.md) | | 131 | 131 | | Numeric-update-data | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/numeric-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/numeric-update-data.js,samples/README.md) | | | 132 | +| Showcase the rules for case-sensitivity and case folding for a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-case-sensitivity.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-case-sensitivity.js,samples/README.md) | | | 133 | +| Creates a PostgreSQL Database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-database-create.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-database-create.js,samples/README.md) | | | 134 | +| Use cast operator to cast from one data type to another in a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-datatypes-casting.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-datatypes-casting.js,samples/README.md) | | | 135 | +| Execute a batch of DML statements on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-dml-batch.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-dml-batch.js,samples/README.md) | | | 136 | +| Execute a Partitioned DML on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-dml-partitioned.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-dml-partitioned.js,samples/README.md) | | | 137 | +| Execute a DML statement with parameters on a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-dml-with-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-dml-with-parameter.js,samples/README.md) | | | 138 | +| 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) | | | 139 | +| 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) | | | 140 | +| 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) | | | 141 | +| 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) | | | 142 | +| 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) | | | 143 | +| Query the information schema metadata in a Spanner PostgreSQL database. | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/pg-schema-information.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-schema-information.js,samples/README.md) | | 132 | 144 | | Queryoptions | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/queryoptions.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/queryoptions.js,samples/README.md) | | 133 | 145 | | Quickstart | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/quickstart.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/quickstart.js,samples/README.md) | | 134 | 146 | | Sets a request tag for a single query | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/request-tag.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/request-tag.js,samples/README.md) | | |
Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/src/codec.ts +45-2Lines changed: 45 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,9 +18,10 @@ import {PreciseDate} from '@google-cloud/precise-date'; | 18 | 18 | import arrify = require('arrify'); | 19 | 19 | import {Big} from 'big.js'; | 20 | 20 | import * as is from 'is'; | 21 | | -import {common as p} from 'protobufjs'; | | 21 | +import {common as p, util} from 'protobufjs'; | 22 | 22 | import {google as spannerClient} from '../protos/protos'; | 23 | 23 | import {GoogleError} from 'google-gax'; | | 24 | +import float = util.float; | 24 | 25 |
| 25 | 26 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | 26 | 27 | export type Value = any; | @@ -216,6 +217,26 @@ export class Numeric { | 216 | 217 | } | 217 | 218 | } | 218 | 219 |
| | 220 | +/** | | 221 | + * @typedef PGNumeric | | 222 | + * @see Spanner.pgNumeric | | 223 | + */ | | 224 | +export class PGNumeric { | | 225 | + value: string; | | 226 | + constructor(pgValue: string | number) { | | 227 | + this.value = pgValue.toString(); | | 228 | + } | | 229 | + valueOf(): Big { | | 230 | + if (this.value.toLowerCase() === 'nan') { | | 231 | + throw new Error(`${this.value} cannot be converted to a numeric value`); | | 232 | + } | | 233 | + return new Big(this.value); | | 234 | + } | | 235 | + toJSON(): string { | | 236 | + return this.valueOf().toJSON(); | | 237 | + } | | 238 | +} | | 239 | + | 219 | 240 | /** | 220 | 241 | * @typedef JSONOptions | 221 | 242 | * @property {boolean} [wrapNumbers=false] Indicates if the numbers should be | @@ -333,6 +354,14 @@ function decode(value: Value, type: spannerClient.spanner.v1.Type): Value { | 333 | 354 | break; | 334 | 355 | case spannerClient.spanner.v1.TypeCode.NUMERIC: | 335 | 356 | case 'NUMERIC': | | 357 | + if ( | | 358 | + type.typeAnnotation === | | 359 | + spannerClient.spanner.v1.TypeAnnotationCode.PG_NUMERIC || | | 360 | + type.typeAnnotation === 'PG_NUMERIC' | | 361 | + ) { | | 362 | + decoded = new PGNumeric(decoded); | | 363 | + break; | | 364 | + } | 336 | 365 | decoded = new Numeric(decoded); | 337 | 366 | break; | 338 | 367 | case spannerClient.spanner.v1.TypeCode.TIMESTAMP: | @@ -413,6 +442,10 @@ function encodeValue(value: Value): Value { | 413 | 442 | return value.value; | 414 | 443 | } | 415 | 444 |
| | 445 | + if (value instanceof PGNumeric) { | | 446 | + return value.value; | | 447 | + } | | 448 | + | 416 | 449 | if (Buffer.isBuffer(value)) { | 417 | 450 | return value.toString('base64'); | 418 | 451 | } | @@ -446,6 +479,7 @@ const TypeCode: { | 446 | 479 | int64: 'INT64', | 447 | 480 | float64: 'FLOAT64', | 448 | 481 | numeric: 'NUMERIC', | | 482 | + pgNumeric: 'NUMERIC', | 449 | 483 | timestamp: 'TIMESTAMP', | 450 | 484 | date: 'DATE', | 451 | 485 | string: 'STRING', | @@ -524,6 +558,10 @@ function getType(value: Value): Type { | 524 | 558 | return {type: 'numeric'}; | 525 | 559 | } | 526 | 560 |
| | 561 | + if (value instanceof PGNumeric) { | | 562 | + return {type: 'pgNumeric'}; | | 563 | + } | | 564 | + | 527 | 565 | if (is.boolean(value)) { | 528 | 566 | return {type: 'bool'}; | 529 | 567 | } | @@ -639,7 +677,7 @@ function createTypeObject( | 639 | 677 | friendlyType = 'unspecified'; | 640 | 678 | } | 641 | 679 |
| 642 | | - if (is.string(friendlyType)) { | | 680 | + if (typeof friendlyType === 'string') { | 643 | 681 | friendlyType = {type: friendlyType} as Type; | 644 | 682 | } | 645 | 683 |
| @@ -662,6 +700,10 @@ function createTypeObject( | 662 | 700 | }; | 663 | 701 | } | 664 | 702 |
| | 703 | + if (friendlyType.type === 'pgNumeric') { | | 704 | + type.typeAnnotation = | | 705 | + spannerClient.spanner.v1.TypeAnnotationCode.PG_NUMERIC; | | 706 | + } | 665 | 707 | return type; | 666 | 708 | } | 667 | 709 |
| @@ -674,6 +716,7 @@ export const codec = { | 674 | 716 | Float, | 675 | 717 | Int, | 676 | 718 | Numeric, | | 719 | + PGNumeric, | 677 | 720 | convertFieldsToJson, | 678 | 721 | decode, | 679 | 722 | encode, | |
Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/src/index.ts +31-2Lines changed: 31 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,7 +24,15 @@ import * as path from 'path'; | 24 | 24 | import {common as p} from 'protobufjs'; | 25 | 25 | import * as streamEvents from 'stream-events'; | 26 | 26 | import * as through from 'through2'; | 27 | | -import {codec, Float, Int, Numeric, SpannerDate, Struct} from './codec'; | | 27 | +import { | | 28 | + codec, | | 29 | + Float, | | 30 | + Int, | | 31 | + Numeric, | | 32 | + PGNumeric, | | 33 | + SpannerDate, | | 34 | + Struct, | | 35 | +} from './codec'; | 28 | 36 | import {Backup} from './backup'; | 29 | 37 | import {Database} from './database'; | 30 | 38 | import { | @@ -33,7 +41,7 @@ import { | 33 | 41 | CreateInstanceResponse, | 34 | 42 | } from './instance'; | 35 | 43 | import {grpc, GrpcClientOptions, CallOptions, GoogleError} from 'google-gax'; | 36 | | -import {google as instanceAdmin} from '../protos/protos'; | | 44 | +import {google, google as instanceAdmin} from '../protos/protos'; | 37 | 45 | import { | 38 | 46 | PagedOptions, | 39 | 47 | PagedResponse, | @@ -185,6 +193,10 @@ class Spanner extends GrpcService { | 185 | 193 | * @type {string} | 186 | 194 | */ | 187 | 195 | static COMMIT_TIMESTAMP = 'spanner.commit_timestamp()'; | | 196 | + static POSTGRESQL = | | 197 | + google.spanner.admin.database.v1.DatabaseDialect.POSTGRESQL; | | 198 | + static GOOGLE_STANDARD_SQL = | | 199 | + google.spanner.admin.database.v1.DatabaseDialect.GOOGLE_STANDARD_SQL; | 188 | 200 |
| 189 | 201 | /** | 190 | 202 | * Gets the configured Spanner emulator host from an environment variable. | @@ -1274,6 +1286,22 @@ class Spanner extends GrpcService { | 1274 | 1286 | return new codec.Numeric(value); | 1275 | 1287 | } | 1276 | 1288 |
| | 1289 | + /** | | 1290 | + * Helper function to get a Cloud Spanner pgNumeric object. | | 1291 | + * | | 1292 | + * @param {string} value The pgNumeric value as a string. | | 1293 | + * @returns {PGNumeric} | | 1294 | + * | | 1295 | + * @example | | 1296 | + * ``` | | 1297 | + * const {Spanner} = require('@google-cloud/spanner'); | | 1298 | + * const pgNumeric = Spanner.pgNumeric("3.141592653"); | | 1299 | + * ``` | | 1300 | + */ | | 1301 | + static pgNumeric(value): PGNumeric { | | 1302 | + return new codec.PGNumeric(value); | | 1303 | + } | | 1304 | + | 1277 | 1305 | /** | 1278 | 1306 | * Helper function to get a Cloud Spanner Struct object. | 1279 | 1307 | * | @@ -1309,6 +1337,7 @@ promisifyAll(Spanner, { | 1309 | 1337 | 'instance', | 1310 | 1338 | 'int', | 1311 | 1339 | 'numeric', | | 1340 | + 'pgNumeric', | 1312 | 1341 | 'operation', | 1313 | 1342 | 'timestamp', | 1314 | 1343 | ], | |
Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/src/instance.ts +8-1Lines changed: 8 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -888,10 +888,17 @@ class Instance extends common.GrpcServiceObject { | 888 | 888 |
| 889 | 889 | const poolOptions = options.poolOptions; | 890 | 890 | const poolCtor = options.poolCtor; | | 891 | + let createStatement = 'CREATE DATABASE `' + name.split('/').pop() + '`'; | | 892 | + if ( | | 893 | + databaseAdmin.spanner.admin.database.v1.DatabaseDialect.POSTGRESQL === | | 894 | + options.databaseDialect | | 895 | + ) { | | 896 | + createStatement = 'CREATE DATABASE "' + name.split('/').pop() + '"'; | | 897 | + } | 891 | 898 | const reqOpts = extend( | 892 | 899 | { | 893 | 900 | parent: this.formattedName_, | 894 | | - createStatement: 'CREATE DATABASE `' + name.split('/').pop() + '`', | | 901 | + createStatement: createStatement, | 895 | 902 | }, | 896 | 903 | options | 897 | 904 | ); | |
Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/test/codec.ts +83Lines changed: 83 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -188,6 +188,46 @@ describe('codec', () => { | 188 | 188 | }); | 189 | 189 | }); | 190 | 190 |
| | 191 | + describe('PGNumeric', () => { | | 192 | + it('should store value as a string', () => { | | 193 | + const value = '8.01911'; | | 194 | + const pgNumeric = new codec.PGNumeric(value); | | 195 | + | | 196 | + assert.strictEqual(pgNumeric.value, '8.01911'); | | 197 | + }); | | 198 | + | | 199 | + it('should store NaN value as a string', () => { | | 200 | + const value = 'NaN'; | | 201 | + const pgNumeric = new codec.PGNumeric(value); | | 202 | + | | 203 | + assert.strictEqual(pgNumeric.value, 'NaN'); | | 204 | + }); | | 205 | + | | 206 | + it('should return as a Big', () => { | | 207 | + const value = '8.01911'; | | 208 | + const pgNumeric = new codec.PGNumeric(value); | | 209 | + | | 210 | + const expected = new Big(value); | | 211 | + assert.ok(pgNumeric.valueOf().eq(expected)); | | 212 | + }); | | 213 | + | | 214 | + it('should throw an error when trying to return NaN as a Big', () => { | | 215 | + const value = 'NaN'; | | 216 | + const pgNumeric = new codec.PGNumeric(value); | | 217 | + | | 218 | + assert.throws(() => { | | 219 | + pgNumeric.valueOf(); | | 220 | + }, new RegExp('NaN cannot be converted to a numeric value')); | | 221 | + }); | | 222 | + | | 223 | + it('toJSON', () => { | | 224 | + const value = '8.01911'; | | 225 | + const pgNumeric = new codec.PGNumeric(value); | | 226 | + | | 227 | + assert.strictEqual(pgNumeric.toJSON(), value); | | 228 | + }); | | 229 | + }); | | 230 | + | 191 | 231 | describe('Struct', () => { | 192 | 232 | describe('toJSON', () => { | 193 | 233 | it('should covert the struct to JSON', () => { | @@ -463,6 +503,18 @@ describe('codec', () => { | 463 | 503 | assert.strictEqual(decoded.value, value); | 464 | 504 | }); | 465 | 505 |
| | 506 | + it('should decode PG NUMERIC', () => { | | 507 | + const value = '8.01911'; | | 508 | + | | 509 | + const decoded = codec.decode(value, { | | 510 | + code: google.spanner.v1.TypeCode.NUMERIC, | | 511 | + typeAnnotation: google.spanner.v1.TypeAnnotationCode.PG_NUMERIC, | | 512 | + }); | | 513 | + | | 514 | + assert(decoded instanceof codec.PGNumeric); | | 515 | + assert.strictEqual(decoded.value, value); | | 516 | + }); | | 517 | + | 466 | 518 | it('should decode JSON', () => { | 467 | 519 | const value = '{"result":true, "count":42}'; | 468 | 520 | const expected = JSON.parse(value); | @@ -618,6 +670,14 @@ describe('codec', () => { | 618 | 670 | assert.strictEqual(encoded, value.value); | 619 | 671 | }); | 620 | 672 |
| | 673 | + it('should stringify PG NUMERIC', () => { | | 674 | + const value = new codec.PGNumeric('8.01911'); | | 675 | + | | 676 | + const encoded = codec.encode(value); | | 677 | + | | 678 | + assert.strictEqual(encoded, value.value); | | 679 | + }); | | 680 | + | 621 | 681 | it('should encode ARRAY and inner members', () => { | 622 | 682 | const value = [5]; | 623 | 683 |
| @@ -811,6 +871,12 @@ describe('codec', () => { | 811 | 871 | }, | 812 | 872 | }); | 813 | 873 | }); | | 874 | + | | 875 | + it('should determine if the value is a PGNumeric', () => { | | 876 | + assert.deepStrictEqual(codec.getType(new codec.PGNumeric('7248')), { | | 877 | + type: 'pgNumeric', | | 878 | + }); | | 879 | + }); | 814 | 880 | }); | 815 | 881 |
| 816 | 882 | describe('convertToListValue', () => { | @@ -1019,5 +1085,22 @@ describe('codec', () => { | 1019 | 1085 | }, | 1020 | 1086 | }); | 1021 | 1087 | }); | | 1088 | + it('should set code and typeAnnotation for pgNumeric string', () => { | | 1089 | + const type = codec.createTypeObject('pgNumeric'); | | 1090 | + | | 1091 | + assert.deepStrictEqual(type, { | | 1092 | + code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.NUMERIC], | | 1093 | + typeAnnotation: google.spanner.v1.TypeAnnotationCode.PG_NUMERIC, | | 1094 | + }); | | 1095 | + }); | | 1096 | + | | 1097 | + it('should set code and typeAnnotation for pgNumeric friendlyType object', () => { | | 1098 | + const type = codec.createTypeObject({type: 'pgNumeric'}); | | 1099 | + | | 1100 | + assert.deepStrictEqual(type, { | | 1101 | + code: google.spanner.v1.TypeCode[google.spanner.v1.TypeCode.NUMERIC], | | 1102 | + typeAnnotation: google.spanner.v1.TypeAnnotationCode.PG_NUMERIC, | | 1103 | + }); | | 1104 | + }); | 1022 | 1105 | }); | 1023 | 1106 | }); |
Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/test/index.ts +18Lines changed: 18 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -85,6 +85,7 @@ const fakePfy = extend({}, pfy, { | 85 | 85 | 'instance', | 86 | 86 | 'int', | 87 | 87 | 'numeric', | | 88 | + 'pgNumeric', | 88 | 89 | 'operation', | 89 | 90 | 'timestamp', | 90 | 91 | ]); | @@ -514,6 +515,23 @@ describe('Spanner', () => { | 514 | 515 | }); | 515 | 516 | }); | 516 | 517 |
| | 518 | + describe('pgNumeric', () => { | | 519 | + it('should create a PGNumeric instance', () => { | | 520 | + const value = '3.145'; | | 521 | + const customValue = {value: '3.145'}; | | 522 | + | | 523 | + fakeCodec.PGNumeric = class { | | 524 | + constructor(value_) { | | 525 | + assert.strictEqual(value_, value); | | 526 | + return customValue; | | 527 | + } | | 528 | + }; | | 529 | + | | 530 | + const pgNumeric = Spanner.pgNumeric(value); | | 531 | + assert.strictEqual(pgNumeric, customValue); | | 532 | + }); | | 533 | + }); | | 534 | + | 517 | 535 | describe('createInstance', () => { | 518 | 536 | const NAME = 'instance-name'; | 519 | 537 | let PATH; | |
|