@google-cloud/spanner 7.18.0 → 7.19.0
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 +30 -0
- package/README.md +1 -0
- package/build/protos/google/spanner/admin/database/v1/backup.proto +773 -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 +1279 -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 +622 -0
- package/build/protos/google/spanner/v1/type.proto +214 -0
- package/build/protos/protos.d.ts +39869 -0
- package/build/protos/protos.js +101659 -0
- package/build/protos/protos.json +10391 -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 +1660 -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 +135 -0
- package/build/src/multiplexed-session.js +194 -0
- package/build/src/partial-result-stream.d.ts +253 -0
- package/build/src/partial-result-stream.js +400 -0
- package/build/src/request_id_header.d.ts +41 -0
- package/build/src/request_id_header.js +138 -0
- package/build/src/session-factory.d.ts +115 -0
- package/build/src/session-factory.js +106 -0
- package/build/src/session-pool.d.ts +471 -0
- package/build/src/session-pool.js +765 -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 +2003 -0
- package/build/src/v1/database_admin_client.d.ts +2332 -0
- package/build/src/v1/database_admin_client.js +2364 -0
- package/build/src/v1/database_admin_client_config.json +165 -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,781 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*!
|
|
3
|
+
* Copyright 2016 Google Inc. All Rights Reserved.
|
|
4
|
+
*
|
|
5
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License.
|
|
7
|
+
* You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.Instance = void 0;
|
|
19
|
+
const arrify = require("arrify");
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
21
|
+
const common = require('./common-grpc/service-object');
|
|
22
|
+
const promisify_1 = require("@google-cloud/promisify");
|
|
23
|
+
const extend = require("extend");
|
|
24
|
+
const snakeCase = require("lodash.snakecase");
|
|
25
|
+
const database_1 = require("./database");
|
|
26
|
+
const common_1 = require("./common");
|
|
27
|
+
const google_gax_1 = require("google-gax");
|
|
28
|
+
const backup_1 = require("./backup");
|
|
29
|
+
const protos_1 = require("../protos/protos");
|
|
30
|
+
/**
|
|
31
|
+
* The {@link Instance} class represents a [Cloud Spanner
|
|
32
|
+
* instance](https://cloud.google.com/spanner/docs/instances).
|
|
33
|
+
*
|
|
34
|
+
* Create an `Instance` object to interact with a Cloud Spanner instance.
|
|
35
|
+
*
|
|
36
|
+
* @class
|
|
37
|
+
*
|
|
38
|
+
* @param {Spanner} spanner {@link Spanner} instance.
|
|
39
|
+
* @param {string} name Name of the instance.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```
|
|
43
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
44
|
+
* const spanner = new Spanner();
|
|
45
|
+
* const instance = spanner.instance('my-instance');
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
class Instance extends common.GrpcServiceObject {
|
|
49
|
+
constructor(spanner, name) {
|
|
50
|
+
var _a;
|
|
51
|
+
const formattedName_ = Instance.formatName_(spanner.projectId, name);
|
|
52
|
+
const methods = {
|
|
53
|
+
/**
|
|
54
|
+
* Create an instance.
|
|
55
|
+
*
|
|
56
|
+
* Wrapper around {@link v1.InstanceAdminClient#createInstance}.
|
|
57
|
+
*
|
|
58
|
+
* @see {@link v1.InstanceAdminClient#createInstance}
|
|
59
|
+
* @see [CreateInstance API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.CreateInstance)
|
|
60
|
+
*
|
|
61
|
+
* @method Instance#create
|
|
62
|
+
* @param {CreateInstanceRequest} config Configuration object.
|
|
63
|
+
* @param {CreateInstanceCallback} [callback] Callback function.
|
|
64
|
+
* @returns {Promise<CreateInstanceResponse>}
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```
|
|
68
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
69
|
+
* const spanner = new Spanner();
|
|
70
|
+
*
|
|
71
|
+
* const instance = spanner.instance('my-instance');
|
|
72
|
+
*
|
|
73
|
+
* instance.create(function(err, instance, operation, apiResponse) {
|
|
74
|
+
* if (err) {
|
|
75
|
+
* // Error handling omitted.
|
|
76
|
+
* }
|
|
77
|
+
*
|
|
78
|
+
* operation
|
|
79
|
+
* .on('error', function(err) {})
|
|
80
|
+
* .on('complete', function() {
|
|
81
|
+
* // Instance created successfully.
|
|
82
|
+
* });
|
|
83
|
+
* });
|
|
84
|
+
*
|
|
85
|
+
* //-
|
|
86
|
+
* // If the callback is omitted, we'll return a Promise.
|
|
87
|
+
* //-
|
|
88
|
+
* instance.create()
|
|
89
|
+
* .then(function(data) {
|
|
90
|
+
* const operation = data[0];
|
|
91
|
+
* const apiResponse = data[1];
|
|
92
|
+
*
|
|
93
|
+
* return operation.promise();
|
|
94
|
+
* })
|
|
95
|
+
* .then(function() {
|
|
96
|
+
* // Instance created successfully.
|
|
97
|
+
* });
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
create: true,
|
|
101
|
+
};
|
|
102
|
+
super({
|
|
103
|
+
parent: spanner,
|
|
104
|
+
/**
|
|
105
|
+
* @name Instance#id
|
|
106
|
+
* @type {string}
|
|
107
|
+
*/
|
|
108
|
+
id: name,
|
|
109
|
+
methods,
|
|
110
|
+
createMethod(_, options, callback) {
|
|
111
|
+
spanner.createInstance(formattedName_, options, callback);
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
this.formattedName_ = formattedName_;
|
|
115
|
+
this.request = spanner.request.bind(spanner);
|
|
116
|
+
this.requestStream = spanner.requestStream.bind(spanner);
|
|
117
|
+
this.databases_ = new Map();
|
|
118
|
+
this._observabilityOptions = spanner._observabilityOptions;
|
|
119
|
+
this.commonHeaders_ = (0, common_1.getCommonHeaders)(this.formattedName_, (_a = this._observabilityOptions) === null || _a === void 0 ? void 0 : _a.enableEndToEndTracing);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Get a reference to a Backup object.
|
|
123
|
+
*
|
|
124
|
+
* @throws {GoogleError} If any parameter is not provided.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} backupId The name of the backup.
|
|
127
|
+
* @return {Backup} A Backup object.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```
|
|
131
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
132
|
+
* const spanner = new Spanner();
|
|
133
|
+
* const instance = spanner.instance('my-instance');
|
|
134
|
+
* const backup = instance.backup('my-backup');
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
137
|
+
backup(backupId) {
|
|
138
|
+
if (!backupId) {
|
|
139
|
+
throw new google_gax_1.GoogleError('A backup ID is required to create a Backup.');
|
|
140
|
+
}
|
|
141
|
+
return new backup_1.Backup(this, backupId);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Get a reference to a Backup object.
|
|
145
|
+
*
|
|
146
|
+
* @throws {GoogleError} If any parameter is not provided.
|
|
147
|
+
*
|
|
148
|
+
* @typedef {object} CopyBackupOptions
|
|
149
|
+
* * @property {string|null}
|
|
150
|
+
* * sourceBackup The full path of the backup to be copied
|
|
151
|
+
* * @property {string|number|google.protobuf.Timestamp|external:PreciseDate}
|
|
152
|
+
* * expireTime The expire time of the backup.
|
|
153
|
+
* * @property {google.spanner.admin.database.v1.ICopyBackupEncryptionConfig}
|
|
154
|
+
* * encryptionConfig An encryption configuration describing the
|
|
155
|
+
* * encryption type and key resources in Cloud KMS to be used to encrypt
|
|
156
|
+
* * the copy backup.
|
|
157
|
+
* * @property {object} [gaxOptions] The request configuration options,
|
|
158
|
+
* * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
|
|
159
|
+
* * for more details.
|
|
160
|
+
* */
|
|
161
|
+
/**
|
|
162
|
+
* @callback CopyBackupCallback
|
|
163
|
+
* @param {string} sourceBackupId Full path of the source backup to be copied.
|
|
164
|
+
* @param {string} backupId The name of the backup.
|
|
165
|
+
* @param {CopyBackupOptions}
|
|
166
|
+
* @return {Backup} A Backup object.
|
|
167
|
+
*
|
|
168
|
+
* @example
|
|
169
|
+
* ```
|
|
170
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
171
|
+
* const spanner = new Spanner();
|
|
172
|
+
* const instance = spanner.instance('my-instance');
|
|
173
|
+
* const backup = instance.copyBackup('my-source-backup','my-backup',{
|
|
174
|
+
* expireTime: expireTime,
|
|
175
|
+
* encryptionConfig: {
|
|
176
|
+
* encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION',
|
|
177
|
+
* kmsKeyName: 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key',
|
|
178
|
+
* },);
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
copyBackup(sourceBackupId, backupId, options, callback) {
|
|
182
|
+
if (!backupId || !sourceBackupId) {
|
|
183
|
+
throw new google_gax_1.GoogleError('A backup ID and source backup ID is required to create a copy of the source backup.');
|
|
184
|
+
}
|
|
185
|
+
const copyOfBackup = new backup_1.Backup(this, backupId, sourceBackupId);
|
|
186
|
+
if (callback) {
|
|
187
|
+
return copyOfBackup.create(options, callback);
|
|
188
|
+
}
|
|
189
|
+
return copyOfBackup.create(options);
|
|
190
|
+
}
|
|
191
|
+
getBackups(optionsOrCallback, cb) {
|
|
192
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
193
|
+
const options = typeof optionsOrCallback === 'object'
|
|
194
|
+
? optionsOrCallback
|
|
195
|
+
: {};
|
|
196
|
+
const gaxOpts = extend(true, {}, options.gaxOptions);
|
|
197
|
+
let reqOpts = extend({}, options, {
|
|
198
|
+
parent: this.formattedName_,
|
|
199
|
+
});
|
|
200
|
+
delete reqOpts.gaxOptions;
|
|
201
|
+
// Copy over pageSize and pageToken values from gaxOptions.
|
|
202
|
+
// However values set on options take precedence.
|
|
203
|
+
if (gaxOpts) {
|
|
204
|
+
reqOpts = extend({}, {
|
|
205
|
+
pageSize: gaxOpts.pageSize,
|
|
206
|
+
pageToken: gaxOpts.pageToken,
|
|
207
|
+
}, reqOpts);
|
|
208
|
+
delete gaxOpts.pageSize;
|
|
209
|
+
delete gaxOpts.pageToken;
|
|
210
|
+
}
|
|
211
|
+
this.request({
|
|
212
|
+
client: 'DatabaseAdminClient',
|
|
213
|
+
method: 'listBackups',
|
|
214
|
+
reqOpts,
|
|
215
|
+
gaxOpts,
|
|
216
|
+
headers: this.commonHeaders_,
|
|
217
|
+
}, (err, backups, nextPageRequest, ...args) => {
|
|
218
|
+
let backupInstances = null;
|
|
219
|
+
if (backups) {
|
|
220
|
+
backupInstances = backups.map(backup => {
|
|
221
|
+
const backupInstance = this.backup(backup.name);
|
|
222
|
+
backupInstance.metadata = backup;
|
|
223
|
+
return backupInstance;
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
const nextQuery = nextPageRequest
|
|
227
|
+
? extend({}, options, nextPageRequest)
|
|
228
|
+
: null;
|
|
229
|
+
callback(err, backupInstances, nextQuery, ...args);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Get a list of backups as a readable object stream.
|
|
234
|
+
*
|
|
235
|
+
* Wrapper around {@link v1.DatabaseAdminClient#listBackups}.
|
|
236
|
+
*
|
|
237
|
+
* @see {@link v1.DatabaseAdminClient#listBackups}
|
|
238
|
+
* @see [ListBackups API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.ListBackups)
|
|
239
|
+
*
|
|
240
|
+
* @method Spanner#getBackupsStream
|
|
241
|
+
* @param {GetBackupOptions} [options] Query object for listing backups.
|
|
242
|
+
* @returns {ReadableStream} A readable stream that emits {@link Backup}
|
|
243
|
+
* instances.
|
|
244
|
+
*
|
|
245
|
+
* @example
|
|
246
|
+
* ```
|
|
247
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
248
|
+
* const spanner = new Spanner();
|
|
249
|
+
*
|
|
250
|
+
* const instance = spanner.instance('my-instance');
|
|
251
|
+
*
|
|
252
|
+
* instance.getBackupsStream()
|
|
253
|
+
* .on('error', console.error)
|
|
254
|
+
* .on('data', function(database) {
|
|
255
|
+
* // `backups` is a `Backup` object.
|
|
256
|
+
* })
|
|
257
|
+
* .on('end', function() {
|
|
258
|
+
* // All backups retrieved.
|
|
259
|
+
* });
|
|
260
|
+
*
|
|
261
|
+
* //-
|
|
262
|
+
* // If you anticipate many results, you can end a stream early to prevent
|
|
263
|
+
* // unnecessary processing and API requests.
|
|
264
|
+
* //-
|
|
265
|
+
* instance.getBackupsStream()
|
|
266
|
+
* .on('data', function(database) {
|
|
267
|
+
* this.end();
|
|
268
|
+
* });
|
|
269
|
+
* ```
|
|
270
|
+
*/
|
|
271
|
+
getBackupsStream(options = {}) {
|
|
272
|
+
const gaxOpts = extend(true, {}, options.gaxOptions);
|
|
273
|
+
let reqOpts = extend({}, options, {
|
|
274
|
+
parent: this.formattedName_,
|
|
275
|
+
});
|
|
276
|
+
delete reqOpts.gaxOptions;
|
|
277
|
+
// Copy over pageSize and pageToken values from gaxOptions.
|
|
278
|
+
// However values set on options take precedence.
|
|
279
|
+
if (gaxOpts) {
|
|
280
|
+
reqOpts = extend({}, {
|
|
281
|
+
pageSize: gaxOpts.pageSize,
|
|
282
|
+
pageToken: gaxOpts.pageToken,
|
|
283
|
+
}, reqOpts);
|
|
284
|
+
delete gaxOpts.pageSize;
|
|
285
|
+
delete gaxOpts.pageToken;
|
|
286
|
+
}
|
|
287
|
+
return this.requestStream({
|
|
288
|
+
client: 'DatabaseAdminClient',
|
|
289
|
+
method: 'listBackupsStream',
|
|
290
|
+
reqOpts,
|
|
291
|
+
gaxOpts,
|
|
292
|
+
headers: this.commonHeaders_,
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* List pending and completed backup operations for all databases in the instance.
|
|
297
|
+
*
|
|
298
|
+
* @see {@link #listOperations}
|
|
299
|
+
*
|
|
300
|
+
* @param {GetBackupOperationsOptions} [options] The query object for listing
|
|
301
|
+
* backup operations.
|
|
302
|
+
* @param {gax.CallOptions} [options.gaxOptions] The request configuration
|
|
303
|
+
* options, See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
|
|
304
|
+
* for more details.
|
|
305
|
+
* @returns {Promise<GetBackupOperationsResponse>} When resolved, contains a
|
|
306
|
+
* paged list of backup operations.
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```
|
|
310
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
311
|
+
* const spanner = new Spanner();
|
|
312
|
+
* const instance = spanner.instance('my-instance');
|
|
313
|
+
* const [operations] = await instance.getBackupOperations();
|
|
314
|
+
*
|
|
315
|
+
* //-
|
|
316
|
+
* // To manually handle pagination, set autoPaginate:false in gaxOptions.
|
|
317
|
+
* //-
|
|
318
|
+
* let pageToken = undefined;
|
|
319
|
+
* do {
|
|
320
|
+
* const [operations, , response] = await instance.getBackupOperations({
|
|
321
|
+
* pageSize: 3,
|
|
322
|
+
* pageToken,
|
|
323
|
+
* gaxOptions: {autoPaginate: false},
|
|
324
|
+
* });
|
|
325
|
+
* operations.forEach(operation => {
|
|
326
|
+
* // Do something with operation
|
|
327
|
+
* });
|
|
328
|
+
* pageToken = response.nextPageToken;
|
|
329
|
+
* } while (pageToken);
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
getBackupOperations(optionsOrCallback, cb) {
|
|
333
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
334
|
+
const options = typeof optionsOrCallback === 'object'
|
|
335
|
+
? optionsOrCallback
|
|
336
|
+
: {};
|
|
337
|
+
const gaxOpts = extend(true, {}, options.gaxOptions);
|
|
338
|
+
let reqOpts = extend({}, options, {
|
|
339
|
+
parent: this.formattedName_,
|
|
340
|
+
});
|
|
341
|
+
delete reqOpts.gaxOptions;
|
|
342
|
+
// Copy over pageSize and pageToken values from gaxOptions.
|
|
343
|
+
// However values set on options take precedence.
|
|
344
|
+
if (gaxOpts) {
|
|
345
|
+
reqOpts = extend({}, {
|
|
346
|
+
pageSize: gaxOpts.pageSize,
|
|
347
|
+
pageToken: gaxOpts.pageToken,
|
|
348
|
+
}, reqOpts);
|
|
349
|
+
delete gaxOpts.pageSize;
|
|
350
|
+
delete gaxOpts.pageToken;
|
|
351
|
+
}
|
|
352
|
+
this.request({
|
|
353
|
+
client: 'DatabaseAdminClient',
|
|
354
|
+
method: 'listBackupOperations',
|
|
355
|
+
reqOpts,
|
|
356
|
+
gaxOpts,
|
|
357
|
+
headers: this.commonHeaders_,
|
|
358
|
+
}, (err, operations, nextPageRequest, ...args) => {
|
|
359
|
+
const nextQuery = nextPageRequest
|
|
360
|
+
? extend({}, options, nextPageRequest)
|
|
361
|
+
: null;
|
|
362
|
+
callback(err, operations, nextQuery, ...args);
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* List pending and completed operations for all databases in the instance.
|
|
367
|
+
*
|
|
368
|
+
* @see {@link Database.getDatabaseOperations}
|
|
369
|
+
*
|
|
370
|
+
* @param {GetDatabaseOperationsOptions} [options] The query object for
|
|
371
|
+
* listing database operations.
|
|
372
|
+
* @param {gax.CallOptions} [options.gaxOptions] The request configuration
|
|
373
|
+
* options, See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
|
|
374
|
+
* for more details.
|
|
375
|
+
* @returns {Promise<GetDatabaseOperationsResponse>} When resolved, contains a
|
|
376
|
+
* paged list of database operations.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* ```
|
|
380
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
381
|
+
* const spanner = new Spanner();
|
|
382
|
+
* const instance = spanner.instance('my-instance');
|
|
383
|
+
* const [operations] = await instance.getDatabaseOperations();
|
|
384
|
+
* // ... then do something with the operations
|
|
385
|
+
*
|
|
386
|
+
* //-
|
|
387
|
+
* // To manually handle pagination, set autoPaginate:false in gaxOptions.
|
|
388
|
+
* //-
|
|
389
|
+
* let pageToken = undefined;
|
|
390
|
+
* do {
|
|
391
|
+
* const [operations, , response] = await instance.getDatabaseOperations({
|
|
392
|
+
* pageSize: 3,
|
|
393
|
+
* pageToken,
|
|
394
|
+
* gaxOptions: {autoPaginate: false},
|
|
395
|
+
* });
|
|
396
|
+
* operations.forEach(operation => {
|
|
397
|
+
* // Do something with operation
|
|
398
|
+
* });
|
|
399
|
+
* pageToken = response.nextPageToken;
|
|
400
|
+
* } while (pageToken);
|
|
401
|
+
* ```
|
|
402
|
+
*/
|
|
403
|
+
getDatabaseOperations(optionsOrCallback, cb) {
|
|
404
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
405
|
+
const options = typeof optionsOrCallback === 'object'
|
|
406
|
+
? optionsOrCallback
|
|
407
|
+
: {};
|
|
408
|
+
const gaxOpts = extend(true, {}, options.gaxOptions);
|
|
409
|
+
let reqOpts = extend({}, options, {
|
|
410
|
+
parent: this.formattedName_,
|
|
411
|
+
});
|
|
412
|
+
delete reqOpts.gaxOptions;
|
|
413
|
+
// Copy over pageSize and pageToken values from gaxOptions.
|
|
414
|
+
// However values set on options take precedence.
|
|
415
|
+
if (gaxOpts) {
|
|
416
|
+
reqOpts = extend({}, {
|
|
417
|
+
pageSize: gaxOpts.pageSize,
|
|
418
|
+
pageToken: gaxOpts.pageToken,
|
|
419
|
+
}, reqOpts);
|
|
420
|
+
delete gaxOpts.pageSize;
|
|
421
|
+
delete gaxOpts.pageToken;
|
|
422
|
+
}
|
|
423
|
+
this.request({
|
|
424
|
+
client: 'DatabaseAdminClient',
|
|
425
|
+
method: 'listDatabaseOperations',
|
|
426
|
+
reqOpts,
|
|
427
|
+
gaxOpts,
|
|
428
|
+
headers: this.commonHeaders_,
|
|
429
|
+
}, (err, operations, nextPageRequest, ...args) => {
|
|
430
|
+
const nextQuery = nextPageRequest
|
|
431
|
+
? extend({}, options, nextPageRequest)
|
|
432
|
+
: null;
|
|
433
|
+
callback(err, operations, nextQuery, ...args);
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
createDatabase(name, optionsOrCallback, cb) {
|
|
437
|
+
if (!name) {
|
|
438
|
+
throw new google_gax_1.GoogleError('A name is required to create a database.');
|
|
439
|
+
}
|
|
440
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
441
|
+
const options = typeof optionsOrCallback === 'object'
|
|
442
|
+
? optionsOrCallback
|
|
443
|
+
: {};
|
|
444
|
+
const poolOptions = options.poolOptions;
|
|
445
|
+
const poolCtor = options.poolCtor;
|
|
446
|
+
let createStatement = 'CREATE DATABASE `' + name.split('/').pop() + '`';
|
|
447
|
+
if (protos_1.google.spanner.admin.database.v1.DatabaseDialect.POSTGRESQL ===
|
|
448
|
+
options.databaseDialect) {
|
|
449
|
+
createStatement = 'CREATE DATABASE "' + name.split('/').pop() + '"';
|
|
450
|
+
}
|
|
451
|
+
const reqOpts = extend({
|
|
452
|
+
parent: this.formattedName_,
|
|
453
|
+
createStatement: createStatement,
|
|
454
|
+
}, options);
|
|
455
|
+
delete reqOpts.poolOptions;
|
|
456
|
+
delete reqOpts.poolCtor;
|
|
457
|
+
delete reqOpts.gaxOptions;
|
|
458
|
+
if (reqOpts.schema) {
|
|
459
|
+
reqOpts.extraStatements = arrify(reqOpts.schema);
|
|
460
|
+
delete reqOpts.schema;
|
|
461
|
+
}
|
|
462
|
+
this.request({
|
|
463
|
+
client: 'DatabaseAdminClient',
|
|
464
|
+
method: 'createDatabase',
|
|
465
|
+
reqOpts,
|
|
466
|
+
gaxOpts: options.gaxOptions,
|
|
467
|
+
headers: this.commonHeaders_,
|
|
468
|
+
}, (err, operation, resp) => {
|
|
469
|
+
if (err) {
|
|
470
|
+
callback(err, null, null, resp);
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
const database = this.database(name, poolOptions || poolCtor);
|
|
474
|
+
database._observabilityOptions = this._observabilityOptions;
|
|
475
|
+
callback(null, database, operation, resp);
|
|
476
|
+
});
|
|
477
|
+
}
|
|
478
|
+
/**
|
|
479
|
+
* Get a reference to a Database object.
|
|
480
|
+
*
|
|
481
|
+
* @throws {GoogleError} If a name is not provided.
|
|
482
|
+
*
|
|
483
|
+
* @param {string} name The name of the instance.
|
|
484
|
+
* @param {SessionPoolOptions|SessionPoolCtor} [poolOptions] Session pool
|
|
485
|
+
* configuration options.
|
|
486
|
+
* @param {spannerClient.spanner.v1.ExecuteSqlRequest.IQueryOptions} [queryOptions]
|
|
487
|
+
* Default query options to use with the database. These options will be
|
|
488
|
+
* overridden by any query options set in environment variables or that
|
|
489
|
+
* are specified on a per-query basis.
|
|
490
|
+
* @return {Database} A Database object.
|
|
491
|
+
*
|
|
492
|
+
* @example
|
|
493
|
+
* ```
|
|
494
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
495
|
+
* const spanner = new Spanner();
|
|
496
|
+
*
|
|
497
|
+
* const instance = spanner.instance('my-instance');
|
|
498
|
+
* const database = instance.database('my-database');
|
|
499
|
+
* ```
|
|
500
|
+
*/
|
|
501
|
+
database(name, poolOptions, queryOptions) {
|
|
502
|
+
if (!name) {
|
|
503
|
+
throw new google_gax_1.GoogleError('A name is required to access a Database object.');
|
|
504
|
+
}
|
|
505
|
+
// Only add an additional key for SessionPoolOptions and QueryOptions if an
|
|
506
|
+
// options object with at least one value was passed in.
|
|
507
|
+
let optionsKey = poolOptions && Object.keys(poolOptions).length > 0
|
|
508
|
+
? '/' + JSON.stringify(Object.entries(poolOptions).sort())
|
|
509
|
+
: '';
|
|
510
|
+
if (queryOptions && Object.keys(queryOptions).length > 0) {
|
|
511
|
+
optionsKey =
|
|
512
|
+
optionsKey + '/' + JSON.stringify(Object.entries(queryOptions).sort());
|
|
513
|
+
}
|
|
514
|
+
const key = name.split('/').pop() + optionsKey;
|
|
515
|
+
if (!this.databases_.has(key)) {
|
|
516
|
+
const db = new database_1.Database(this, name, poolOptions, queryOptions);
|
|
517
|
+
db._observabilityOptions = this._observabilityOptions;
|
|
518
|
+
this.databases_.set(key, db);
|
|
519
|
+
}
|
|
520
|
+
return this.databases_.get(key);
|
|
521
|
+
}
|
|
522
|
+
delete(optionsOrCallback, cb) {
|
|
523
|
+
const gaxOpts = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
|
524
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
525
|
+
const reqOpts = {
|
|
526
|
+
name: this.formattedName_,
|
|
527
|
+
};
|
|
528
|
+
Promise.all(Array.from(this.databases_.values()).map(database => {
|
|
529
|
+
return database.close();
|
|
530
|
+
}))
|
|
531
|
+
.catch(() => { })
|
|
532
|
+
.then(() => {
|
|
533
|
+
this.databases_.clear();
|
|
534
|
+
this.request({
|
|
535
|
+
client: 'InstanceAdminClient',
|
|
536
|
+
method: 'deleteInstance',
|
|
537
|
+
reqOpts,
|
|
538
|
+
gaxOpts,
|
|
539
|
+
headers: this.commonHeaders_,
|
|
540
|
+
}, (err, resp) => {
|
|
541
|
+
if (!err) {
|
|
542
|
+
this.parent.instances_.delete(this.id);
|
|
543
|
+
}
|
|
544
|
+
callback(err, resp);
|
|
545
|
+
});
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
exists(optionsOrCallback, cb) {
|
|
549
|
+
const gaxOptions = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
|
550
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
551
|
+
const NOT_FOUND = 5;
|
|
552
|
+
this.getMetadata({ gaxOptions }, err => {
|
|
553
|
+
if (err && err.code !== NOT_FOUND) {
|
|
554
|
+
callback(err, null);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
const exists = !err || err.code !== NOT_FOUND;
|
|
558
|
+
callback(null, exists);
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
get(optionsOrCallback, cb) {
|
|
562
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
563
|
+
const options = typeof optionsOrCallback === 'object'
|
|
564
|
+
? optionsOrCallback
|
|
565
|
+
: {};
|
|
566
|
+
const getMetadataOptions = new Object(null);
|
|
567
|
+
if (options.fieldNames) {
|
|
568
|
+
getMetadataOptions.fieldNames = options.fieldNames;
|
|
569
|
+
}
|
|
570
|
+
if (options.gaxOptions) {
|
|
571
|
+
getMetadataOptions.gaxOptions = options.gaxOptions;
|
|
572
|
+
}
|
|
573
|
+
this.getMetadata(getMetadataOptions, (err, metadata) => {
|
|
574
|
+
if (err) {
|
|
575
|
+
if (err.code === 5 && options.autoCreate) {
|
|
576
|
+
const createOptions = extend(true, {}, options);
|
|
577
|
+
delete createOptions.fieldNames;
|
|
578
|
+
delete createOptions.autoCreate;
|
|
579
|
+
this.create(createOptions, (err, instance, operation) => {
|
|
580
|
+
if (err) {
|
|
581
|
+
callback(err);
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
operation
|
|
585
|
+
.on('error', callback)
|
|
586
|
+
.on('complete', (metadata) => {
|
|
587
|
+
this.metadata = metadata;
|
|
588
|
+
callback(null, this, metadata);
|
|
589
|
+
});
|
|
590
|
+
});
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
callback(err);
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
callback(null, this, metadata);
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
getDatabases(optionsOrCallback, cb) {
|
|
600
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
601
|
+
const self = this;
|
|
602
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
603
|
+
const options = typeof optionsOrCallback === 'object'
|
|
604
|
+
? optionsOrCallback
|
|
605
|
+
: {};
|
|
606
|
+
const gaxOpts = extend(true, {}, options.gaxOptions);
|
|
607
|
+
let reqOpts = extend({}, options, {
|
|
608
|
+
parent: this.formattedName_,
|
|
609
|
+
});
|
|
610
|
+
delete reqOpts.gaxOptions;
|
|
611
|
+
// Copy over pageSize and pageToken values from gaxOptions.
|
|
612
|
+
// However values set on options take precedence.
|
|
613
|
+
if (gaxOpts) {
|
|
614
|
+
reqOpts = extend({}, {
|
|
615
|
+
pageSize: gaxOpts.pageSize,
|
|
616
|
+
pageToken: gaxOpts.pageToken,
|
|
617
|
+
}, reqOpts);
|
|
618
|
+
delete gaxOpts.pageSize;
|
|
619
|
+
delete gaxOpts.pageToken;
|
|
620
|
+
}
|
|
621
|
+
this.request({
|
|
622
|
+
client: 'DatabaseAdminClient',
|
|
623
|
+
method: 'listDatabases',
|
|
624
|
+
reqOpts,
|
|
625
|
+
gaxOpts,
|
|
626
|
+
headers: this.commonHeaders_,
|
|
627
|
+
}, (err, rowDatabases, nextPageRequest, ...args) => {
|
|
628
|
+
let databases = null;
|
|
629
|
+
if (rowDatabases) {
|
|
630
|
+
databases = rowDatabases.map(database => {
|
|
631
|
+
const databaseInstance = self.database(database.name, { min: 0 });
|
|
632
|
+
databaseInstance.metadata = database;
|
|
633
|
+
databaseInstance._observabilityOptions = this._observabilityOptions;
|
|
634
|
+
return databaseInstance;
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
const nextQuery = nextPageRequest
|
|
638
|
+
? extend({}, options, nextPageRequest)
|
|
639
|
+
: null;
|
|
640
|
+
callback(err, databases, nextQuery, ...args);
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Get a list of databases as a readable object stream.
|
|
645
|
+
*
|
|
646
|
+
* Wrapper around {@link v1.DatabaseAdminClient#listDatabases}.
|
|
647
|
+
*
|
|
648
|
+
* @see {@link v1.DatabaseAdminClient#listDatabases}
|
|
649
|
+
* @see [ListDatabases API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.database.v1#google.spanner.admin.database.v1.DatabaseAdmin.ListDatabases)
|
|
650
|
+
*
|
|
651
|
+
* @method Spanner#getDatabasesStream
|
|
652
|
+
* @param {GetDatabasesOptions} [options] Query object for listing databases.
|
|
653
|
+
* @returns {ReadableStream} A readable stream that emits {@link Database}
|
|
654
|
+
* instances.
|
|
655
|
+
*
|
|
656
|
+
* @example
|
|
657
|
+
* ```
|
|
658
|
+
* const {Spanner} = require('@google-cloud/spanner');
|
|
659
|
+
* const spanner = new Spanner();
|
|
660
|
+
*
|
|
661
|
+
* const instance = spanner.instance('my-instance');
|
|
662
|
+
*
|
|
663
|
+
* instance.getDatabasesStream()
|
|
664
|
+
* .on('error', console.error)
|
|
665
|
+
* .on('data', function(database) {
|
|
666
|
+
* // `database` is a `Database` object.
|
|
667
|
+
* })
|
|
668
|
+
* .on('end', function() {
|
|
669
|
+
* // All databases retrieved.
|
|
670
|
+
* });
|
|
671
|
+
*
|
|
672
|
+
* //-
|
|
673
|
+
* // If you anticipate many results, you can end a stream early to prevent
|
|
674
|
+
* // unnecessary processing and API requests.
|
|
675
|
+
* //-
|
|
676
|
+
* instance.getDatabasesStream()
|
|
677
|
+
* .on('data', function(database) {
|
|
678
|
+
* this.end();
|
|
679
|
+
* });
|
|
680
|
+
* ```
|
|
681
|
+
*/
|
|
682
|
+
getDatabasesStream(options = {}) {
|
|
683
|
+
const gaxOpts = extend(true, {}, options.gaxOptions);
|
|
684
|
+
let reqOpts = extend({}, options, {
|
|
685
|
+
parent: this.formattedName_,
|
|
686
|
+
});
|
|
687
|
+
delete reqOpts.gaxOptions;
|
|
688
|
+
// Copy over pageSize and pageToken values from gaxOptions.
|
|
689
|
+
// However values set on options take precedence.
|
|
690
|
+
if (gaxOpts) {
|
|
691
|
+
reqOpts = extend({}, {
|
|
692
|
+
pageSize: gaxOpts.pageSize,
|
|
693
|
+
pageToken: gaxOpts.pageToken,
|
|
694
|
+
}, reqOpts);
|
|
695
|
+
delete gaxOpts.pageSize;
|
|
696
|
+
delete gaxOpts.pageToken;
|
|
697
|
+
}
|
|
698
|
+
return this.requestStream({
|
|
699
|
+
client: 'DatabaseAdminClient',
|
|
700
|
+
method: 'listDatabasesStream',
|
|
701
|
+
reqOpts,
|
|
702
|
+
gaxOpts,
|
|
703
|
+
headers: this.commonHeaders_,
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
getMetadata(optionsOrCallback, cb) {
|
|
707
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
708
|
+
const options = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
|
709
|
+
const reqOpts = {
|
|
710
|
+
name: this.formattedName_,
|
|
711
|
+
};
|
|
712
|
+
if (options.fieldNames) {
|
|
713
|
+
reqOpts['fieldMask'] = {
|
|
714
|
+
paths: arrify(options['fieldNames']).map(snakeCase),
|
|
715
|
+
};
|
|
716
|
+
}
|
|
717
|
+
return this.request({
|
|
718
|
+
client: 'InstanceAdminClient',
|
|
719
|
+
method: 'getInstance',
|
|
720
|
+
reqOpts,
|
|
721
|
+
gaxOpts: options.gaxOptions,
|
|
722
|
+
headers: this.commonHeaders_,
|
|
723
|
+
}, (err, resp) => {
|
|
724
|
+
if (resp) {
|
|
725
|
+
this.metadata = resp;
|
|
726
|
+
}
|
|
727
|
+
callback(err, resp);
|
|
728
|
+
});
|
|
729
|
+
}
|
|
730
|
+
setMetadata(metadata, optionsOrCallback, cb) {
|
|
731
|
+
const gaxOpts = typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
|
|
732
|
+
const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
|
|
733
|
+
const reqOpts = {
|
|
734
|
+
instance: extend({
|
|
735
|
+
name: this.formattedName_,
|
|
736
|
+
}, metadata),
|
|
737
|
+
fieldMask: {
|
|
738
|
+
paths: Object.keys(metadata).map(snakeCase),
|
|
739
|
+
},
|
|
740
|
+
};
|
|
741
|
+
return this.request({
|
|
742
|
+
client: 'InstanceAdminClient',
|
|
743
|
+
method: 'updateInstance',
|
|
744
|
+
reqOpts,
|
|
745
|
+
gaxOpts,
|
|
746
|
+
headers: this.commonHeaders_,
|
|
747
|
+
}, callback);
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Format the instance name to include the project ID.
|
|
751
|
+
*
|
|
752
|
+
* @private
|
|
753
|
+
*
|
|
754
|
+
* @param {string} projectId The project ID.
|
|
755
|
+
* @param {string} name The instance name.
|
|
756
|
+
* @returns {string}
|
|
757
|
+
*
|
|
758
|
+
* @example
|
|
759
|
+
* ```
|
|
760
|
+
* Instance.formatName_('grape-spaceship-123', 'my-instance');
|
|
761
|
+
* // 'projects/grape-spaceship-123/instances/my-instance'
|
|
762
|
+
* ```
|
|
763
|
+
*/
|
|
764
|
+
static formatName_(projectId, name) {
|
|
765
|
+
if (name.indexOf('/') > -1) {
|
|
766
|
+
return name;
|
|
767
|
+
}
|
|
768
|
+
const instanceName = name.split('/').pop();
|
|
769
|
+
return 'projects/' + projectId + '/instances/' + instanceName;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
exports.Instance = Instance;
|
|
773
|
+
/*! Developer Documentation
|
|
774
|
+
*
|
|
775
|
+
* All async methods (except for streams) will return a Promise in the event
|
|
776
|
+
* that a callback is omitted.
|
|
777
|
+
*/
|
|
778
|
+
(0, promisify_1.promisifyAll)(Instance, {
|
|
779
|
+
exclude: ['database', 'backup'],
|
|
780
|
+
});
|
|
781
|
+
//# sourceMappingURL=instance.js.map
|