Skip to content

Commit 24779b8

Browse files
feat: introduce import/export + Index class (#734)
1 parent a5dc56b commit 24779b8

8 files changed

Lines changed: 1598 additions & 40 deletions

File tree

handwritten/datastore/package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
],
2525
"scripts": {
2626
"clean": "gts clean",
27-
"compile": "tsc -p . && cp -r proto* build/",
27+
"compile": "tsc -p . && cp -r proto* build/ && cp -r system-test/data build/system-test",
2828
"compile-protos": "compileProtos src",
2929
"docs": "jsdoc -c .jsdoc.js",
3030
"predocs-test": "npm run docs",
@@ -49,34 +49,40 @@
4949
"extend": "^3.0.2",
5050
"google-gax": "^2.2.0",
5151
"is": "^3.3.0",
52+
"pumpify": "^2.0.1",
5253
"split-array-stream": "^2.0.0",
5354
"stream-events": "^1.0.5"
5455
},
5556
"devDependencies": {
57+
"@google-cloud/storage": "^5.3.0",
58+
"@microsoft/api-documenter": "^7.8.10",
59+
"@microsoft/api-extractor": "^7.8.10",
5660
"@types/extend": "^3.0.1",
5761
"@types/is": "0.0.21",
62+
"@types/js-yaml": "^3.12.5",
5863
"@types/mocha": "^8.0.0",
5964
"@types/node": "^13.9.0",
6065
"@types/proxyquire": "^1.3.28",
66+
"@types/pumpify": "^1.4.1",
6167
"@types/sinon": "^9.0.0",
6268
"c8": "^7.1.0",
6369
"codecov": "^3.6.5",
6470
"gts": "^2.0.0",
71+
"js-yaml": "^3.14.0",
6572
"jsdoc": "^3.6.3",
6673
"jsdoc-fresh": "^1.0.2",
6774
"jsdoc-region-tag": "^1.0.4",
6875
"linkinator": "^2.0.3",
6976
"mocha": "^8.0.0",
7077
"null-loader": "^4.0.0",
78+
"p-queue": "^6.6.1",
7179
"pack-n-play": "^1.0.0-2",
7280
"proxyquire": "^2.1.3",
7381
"sinon": "^9.0.1",
7482
"ts-loader": "^8.0.0",
7583
"typescript": "^3.8.3",
7684
"webpack": "^4.42.0",
77-
"webpack-cli": "^3.3.11",
78-
"@microsoft/api-documenter": "^7.8.10",
79-
"@microsoft/api-extractor": "^7.8.10"
85+
"webpack-cli": "^3.3.11"
8086
},
8187
"engines": {
8288
"node": ">=10"
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)