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" />
|
17 | import { Key } from 'readline';
|
18 | import { Datastore } from '.';
|
19 | import { Entity } from './entity';
|
20 | import { Transaction } from './transaction';
|
21 | import { CallOptions } from 'google-gax';
|
22 | import { RunQueryStreamOptions } from '../src/request';
|
23 | export declare type Operator = '=' | '<' | '>' | '<=' | '>=' | 'HAS_ANCESTOR';
|
24 | export interface OrderOptions {
|
25 | descending?: boolean;
|
26 | }
|
27 | export interface Order {
|
28 | name: string;
|
29 | sign: '-' | '+';
|
30 | }
|
31 | export interface Filter {
|
32 | name: string;
|
33 | val: any;
|
34 | op: Operator;
|
35 | }
|
36 | /**
|
37 | * Build a Query object.
|
38 | *
|
39 | * **Queries are built with {module:datastore#createQuery} and
|
40 | * {@link Transaction#createQuery}.**
|
41 | *
|
42 | * @see [Datastore Queries]{@link http://goo.gl/Cag0r6}
|
43 | *
|
44 | * @class
|
45 | * @param {Datastore|Transaction} scope The parent scope the query was created
|
46 | * from.
|
47 | * @param {string} [namespace] Namespace to query entities from.
|
48 | * @param {string[]} kinds Kind to query.
|
49 | *
|
50 | * @example
|
51 | * const {Datastore} = require('@google-cloud/datastore');
|
52 | * const datastore = new Datastore();
|
53 | * const query = datastore.createQuery('AnimalNamespace', 'Lion');
|
54 | */
|
55 | declare class Query {
|
56 | scope?: Datastore | Transaction;
|
57 | namespace?: string | null;
|
58 | kinds: string[];
|
59 | filters: Filter[];
|
60 | orders: Order[];
|
61 | groupByVal: Array<{}>;
|
62 | selectVal: Array<{}>;
|
63 | startVal: string | Buffer | null;
|
64 | endVal: string | Buffer | null;
|
65 | limitVal: number;
|
66 | offsetVal: number;
|
67 | constructor(scope?: Datastore | Transaction, kinds?: string[] | null);
|
68 | constructor(scope?: Datastore | Transaction, namespace?: string | null, kinds?: string[]);
|
69 | filter(property: string, value: {}): Query;
|
70 | filter(property: string, operator: Operator, value: {}): Query;
|
71 | /**
|
72 | * Filter a query by ancestors.
|
73 | *
|
74 | * @see [Datastore Ancestor Filters]{@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ancestor-query-nodejs}
|
75 | *
|
76 | * @param {Key} key Key object to filter by.
|
77 | * {Query}
|
78 | *
|
79 | *
|
80 | * const {Datastore} = require('@google-cloud/datastore');
|
81 | * const datastore = new Datastore();
|
82 | * const query = datastore.createQuery('MyKind');
|
83 | * const ancestoryQuery = query.hasAncestor(datastore.key(['Parent', 123]));
|
84 | */
|
85 | hasAncestor(key: Key): this;
|
86 | /**
|
87 | * Sort the results by a property name in ascending or descending order. By
|
88 | * default, an ascending sort order will be used.
|
89 | *
|
90 | * @see [Datastore Sort Orders]{@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ascending-sort-nodejs}
|
91 | *
|
92 | * @param {string} property The property to order by.
|
93 | * @param {object} [options] Options object.
|
94 | * @param {boolean} [options.descending=false] Sort the results by a property
|
95 | * name in descending order.
|
96 | * @returns {Query}
|
97 | *
|
98 | * @example
|
99 | * const {Datastore} = require('@google-cloud/datastore');
|
100 | * const datastore = new Datastore();
|
101 | * const companyQuery = datastore.createQuery('Company');
|
102 | *
|
103 | * // Sort by size ascendingly.
|
104 | * const companiesAscending = companyQuery.order('size');
|
105 | *
|
106 | * // Sort by size descendingly.
|
107 | * const companiesDescending = companyQuery.order('size', {
|
108 | * descending: true
|
109 | * });
|
110 | */
|
111 | order(property: string, options?: OrderOptions): this;
|
112 | /**
|
113 | * Group query results by a list of properties.
|
114 | *
|
115 | * @param {array} properties Properties to group by.
|
116 | * @returns {Query}
|
117 | *
|
118 | * @example
|
119 | * const {Datastore} = require('@google-cloud/datastore');
|
120 | * const datastore = new Datastore();
|
121 | * const companyQuery = datastore.createQuery('Company');
|
122 | * const groupedQuery = companyQuery.groupBy(['name', 'size']);
|
123 | */
|
124 | groupBy(fieldNames: string | string[]): this;
|
125 | /**
|
126 | * Retrieve only select properties from the matched entities.
|
127 | *
|
128 | * Queries that select a subset of properties are called Projection Queries.
|
129 | *
|
130 | * @see [Projection Queries]{@link https://cloud.google.com/datastore/docs/concepts/projectionqueries}
|
131 | *
|
132 | * @param {string|string[]} fieldNames Properties to return from the matched
|
133 | * entities.
|
134 | * @returns {Query}
|
135 | *
|
136 | * @example
|
137 | * const {Datastore} = require('@google-cloud/datastore');
|
138 | * const datastore = new Datastore();
|
139 | * const companyQuery = datastore.createQuery('Company');
|
140 | *
|
141 | * // Only retrieve the name property.
|
142 | * const selectQuery = companyQuery.select('name');
|
143 | *
|
144 | * // Only retrieve the name and size properties.
|
145 | * const selectQuery = companyQuery.select(['name', 'size']);
|
146 | */
|
147 | select(fieldNames: string | string[]): this;
|
148 | /**
|
149 | * Set a starting cursor to a query.
|
150 | *
|
151 | * @see [Query Cursors]{@link https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets}
|
152 | *
|
153 | * @param {string} cursorToken The starting cursor token.
|
154 | * @returns {Query}
|
155 | *
|
156 | * @example
|
157 | * const {Datastore} = require('@google-cloud/datastore');
|
158 | * const datastore = new Datastore();
|
159 | * const companyQuery = datastore.createQuery('Company');
|
160 | *
|
161 | * const cursorToken = 'X';
|
162 | *
|
163 | * // Retrieve results starting from cursorToken.
|
164 | * const startQuery = companyQuery.start(cursorToken);
|
165 | */
|
166 | start(start: string | Buffer): this;
|
167 | /**
|
168 | * Set an ending cursor to a query.
|
169 | *
|
170 | * @see [Query Cursors]{@link https://cloud.google.com/datastore/docs/concepts/queries#Datastore_Query_cursors}
|
171 | *
|
172 | * @param {string} cursorToken The ending cursor token.
|
173 | * @returns {Query}
|
174 | *
|
175 | * @example
|
176 | * const {Datastore} = require('@google-cloud/datastore');
|
177 | * const datastore = new Datastore();
|
178 | * const companyQuery = datastore.createQuery('Company');
|
179 | *
|
180 | * const cursorToken = 'X';
|
181 | *
|
182 | * // Retrieve results limited to the extent of cursorToken.
|
183 | * const endQuery = companyQuery.end(cursorToken);
|
184 | */
|
185 | end(end: string | Buffer): this;
|
186 | /**
|
187 | * Set a limit on a query.
|
188 | *
|
189 | * @see [Query Limits]{@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs}
|
190 | *
|
191 | * @param {number} n The number of results to limit the query to.
|
192 | * @returns {Query}
|
193 | *
|
194 | * @example
|
195 | * const {Datastore} = require('@google-cloud/datastore');
|
196 | * const datastore = new Datastore();
|
197 | * const companyQuery = datastore.createQuery('Company');
|
198 | *
|
199 | * // Limit the results to 10 entities.
|
200 | * const limitQuery = companyQuery.limit(10);
|
201 | */
|
202 | limit(n: number): this;
|
203 | /**
|
204 | * Set an offset on a query.
|
205 | *
|
206 | * @see [Query Offsets]{@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs}
|
207 | *
|
208 | * @param {number} n The offset to start from after the start cursor.
|
209 | * @returns {Query}
|
210 | *
|
211 | * @example
|
212 | * const {Datastore} = require('@google-cloud/datastore');
|
213 | * const datastore = new Datastore();
|
214 | * const companyQuery = datastore.createQuery('Company');
|
215 | *
|
216 | * // Start from the 101st result.
|
217 | * const offsetQuery = companyQuery.offset(100);
|
218 | */
|
219 | offset(n: number): this;
|
220 | run(options?: RunQueryOptions): Promise<RunQueryResponse>;
|
221 | run(options: RunQueryOptions, callback: RunQueryCallback): void;
|
222 | run(callback: RunQueryCallback): void;
|
223 | /**
|
224 | * Run the query as a readable object stream.
|
225 | *
|
226 | * @method Query#runStream
|
227 | * @param {object} [options] Optional configuration. See
|
228 | * {@link Query#run} for a complete list of options.
|
229 | * @returns {stream}
|
230 | *
|
231 | * @example
|
232 | * const {Datastore} = require('@google-cloud/datastore');
|
233 | * const datastore = new Datastore();
|
234 | * const query = datastore.createQuery('Company');
|
235 | *
|
236 | * query.runStream()
|
237 | * .on('error', console.error)
|
238 | * .on('data', function (entity) {
|
239 | * // Access the Key object for this entity.
|
240 | * const key = entity[datastore.KEY];
|
241 | * })
|
242 | * .on('info', (info) => {})
|
243 | * .on('end', () => {
|
244 | * // All entities retrieved.
|
245 | * });
|
246 | *
|
247 | * //-
|
248 | * // If you anticipate many results, you can end a stream early to prevent
|
249 | * // unnecessary processing and API requests.
|
250 | * //-
|
251 | * query.runStream()
|
252 | * .on('data', function (entity) {
|
253 | * this.end();
|
254 | * });
|
255 | */
|
256 | runStream(options?: RunQueryStreamOptions): import("stream").Transform;
|
257 | }
|
258 | export interface QueryProto {
|
259 | startCursor?: string | Buffer;
|
260 | distinctOn: {};
|
261 | kind: {};
|
262 | order: {};
|
263 | projection: {};
|
264 | endCursor?: string | Buffer;
|
265 | limit?: {};
|
266 | offset?: number;
|
267 | filter?: {};
|
268 | }
|
269 | /**
|
270 | * Reference to the {@link Query} class.
|
271 | * @name module:@google-cloud/datastore.Query
|
272 | * @see Query
|
273 | */
|
274 | export { Query };
|
275 | export interface IntegerTypeCastOptions {
|
276 | integerTypeCastFunction: Function;
|
277 | properties?: string | string[];
|
278 | }
|
279 | export interface RunQueryOptions {
|
280 | consistency?: 'strong' | 'eventual';
|
281 | gaxOptions?: CallOptions;
|
282 | wrapNumbers?: boolean | IntegerTypeCastOptions;
|
283 | }
|
284 | export interface RunQueryCallback {
|
285 | (err: Error | null, entities?: Entity[], info?: RunQueryInfo): void;
|
286 | }
|
287 | export declare type RunQueryResponse = [Entity[], RunQueryInfo];
|
288 | export interface RunQueryInfo {
|
289 | endCursor?: string;
|
290 | moreResults?: 'MORE_RESULTS_TYPE_UNSPECIFIED' | 'NOT_FINISHED' | 'MORE_RESULTS_AFTER_LIMIT' | 'MORE_RESULTS_AFTER_CURSOR' | 'NO_MORE_RESULTS';
|
291 | }
|