@google-cloud/spanner 7.18.0 → 7.18.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +1 -0
- package/build/protos/google/spanner/admin/database/v1/backup.proto +757 -0
- package/build/protos/google/spanner/admin/database/v1/backup_schedule.proto +230 -0
- package/build/protos/google/spanner/admin/database/v1/common.proto +132 -0
- package/build/protos/google/spanner/admin/database/v1/spanner_database_admin.proto +1209 -0
- package/build/protos/google/spanner/admin/instance/v1/common.proto +64 -0
- package/build/protos/google/spanner/admin/instance/v1/spanner_instance_admin.proto +2110 -0
- package/build/protos/google/spanner/executor/v1/cloud_executor.proto +1520 -0
- package/build/protos/google/spanner/v1/commit_response.proto +59 -0
- package/build/protos/google/spanner/v1/keys.proto +163 -0
- package/build/protos/google/spanner/v1/mutation.proto +107 -0
- package/build/protos/google/spanner/v1/query_plan.proto +128 -0
- package/build/protos/google/spanner/v1/result_set.proto +237 -0
- package/build/protos/google/spanner/v1/spanner.proto +1389 -0
- package/build/protos/google/spanner/v1/transaction.proto +575 -0
- package/build/protos/google/spanner/v1/type.proto +214 -0
- package/build/protos/protos.d.ts +39317 -0
- package/build/protos/protos.js +100360 -0
- package/build/protos/protos.json +10271 -0
- package/build/src/backup.d.ts +349 -0
- package/build/src/backup.js +246 -0
- package/build/src/batch-transaction.d.ts +247 -0
- package/build/src/batch-transaction.js +384 -0
- package/build/src/codec.d.ts +362 -0
- package/build/src/codec.js +791 -0
- package/build/src/common-grpc/service-object.d.ts +80 -0
- package/build/src/common-grpc/service-object.js +101 -0
- package/build/src/common-grpc/service.d.ts +445 -0
- package/build/src/common-grpc/service.js +826 -0
- package/build/src/common.d.ts +66 -0
- package/build/src/common.js +54 -0
- package/build/src/database.d.ts +2103 -0
- package/build/src/database.js +1655 -0
- package/build/src/extra_proto_list.json +1 -0
- package/build/src/helper.d.ts +45 -0
- package/build/src/helper.js +71 -0
- package/build/src/index.d.ts +1195 -0
- package/build/src/index.js +1059 -0
- package/build/src/instance-config.d.ts +294 -0
- package/build/src/instance-config.js +216 -0
- package/build/src/instance.d.ts +832 -0
- package/build/src/instance.js +781 -0
- package/build/src/instrument.d.ts +85 -0
- package/build/src/instrument.js +215 -0
- package/build/src/multiplexed-session.d.ts +136 -0
- package/build/src/multiplexed-session.js +197 -0
- package/build/src/partial-result-stream.d.ts +253 -0
- package/build/src/partial-result-stream.js +400 -0
- package/build/src/session-factory.d.ts +100 -0
- package/build/src/session-factory.js +93 -0
- package/build/src/session-pool.d.ts +471 -0
- package/build/src/session-pool.js +754 -0
- package/build/src/session.d.ts +258 -0
- package/build/src/session.js +348 -0
- package/build/src/spanner_grpc_config.json +92 -0
- package/build/src/table.d.ts +831 -0
- package/build/src/table.js +277 -0
- package/build/src/transaction-runner.d.ts +185 -0
- package/build/src/transaction-runner.js +282 -0
- package/build/src/transaction.d.ts +1691 -0
- package/build/src/transaction.js +2004 -0
- package/build/src/v1/database_admin_client.d.ts +2301 -0
- package/build/src/v1/database_admin_client.js +2286 -0
- package/build/src/v1/database_admin_client_config.json +160 -0
- package/build/src/v1/database_admin_proto_list.json +6 -0
- package/build/src/v1/gapic_metadata.json +27 -0
- package/build/src/v1/index.d.ts +4 -0
- package/build/src/v1/index.js +29 -0
- package/build/src/v1/instance_admin_client.d.ts +1985 -0
- package/build/src/v1/instance_admin_client.js +1768 -0
- package/build/src/v1/instance_admin_client_config.json +129 -0
- package/build/src/v1/instance_admin_proto_list.json +4 -0
- package/build/src/v1/spanner_client.d.ts +1250 -0
- package/build/src/v1/spanner_client.js +1086 -0
- package/build/src/v1/spanner_client_config.json +119 -0
- package/build/src/v1/spanner_executor_proxy_client.d.ts +418 -0
- package/build/src/v1/spanner_executor_proxy_client.js +706 -0
- package/build/src/v1/spanner_executor_proxy_client_config.json +30 -0
- package/build/src/v1/spanner_executor_proxy_proto_list.json +14 -0
- package/build/src/v1/spanner_proto_list.json +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,791 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.codec = exports.PGOid = exports.PGJsonb = exports.ProtoEnum = exports.ProtoMessage = exports.PGNumeric = exports.Numeric = exports.Struct = exports.Int = exports.Float = exports.Float32 = exports.SpannerDate = void 0;
|
|
4
|
+
/*!
|
|
5
|
+
* Copyright 2017 Google Inc. All Rights Reserved.
|
|
6
|
+
*
|
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
8
|
+
* you may not use this file except in compliance with the License.
|
|
9
|
+
* You may obtain a copy of the License at
|
|
10
|
+
*
|
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
12
|
+
*
|
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
16
|
+
* See the License for the specific language governing permissions and
|
|
17
|
+
* limitations under the License.
|
|
18
|
+
*/
|
|
19
|
+
const service_1 = require("./common-grpc/service");
|
|
20
|
+
const precise_date_1 = require("@google-cloud/precise-date");
|
|
21
|
+
const arrify = require("arrify");
|
|
22
|
+
const big_js_1 = require("big.js");
|
|
23
|
+
const is = require("is");
|
|
24
|
+
const protos_1 = require("../protos/protos");
|
|
25
|
+
const google_gax_1 = require("google-gax");
|
|
26
|
+
/**
|
|
27
|
+
* Date-like object used to represent Cloud Spanner Dates. DATE types represent
|
|
28
|
+
* a logical calendar date, independent of time zone. DATE values do not
|
|
29
|
+
* represent a specific 24-hour period. Rather, a given DATE value represents a
|
|
30
|
+
* different 24-hour period when interpreted in a different time zone. Because
|
|
31
|
+
* of this, all values passed to {@link Spanner.date} will be interpreted as
|
|
32
|
+
* local time.
|
|
33
|
+
*
|
|
34
|
+
* To represent an absolute point in time, use {@link Spanner.timestamp}.
|
|
35
|
+
*
|
|
36
|
+
* @see Spanner.date
|
|
37
|
+
* @see https://cloud.google.com/spanner/docs/data-types#date-type
|
|
38
|
+
*
|
|
39
|
+
* @class
|
|
40
|
+
* @extends Date
|
|
41
|
+
*
|
|
42
|
+
* @param {string|number} [date] String representing the date or number
|
|
43
|
+
* representing the year. If year is a number between 0 and 99, then year is
|
|
44
|
+
* assumed to be 1900 + year.
|
|
45
|
+
* @param {number} [month] Number representing the month (0 = January).
|
|
46
|
+
* @param {number} [date] Number representing the date.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```
|
|
50
|
+
* Spanner.date('3-3-1933');
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
class SpannerDate extends Date {
|
|
54
|
+
constructor(...dateFields) {
|
|
55
|
+
const yearOrDateString = dateFields[0];
|
|
56
|
+
// yearOrDateString could be 0 (number).
|
|
57
|
+
if (yearOrDateString === null || yearOrDateString === undefined) {
|
|
58
|
+
dateFields[0] = new Date().toDateString();
|
|
59
|
+
}
|
|
60
|
+
// JavaScript Date objects will interpret ISO date strings as Zulu time,
|
|
61
|
+
// but by formatting it, we can infer local time.
|
|
62
|
+
if (/^\d{4}-\d{1,2}-\d{1,2}/.test(yearOrDateString)) {
|
|
63
|
+
const [year, month, date] = yearOrDateString.split(/-|T/);
|
|
64
|
+
dateFields = [`${month}-${date}-${year}`];
|
|
65
|
+
}
|
|
66
|
+
super(...dateFields.slice(0, 3));
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Returns the date in ISO date format.
|
|
70
|
+
* `YYYY-MM-DD`
|
|
71
|
+
*
|
|
72
|
+
* @returns {string}
|
|
73
|
+
*/
|
|
74
|
+
toJSON() {
|
|
75
|
+
const year = this.getFullYear().toString();
|
|
76
|
+
const month = (this.getMonth() + 1).toString();
|
|
77
|
+
const date = this.getDate().toString();
|
|
78
|
+
return `${year.padStart(4, '0')}-${month.padStart(2, '0')}-${date.padStart(2, '0')}`;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
exports.SpannerDate = SpannerDate;
|
|
82
|
+
/**
|
|
83
|
+
* Using an abstract class to simplify checking for wrapped numbers.
|
|
84
|
+
*
|
|
85
|
+
* @private
|
|
86
|
+
*/
|
|
87
|
+
class WrappedNumber {
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @typedef Float32
|
|
91
|
+
* @see Spanner.float32
|
|
92
|
+
*/
|
|
93
|
+
class Float32 extends WrappedNumber {
|
|
94
|
+
constructor(value) {
|
|
95
|
+
super();
|
|
96
|
+
this.value = value;
|
|
97
|
+
}
|
|
98
|
+
valueOf() {
|
|
99
|
+
return Number(this.value);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
exports.Float32 = Float32;
|
|
103
|
+
/**
|
|
104
|
+
* @typedef Float
|
|
105
|
+
* @see Spanner.float
|
|
106
|
+
*/
|
|
107
|
+
class Float extends WrappedNumber {
|
|
108
|
+
constructor(value) {
|
|
109
|
+
super();
|
|
110
|
+
this.value = value;
|
|
111
|
+
}
|
|
112
|
+
valueOf() {
|
|
113
|
+
return Number(this.value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
exports.Float = Float;
|
|
117
|
+
/**
|
|
118
|
+
* @typedef Int
|
|
119
|
+
* @see Spanner.int
|
|
120
|
+
*/
|
|
121
|
+
class Int extends WrappedNumber {
|
|
122
|
+
constructor(value) {
|
|
123
|
+
super();
|
|
124
|
+
this.value = value.toString();
|
|
125
|
+
}
|
|
126
|
+
valueOf() {
|
|
127
|
+
const num = Number(this.value);
|
|
128
|
+
if (num > Number.MAX_SAFE_INTEGER) {
|
|
129
|
+
throw new google_gax_1.GoogleError(`Integer ${this.value} is out of bounds.`);
|
|
130
|
+
}
|
|
131
|
+
return num;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.Int = Int;
|
|
135
|
+
/**
|
|
136
|
+
* @typedef Struct
|
|
137
|
+
* @see Spanner.struct
|
|
138
|
+
*/
|
|
139
|
+
class Struct extends Array {
|
|
140
|
+
/**
|
|
141
|
+
* Converts struct into a pojo (plain old JavaScript object).
|
|
142
|
+
*
|
|
143
|
+
* @param {JSONOptions} [options] JSON options.
|
|
144
|
+
* @returns {object}
|
|
145
|
+
*/
|
|
146
|
+
toJSON(options) {
|
|
147
|
+
return exports.codec.convertFieldsToJson(this, options);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Converts an array of fields to a struct.
|
|
151
|
+
*
|
|
152
|
+
* @private
|
|
153
|
+
*
|
|
154
|
+
* @param {object[]} fields List of struct fields.
|
|
155
|
+
* @return {Struct}
|
|
156
|
+
*/
|
|
157
|
+
static fromArray(fields) {
|
|
158
|
+
return new Struct(...fields);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Converts a JSON object to a struct.
|
|
162
|
+
*
|
|
163
|
+
* @private
|
|
164
|
+
*
|
|
165
|
+
* @param {object} json Struct JSON.
|
|
166
|
+
* @return {Struct}
|
|
167
|
+
*/
|
|
168
|
+
static fromJSON(json) {
|
|
169
|
+
const fields = Object.keys(json || {}).map(name => {
|
|
170
|
+
const value = json[name];
|
|
171
|
+
return { name, value };
|
|
172
|
+
});
|
|
173
|
+
return Struct.fromArray(fields);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.Struct = Struct;
|
|
177
|
+
/**
|
|
178
|
+
* @typedef Numeric
|
|
179
|
+
* @see Spanner.numeric
|
|
180
|
+
*/
|
|
181
|
+
class Numeric {
|
|
182
|
+
constructor(value) {
|
|
183
|
+
this.value = value;
|
|
184
|
+
}
|
|
185
|
+
valueOf() {
|
|
186
|
+
return new big_js_1.Big(this.value);
|
|
187
|
+
}
|
|
188
|
+
toJSON() {
|
|
189
|
+
return this.valueOf().toJSON();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
exports.Numeric = Numeric;
|
|
193
|
+
/**
|
|
194
|
+
* @typedef PGNumeric
|
|
195
|
+
* @see Spanner.pgNumeric
|
|
196
|
+
*/
|
|
197
|
+
class PGNumeric {
|
|
198
|
+
constructor(pgValue) {
|
|
199
|
+
this.value = pgValue.toString();
|
|
200
|
+
}
|
|
201
|
+
valueOf() {
|
|
202
|
+
if (this.value.toLowerCase() === 'nan') {
|
|
203
|
+
throw new Error(`${this.value} cannot be converted to a numeric value`);
|
|
204
|
+
}
|
|
205
|
+
return new big_js_1.Big(this.value);
|
|
206
|
+
}
|
|
207
|
+
toJSON() {
|
|
208
|
+
return this.valueOf().toJSON();
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
exports.PGNumeric = PGNumeric;
|
|
212
|
+
/**
|
|
213
|
+
* @typedef ProtoMessage
|
|
214
|
+
* @see Spanner.protoMessage
|
|
215
|
+
*/
|
|
216
|
+
class ProtoMessage {
|
|
217
|
+
constructor(protoMessageParams) {
|
|
218
|
+
this.fullName = protoMessageParams.fullName;
|
|
219
|
+
this.messageFunction = protoMessageParams.messageFunction;
|
|
220
|
+
if (protoMessageParams.value instanceof Buffer) {
|
|
221
|
+
this.value = protoMessageParams.value;
|
|
222
|
+
}
|
|
223
|
+
else if (protoMessageParams.messageFunction) {
|
|
224
|
+
this.value = protoMessageParams.messageFunction['encode'](protoMessageParams.value).finish();
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
throw new google_gax_1.GoogleError(`protoMessageParams cannot be used to construct
|
|
228
|
+
the ProtoMessage. Pass the serialized buffer of the
|
|
229
|
+
proto message as the value or provide the message object along with the
|
|
230
|
+
corresponding messageFunction generated by protobufjs-cli.`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
toJSON() {
|
|
234
|
+
if (this.messageFunction) {
|
|
235
|
+
return this.messageFunction['toObject'](this.messageFunction['decode'](this.value));
|
|
236
|
+
}
|
|
237
|
+
return this.value.toString();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
exports.ProtoMessage = ProtoMessage;
|
|
241
|
+
/**
|
|
242
|
+
* @typedef ProtoEnum
|
|
243
|
+
* @see Spanner.protoEnum
|
|
244
|
+
*/
|
|
245
|
+
class ProtoEnum {
|
|
246
|
+
constructor(protoEnumParams) {
|
|
247
|
+
this.fullName = protoEnumParams.fullName;
|
|
248
|
+
this.enumObject = protoEnumParams.enumObject;
|
|
249
|
+
/**
|
|
250
|
+
* @code{IProtoEnumParams} can accept either a number or a string as a value so
|
|
251
|
+
* converting to string and checking whether it's numeric using regex.
|
|
252
|
+
*/
|
|
253
|
+
if (/^\d+$/.test(protoEnumParams.value.toString())) {
|
|
254
|
+
this.value = protoEnumParams.value.toString();
|
|
255
|
+
}
|
|
256
|
+
else if (protoEnumParams.enumObject &&
|
|
257
|
+
protoEnumParams.enumObject[protoEnumParams.value]) {
|
|
258
|
+
this.value = protoEnumParams.enumObject[protoEnumParams.value];
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
throw new google_gax_1.GoogleError(`protoEnumParams cannot be used for constructing the
|
|
262
|
+
ProtoEnum. Pass the number as the value or provide the enum string
|
|
263
|
+
constant as the value along with the corresponding enumObject generated
|
|
264
|
+
by protobufjs-cli.`);
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
toJSON() {
|
|
268
|
+
if (this.enumObject) {
|
|
269
|
+
return Object.getPrototypeOf(this.enumObject)[this.value];
|
|
270
|
+
}
|
|
271
|
+
return this.value.toString();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
exports.ProtoEnum = ProtoEnum;
|
|
275
|
+
/**
|
|
276
|
+
* @typedef PGJsonb
|
|
277
|
+
* @see Spanner.pgJsonb
|
|
278
|
+
*/
|
|
279
|
+
class PGJsonb {
|
|
280
|
+
constructor(pgValue) {
|
|
281
|
+
if (typeof pgValue === 'string') {
|
|
282
|
+
pgValue = JSON.parse(pgValue);
|
|
283
|
+
}
|
|
284
|
+
this.value = pgValue;
|
|
285
|
+
}
|
|
286
|
+
toString() {
|
|
287
|
+
return JSON.stringify(this.value);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
exports.PGJsonb = PGJsonb;
|
|
291
|
+
/**
|
|
292
|
+
* @typedef PGOid
|
|
293
|
+
* @see Spanner.pgOid
|
|
294
|
+
*/
|
|
295
|
+
class PGOid extends WrappedNumber {
|
|
296
|
+
constructor(value) {
|
|
297
|
+
super();
|
|
298
|
+
this.value = value.toString();
|
|
299
|
+
}
|
|
300
|
+
valueOf() {
|
|
301
|
+
const num = Number(this.value);
|
|
302
|
+
if (num > Number.MAX_SAFE_INTEGER) {
|
|
303
|
+
throw new google_gax_1.GoogleError(`PG.OID ${this.value} is out of bounds.`);
|
|
304
|
+
}
|
|
305
|
+
return num;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
exports.PGOid = PGOid;
|
|
309
|
+
/**
|
|
310
|
+
* @typedef JSONOptions
|
|
311
|
+
* @property {boolean} [wrapNumbers=false] Indicates if the numbers should be
|
|
312
|
+
* wrapped in Int/Float wrappers.
|
|
313
|
+
* @property {boolean} [wrapStructs=false] Indicates if the structs should be
|
|
314
|
+
* wrapped in Struct wrapper.
|
|
315
|
+
* @property {boolean} [includeNameless=false] Indicates if nameless columns
|
|
316
|
+
* should be included in the result. If true, nameless columns will be
|
|
317
|
+
* assigned the name '_{column_index}'.
|
|
318
|
+
*/
|
|
319
|
+
/**
|
|
320
|
+
* Wherever a row or struct object is returned, it is assigned a "toJSON"
|
|
321
|
+
* function. This function will generate the JSON for that row.
|
|
322
|
+
*
|
|
323
|
+
* @private
|
|
324
|
+
*
|
|
325
|
+
* @param {array} row The row to generate JSON for.
|
|
326
|
+
* @param {JSONOptions} [options] JSON options.
|
|
327
|
+
* @returns {object}
|
|
328
|
+
*/
|
|
329
|
+
function convertFieldsToJson(fields, options) {
|
|
330
|
+
const json = {};
|
|
331
|
+
const defaultOptions = {
|
|
332
|
+
wrapNumbers: false,
|
|
333
|
+
wrapStructs: false,
|
|
334
|
+
includeNameless: false,
|
|
335
|
+
};
|
|
336
|
+
options = Object.assign(defaultOptions, options);
|
|
337
|
+
let index = 0;
|
|
338
|
+
for (const { name, value } of fields) {
|
|
339
|
+
if (!name && !options.includeNameless) {
|
|
340
|
+
continue;
|
|
341
|
+
}
|
|
342
|
+
const fieldName = name ? name : `_${index}`;
|
|
343
|
+
try {
|
|
344
|
+
json[fieldName] = convertValueToJson(value, options);
|
|
345
|
+
}
|
|
346
|
+
catch (e) {
|
|
347
|
+
e.message = [
|
|
348
|
+
`Serializing column "${fieldName}" encountered an error: ${e.message}`,
|
|
349
|
+
'Call row.toJSON({ wrapNumbers: true }) to receive a custom type.',
|
|
350
|
+
].join(' ');
|
|
351
|
+
throw e;
|
|
352
|
+
}
|
|
353
|
+
index++;
|
|
354
|
+
}
|
|
355
|
+
return json;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Attempts to convert a wrapped or nested value into a native JavaScript type.
|
|
359
|
+
*
|
|
360
|
+
* @private
|
|
361
|
+
*
|
|
362
|
+
* @param {*} value The value to convert.
|
|
363
|
+
* @param {JSONOptions} options JSON options.
|
|
364
|
+
* @return {*}
|
|
365
|
+
*/
|
|
366
|
+
function convertValueToJson(value, options) {
|
|
367
|
+
if (!options.wrapNumbers && value instanceof WrappedNumber) {
|
|
368
|
+
return value.valueOf();
|
|
369
|
+
}
|
|
370
|
+
if (value instanceof Struct) {
|
|
371
|
+
if (!options.wrapStructs) {
|
|
372
|
+
return value.toJSON(options);
|
|
373
|
+
}
|
|
374
|
+
return value.map(({ name, value }) => {
|
|
375
|
+
value = convertValueToJson(value, options);
|
|
376
|
+
return { name, value };
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
if (Array.isArray(value)) {
|
|
380
|
+
return value.map(child => convertValueToJson(child, options));
|
|
381
|
+
}
|
|
382
|
+
if (value instanceof ProtoMessage || value instanceof ProtoEnum) {
|
|
383
|
+
return value.toJSON();
|
|
384
|
+
}
|
|
385
|
+
return value;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Re-decode after the generic gRPC decoding step.
|
|
389
|
+
*
|
|
390
|
+
* @private
|
|
391
|
+
*
|
|
392
|
+
* @param {*} value Value to decode
|
|
393
|
+
* @param {object[]} type Value type object.
|
|
394
|
+
* @param columnMetadata Optional parameter to deserialize data
|
|
395
|
+
* @returns {*}
|
|
396
|
+
*/
|
|
397
|
+
function decode(value, type, columnMetadata) {
|
|
398
|
+
if (is.null(value)) {
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
let decoded = value;
|
|
402
|
+
let fields;
|
|
403
|
+
switch (type.code) {
|
|
404
|
+
case protos_1.google.spanner.v1.TypeCode.BYTES:
|
|
405
|
+
case 'BYTES':
|
|
406
|
+
decoded = Buffer.from(decoded, 'base64');
|
|
407
|
+
break;
|
|
408
|
+
case protos_1.google.spanner.v1.TypeCode.PROTO:
|
|
409
|
+
case 'PROTO':
|
|
410
|
+
decoded = Buffer.from(decoded, 'base64');
|
|
411
|
+
decoded = new ProtoMessage({
|
|
412
|
+
value: decoded,
|
|
413
|
+
fullName: type.protoTypeFqn,
|
|
414
|
+
messageFunction: columnMetadata,
|
|
415
|
+
});
|
|
416
|
+
break;
|
|
417
|
+
case protos_1.google.spanner.v1.TypeCode.ENUM:
|
|
418
|
+
case 'ENUM':
|
|
419
|
+
decoded = new ProtoEnum({
|
|
420
|
+
value: decoded,
|
|
421
|
+
fullName: type.protoTypeFqn,
|
|
422
|
+
enumObject: columnMetadata,
|
|
423
|
+
});
|
|
424
|
+
break;
|
|
425
|
+
case protos_1.google.spanner.v1.TypeCode.FLOAT32:
|
|
426
|
+
case 'FLOAT32':
|
|
427
|
+
decoded = new Float32(decoded);
|
|
428
|
+
break;
|
|
429
|
+
case protos_1.google.spanner.v1.TypeCode.FLOAT64:
|
|
430
|
+
case 'FLOAT64':
|
|
431
|
+
decoded = new Float(decoded);
|
|
432
|
+
break;
|
|
433
|
+
case protos_1.google.spanner.v1.TypeCode.INT64:
|
|
434
|
+
case 'INT64':
|
|
435
|
+
if (type.typeAnnotation ===
|
|
436
|
+
protos_1.google.spanner.v1.TypeAnnotationCode.PG_OID ||
|
|
437
|
+
type.typeAnnotation === 'PG_OID') {
|
|
438
|
+
decoded = new PGOid(decoded);
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
decoded = new Int(decoded);
|
|
442
|
+
break;
|
|
443
|
+
case protos_1.google.spanner.v1.TypeCode.NUMERIC:
|
|
444
|
+
case 'NUMERIC':
|
|
445
|
+
if (type.typeAnnotation ===
|
|
446
|
+
protos_1.google.spanner.v1.TypeAnnotationCode.PG_NUMERIC ||
|
|
447
|
+
type.typeAnnotation === 'PG_NUMERIC') {
|
|
448
|
+
decoded = new PGNumeric(decoded);
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
decoded = new Numeric(decoded);
|
|
452
|
+
break;
|
|
453
|
+
case protos_1.google.spanner.v1.TypeCode.TIMESTAMP:
|
|
454
|
+
case 'TIMESTAMP':
|
|
455
|
+
decoded = new precise_date_1.PreciseDate(decoded);
|
|
456
|
+
break;
|
|
457
|
+
case protos_1.google.spanner.v1.TypeCode.DATE:
|
|
458
|
+
case 'DATE':
|
|
459
|
+
decoded = new SpannerDate(decoded);
|
|
460
|
+
break;
|
|
461
|
+
case protos_1.google.spanner.v1.TypeCode.JSON:
|
|
462
|
+
case 'JSON':
|
|
463
|
+
if (type.typeAnnotation ===
|
|
464
|
+
protos_1.google.spanner.v1.TypeAnnotationCode.PG_JSONB ||
|
|
465
|
+
type.typeAnnotation === 'PG_JSONB') {
|
|
466
|
+
decoded = new PGJsonb(decoded);
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
decoded = JSON.parse(decoded);
|
|
470
|
+
break;
|
|
471
|
+
case protos_1.google.spanner.v1.TypeCode.ARRAY:
|
|
472
|
+
case 'ARRAY':
|
|
473
|
+
decoded = decoded.map(value => {
|
|
474
|
+
return decode(value, type.arrayElementType, columnMetadata);
|
|
475
|
+
});
|
|
476
|
+
break;
|
|
477
|
+
case protos_1.google.spanner.v1.TypeCode.STRUCT:
|
|
478
|
+
case 'STRUCT':
|
|
479
|
+
fields = type.structType.fields.map(({ name, type }, index) => {
|
|
480
|
+
const value = decode((!Array.isArray(decoded) && decoded[name]) || decoded[index], type, columnMetadata);
|
|
481
|
+
return { name, value };
|
|
482
|
+
});
|
|
483
|
+
decoded = Struct.fromArray(fields);
|
|
484
|
+
break;
|
|
485
|
+
default:
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
return decoded;
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Encode a value in the format the API expects.
|
|
492
|
+
*
|
|
493
|
+
* @private
|
|
494
|
+
*
|
|
495
|
+
* @param {*} value The value to be encoded.
|
|
496
|
+
* @returns {object} google.protobuf.Value
|
|
497
|
+
*/
|
|
498
|
+
function encode(value) {
|
|
499
|
+
return service_1.GrpcService.encodeValue_(encodeValue(value));
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Formats values into expected format of google.protobuf.Value. The actual
|
|
503
|
+
* conversion to a google.protobuf.Value object happens via
|
|
504
|
+
* `Service.encodeValue_`
|
|
505
|
+
*
|
|
506
|
+
* @private
|
|
507
|
+
*
|
|
508
|
+
* @param {*} value The value to be encoded.
|
|
509
|
+
* @returns {*}
|
|
510
|
+
*/
|
|
511
|
+
function encodeValue(value) {
|
|
512
|
+
if (is.number(value) && !is.decimal(value)) {
|
|
513
|
+
return value.toString();
|
|
514
|
+
}
|
|
515
|
+
if (is.date(value)) {
|
|
516
|
+
return value.toJSON();
|
|
517
|
+
}
|
|
518
|
+
if (value instanceof WrappedNumber) {
|
|
519
|
+
return value.value;
|
|
520
|
+
}
|
|
521
|
+
if (value instanceof Numeric) {
|
|
522
|
+
return value.value;
|
|
523
|
+
}
|
|
524
|
+
if (value instanceof PGNumeric) {
|
|
525
|
+
return value.value;
|
|
526
|
+
}
|
|
527
|
+
if (Buffer.isBuffer(value)) {
|
|
528
|
+
return value.toString('base64');
|
|
529
|
+
}
|
|
530
|
+
if (value instanceof ProtoMessage) {
|
|
531
|
+
return value.value.toString('base64');
|
|
532
|
+
}
|
|
533
|
+
if (value instanceof ProtoEnum) {
|
|
534
|
+
return value.value;
|
|
535
|
+
}
|
|
536
|
+
if (value instanceof Struct) {
|
|
537
|
+
return Array.from(value).map(field => encodeValue(field.value));
|
|
538
|
+
}
|
|
539
|
+
if (is.array(value)) {
|
|
540
|
+
return value.map(encodeValue);
|
|
541
|
+
}
|
|
542
|
+
if (value instanceof PGJsonb) {
|
|
543
|
+
return value.toString();
|
|
544
|
+
}
|
|
545
|
+
if (is.object(value)) {
|
|
546
|
+
return JSON.stringify(value);
|
|
547
|
+
}
|
|
548
|
+
return value;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Just a map with friendlier names for the types.
|
|
552
|
+
*
|
|
553
|
+
* @private
|
|
554
|
+
* @enum {string}
|
|
555
|
+
*/
|
|
556
|
+
const TypeCode = {
|
|
557
|
+
unspecified: 'TYPE_CODE_UNSPECIFIED',
|
|
558
|
+
bool: 'BOOL',
|
|
559
|
+
int64: 'INT64',
|
|
560
|
+
pgOid: 'INT64',
|
|
561
|
+
float32: 'FLOAT32',
|
|
562
|
+
float64: 'FLOAT64',
|
|
563
|
+
numeric: 'NUMERIC',
|
|
564
|
+
pgNumeric: 'NUMERIC',
|
|
565
|
+
timestamp: 'TIMESTAMP',
|
|
566
|
+
date: 'DATE',
|
|
567
|
+
string: 'STRING',
|
|
568
|
+
bytes: 'BYTES',
|
|
569
|
+
json: 'JSON',
|
|
570
|
+
jsonb: 'JSON',
|
|
571
|
+
proto: 'PROTO',
|
|
572
|
+
enum: 'ENUM',
|
|
573
|
+
array: 'ARRAY',
|
|
574
|
+
struct: 'STRUCT',
|
|
575
|
+
};
|
|
576
|
+
/**
|
|
577
|
+
* @typedef {ParamType} StructField
|
|
578
|
+
* @property {string} name The name of the field.
|
|
579
|
+
*/
|
|
580
|
+
/**
|
|
581
|
+
* @typedef {object} ParamType
|
|
582
|
+
* @property {string} type The param type. Must be one of the following:
|
|
583
|
+
* - float32
|
|
584
|
+
* - float64
|
|
585
|
+
* - int64
|
|
586
|
+
* - numeric
|
|
587
|
+
* - bool
|
|
588
|
+
* - string
|
|
589
|
+
* - bytes
|
|
590
|
+
* - json
|
|
591
|
+
* - proto
|
|
592
|
+
* - enum
|
|
593
|
+
* - timestamp
|
|
594
|
+
* - date
|
|
595
|
+
* - struct
|
|
596
|
+
* - array
|
|
597
|
+
* @property {StructField[]} [fields] **For struct types only**. Type
|
|
598
|
+
* definitions for the individual fields.
|
|
599
|
+
* @property {string|ParamType} [child] **For array types only**. The array
|
|
600
|
+
* element type.
|
|
601
|
+
*/
|
|
602
|
+
/**
|
|
603
|
+
* Get the corresponding Spanner data type for the provided value.
|
|
604
|
+
*
|
|
605
|
+
* @private
|
|
606
|
+
*
|
|
607
|
+
* @param {*} value - The value.
|
|
608
|
+
* @returns {object}
|
|
609
|
+
*
|
|
610
|
+
* @example
|
|
611
|
+
* ```
|
|
612
|
+
* codec.getType(NaN);
|
|
613
|
+
* // {type: 'float64'}
|
|
614
|
+
* ```
|
|
615
|
+
*/
|
|
616
|
+
function getType(value) {
|
|
617
|
+
const isSpecialNumber = is.infinite(value) || (is.number(value) && isNaN(value));
|
|
618
|
+
if (value instanceof Float32) {
|
|
619
|
+
return { type: 'float32' };
|
|
620
|
+
}
|
|
621
|
+
if (is.decimal(value) || isSpecialNumber || value instanceof Float) {
|
|
622
|
+
return { type: 'float64' };
|
|
623
|
+
}
|
|
624
|
+
if (is.number(value) || value instanceof Int) {
|
|
625
|
+
return { type: 'int64' };
|
|
626
|
+
}
|
|
627
|
+
if (value instanceof Numeric) {
|
|
628
|
+
return { type: 'numeric' };
|
|
629
|
+
}
|
|
630
|
+
if (value instanceof PGNumeric) {
|
|
631
|
+
return { type: 'pgNumeric' };
|
|
632
|
+
}
|
|
633
|
+
if (value instanceof PGJsonb) {
|
|
634
|
+
return { type: 'pgJsonb' };
|
|
635
|
+
}
|
|
636
|
+
if (value instanceof PGOid) {
|
|
637
|
+
return { type: 'pgOid' };
|
|
638
|
+
}
|
|
639
|
+
if (value instanceof ProtoMessage) {
|
|
640
|
+
return { type: 'proto', fullName: value.fullName };
|
|
641
|
+
}
|
|
642
|
+
if (value instanceof ProtoEnum) {
|
|
643
|
+
return { type: 'enum', fullName: value.fullName };
|
|
644
|
+
}
|
|
645
|
+
if (is.boolean(value)) {
|
|
646
|
+
return { type: 'bool' };
|
|
647
|
+
}
|
|
648
|
+
if (is.string(value)) {
|
|
649
|
+
return { type: 'string' };
|
|
650
|
+
}
|
|
651
|
+
if (Buffer.isBuffer(value)) {
|
|
652
|
+
return { type: 'bytes' };
|
|
653
|
+
}
|
|
654
|
+
if (value instanceof SpannerDate) {
|
|
655
|
+
return { type: 'date' };
|
|
656
|
+
}
|
|
657
|
+
if (is.date(value)) {
|
|
658
|
+
return { type: 'timestamp' };
|
|
659
|
+
}
|
|
660
|
+
if (value instanceof Struct) {
|
|
661
|
+
return {
|
|
662
|
+
type: 'struct',
|
|
663
|
+
fields: Array.from(value).map(({ name, value }) => {
|
|
664
|
+
return Object.assign({ name }, getType(value));
|
|
665
|
+
}),
|
|
666
|
+
};
|
|
667
|
+
}
|
|
668
|
+
if (is.array(value)) {
|
|
669
|
+
let child;
|
|
670
|
+
for (let i = 0; i < value.length; i++) {
|
|
671
|
+
child = value[i];
|
|
672
|
+
if (!is.null(child)) {
|
|
673
|
+
break;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
return {
|
|
677
|
+
type: 'array',
|
|
678
|
+
child: getType(child),
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
if (is.object(value)) {
|
|
682
|
+
return { type: 'json' };
|
|
683
|
+
}
|
|
684
|
+
return { type: 'unspecified' };
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Converts a value to google.protobuf.ListValue
|
|
688
|
+
*
|
|
689
|
+
* @private
|
|
690
|
+
*
|
|
691
|
+
* @param {*} value The value to convert.
|
|
692
|
+
* @returns {object}
|
|
693
|
+
*/
|
|
694
|
+
function convertToListValue(value) {
|
|
695
|
+
const values = arrify(value).map(exports.codec.encode);
|
|
696
|
+
return { values };
|
|
697
|
+
}
|
|
698
|
+
/**
|
|
699
|
+
* Converts milliseconds to google.protobuf.Timestamp
|
|
700
|
+
*
|
|
701
|
+
* @private
|
|
702
|
+
*
|
|
703
|
+
* @param {number} ms The milliseconds to convert.
|
|
704
|
+
* @returns {object}
|
|
705
|
+
*/
|
|
706
|
+
function convertMsToProtoTimestamp(ms) {
|
|
707
|
+
const rawSeconds = ms / 1000;
|
|
708
|
+
const seconds = Math.floor(rawSeconds);
|
|
709
|
+
const nanos = Math.round((rawSeconds - seconds) * 1e9);
|
|
710
|
+
return { seconds, nanos };
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Converts google.protobuf.Timestamp to Date object.
|
|
714
|
+
*
|
|
715
|
+
* @private
|
|
716
|
+
*
|
|
717
|
+
* @param {object} timestamp The protobuf timestamp.
|
|
718
|
+
* @returns {Date}
|
|
719
|
+
*/
|
|
720
|
+
function convertProtoTimestampToDate({ nanos = 0, seconds = 0, }) {
|
|
721
|
+
const ms = Math.floor(nanos) / 1e6;
|
|
722
|
+
const s = Math.floor(seconds);
|
|
723
|
+
return new Date(s * 1000 + ms);
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Encodes paramTypes into correct structure.
|
|
727
|
+
*
|
|
728
|
+
* @private
|
|
729
|
+
*
|
|
730
|
+
* @param {object|string} [config='unspecified'] Type config.
|
|
731
|
+
* @return {object}
|
|
732
|
+
*/
|
|
733
|
+
function createTypeObject(friendlyType) {
|
|
734
|
+
if (!friendlyType) {
|
|
735
|
+
friendlyType = 'unspecified';
|
|
736
|
+
}
|
|
737
|
+
if (typeof friendlyType === 'string') {
|
|
738
|
+
friendlyType = { type: friendlyType };
|
|
739
|
+
}
|
|
740
|
+
const config = friendlyType;
|
|
741
|
+
const code = TypeCode[config.type] || TypeCode.unspecified;
|
|
742
|
+
const type = {
|
|
743
|
+
code,
|
|
744
|
+
};
|
|
745
|
+
if (code === 'PROTO' || code === 'ENUM') {
|
|
746
|
+
type.protoTypeFqn = config.fullName || '';
|
|
747
|
+
}
|
|
748
|
+
if (code === 'ARRAY') {
|
|
749
|
+
type.arrayElementType = exports.codec.createTypeObject(config.child);
|
|
750
|
+
}
|
|
751
|
+
if (code === 'STRUCT') {
|
|
752
|
+
type.structType = {
|
|
753
|
+
fields: arrify(config.fields).map(field => {
|
|
754
|
+
return { name: field.name, type: exports.codec.createTypeObject(field) };
|
|
755
|
+
}),
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
if (friendlyType.type === 'pgNumeric') {
|
|
759
|
+
type.typeAnnotation =
|
|
760
|
+
protos_1.google.spanner.v1.TypeAnnotationCode.PG_NUMERIC;
|
|
761
|
+
}
|
|
762
|
+
else if (friendlyType.type === 'jsonb') {
|
|
763
|
+
type.typeAnnotation = protos_1.google.spanner.v1.TypeAnnotationCode.PG_JSONB;
|
|
764
|
+
}
|
|
765
|
+
else if (friendlyType.type === 'pgOid') {
|
|
766
|
+
type.typeAnnotation = protos_1.google.spanner.v1.TypeAnnotationCode.PG_OID;
|
|
767
|
+
}
|
|
768
|
+
return type;
|
|
769
|
+
}
|
|
770
|
+
exports.codec = {
|
|
771
|
+
convertToListValue,
|
|
772
|
+
convertMsToProtoTimestamp,
|
|
773
|
+
convertProtoTimestampToDate,
|
|
774
|
+
createTypeObject,
|
|
775
|
+
SpannerDate,
|
|
776
|
+
Float32,
|
|
777
|
+
Float,
|
|
778
|
+
Int,
|
|
779
|
+
Numeric,
|
|
780
|
+
PGNumeric,
|
|
781
|
+
PGJsonb,
|
|
782
|
+
ProtoMessage,
|
|
783
|
+
ProtoEnum,
|
|
784
|
+
PGOid,
|
|
785
|
+
convertFieldsToJson,
|
|
786
|
+
decode,
|
|
787
|
+
encode,
|
|
788
|
+
getType,
|
|
789
|
+
Struct,
|
|
790
|
+
};
|
|
791
|
+
//# sourceMappingURL=codec.js.map
|