Skip to content

Commit b31705d

Browse files
committed
feat(spanner): support for type UUID
1 parent 93b8fcb commit b31705d

File tree

7 files changed

+304
-100
lines changed

7 files changed

+304
-100
lines changed

src/codec.ts

+6-27
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as is from 'is';
2121
import {common as p} from 'protobufjs';
2222
import {google as spannerClient} from '../protos/protos';
2323
import {GoogleError} from 'google-gax';
24+
import * as uuid from 'uuid';
2425

2526
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2627
export type Value = any;
@@ -142,20 +143,6 @@ export class SpannerDate extends Date {
142143
}
143144
}
144145

145-
/**
146-
* @typedef UUID
147-
* @see Spanner.uuid
148-
*/
149-
export class UUID {
150-
value: string;
151-
constructor(value: string) {
152-
this.value = value;
153-
}
154-
valueOf(): string {
155-
return String(this.value);
156-
}
157-
}
158-
159146
/**
160147
* Using an abstract class to simplify checking for wrapped numbers.
161148
*
@@ -543,10 +530,6 @@ function decode(
543530
enumObject: columnMetadata as object,
544531
});
545532
break;
546-
case spannerClient.spanner.v1.TypeCode.UUID:
547-
case 'UUID':
548-
decoded = new UUID(decoded);
549-
break;
550533
case spannerClient.spanner.v1.TypeCode.FLOAT32:
551534
case 'FLOAT32':
552535
decoded = new Float32(decoded);
@@ -683,10 +666,6 @@ function encodeValue(value: Value): Value {
683666
return value.value;
684667
}
685668

686-
if (value instanceof UUID) {
687-
return value.value;
688-
}
689-
690669
if (value instanceof Struct) {
691670
return Array.from(value).map(field => encodeValue(field.value));
692671
}
@@ -760,6 +739,7 @@ interface FieldType extends Type {
760739
/**
761740
* @typedef {object} ParamType
762741
* @property {string} type The param type. Must be one of the following:
742+
* - uuid
763743
* - float32
764744
* - float64
765745
* - int64
@@ -797,10 +777,6 @@ function getType(value: Value): Type {
797777
const isSpecialNumber =
798778
is.infinite(value) || (is.number(value) && isNaN(value));
799779

800-
if (value instanceof UUID) {
801-
return {type: 'uuid'};
802-
}
803-
804780
if (value instanceof Float32) {
805781
return {type: 'float32'};
806782
}
@@ -841,6 +817,10 @@ function getType(value: Value): Type {
841817
return {type: 'bool'};
842818
}
843819

820+
if (uuid.validate(value)) {
821+
return {type: 'unspecified'};
822+
}
823+
844824
if (is.string(value)) {
845825
return {type: 'string'};
846826
}
@@ -996,7 +976,6 @@ export const codec = {
996976
convertProtoTimestampToDate,
997977
createTypeObject,
998978
SpannerDate,
999-
UUID,
1000979
Float32,
1001980
Float,
1002981
Int,

src/index.ts

+1-20
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import * as streamEvents from 'stream-events';
2626
import * as through from 'through2';
2727
import {
2828
codec,
29-
UUID,
3029
Float32,
3130
Float,
3231
Int,
@@ -1705,23 +1704,6 @@ class Spanner extends GrpcService {
17051704
return new PreciseDate(value as number);
17061705
}
17071706

1708-
/**
1709-
* Helper function to get a Cloud Spanner UUID object.
1710-
*
1711-
* @param {string} value The uuid as a string.
1712-
* @returns {UUID}
1713-
*
1714-
* @example
1715-
* ```
1716-
* const {Spanner} = require('@google-cloud/spanner');
1717-
* const value = uuidv4();
1718-
* const uuid = Spanner.uuid(value);
1719-
* ```
1720-
*/
1721-
static uuid(value): UUID {
1722-
return new codec.UUID(value);
1723-
}
1724-
17251707
/**
17261708
* Helper function to get a Cloud Spanner Float32 object.
17271709
*
@@ -1909,7 +1891,6 @@ class Spanner extends GrpcService {
19091891
promisifyAll(Spanner, {
19101892
exclude: [
19111893
'date',
1912-
'uuid',
19131894
'float32',
19141895
'float',
19151896
'instance',
@@ -2089,5 +2070,5 @@ import * as protos from '../protos/protos';
20892070
import IInstanceConfig = instanceAdmin.spanner.admin.instance.v1.IInstanceConfig;
20902071
export {v1, protos};
20912072
export default {Spanner};
2092-
export {UUID, Float32, Float, Int, Struct, Numeric, PGNumeric, SpannerDate};
2073+
export {Float32, Float, Int, Struct, Numeric, PGNumeric, SpannerDate};
20932074
export {ObservabilityOptions};

src/transaction.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -1521,7 +1521,17 @@ export class Snapshot extends EventEmitter {
15211521
if (!is.empty(typeMap)) {
15221522
Object.keys(typeMap).forEach(param => {
15231523
const type = typeMap[param];
1524-
paramTypes[param] = codec.createTypeObject(type);
1524+
const typeObject = codec.createTypeObject(type);
1525+
if (type.child) {
1526+
if (
1527+
typeObject.code === 'ARRAY' &&
1528+
typeObject.arrayElementType?.code !== 'TYPE_CODE_UNSPECIFIED'
1529+
) {
1530+
paramTypes[param] = codec.createTypeObject(type);
1531+
}
1532+
} else if (typeObject.code !== 'TYPE_CODE_UNSPECIFIED') {
1533+
paramTypes[param] = codec.createTypeObject(type);
1534+
}
15251535
});
15261536
}
15271537

0 commit comments

Comments
 (0)