UNPKG

8.29 kBTypeScriptView Raw
1/*!
2 * Copyright 2014 Google LLC.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/// <reference types="node" />
17import { google } from '../protos/protos';
18import { CallOptions } from 'google-gax';
19import { Duplex, Transform } from 'stream';
20export interface AbortableDuplex extends Duplex {
21 abort(): void;
22}
23import { entity, Entity, EntityProto, Entities } from './entity';
24import { Query, QueryProto, RunQueryOptions, RunQueryResponse, RunQueryCallback } from './query';
25import { Datastore } from '.';
26/**
27 * Handle logic for Datastore API operations. Handles request logic for
28 * Datastore.
29 *
30 * Creates requests to the Datastore endpoint. Designed to be inherited by
31 * the {@link Datastore} and {@link Transaction} classes.
32 *
33 * @class
34 */
35declare class DatastoreRequest {
36 id: string | undefined;
37 requests_: Entity | {
38 mutations: Array<{}>;
39 };
40 requestCallbacks_: Array<(err: Error | null, resp: Entity | null) => void> | Entity;
41 datastore: Datastore;
42 [key: string]: Entity;
43 /**
44 * Format a user's input to mutation methods. This will create a deep clone of
45 * the input, as well as allow users to pass an object in the format of an
46 * entity.
47 *
48 * Both of the following formats can be supplied supported:
49 *
50 * datastore.save({
51 * key: datastore.key('Kind'),
52 * data: { foo: 'bar' }
53 * }, (err) => {})
54 *
55 * const entity = { foo: 'bar' }
56 * entity[datastore.KEY] = datastore.key('Kind')
57 * datastore.save(entity, (err) => {})
58 *
59 * @private
60 *
61 * @see [#1803]{@link https://github.com/GoogleCloudPlatform/google-cloud-node/issues/1803}
62 *
63 * @param {object} obj The user's input object.
64 */
65 static prepareEntityObject_(obj: Entity): PrepareEntityObjectResponse;
66 allocateIds(key: entity.Key, options: AllocateIdsOptions | number): Promise<AllocateIdsResponse>;
67 allocateIds(key: entity.Key, options: AllocateIdsOptions | number, callback: AllocateIdsCallback): void;
68 /**
69 * Retrieve the entities as a readable object stream.
70 *
71 * @throws {Error} If at least one Key object is not provided.
72 *
73 * @param {Key|Key[]} keys Datastore key object(s).
74 * @param {object} [options] Optional configuration. See {@link Datastore#get}
75 * for a complete list of options.
76 *
77 * @example
78 * const keys = [
79 * datastore.key(['Company', 123]),
80 * datastore.key(['Product', 'Computer'])
81 * ];
82 *
83 * datastore.createReadStream(keys)
84 * .on('error', (err) => {})
85 * .on('data', (entity) => {
86 * // entity is an entity object.
87 * })
88 * .on('end', () => {
89 * // All entities retrieved.
90 * });
91 */
92 createReadStream(keys: Entities, options?: CreateReadStreamOptions): Transform;
93 delete(keys: Entities, gaxOptions?: CallOptions): Promise<DeleteResponse>;
94 delete(keys: Entities, callback: DeleteCallback): void;
95 delete(keys: Entities, gaxOptions: CallOptions, callback: DeleteCallback): void;
96 get(keys: entity.Key | entity.Key[], options?: CreateReadStreamOptions): Promise<GetResponse>;
97 get(keys: entity.Key | entity.Key[], callback: GetCallback): void;
98 get(keys: entity.Key | entity.Key[], options: CreateReadStreamOptions, callback: GetCallback): void;
99 runQuery(query: Query, options?: RunQueryOptions): Promise<RunQueryResponse>;
100 runQuery(query: Query, options: RunQueryOptions, callback: RunQueryCallback): void;
101 runQuery(query: Query, callback: RunQueryCallback): void;
102 /**
103 * Get a list of entities as a readable object stream.
104 *
105 * See {@link Datastore#runQuery} for a list of all available options.
106 *
107 * @param {Query} query Query object.
108 * @param {object} [options] Optional configuration.
109 * @param {object} [options.gaxOptions] Request configuration options, outlined
110 * here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
111 *
112 * @example
113 * datastore.runQueryStream(query)
114 * .on('error', console.error)
115 * .on('data', (entity) => {
116 * // Access the Key object for this entity.
117 * const key = entity[datastore.KEY];
118 * })
119 * .on('info', (info) => {})
120 * .on('end', () => {
121 * // All entities retrieved.
122 * });
123 *
124 * //-
125 * // If you anticipate many results, you can end a stream early to prevent
126 * // unnecessary processing and API requests.
127 * //-
128 * datastore.runQueryStream(query)
129 * .on('data', (entity) => {
130 * this.end();
131 * });
132 */
133 runQueryStream(query: Query, options?: RunQueryStreamOptions): Transform;
134 merge(entities: Entities): Promise<CommitResponse>;
135 merge(entities: Entities, callback: SaveCallback): void;
136 /**
137 * @private
138 */
139 prepareGaxRequest_(config: RequestConfig, callback: Function): void;
140 request_(config: RequestConfig, callback: RequestCallback): void;
141 /**
142 * Make a request as a stream.
143 *
144 * @param {object} config Configuration object.
145 * @param {object} config.gaxOpts GAX options.
146 * @param {string} config.client The name of the gax client.
147 * @param {string} config.method The gax method to call.
148 * @param {object} config.reqOpts Request options.
149 */
150 requestStream_(config: RequestConfig): AbortableDuplex;
151}
152export interface ConsistencyProtoCode {
153 [key: string]: number;
154}
155export declare type AllocateIdsResponse = [entity.Key[], google.datastore.v1.IAllocateIdsResponse];
156export interface AllocateIdsCallback {
157 (a: Error | null, b: entity.Key[] | null, c: google.datastore.v1.IAllocateIdsResponse): void;
158}
159export interface AllocateIdsOptions {
160 allocations?: number;
161 gaxOptions?: CallOptions;
162}
163export declare type CreateReadStreamOptions = RunQueryOptions;
164export interface GetCallback {
165 (err?: Error | null, entity?: Entities): void;
166}
167export declare type GetResponse = [Entities];
168export interface Mutation {
169 [key: string]: EntityProto;
170}
171export interface PrepareEntityObject {
172 [key: string]: google.datastore.v1.Key | undefined;
173}
174export interface PrepareEntityObjectResponse {
175 key?: google.datastore.v1.Key;
176 data?: google.datastore.v1.Entity;
177 method?: string;
178}
179export interface RequestCallback {
180 (a?: Error | null, b?: any): void;
181}
182export interface RequestConfig {
183 client: string;
184 gaxOpts?: CallOptions;
185 method: string;
186 prepared?: boolean;
187 reqOpts?: RequestOptions;
188}
189export interface RequestOptions {
190 mutations?: google.datastore.v1.IMutation[];
191 keys?: Entity;
192 readOptions?: {
193 readConsistency?: number;
194 transaction?: string;
195 };
196 partitionId?: google.datastore.v1.IPartitionId | null;
197 transactionOptions?: {
198 readOnly?: {};
199 readWrite?: {
200 previousTransaction?: string;
201 };
202 } | null;
203 transaction?: string | null;
204 mode?: string;
205 projectId?: string;
206 query?: QueryProto;
207 filter?: string;
208 indexId?: string;
209 entityFilter?: google.datastore.admin.v1.IEntityFilter;
210}
211export declare type RunQueryStreamOptions = RunQueryOptions;
212export interface CommitCallback {
213 (err?: Error | null, resp?: google.datastore.v1.ICommitResponse): void;
214}
215export declare type CommitResponse = [google.datastore.v1.ICommitResponse];
216export declare type SaveCallback = CommitCallback;
217export declare type SaveResponse = CommitResponse;
218export declare type DeleteCallback = CommitCallback;
219export declare type DeleteResponse = CommitResponse;
220/**
221 * Reference to the {@link DatastoreRequest} class.
222 * @name module:@google-cloud/datastore.DatastoreRequest
223 * @see DatastoreRequest
224 */
225export { DatastoreRequest };