|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import {CallOptions, ServiceError} from 'google-gax'; |
| 16 | +import {promisifyAll} from '@google-cloud/promisify'; |
| 17 | + |
| 18 | +import {Datastore} from './'; |
| 19 | +import {google} from '../protos/protos'; |
| 20 | + |
| 21 | +export interface GenericIndexCallback<T> { |
| 22 | + ( |
| 23 | + err?: ServiceError | null, |
| 24 | + index?: Index | null, |
| 25 | + apiResponse?: T | null |
| 26 | + ): void; |
| 27 | +} |
| 28 | + |
| 29 | +export type GetIndexCallback = GenericIndexCallback<IIndex>; |
| 30 | +export type GetIndexResponse = [Index, IIndex]; |
| 31 | + |
| 32 | +export type IndexGetMetadataCallback = ( |
| 33 | + err?: ServiceError | null, |
| 34 | + metadata?: IIndex | null |
| 35 | +) => void; |
| 36 | +export type IndexGetMetadataResponse = [IIndex]; |
| 37 | + |
| 38 | +export interface GetIndexesOptions { |
| 39 | + filter?: string; |
| 40 | + gaxOptions?: CallOptions; |
| 41 | + pageSize?: number; |
| 42 | + pageToken?: string; |
| 43 | + autoPaginate?: boolean; |
| 44 | +} |
| 45 | +export type GetIndexesResponse = [ |
| 46 | + Index[], |
| 47 | + GetIndexesOptions, |
| 48 | + google.datastore.admin.v1.IListIndexesResponse |
| 49 | +]; |
| 50 | +export type GetIndexesCallback = ( |
| 51 | + err?: ServiceError | null, |
| 52 | + indexes?: Index[], |
| 53 | + nextQuery?: GetIndexesOptions, |
| 54 | + apiResponse?: google.datastore.admin.v1.IListIndexesResponse |
| 55 | +) => void; |
| 56 | + |
| 57 | +export type IIndex = google.datastore.admin.v1.IIndex; |
| 58 | + |
| 59 | +/** |
| 60 | + * @class |
| 61 | + * @param {Datastore} datastore The parent instance of this index. |
| 62 | + * @param {string} id The index name or id. |
| 63 | + * |
| 64 | + * @example |
| 65 | + * const {Datastore} = require('@google-cloud/datastore'); |
| 66 | + * const datastore = new Datastore(); |
| 67 | + * const index = datastore.index('my-index'); |
| 68 | + */ |
| 69 | +export class Index { |
| 70 | + datastore: Datastore; |
| 71 | + id: string; |
| 72 | + metadata?: IIndex; |
| 73 | + |
| 74 | + constructor(datastore: Datastore, id: string) { |
| 75 | + this.datastore = datastore; |
| 76 | + this.id = id.split('/').pop()!; |
| 77 | + } |
| 78 | + |
| 79 | + get(gaxOptions?: CallOptions): Promise<GetIndexResponse>; |
| 80 | + get(callback: GetIndexCallback): void; |
| 81 | + get(gaxOptions: CallOptions, callback: GetIndexCallback): void; |
| 82 | + /** |
| 83 | + * Get an index if it exists. |
| 84 | + * |
| 85 | + * @param {object} [gaxOptions] Request configuration options, outlined here: |
| 86 | + * https://googleapis.github.io/gax-nodejs/CallSettings.html. |
| 87 | + * @param {function} callback The callback function. |
| 88 | + * @param {?error} callback.err An error returned while making this request. |
| 89 | + * @param {Index} callback.index The Index instance. |
| 90 | + * @param {object} callback.apiResponse The full API response. |
| 91 | + */ |
| 92 | + get( |
| 93 | + gaxOptionsOrCallback?: CallOptions | GetIndexCallback, |
| 94 | + cb?: GetIndexCallback |
| 95 | + ): void | Promise<GetIndexResponse> { |
| 96 | + const gaxOpts = |
| 97 | + typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {}; |
| 98 | + const callback = |
| 99 | + typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; |
| 100 | + |
| 101 | + this.getMetadata(gaxOpts, (err, metadata) => { |
| 102 | + callback(err, err ? null : this, metadata); |
| 103 | + }); |
| 104 | + } |
| 105 | + |
| 106 | + getMetadata(gaxOptions?: CallOptions): Promise<IndexGetMetadataResponse>; |
| 107 | + getMetadata(callback: IndexGetMetadataCallback): void; |
| 108 | + getMetadata( |
| 109 | + gaxOptions: CallOptions, |
| 110 | + callback: IndexGetMetadataCallback |
| 111 | + ): void; |
| 112 | + /** |
| 113 | + * Get the metadata of this index. |
| 114 | + * |
| 115 | + * @param {object} [gaxOptions] Request configuration options, outlined here: |
| 116 | + * https://googleapis.github.io/gax-nodejs/CallSettings.html. |
| 117 | + * @param {function} callback The callback function. |
| 118 | + * @param {?error} callback.err An error returned while making this request. |
| 119 | + * @param {object} callback.metadata The metadata. |
| 120 | + */ |
| 121 | + getMetadata( |
| 122 | + gaxOptionsOrCallback?: CallOptions | IndexGetMetadataCallback, |
| 123 | + cb?: IndexGetMetadataCallback |
| 124 | + ): void | Promise<IndexGetMetadataResponse> { |
| 125 | + const gaxOpts = |
| 126 | + typeof gaxOptionsOrCallback === 'object' ? gaxOptionsOrCallback : {}; |
| 127 | + const callback = |
| 128 | + typeof gaxOptionsOrCallback === 'function' ? gaxOptionsOrCallback : cb!; |
| 129 | + |
| 130 | + this.datastore.request_( |
| 131 | + { |
| 132 | + client: 'DatastoreAdminClient', |
| 133 | + method: 'getIndex', |
| 134 | + reqOpts: { |
| 135 | + indexId: this.id, |
| 136 | + }, |
| 137 | + gaxOpts, |
| 138 | + }, |
| 139 | + (err, resp) => { |
| 140 | + if (resp) { |
| 141 | + this.metadata = resp; |
| 142 | + } |
| 143 | + callback(err as ServiceError, resp); |
| 144 | + } |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + // @TODO implement create() |
| 149 | + // @TODO implement delete() |
| 150 | + // @TODO implement exists() |
| 151 | +} |
| 152 | + |
| 153 | +/*! Developer Documentation |
| 154 | + * |
| 155 | + * All async methods (except for streams) will return a Promise in the event |
| 156 | + * that a callback is omitted. |
| 157 | + */ |
| 158 | +promisifyAll(Index); |
0 commit comments