UNPKG

19.1 kBTypeScriptView Raw
1import { Query, QueryProto, IntegerTypeCastOptions } from './query';
2import { PathType } from '.';
3import { protobuf as Protobuf } from 'google-gax';
4import { google } from '../protos/protos';
5export declare namespace entity {
6 interface InvalidKeyErrorOptions {
7 code: string;
8 }
9 class InvalidKeyError extends Error {
10 constructor(opts: InvalidKeyErrorOptions);
11 }
12 /**
13 * A symbol to access the Key object from an entity object.
14 *
15 * @type {symbol}
16 * @private
17 */
18 const KEY_SYMBOL: unique symbol;
19 /**
20 * Build a Datastore Double object. For long doubles, a string can be
21 * provided.
22 *
23 * @class
24 * @param {number} value The double value.
25 *
26 * @example
27 * ```
28 * const {Datastore} = require('@google-cloud/datastore');
29 * const datastore = new Datastore();
30 * const aDouble = datastore.double(7.3);
31 * ```
32 */
33 class Double {
34 type: string;
35 value: number;
36 constructor(value: number);
37 }
38 /**
39 * Check if something is a Datastore Double object.
40 *
41 * @private
42 * @param {*} value The value to check if it is a datastore double.
43 * @returns {boolean}
44 */
45 function isDsDouble(value?: {}): value is entity.Double;
46 /**
47 * Check if a value is a Datastore Double object converted from JSON.
48 *
49 * @private
50 * @param {*} value The value to check if it is datastore double like.
51 * @returns {boolean}
52 */
53 function isDsDoubleLike(value: unknown): boolean;
54 /**
55 * Build a Datastore Int object. For long integers, a string can be provided.
56 *
57 * @class
58 * @param {number|string} value The integer value.
59 * @param {object} [typeCastOptions] Configuration to convert
60 * values of `integerValue` type to a custom value. Must provide an
61 * `integerTypeCastFunction` to handle `integerValue` conversion.
62 * @param {function} typeCastOptions.integerTypeCastFunction A custom user
63 * provided function to convert `integerValue`.
64 * @param {string|string[]} [typeCastOptions.properties] `Entity` property
65 * names to be converted using `integerTypeCastFunction`.
66 *
67 * @example
68 * ```
69 * const {Datastore} = require('@google-cloud/datastore');
70 * const datastore = new Datastore();
71 * const anInt = datastore.int(7);
72 * ```
73 */
74 class Int extends Number {
75 type: string;
76 value: string;
77 typeCastFunction?: Function;
78 typeCastProperties?: string[];
79 private _entityPropertyName;
80 constructor(value: number | string | ValueProto, typeCastOptions?: IntegerTypeCastOptions);
81 valueOf(): any;
82 toJSON(): Json;
83 }
84 /**
85 * Check if something is a Datastore Int object.
86 *
87 * @private
88 * @param {*} value The value to check if it is a Datastore Int
89 * @returns {boolean}
90 */
91 function isDsInt(value?: {}): value is entity.Int;
92 /**
93 * Check if a value is a Datastore Int object converted from JSON.
94 *
95 * @private
96 * @param {*} value The value to check if it is Datastore IntLike
97 * @returns {boolean}
98 */
99 function isDsIntLike(value: unknown): boolean;
100 interface Coordinates {
101 latitude: number;
102 longitude: number;
103 }
104 /**
105 * Build a Datastore Geo Point object.
106 *
107 * @class
108 * @param {object} coordinates Coordinate value.
109 * @param {number} coordinates.latitude Latitudinal value.
110 * @param {number} coordinates.longitude Longitudinal value.
111 *
112 * @example
113 * ```
114 * const {Datastore} = require('@google-cloud/datastore');
115 * const datastore = new Datastore();
116 * const coordinates = {
117 * latitude: 40.6894,
118 * longitude: -74.0447
119 * };
120 *
121 * const geoPoint = datastore.geoPoint(coordinates);
122 * ```
123 */
124 class GeoPoint {
125 value: Coordinates;
126 constructor(coordinates: Coordinates);
127 }
128 /**
129 * Check if something is a Datastore Geo Point object.
130 *
131 * @private
132 * @param {*} value The value to check if it is a Geo point.
133 * @returns {boolean}
134 */
135 function isDsGeoPoint(value?: {}): value is entity.GeoPoint;
136 interface KeyOptions {
137 namespace?: string;
138 path: PathType[];
139 }
140 /**
141 * Build a Datastore Key object.
142 *
143 * @class
144 * @param {object} options Configuration object.
145 * @param {array} options.path Key path.
146 * @param {string} [options.namespace] Optional namespace.
147 *
148 * @example
149 * ```
150 * //-
151 * // Create an incomplete key with a kind value of `Company`.
152 * //-
153 * const {Datastore} = require('@google-cloud/datastore');
154 * const datastore = new Datastore();
155 * const key = datastore.key('Company');
156 *
157 * ```
158 * @example
159 * ```
160 * //-
161 * // Create a complete key with a kind value of `Company` and id`123`.
162 * //-
163 * const {Datastore} = require('@google-cloud/datastore');
164 * const datastore = new Datastore();
165 * const key = datastore.key(['Company', 123]);
166 *
167 * ```
168 * @example
169 * ```
170 * //-
171 * // If the ID integer is outside the bounds of a JavaScript Number
172 * // object, create an Int.
173 * //-
174 * const {Datastore} = require('@google-cloud/datastore');
175 * const datastore = new Datastore();
176 * const key = datastore.key([
177 * 'Company',
178 * datastore.int('100000000000001234')
179 * ]);
180 *
181 * ```
182 * @example
183 * ```
184 * const {Datastore} = require('@google-cloud/datastore');
185 * const datastore = new Datastore();
186 * // Create a complete key with a kind value of `Company` and name `Google`.
187 * // Note: `id` is used for numeric identifiers and `name` is used otherwise.
188 * const key = datastore.key(['Company', 'Google']);
189 *
190 * ```
191 * @example
192 * ```
193 * //-
194 * // Create a complete key from a provided namespace and path.
195 * //-
196 * const {Datastore} = require('@google-cloud/datastore');
197 * const datastore = new Datastore();
198 * const key = datastore.key({
199 * namespace: 'My-NS',
200 * path: ['Company', 123]
201 * });
202 *
203 * ```
204 * @example Serialize the key for later re-use.
205 * ```
206 * const {Datastore} = require('@google-cloud/datastore');
207 * const datastore = new Datastore();
208 * const key = datastore.key({
209 * namespace: 'My-NS',
210 * path: ['Company', 123]
211 * });
212 * // Later...
213 * const key = datastore.key(key.serialized);
214 * ```
215 */
216 class Key {
217 namespace?: string;
218 id?: string;
219 name?: string;
220 kind: string;
221 parent?: Key;
222 path: Array<string | number>;
223 constructor(options: KeyOptions);
224 /**
225 * Access the `serialized` property for a library-compatible way to re-use a
226 * key.
227 *
228 * @returns {object}
229 *
230 * @example
231 * ```
232 * const key = datastore.key({
233 * namespace: 'My-NS',
234 * path: ['Company', 123]
235 * });
236 *
237 * // Later...
238 * const key = datastore.key(key.serialized);
239 * ```
240 */
241 get serialized(): {
242 namespace: string | undefined;
243 path: (string | Int)[];
244 };
245 }
246 /**
247 * Check if something is a Datastore Key object.
248 *
249 * @private
250 * @param {*} value
251 * @returns {boolean}
252 */
253 function isDsKey(value?: {}): value is entity.Key;
254 /**
255 * @typedef {object} IntegerTypeCastOptions Configuration to convert
256 * values of `integerValue` type to a custom value. Must provide an
257 * `integerTypeCastFunction` to handle `integerValue` conversion.
258 * @property {function} integerTypeCastFunction A custom user
259 * provided function to convert `integerValue`.
260 * @property {string | string[]} [properties] `Entity` property
261 * names to be converted using `integerTypeCastFunction`.
262 */
263 /**
264 * Convert a protobuf Value message to its native value.
265 *
266 * @private
267 * @param {object} valueProto The protobuf Value message to convert.
268 * @param {boolean | IntegerTypeCastOptions} [wrapNumbers=false] Wrap values of integerValue type in
269 * {@link Datastore#Int} objects.
270 * If a `boolean`, this will wrap values in {@link Datastore#Int} objects.
271 * If an `object`, this will return a value returned by
272 * `wrapNumbers.integerTypeCastFunction`.
273 * Please see {@link IntegerTypeCastOptions} for options descriptions.
274 * @returns {*}
275 *
276 * @example
277 * ```
278 * decodeValueProto({
279 * booleanValue: false
280 * });
281 * // false
282 *
283 * decodeValueProto({
284 * stringValue: 'Hi'
285 * });
286 * // 'Hi'
287 *
288 * decodeValueProto({
289 * blobValue: Buffer.from('68656c6c6f')
290 * });
291 * // <Buffer 68 65 6c 6c 6f>
292 * ```
293 */
294 function decodeValueProto(valueProto: ValueProto, wrapNumbers?: boolean | IntegerTypeCastOptions): any;
295 /**
296 * Convert any native value to a protobuf Value message object.
297 *
298 * @private
299 * @param {*} value Native value.
300 * @param {string} property The property to use for the average calculation.
301 * @returns {object}
302 *
303 * @example
304 * ```
305 * encodeValue('Hi');
306 * // {
307 * // stringValue: 'Hi'
308 * // }
309 * ```
310 */
311 function encodeValue(value: any, property: string): ValueProto;
312 /**
313 * Convert any entity protocol to a plain object.
314 *
315 * @todo Use registered metadata if provided.
316 *
317 * @private
318 * @param {object} entityProto The protocol entity object to convert.
319 * @param {boolean | IntegerTypeCastOptions} [wrapNumbers=false] Wrap values of integerValue type in
320 * {@link Datastore#Int} objects.
321 * If a `boolean`, this will wrap values in {@link Datastore#Int} objects.
322 * If an `object`, this will return a value returned by
323 * `wrapNumbers.integerTypeCastFunction`.
324 * Please see {@link IntegerTypeCastOptions} for options descriptions.
325 * @returns {object}
326 *
327 * @example
328 * ```
329 * entityFromEntityProto({
330 * properties: {
331 * map: {
332 * name: {
333 * value: {
334 * valueType: 'stringValue',
335 * stringValue: 'Stephen'
336 * }
337 * }
338 * }
339 * }
340 * });
341 * // {
342 * // name: 'Stephen'
343 * // }
344 * ```
345 */
346 function entityFromEntityProto(entityProto: EntityProto, wrapNumbers?: boolean | IntegerTypeCastOptions): any;
347 /**
348 * Convert an entity object to an entity protocol object.
349 *
350 * @private
351 * @param {object} entityObject The entity object to convert.
352 * @returns {object}
353 *
354 * @example
355 * ```
356 * entityToEntityProto({
357 * excludeFromIndexes: [
358 * 'name'
359 * ],
360 * data: {
361 * name: 'Burcu',
362 * legit: true
363 * }
364 * });
365 * // {
366 * // key: null,
367 * // properties: {
368 * // name: {
369 * // stringValue: 'Burcu'
370 * // excludeFromIndexes: true
371 * // },
372 * // legit: {
373 * // booleanValue: true
374 * // }
375 * // }
376 * // }
377 * ```
378 */
379 function entityToEntityProto(entityObject: EntityObject): EntityProto;
380 /**
381 *
382 * @param {string[] | undefined} [entities.excludeFromIndexes] Exclude properties from
383 * indexing using a simple JSON path notation. See the examples in
384 * {@link Datastore#save} to see how to target properties at different
385 * levels of nesting within your entity.
386 * @param {object} entityProto The protocol entity object to convert.
387 */
388 function addExcludeFromIndexes(excludeFromIndexes: string[] | undefined, entityProto: EntityProto): EntityProto;
389 /**
390 * Convert an API response array to a qualified Key and data object.
391 *
392 * @private
393 * @param {object[]} results The response array.
394 * @param {object} results.entity An entity object.
395 * @param {object} results.entity.key The entity's key.
396 * @param {boolean | IntegerTypeCastOptions} [wrapNumbers=false] Wrap values of integerValue type in
397 * {@link Datastore#Int} objects.
398 * If a `boolean`, this will wrap values in {@link Datastore#Int} objects.
399 * If an `object`, this will return a value returned by
400 * `wrapNumbers.integerTypeCastFunction`.
401 * Please see {@link IntegerTypeCastOptions} for options descriptions.
402 *
403 * @example
404 * ```
405 * request_('runQuery', {}, (err, response) => {
406 * const entityObjects = formatArray(response.batch.entityResults);
407 * // {
408 * // key: {},
409 * // data: {
410 * // fieldName: 'value'
411 * // }
412 * // }
413 * //
414 * });
415 * ```
416 */
417 function formatArray(results: ResponseResult[], wrapNumbers?: boolean | IntegerTypeCastOptions): any[];
418 /**
419 * Find the properties which value size is large than 1500 bytes,
420 * with excludeLargeProperties enabled, automatically exclude properties from indexing.
421 * This will allow storing string values larger than 1500 bytes
422 *
423 * @param entities Datastore key object(s).
424 * @param path namespace of provided entity properties
425 * @param properties properties which value size is large than 1500 bytes
426 */
427 function findLargeProperties_(entities: Entities, path: string, properties?: string[]): string[];
428 /**
429 * Check if a key is complete.
430 *
431 * @private
432 * @param {Key} key The Key object.
433 * @returns {boolean}
434 *
435 * @example
436 * ```
437 * isKeyComplete(new Key(['Company', 'Google'])); // true
438 * isKeyComplete(new Key('Company')); // false
439 * ```
440 */
441 function isKeyComplete(key: Key): boolean;
442 /**
443 * Convert a key protocol object to a Key object.
444 *
445 * @private
446 * @param {object} keyProto The key protocol object to convert.
447 * @returns {Key}
448 *
449 * @example
450 * ```
451 * const key = keyFromKeyProto({
452 * partitionId: {
453 * projectId: 'project-id',
454 * namespaceId: ''
455 * },
456 * path: [
457 * {
458 * kind: 'Kind',
459 * id: '4790047639339008'
460 * }
461 * ]
462 * });
463 * ```
464 */
465 function keyFromKeyProto(keyProto: KeyProto): Key;
466 /**
467 * Convert a Key object to a key protocol object.
468 *
469 * @private
470 * @param {Key} key The Key object to convert.
471 * @returns {object}
472 *
473 * @example
474 * ```
475 * const keyProto = keyToKeyProto(new Key(['Company', 1]));
476 * // {
477 * // path: [
478 * // {
479 * // kind: 'Company',
480 * // id: 1
481 * // }
482 * // ]
483 * // }
484 * ```
485 */
486 function keyToKeyProto(key: Key): KeyProto;
487 /**
488 * Convert a query object to a query protocol object.
489 *
490 * @private
491 * @param {object} q The query object to convert.
492 * @returns {object}
493 *
494 * @example
495 * ```
496 * queryToQueryProto({
497 * namespace: '',
498 * kinds: [
499 * 'Kind'
500 * ],
501 * filters: [],
502 * orders: [],
503 * groupByVal: [],
504 * selectVal: [],
505 * startVal: null,
506 * endVal: null,
507 * limitVal: -1,
508 * offsetVal: -1
509 * });
510 * // {
511 * // projection: [],
512 * // kinds: [
513 * // {
514 * // name: 'Kind'
515 * // }
516 * // ],
517 * // order: [],
518 * // groupBy: []
519 * // }
520 * ```
521 */
522 function queryToQueryProto(query: Query): QueryProto;
523 /**
524 * URL safe key encoding and decoding helper utility.
525 *
526 * This is intended to work with the "legacy" representation of a
527 * datastore "Key" used within Google App Engine (a so-called "Reference").
528 *
529 * @private
530 * @class
531 */
532 class URLSafeKey {
533 protos: any;
534 constructor();
535 /**
536 * Load AppEngine protobuf file.
537 *
538 * @private
539 */
540 loadProtos_(): {
541 [k: string]: Protobuf.ReflectionObject;
542 } | undefined;
543 /**
544 * Convert key to url safe base64 encoded string.
545 *
546 * @private
547 * @param {string} projectId Project Id.
548 * @param {entity.Key} key Entity key object.
549 * @param {string} locationPrefix Optional .
550 * The location prefix of an App Engine project ID.
551 * Often this value is 's~', but may also be 'e~', or other location prefixes
552 * currently unknown.
553 * @returns {string} base64 endocded urlsafe key.
554 */
555 legacyEncode(projectId: string, key: entity.Key, locationPrefix?: string): string;
556 /**
557 * Helper to convert URL safe key string to entity key object
558 *
559 * This is intended to work with the "legacy" representation of a
560 * datastore "Key" used within Google App Engine (a so-called "Reference").
561 *
562 * @private
563 * @param {entity.Key} key Entity key object.
564 * @param {string} locationPrefix Optional .
565 * The location prefix of an App Engine project ID.
566 * Often this value is 's~', but may also be 'e~', or other location prefixes
567 * currently unknown.
568 * @returns {string} Created urlsafe key.
569 */
570 legacyDecode(key: string): entity.Key;
571 /**
572 * Convert buffer to base64 encoding.
573 *
574 * @private
575 * @param {Buffer} buffer The buffer to convert
576 * @returns {string} Base64 encoded string.
577 */
578 convertToBase64_(buffer: Buffer): string;
579 /**
580 * Rebuild base64 from encoded url safe string and convert to buffer.
581 *
582 * @private
583 * @param {string} val Encoded url safe string.
584 * @returns {string} Base64 encoded string.
585 */
586 convertToBuffer_(val: string): Buffer;
587 }
588}
589export interface ValueProto {
590 [index: string]: any;
591 valueType?: string;
592 values?: ValueProto[];
593 value?: any;
594 propertyName?: string;
595}
596export interface EntityProto {
597 key?: KeyProto | null;
598 properties?: {
599 [k: string]: ValueProto;
600 };
601 excludeFromIndexes?: boolean;
602}
603export type Entity = any;
604export type Entities = Entity | Entity[];
605interface KeyProtoPathElement extends google.datastore.v1.Key.IPathElement {
606 [index: string]: any;
607 idType?: string;
608}
609export interface KeyProto {
610 partitionId?: google.datastore.v1.IPartitionId | null;
611 path?: KeyProtoPathElement[] | null;
612}
613export interface ResponseResult {
614 entity: EntityProto;
615}
616export interface EntityObject {
617 data: {
618 [k: string]: Entity;
619 };
620 excludeFromIndexes?: string[];
621}
622export interface Json {
623 [field: string]: string;
624}
625export {};