@google-cloud/spanner 6.2.0 → 6.4.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.
@@ -18,10 +18,11 @@ import { GrpcService } from './common-grpc/service';
18
18
  import { PreciseDate } from '@google-cloud/precise-date';
19
19
  import { GoogleAuth, GoogleAuthOptions } from 'google-auth-library';
20
20
  import { common as p } from 'protobufjs';
21
- import { Float, Int, Numeric, PGNumeric, Struct } from './codec';
21
+ import { Float, Int, Numeric, PGNumeric, PGJsonb, Struct } from './codec';
22
22
  import { Backup } from './backup';
23
23
  import { Database } from './database';
24
24
  import { Instance, CreateInstanceCallback, CreateInstanceResponse } from './instance';
25
+ import { InstanceConfig, CreateInstanceConfigCallback, CreateInstanceConfigResponse } from './instance-config';
25
26
  import { grpc, GrpcClientOptions, CallOptions } from 'google-gax';
26
27
  import { google, google as instanceAdmin } from '../protos/protos';
27
28
  import { PagedOptions, PagedResponse, PagedCallback, PagedOptionsWithFilter, NormalCallback } from './common';
@@ -42,6 +43,9 @@ export interface GetInstanceConfigOptions {
42
43
  }
43
44
  export declare type GetInstanceConfigResponse = [IInstanceConfig];
44
45
  export declare type GetInstanceConfigCallback = NormalCallback<IInstanceConfig>;
46
+ export declare type GetInstanceConfigOperationsOptions = PagedOptionsWithFilter;
47
+ export declare type GetInstanceConfigOperationsResponse = PagedResponse<IOperation, instanceAdmin.spanner.admin.instance.v1.IListInstanceConfigOperationsResponse>;
48
+ export declare type GetInstanceConfigOperationsCallback = PagedCallback<IOperation, instanceAdmin.spanner.admin.instance.v1.IListInstanceConfigOperationsResponse>;
45
49
  export interface SpannerOptions extends GrpcClientOptions {
46
50
  apiEndpoint?: string;
47
51
  servicePath?: string;
@@ -67,6 +71,17 @@ export interface CreateInstanceRequest {
67
71
  } | null;
68
72
  gaxOptions?: CallOptions;
69
73
  }
74
+ export interface CreateInstanceConfigRequest {
75
+ displayName?: string;
76
+ replicas?: google.spanner.admin.instance.v1.IReplicaInfo[];
77
+ baseConfig?: string;
78
+ labels?: {
79
+ [k: string]: string;
80
+ } | null;
81
+ etag?: string;
82
+ validateOnly?: boolean;
83
+ gaxOptions?: CallOptions;
84
+ }
70
85
  /**
71
86
  * Translates enum values to string keys.
72
87
  *
@@ -133,6 +148,7 @@ declare class Spanner extends GrpcService {
133
148
  auth: GoogleAuth;
134
149
  clients_: Map<string, {}>;
135
150
  instances_: Map<string, Instance>;
151
+ instanceConfigs_: Map<string, InstanceConfig>;
136
152
  projectIdReplaced_: boolean;
137
153
  projectFormattedName_: string;
138
154
  resourceHeader_: {
@@ -374,6 +390,106 @@ declare class Spanner extends GrpcService {
374
390
  * ```
375
391
  */
376
392
  getInstancesStream(options?: GetInstancesOptions): NodeJS.ReadableStream;
393
+ /**
394
+ * Config for the new instance config.
395
+ *
396
+ * @typedef {object} CreateInstanceConfigRequest
397
+ * @property {string} [displayName] The name of this instance configuration as
398
+ * it appears in the user interface.
399
+ * @property {google.spanner.admin.instance.v1.IReplicaInfo[]} [replicas] The
400
+ * geographic placement of nodes in this instance configuration and their
401
+ * replication properties.
402
+ * @property {string} [baseConfig] Base configuration name,
403
+ * e.g. projects/<project_name>/instanceConfigs/nam3 based on which this
404
+ * configuration is created.
405
+ * @property {Object.<string, string>} [labels] Cloud Labels are a flexible
406
+ * and lightweight mechanism for organizing cloud resources into groups
407
+ * that reflect a customer's organizational needs and deployment
408
+ * strategies. Cloud Labels can be used to filter collections of
409
+ * resources. They can be used to control how resource metrics are
410
+ * aggregated. And they can be used as arguments to policy management
411
+ * rules (e.g. route, firewall, load balancing, etc.).
412
+ * @property {string} [etag] etag is used for optimistic concurrency control
413
+ * as a way to help prevent simultaneous updates of a instance config from
414
+ * overwriting each other.
415
+ * @property {boolean} [validateOnly] An option to validate, but not actually
416
+ * execute, a request, and provide the same response.
417
+ */
418
+ /**
419
+ * @typedef {array} CreateInstanceConfigResponse
420
+ * @property {InstanceConfig} 0 The new {@link google.spanner.admin.instance.v1.InstanceConfig}.
421
+ * @property {google.longrunning.Operation} 1 An operation object that can be
422
+ * used to check the status of the request.
423
+ * @property {google.longrunning.IOperation} 2 The full API response.
424
+ */
425
+ /**
426
+ * @callback CreateInstanceConfigCallback
427
+ * @param {?Error} err Request error, if any.
428
+ * @param {InstanceConfig} instanceConfig The new {@link google.spanner.admin.instance.v1.InstanceConfig}.
429
+ * @param {google.longrunning.Operation} operation An operation object that
430
+ * can be used to check the status of the request.
431
+ * @param {google.longrunning.IOperation} apiResponse The full API response.
432
+ */
433
+ /**
434
+ * Create an instance config.
435
+ *
436
+ * Wrapper around {@link v1.InstanceAdminClient#createInstanceConfig}.
437
+ *
438
+ * @see {@link v1.InstanceAdminClient#createInstanceConfig}
439
+ * @see [CreateInstanceConfig API Documentation](https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#google.spanner.admin.instance.v1.InstanceAdmin.CreateInstanceConfig)
440
+ *
441
+ * @throws {GoogleError} If a name is not provided.
442
+ * @throws {GoogleError} If a configuration object is not provided.
443
+ * @throws {GoogleError} If a base config is not provided in the configuration
444
+ * object.
445
+ *
446
+ * @param {string} name The name of the instance config to be created.
447
+ * @param {CreateInstanceConfigRequest} config Configuration object.
448
+ * @param {CreateInstanceConfigCallback} [callback] Callback function.
449
+ * @returns {Promise<CreateInstanceConfigResponse>}
450
+ *
451
+ * @example
452
+ * ```
453
+ * const {Spanner} = require('@google-cloud/spanner');
454
+ * const spanner = new Spanner();
455
+ *
456
+ * const [baseInstanceConfig] =
457
+ * await spanner.getInstanceConfig(baseInstanceConfigId);
458
+ * const config = {
459
+ * baseConfig: baseInstanceConfig.name,
460
+ * replicas: baseInstanceConfig.replicas.concat(baseInstanceConfig.optionalReplicas[0])
461
+ * };
462
+ *
463
+ * function callback(err, instance, operation, apiResponse) {
464
+ * if (err) {
465
+ * // Error handling omitted.
466
+ * }
467
+ *
468
+ * operation
469
+ * .on('error', function(err) {})
470
+ * .on('complete', function() {
471
+ * // Instance created successfully.
472
+ * });
473
+ * }
474
+ *
475
+ * spanner.createInstanceConfig('custom-new-instance-config', config, callback);
476
+ *
477
+ * //-
478
+ * // If the callback is omitted, we'll return a Promise.
479
+ * //-
480
+ * spanner.createInstanceConfig('custom-new-instance-config', config)
481
+ * .then(function(data) {
482
+ * const instanceConfig = data[0];
483
+ * const operation = data[1];
484
+ * return operation.promise();
485
+ * })
486
+ * .then(function() {
487
+ * // Instance config created successfully.
488
+ * });
489
+ * ```
490
+ */
491
+ createInstanceConfig(name: string, config: CreateInstanceConfigRequest): Promise<CreateInstanceConfigResponse>;
492
+ createInstanceConfig(name: string, config: CreateInstanceConfigRequest, callback: CreateInstanceConfigCallback): void;
377
493
  /**
378
494
  * Lists the supported instance configurations for a given project.
379
495
  *
@@ -555,6 +671,72 @@ declare class Spanner extends GrpcService {
555
671
  getInstanceConfig(name: string, options: GetInstanceConfigOptions): Promise<GetInstanceConfigResponse>;
556
672
  getInstanceConfig(name: string, callback: GetInstanceConfigCallback): void;
557
673
  getInstanceConfig(name: string, options: GetInstanceConfigOptions, callback: GetInstanceConfigCallback): void;
674
+ /**
675
+ * Query object for listing instance config operations.
676
+ *
677
+ * @typedef {object} GetInstanceConfigOperationsOptions
678
+ * @property {string} [filter] An expression for filtering the results of the
679
+ * request. Filter can be configured as outlined in
680
+ * {@link v1.DatabaseAdminClient#listInstanceConfigOperations}.
681
+ * @property {number} [pageSize] Maximum number of results per page.
682
+ * @property {string} [pageToken] A previously-returned page token
683
+ * representing part of the larger set of results to view.
684
+ * @property {object} [gaxOptions] Request configuration options,
685
+ * See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
686
+ * for more details.
687
+ */
688
+ /**
689
+ * @typedef {array} GetInstanceConfigOperationsResponse
690
+ * @property {google.longrunning.IOperation[]} 0 Array of {@link IOperation} instances.
691
+ * @property {object} 1 A query object to receive more results.
692
+ * @property {object} 2 The full API response.
693
+ */
694
+ /**
695
+ * @callback GetInstanceConfigOperationsCallback
696
+ * @param {?Error} err Request error, if any.
697
+ * @param {google.longrunning.IOperation[]} 0 Array of {@link IOperation} instances.
698
+ * @param {object} nextQuery A query object to receive more results.
699
+ * @param {object} apiResponse The full API response.
700
+ */
701
+ /**
702
+ * List pending and completed instance config operations.
703
+ *
704
+ * @see {@link #listOperations}
705
+ *
706
+ * @param {GetInstanceConfigOperationsOptions} [options] The query object for
707
+ * listing InstanceConfig operations.
708
+ * @param {gax.CallOptions} [options.gaxOptions] The request configuration
709
+ * options, See {@link https://googleapis.dev/nodejs/google-gax/latest/interfaces/CallOptions.html|CallOptions}
710
+ * for more details.
711
+ * @returns {Promise<GetInstanceConfigOperationsResponse>} When resolved,
712
+ * contains a paged list of InstanceConfig operations.
713
+ *
714
+ * @example
715
+ * ```
716
+ * const {Spanner} = require('@google-cloud/spanner');
717
+ * const spanner = new Spanner();
718
+ * const [operations] = await spanner.getInstanceConfigOperations();
719
+ *
720
+ * //-
721
+ * // To manually handle pagination, set autoPaginate:false in gaxOptions.
722
+ * //-
723
+ * let pageToken = undefined;
724
+ * do {
725
+ * const [operations, , response] = await spanner.getInstanceConfigOperations({
726
+ * pageSize: 3,
727
+ * pageToken,
728
+ * gaxOptions: {autoPaginate: false},
729
+ * });
730
+ * operations.forEach(operation => {
731
+ * // Do something with operation
732
+ * });
733
+ * pageToken = response.nextPageToken;
734
+ * } while (pageToken);
735
+ * ```
736
+ */
737
+ getInstanceConfigOperations(options?: GetInstanceConfigOperationsOptions): Promise<GetInstanceConfigOperationsResponse>;
738
+ getInstanceConfigOperations(callback: GetInstanceConfigOperationsCallback): void;
739
+ getInstanceConfigOperations(options: GetInstanceConfigOperationsOptions, callback: GetInstanceConfigOperationsCallback): void;
558
740
  /**
559
741
  * Get a reference to an Instance object.
560
742
  *
@@ -571,6 +753,22 @@ declare class Spanner extends GrpcService {
571
753
  * ```
572
754
  */
573
755
  instance(name: string): Instance;
756
+ /**
757
+ * Get a reference to an InstanceConfig object.
758
+ *
759
+ * @throws {GoogleError} If a name is not provided.
760
+ *
761
+ * @param {string} name The name of the instance config.
762
+ * @returns {InstanceConfig} An InstanceConfig object.
763
+ *
764
+ * @example
765
+ * ```
766
+ * const {Spanner} = require('@google-cloud/spanner');
767
+ * const spanner = new Spanner();
768
+ * const instanceConfig = spanner.instanceConfig('my-instance-config');
769
+ * ```
770
+ */
771
+ instanceConfig(name: string): InstanceConfig;
574
772
  /**
575
773
  * Prepare a gapic request. This will cache the GAX client and replace
576
774
  * {{projectId}} placeholders, if necessary.
@@ -699,6 +897,24 @@ declare class Spanner extends GrpcService {
699
897
  * ```
700
898
  */
701
899
  static pgNumeric(value: any): PGNumeric;
900
+ /**
901
+ * Helper function to get a Cloud Spanner pgJsonb object.
902
+ *
903
+ * @param {object|string} value The pgJsonb value as a string or object.
904
+ * @returns {PGJsonb}
905
+ *
906
+ * @example
907
+ * ```
908
+ * const {Spanner} = require('@google-cloud/spanner');
909
+ * const pgJsonb1 = Spanner.pgJsonb({rating: 6});
910
+ * const pgJsonb2 = Spanner.pgJsonb(`[
911
+ * {
912
+ * "name": null,
913
+ * "open": true
914
+ * }]`)
915
+ * ```
916
+ */
917
+ static pgJsonb(value: any): PGJsonb;
702
918
  /**
703
919
  * Helper function to get a Cloud Spanner Struct object.
704
920
  *
@@ -766,6 +982,14 @@ export { Spanner };
766
982
  * @type {Constructor}
767
983
  */
768
984
  export { Instance };
985
+ /**
986
+ * {@link InstanceConfig} class.
987
+ *
988
+ * @name Spanner.InstanceConfig
989
+ * @see InstanceConfig
990
+ * @type {Constructor}
991
+ */
992
+ export { InstanceConfig };
769
993
  /**
770
994
  * {@link Database} class.
771
995
  *
@@ -15,7 +15,7 @@
15
15
  * limitations under the License.
16
16
  */
17
17
  Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.protos = exports.v1 = exports.Transaction = exports.Snapshot = exports.PartitionedDml = exports.Table = exports.SessionPool = exports.Session = exports.Backup = exports.Database = exports.Instance = exports.Spanner = void 0;
18
+ exports.protos = exports.v1 = exports.Transaction = exports.Snapshot = exports.PartitionedDml = exports.Table = exports.SessionPool = exports.Session = exports.Backup = exports.Database = exports.InstanceConfig = exports.Instance = exports.Spanner = void 0;
19
19
  const service_1 = require("./common-grpc/service");
20
20
  const precise_date_1 = require("@google-cloud/precise-date");
21
21
  const projectify_1 = require("@google-cloud/projectify");
@@ -32,6 +32,8 @@ const database_1 = require("./database");
32
32
  Object.defineProperty(exports, "Database", { enumerable: true, get: function () { return database_1.Database; } });
33
33
  const instance_1 = require("./instance");
34
34
  Object.defineProperty(exports, "Instance", { enumerable: true, get: function () { return instance_1.Instance; } });
35
+ const instance_config_1 = require("./instance-config");
36
+ Object.defineProperty(exports, "InstanceConfig", { enumerable: true, get: function () { return instance_config_1.InstanceConfig; } });
35
37
  const google_gax_1 = require("google-gax");
36
38
  const protos_1 = require("../protos/protos");
37
39
  const common_1 = require("./common");
@@ -143,6 +145,7 @@ class Spanner extends service_1.GrpcService {
143
145
  this.auth = new google_auth_library_1.GoogleAuth(this.options);
144
146
  this.clients_ = new Map();
145
147
  this.instances_ = new Map();
148
+ this.instanceConfigs_ = new Map();
146
149
  this.projectIdReplaced_ = false;
147
150
  this.projectFormattedName_ = 'projects/' + this.projectId;
148
151
  this.resourceHeader_ = {
@@ -337,6 +340,53 @@ class Spanner extends service_1.GrpcService {
337
340
  headers: this.resourceHeader_,
338
341
  });
339
342
  }
343
+ createInstanceConfig(name, config, callback) {
344
+ if (!name) {
345
+ throw new google_gax_1.GoogleError('A name is required to create an instance config.');
346
+ }
347
+ if (!config) {
348
+ throw new google_gax_1.GoogleError([
349
+ 'A configuration object is required to create an instance config.',
350
+ ].join(''));
351
+ }
352
+ if (!config.baseConfig) {
353
+ throw new google_gax_1.GoogleError(['Base instance config is required to create an instance config.'].join(''));
354
+ }
355
+ const formattedName = instance_config_1.InstanceConfig.formatName_(this.projectId, name);
356
+ const displayName = config.displayName || formattedName.split('/').pop();
357
+ const reqOpts = {
358
+ parent: this.projectFormattedName_,
359
+ instanceConfigId: formattedName.split('/').pop(),
360
+ instanceConfig: extend({
361
+ name: formattedName,
362
+ displayName,
363
+ }, config),
364
+ validateOnly: config.validateOnly,
365
+ };
366
+ if (config.baseConfig.indexOf('/') === -1) {
367
+ reqOpts.instanceConfig.baseConfig = `projects/${this.projectId}/instanceConfigs/${config.baseConfig}`;
368
+ }
369
+ // validateOnly need not be passed in if it is null.
370
+ if (reqOpts.validateOnly === null || reqOpts.validateOnly === undefined)
371
+ delete reqOpts.validateOnly;
372
+ // validateOnly and gaxOptions are not fields in InstanceConfig.
373
+ delete reqOpts.instanceConfig.validateOnly;
374
+ delete reqOpts.instanceConfig.gaxOptions;
375
+ this.request({
376
+ client: 'InstanceAdminClient',
377
+ method: 'createInstanceConfig',
378
+ reqOpts,
379
+ gaxOpts: config.gaxOptions,
380
+ headers: this.resourceHeader_,
381
+ }, (err, operation, resp) => {
382
+ if (err) {
383
+ callback(err, null, null, resp);
384
+ return;
385
+ }
386
+ const instanceConfig = this.instanceConfig(formattedName);
387
+ callback(null, instanceConfig, operation, resp);
388
+ });
389
+ }
340
390
  getInstanceConfigs(optionsOrCallback, cb) {
341
391
  const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
342
392
  const options = typeof optionsOrCallback === 'object'
@@ -448,6 +498,39 @@ class Spanner extends service_1.GrpcService {
448
498
  callback(err, instanceConfig);
449
499
  });
450
500
  }
501
+ getInstanceConfigOperations(optionsOrCallback, cb) {
502
+ const callback = typeof optionsOrCallback === 'function' ? optionsOrCallback : cb;
503
+ const options = typeof optionsOrCallback === 'object'
504
+ ? optionsOrCallback
505
+ : {};
506
+ const gaxOpts = extend(true, {}, options.gaxOptions);
507
+ let reqOpts = extend({}, options, {
508
+ parent: this.projectFormattedName_,
509
+ });
510
+ delete reqOpts.gaxOptions;
511
+ // Copy over pageSize and pageToken values from gaxOptions.
512
+ // However, values set on options take precedence.
513
+ if (gaxOpts) {
514
+ reqOpts = extend({}, {
515
+ pageSize: gaxOpts.pageSize,
516
+ pageToken: gaxOpts.pageToken,
517
+ }, reqOpts);
518
+ delete gaxOpts.pageSize;
519
+ delete gaxOpts.pageToken;
520
+ }
521
+ this.request({
522
+ client: 'InstanceAdminClient',
523
+ method: 'listInstanceConfigOperations',
524
+ reqOpts,
525
+ gaxOpts,
526
+ headers: this.resourceHeader_,
527
+ }, (err, operations, nextPageRequest, ...args) => {
528
+ const nextQuery = nextPageRequest
529
+ ? extend({}, options, nextPageRequest)
530
+ : null;
531
+ callback(err, operations, nextQuery, ...args);
532
+ });
533
+ }
451
534
  /**
452
535
  * Get a reference to an Instance object.
453
536
  *
@@ -473,6 +556,31 @@ class Spanner extends service_1.GrpcService {
473
556
  }
474
557
  return this.instances_.get(key);
475
558
  }
559
+ /**
560
+ * Get a reference to an InstanceConfig object.
561
+ *
562
+ * @throws {GoogleError} If a name is not provided.
563
+ *
564
+ * @param {string} name The name of the instance config.
565
+ * @returns {InstanceConfig} An InstanceConfig object.
566
+ *
567
+ * @example
568
+ * ```
569
+ * const {Spanner} = require('@google-cloud/spanner');
570
+ * const spanner = new Spanner();
571
+ * const instanceConfig = spanner.instanceConfig('my-instance-config');
572
+ * ```
573
+ */
574
+ instanceConfig(name) {
575
+ if (!name) {
576
+ throw new google_gax_1.GoogleError('A name is required to access an InstanceConfig object.');
577
+ }
578
+ const key = name.split('/').pop();
579
+ if (!this.instanceConfigs_.has(key)) {
580
+ this.instanceConfigs_.set(key, new instance_config_1.InstanceConfig(this, name));
581
+ }
582
+ return this.instanceConfigs_.get(key);
583
+ }
476
584
  /**
477
585
  * Prepare a gapic request. This will cache the GAX client and replace
478
586
  * {{projectId}} placeholders, if necessary.
@@ -721,6 +829,26 @@ class Spanner extends service_1.GrpcService {
721
829
  static pgNumeric(value) {
722
830
  return new codec_1.codec.PGNumeric(value);
723
831
  }
832
+ /**
833
+ * Helper function to get a Cloud Spanner pgJsonb object.
834
+ *
835
+ * @param {object|string} value The pgJsonb value as a string or object.
836
+ * @returns {PGJsonb}
837
+ *
838
+ * @example
839
+ * ```
840
+ * const {Spanner} = require('@google-cloud/spanner');
841
+ * const pgJsonb1 = Spanner.pgJsonb({rating: 6});
842
+ * const pgJsonb2 = Spanner.pgJsonb(`[
843
+ * {
844
+ * "name": null,
845
+ * "open": true
846
+ * }]`)
847
+ * ```
848
+ */
849
+ static pgJsonb(value) {
850
+ return new codec_1.codec.PGJsonb(value);
851
+ }
724
852
  /**
725
853
  * Helper function to get a Cloud Spanner Struct object.
726
854
  *
@@ -764,9 +892,11 @@ Spanner.GOOGLE_STANDARD_SQL = protos_1.google.spanner.admin.database.v1.Database
764
892
  'date',
765
893
  'float',
766
894
  'instance',
895
+ 'instanceConfig',
767
896
  'int',
768
897
  'numeric',
769
898
  'pgNumeric',
899
+ 'pgJsonb',
770
900
  'operation',
771
901
  'timestamp',
772
902
  ],