Skip to content

Commit 8a15343

Browse files
committed
feat(spanner): support for type UUID
1 parent 2a12f27 commit 8a15343

File tree

7 files changed

+425
-80
lines changed

7 files changed

+425
-80
lines changed

src/codec.ts

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {Big} from 'big.js';
3333
import {common as p} from 'protobufjs';
3434
import {google as spannerClient} from '../protos/protos';
3535
import {GoogleError} from 'google-gax';
36+
import * as uuid from 'uuid';
3637

3738
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3839
export type Value = any;
@@ -154,20 +155,6 @@ export class SpannerDate extends Date {
154155
}
155156
}
156157

157-
/**
158-
* @typedef UUID
159-
* @see Spanner.uuid
160-
*/
161-
export class UUID {
162-
value: string;
163-
constructor(value: string) {
164-
this.value = value;
165-
}
166-
valueOf(): string {
167-
return String(this.value);
168-
}
169-
}
170-
171158
/**
172159
* Using an abstract class to simplify checking for wrapped numbers.
173160
*
@@ -872,10 +859,6 @@ function decode(
872859
enumObject: columnMetadata as object,
873860
});
874861
break;
875-
case spannerClient.spanner.v1.TypeCode.UUID:
876-
case 'UUID':
877-
decoded = new UUID(decoded);
878-
break;
879862
case spannerClient.spanner.v1.TypeCode.FLOAT32:
880863
case 'FLOAT32':
881864
decoded = new Float32(decoded);
@@ -1016,10 +999,6 @@ function encodeValue(value: Value): Value {
1016999
return value.value;
10171000
}
10181001

1019-
if (value instanceof UUID) {
1020-
return value.value;
1021-
}
1022-
10231002
if (value instanceof Struct) {
10241003
return Array.from(value).map(field => encodeValue(field.value));
10251004
}
@@ -1098,6 +1077,7 @@ interface FieldType extends Type {
10981077
/**
10991078
* @typedef {object} ParamType
11001079
* @property {string} type The param type. Must be one of the following:
1080+
* - uuid
11011081
* - float32
11021082
* - float64
11031083
* - int64
@@ -1136,10 +1116,6 @@ function getType(value: Value): Type {
11361116
const isSpecialNumber =
11371117
isInfinite(value) || (isNumber(value) && isNaN(value));
11381118

1139-
if (value instanceof UUID) {
1140-
return {type: 'uuid'};
1141-
}
1142-
11431119
if (value instanceof Float32) {
11441120
return {type: 'float32'};
11451121
}
@@ -1184,6 +1160,10 @@ function getType(value: Value): Type {
11841160
return {type: 'bool'};
11851161
}
11861162

1163+
if (uuid.validate(value)) {
1164+
return {type: 'unspecified'};
1165+
}
1166+
11871167
if (isString(value)) {
11881168
return {type: 'string'};
11891169
}
@@ -1339,7 +1319,6 @@ export const codec = {
13391319
convertProtoTimestampToDate,
13401320
createTypeObject,
13411321
SpannerDate,
1342-
UUID,
13431322
Float32,
13441323
Float,
13451324
Int,

src/index.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import {EventEmitter} from 'events';
2727
import * as through from 'through2';
2828
import {
2929
codec,
30-
UUID,
3130
Float32,
3231
Float,
3332
Int,
@@ -1990,23 +1989,6 @@ class Spanner extends GrpcService {
19901989
return new PreciseDate(value as number);
19911990
}
19921991

1993-
/**
1994-
* Helper function to get a Cloud Spanner UUID object.
1995-
*
1996-
* @param {string} value The uuid as a string.
1997-
* @returns {UUID}
1998-
*
1999-
* @example
2000-
* ```
2001-
* const {Spanner} = require('@google-cloud/spanner');
2002-
* const value = uuidv4();
2003-
* const uuid = Spanner.uuid(value);
2004-
* ```
2005-
*/
2006-
static uuid(value): UUID {
2007-
return new codec.UUID(value);
2008-
}
2009-
20101992
/**
20111993
* Helper function to get a Cloud Spanner Float32 object.
20121994
*
@@ -2238,7 +2220,6 @@ process.on('beforeExit', () => {
22382220
promisifyAll(Spanner, {
22392221
exclude: [
22402222
'date',
2241-
'uuid',
22422223
'float32',
22432224
'float',
22442225
'instance',

src/transaction.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,17 @@ export class Snapshot extends EventEmitter {
17281728
if (!isEmpty(typeMap)) {
17291729
Object.keys(typeMap).forEach(param => {
17301730
const type = typeMap[param];
1731-
paramTypes[param] = codec.createTypeObject(type);
1731+
const typeObject = codec.createTypeObject(type);
1732+
if (type.child) {
1733+
if (
1734+
typeObject.code === 'ARRAY' &&
1735+
typeObject.arrayElementType?.code !== 'TYPE_CODE_UNSPECIFIED'
1736+
) {
1737+
paramTypes[param] = codec.createTypeObject(type);
1738+
}
1739+
} else if (typeObject.code !== 'TYPE_CODE_UNSPECIFIED') {
1740+
paramTypes[param] = codec.createTypeObject(type);
1741+
}
17321742
});
17331743
}
17341744

0 commit comments

Comments
 (0)