UNPKG

42 kBTypeScriptView Raw
1/*!
2 * Copyright 2018 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 { ClientStub, ChannelCredentials, GoogleAuth, GoogleAuthOptions, CallOptions, Operation, ServiceError } from 'google-gax';
18import { entity, Entities, Entity, ValueProto } from './entity';
19import Key = entity.Key;
20export { Entity, Key };
21import { PropertyFilter, and, or } from './filter';
22export { PropertyFilter, and, or };
23import { GetIndexesCallback, GetIndexesOptions, GetIndexesResponse, Index } from './index-class';
24import { Query } from './query';
25import { DatastoreRequest, CommitCallback, CommitResponse, SaveCallback, SaveResponse } from './request';
26import { Transaction } from './transaction';
27import { google } from '../protos/protos';
28import { AggregateQuery } from './aggregate';
29export type PathType = string | number | entity.Int;
30export interface BooleanObject {
31 [key: string]: boolean;
32}
33export interface EntityProtoReduceAccumulator {
34 [key: string]: ValueProto;
35}
36export interface EntityProtoReduceData {
37 value: ValueProto;
38 excludeFromIndexes: ValueProto;
39 name: string | number;
40}
41export type UpdateCallback = CommitCallback;
42export type UpdateResponse = CommitResponse;
43export type UpsertCallback = CommitCallback;
44export type UpsertResponse = CommitResponse;
45export type InsertCallback = CommitCallback;
46export type InsertResponse = CommitResponse;
47export interface LongRunningCallback {
48 (err: ServiceError | null, operation?: Operation, apiResponse?: google.longrunning.IOperation): void;
49}
50export type LongRunningResponse = [Operation, google.longrunning.IOperation];
51export interface ExportEntitiesConfig extends Omit<google.datastore.admin.v1.IExportEntitiesRequest, 'projectId'> {
52 bucket?: string | {
53 name: string;
54 };
55 kinds?: string[];
56 namespaces?: string[];
57 gaxOptions?: CallOptions;
58}
59export interface ImportEntitiesConfig extends Omit<google.datastore.admin.v1.IImportEntitiesRequest, 'projectId'> {
60 file?: string | {
61 bucket: {
62 name: string;
63 };
64 name: string;
65 };
66 kinds?: string[];
67 namespaces?: string[];
68 gaxOptions?: CallOptions;
69}
70/**
71 * Idiomatic class for interacting with Cloud Datastore. Uses the lower-level
72 * {@link DatastoreClient} class under the hood.
73 *
74 * In addition to the constructor options shown here, the {@link Datastore}
75 * class constructor accepts the same options accepted by
76 * {@link DatastoreClient}.
77 *
78 * <h4>The Datastore Emulator</h4>
79 *
80 * Make sure you have the <a href="https://cloud.google.com/sdk/downloads">
81 * gcloud SDK installed</a>, then run:
82 *
83 * <pre>
84 * $ gcloud beta emulators datastore start --no-legacy
85 * </pre>
86 *
87 * You will see the following printed:
88 *
89 * <pre>
90 * [datastore] API endpoint: http://localhost:8005
91 * [datastore] If you are using a library that supports the
92 * DATASTORE_EMULATOR_HOST environment variable, run:
93 * [datastore]
94 * [datastore] export DATASTORE_EMULATOR_HOST=localhost:8005
95 * [datastore]
96 * [datastore] Dev App Server is now running.
97 * </pre>
98 *
99 * Set that environment variable and your localhost Datastore will
100 * automatically be used. You can also pass this address in manually with
101 * `apiEndpoint`.
102 *
103 * Additionally, `DATASTORE_PROJECT_ID` is recognized. If you have this set,
104 * you don't need to provide a `projectId`.
105 *
106 *
107 * See {@link https://cloud.google.com/datastore/docs/concepts/overview| Cloud Datastore Concepts Overview}
108 *
109 * @param {object} [options] Configuration options.
110 * @param {string} [options.apiEndpoint] Override the default API endpoint used
111 * to reach Datastore. This is useful for connecting to your local Datastore
112 * server (usually "http://localhost:8080").
113 * @param {string} [options.namespace] Namespace to isolate transactions to.
114 *
115 * @example Import the client library
116 * ```
117 * const {Datastore} = require('@google-cloud/datastore');
118 *
119 * ```
120 * @example Create a client that uses <a href="https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application">Application Default Credentials (ADC)</a>:
121 * ```
122 * const datastore = new Datastore();
123 *
124 * ```
125 * @example Create a client with <a href="https://cloud.google.com/docs/authentication/production#obtaining_and_providing_service_account_credentials_manually">explicit credentials</a>:
126 * ```
127 * const datastore = new Datastore({
128 * projectId: 'your-project-id',
129 * keyFilename: '/path/to/keyfile.json'
130 * });
131 *
132 * ```
133 * @example Retrieving Records
134 * ```
135 * const {Datastore} = require('@google-cloud/datastore');
136 * const datastore = new Datastore();
137 *
138 * // Records, called "entities" in Datastore, are retrieved by using a key. The
139 * // key is more than a numeric identifier, it is a complex data structure that
140 * // can be used to model relationships. The simplest key has a string `kind`
141 * // value, and either a numeric `id` value, or a string `name` value.
142 * //
143 * // A single record can be retrieved with {@link Datastore#key} and
144 * // {@link Datastore#get}.
145 * //-
146 * const key = datastore.key(['Company', 'Google']);
147 *
148 * datastore.get(key, function(err, entity) {
149 * // entity = The record.
150 * // entity[datastore.KEY] = The key for this entity.
151 * });
152 *
153 * //-
154 * // <h3>Querying Records</h3>
155 * //
156 * // Create a query with {@link Datastore#createQuery}.
157 * //-
158 * const query = datastore.createQuery('Company');
159 *
160 * //-
161 * // Multiple records can be found that match criteria with
162 * // {@link Query#filter}.
163 * //-
164 * query.filter('location', 'CA');
165 *
166 * //-
167 * // Records can also be ordered with {@link Query#order}.
168 * //-
169 * query.order('name');
170 *
171 * //-
172 * // The number of records returned can be specified with
173 * // {@link Query#limit}.
174 * //-
175 * query.limit(5);
176 *
177 * //-
178 * // Records' key structures can also be queried with
179 * // {@link Query#hasAncestor}.
180 * //-
181 * const ancestorKey = datastore.key(['ParentCompany', 'Alphabet']);
182 *
183 * query.hasAncestor(ancestorKey);
184 *
185 * //-
186 * // Run the query with {@link Datastore#runQuery}.
187 * //-
188 * datastore.runQuery(query, (err, entities) => {
189 * // entities = An array of records.
190 *
191 * // Access the Key object for an entity.
192 * const firstEntityKey = entities[0][datastore.KEY];
193 * });
194 *
195 * ```
196 * @example Paginating Records
197 * ```
198 * // Imagine building a website that allows a user to sift through hundreds of
199 * // their contacts. You'll likely want to only display a subset of these at
200 * // once, so you set a limit.
201 * //-
202 * const express = require('express');
203 * const app = express();
204 *
205 * const NUM_RESULTS_PER_PAGE = 15;
206 *
207 * app.get('/contacts', (req, res) => {
208 * const query = datastore.createQuery('Contacts')
209 * .limit(NUM_RESULTS_PER_PAGE);
210 *
211 * if (req.query.nextPageCursor) {
212 * query.start(req.query.nextPageCursor);
213 * }
214 *
215 * datastore.runQuery(query, (err, entities, info) => {
216 * if (err) {
217 * // Error handling omitted.
218 * return;
219 * }
220 *
221 * // Respond to the front end with the contacts and the cursoring token
222 * // from the query we just ran.
223 * const frontEndResponse = {
224 * contacts: entities
225 * };
226 *
227 * // Check if more results may exist.
228 * if (info.moreResults !== datastore.NO_MORE_RESULTS) {
229 * frontEndResponse.nextPageCursor = info.endCursor;
230 * }
231 *
232 * res.render('contacts', frontEndResponse);
233 * });
234 * });
235 *
236 * ```
237 * @example Creating Records
238 * ```
239 * // New entities can be created and persisted with {@link Datastore#save}.
240 * // The entitiy must have a key to be saved. If you don't specify an
241 * // identifier for the key, one is generated for you.
242 * //
243 * // We will create a key with a `name` identifier, "Google".
244 * //-
245 * const key = datastore.key(['Company', 'Google']);
246 *
247 * const data = {
248 * name: 'Google',
249 * location: 'CA'
250 * };
251 *
252 * datastore.save({
253 * key: key,
254 * data: data
255 * }, (err) => {
256 * if (!err) {
257 * // Record saved successfully.
258 * }
259 * });
260 *
261 * //-
262 * // We can verify the data was saved by using {@link Datastore#get}.
263 * //-
264 * datastore.get(key, (err, entity) => {
265 * // entity = {
266 * // name: 'Google',
267 * // location: 'CA'
268 * // }
269 * });
270 *
271 * //-
272 * // If we want to update this record, we can modify the data object and re-
273 * // save it.
274 * //-
275 * data.symbol = 'GOOG';
276 *
277 * datastore.save({
278 * key: key, // defined above (datastore.key(['Company', 'Google']))
279 * data: data
280 * }, (err, entity) => {
281 * if (!err) {
282 * // Record updated successfully.
283 * }
284 * });
285 *
286 * ```
287 * @example Deleting Records
288 * ```
289 * // Entities can be removed from Datastore by passing the entity's key object
290 * // to {@link Datastore#delete}.
291 * //-
292 * const key = datastore.key(['Company', 'Google']);
293 *
294 * datastore.delete(key, (err) => {
295 * if (!err) {
296 * // Record deleted successfully.
297 * }
298 * });
299 *
300 * ```
301 * @example Transactions
302 * ```
303 * // Complex logic can be wrapped in a transaction with
304 * // {@link Datastore#transaction}. All queries and updates run within
305 * // the transaction will be applied when the `done` function is called.
306 * //-
307 * const transaction = datastore.transaction();
308 *
309 * transaction.run((err) => {
310 * if (err) {
311 * // Error handling omitted.
312 * }
313 *
314 * const key = datastore.key(['Company', 'Google']);
315 *
316 * transaction.get(key, (err, entity) => {
317 * if (err) {
318 * // Error handling omitted.
319 * }
320 *
321 * entity.symbol = 'GOOG';
322 *
323 * transaction.save(entity);
324 *
325 * transaction.commit((err) => {
326 * if (!err) {
327 * // Transaction committed successfully.
328 * }
329 * });
330 * });
331 * });
332 *
333 * ```
334 * @example Queries with Ancestors
335 * ```
336 * const {Datastore} = require('@google-cloud/datastore');
337 * const datastore = new Datastore();
338 *
339 * const customerId1 = 2993844;
340 * const customerId2 = 4993882;
341 * const customerKey1 = datastore.key(['Customer', customerId1]);
342 * const customerKey2 = datastore.key(['Customer', customerId2]);
343 * const cookieKey1 = datastore.key(['Customer', customerId1, 'Cookie',
344 * 'cookie28839']); // child entity const cookieKey2 =
345 * datastore.key(['Customer', customerId1, 'Cookie', 'cookie78984']); // child
346 * entity const cookieKey3 = datastore.key(['Customer', customerId2, 'Cookie',
347 * 'cookie93911']); // child entity
348 *
349 * const entities = [];
350 *
351 * entities.push({
352 * key: customerKey1,
353 * data: {
354 * name: 'Jane Doe',
355 * address: '4848 Liller'
356 * }
357 * });
358 *
359 * entities.push({
360 * key: customerKey2,
361 * data: {
362 * name: 'John Smith',
363 * address: '4848 Pine'
364 * }
365 * });
366 *
367 * entities.push({
368 * key: cookieKey1,
369 * data: {
370 * cookieVal: 'dj83kks88rkld'
371 * }
372 * });
373 *
374 * entities.push({
375 * key: cookieKey2,
376 * data: {
377 * cookieVal: 'sj843ka99s'
378 * }
379 * });
380 *
381 * entities.push({
382 * key: cookieKey3,
383 * data: {
384 * cookieVal: 'otk82k2kw'
385 * }
386 * });
387 *
388 * datastore.upsert(entities);
389 *
390 * const query = datastore.createQuery().hasAncestor(customerKey1);
391 *
392 * datastore.runQuery(query, (err, entities) => {
393 * for (let entity of entities) {
394 * console.log(entity[datastore.KEY]);
395 * }
396 * });
397 *
398 * const query2 = datastore.createQuery().hasAncestor(customerKey2);
399 *
400 * datastore.runQuery(query2, (err, entities) => {
401 * for (let entity of entities) {
402 * console.log(entity[datastore.KEY]);
403 * }
404 * });
405 *
406 * datastore.runQuery(query2, (entities) => {
407 * console.log(entities);
408 * });
409 * ```
410 */
411declare class Datastore extends DatastoreRequest {
412 clients_: Map<string, ClientStub>;
413 namespace?: string;
414 defaultBaseUrl_: string;
415 options: DatastoreOptions;
416 baseUrl_?: string;
417 port_?: number;
418 customEndpoint_?: boolean;
419 auth: GoogleAuth;
420 constructor(options?: DatastoreOptions);
421 /**
422 * Create an aggregation query from a Query.
423 *
424 * @param {Query} query A Query object.
425 */
426 createAggregationQuery(query: Query): AggregateQuery;
427 /**
428 * Export entities from this project to a Google Cloud Storage bucket.
429 *
430 * @param {ExportEntitiesConfig} config Configuration object.
431 * @param {string | Bucket} config.bucket The `gs://bucket` path or a
432 * @google-cloud/storage Bucket object.
433 * @param {string[]} [config.kinds] The kinds to include in this import.
434 * @param {string[]} [config.namespaces] The namespace IDs to include in this
435 * import.
436 * @param {object} [config.gaxOptions] Request configuration options, outlined
437 * here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
438 * @param {function} callback The callback function.
439 * @param {?error} callback.err An error returned while making this request.
440 * @param {Operation} callback.operation An operation object that can be used
441 * to check the status of the request.
442 */
443 export(config: ExportEntitiesConfig): Promise<LongRunningResponse>;
444 export(config: ExportEntitiesConfig, callback: LongRunningCallback): void;
445 /**
446 * Get all of the indexes in this project.
447 *
448 * @param {GetIndexesOptions | GetIndexesCallback} [optionsOrCallback]
449 * @param {object} [options.gaxOptions] Request configuration options,
450 * outlined here:
451 * https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
452 * @param {GetIndexesResponse} [callback] The callback function.
453 * @param {?error} callback.error An error returned while making this request.
454 * @param {Index[]} callback.indexes All matching Index instances.
455 * @param {object} callback.apiResponse The full API response.
456 * @return {void | Promise<GetIndexesResponse>}
457 */
458 getIndexes(options?: GetIndexesOptions): Promise<GetIndexesResponse>;
459 getIndexes(options: GetIndexesOptions, callback: GetIndexesCallback): void;
460 getIndexes(callback: GetIndexesCallback): void;
461 /**
462 * Get all of the indexes in this project as a readable object stream.
463 *
464 * @param {GetIndexesOptions} [options] Configuration object. See
465 * {@link Datastore#getIndexes} for a complete list of options.
466 * @returns {ReadableStream<Index>}
467 */
468 getIndexesStream(options?: GetIndexesOptions): NodeJS.ReadableStream;
469 getProjectId(): Promise<string>;
470 /**
471 * Import entities into this project from a remote file.
472 *
473 * @param {ImportEntitiesConfig} config Configuration object.
474 * @param {string | File} config.file The `gs://bucket/file` path or a
475 * @google-cloud/storage File object.
476 * @param {string[]} [config.kinds] The kinds to include in this import.
477 * @param {string[]} [config.namespaces] The namespace IDs to include in this
478 * import.
479 * @param {object} [config.gaxOptions] Request configuration options, outlined
480 * here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
481 * @param {function} callback The callback function.
482 * @param {?error} callback.err An error returned while making this request.
483 * @param {Operation} callback.operation An operation object that can be used
484 * to check the status of the request.
485 */
486 import(config: ImportEntitiesConfig): Promise<LongRunningResponse>;
487 import(config: ImportEntitiesConfig, callback: LongRunningCallback): void;
488 /**
489 * Get a reference to an Index.
490 *
491 * @param {string} id The index name or id.
492 * @returns {Index}
493 */
494 index(id: string): Index;
495 /**
496 * Maps to {@link https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/datastore#_google_cloud_datastore_Datastore_save_member_1_|Datastore#save}, forcing the method to be `insert`.
497 *
498 * @param {object|object[]} entities Datastore key object(s).
499 * @param {Key} entities.key Datastore key object.
500 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
501 * indexing using a simple JSON path notation. See the examples in
502 * {@link Datastore#save} to see how to target properties at different
503 * levels of nesting within your entity.
504 * @param {object} entities.data Data to save with the provided key.
505 * @param {function} callback The callback function.
506 * @param {?error} callback.err An error returned while making this request
507 * @param {object} callback.apiResponse The full API response.
508 */
509 insert(entities: Entities): Promise<InsertResponse>;
510 insert(entities: Entities, callback: InsertCallback): void;
511 /**
512 * Insert or update the specified object(s). If a key is incomplete, its
513 * associated object is inserted and the original Key object is updated to
514 * contain the generated ID.
515 *
516 * This method will determine the correct Datastore method to execute
517 * (`upsert`, `insert`, or `update`) by using the key(s) provided. For
518 * example, if you provide an incomplete key (one without an ID), the request
519 * will create a new entity and have its ID automatically assigned. If you
520 * provide a complete key, the entity will be updated with the data specified.
521 *
522 * By default, all properties are indexed. To prevent a property from being
523 * included in *all* indexes, you must supply an `excludeFromIndexes` array.
524 *
525 * To prevent large properties from being included in *all* indexes, you must supply
526 * `excludeLargeProperties: true`.
527 * See below for an example.
528 *
529 * @borrows {@link Transaction#save} as save
530 *
531 * @throws {Error} If an unrecognized method is provided.
532 *
533 * @param {object|object[]} entities Datastore key object(s).
534 * @param {Key} entities.key Datastore key object.
535 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
536 * indexing using a simple JSON path notation. See the example below to
537 * see how to target properties at different levels of nesting within your
538 * @param {boolean} [entities.excludeLargeProperties] Automatically exclude
539 * large properties from indexing. It help in storing large values.
540 * @param {string} [entities.method] Explicit method to use, either 'insert',
541 * 'update', or 'upsert'.
542 * @param {object} entities.data Data to save with the provided key.
543 * entity.
544 * @param {object} [gaxOptions] Request configuration options, outlined here:
545 * https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
546 * @param {function} callback The callback function.
547 * @param {?error} callback.err An error returned while making this request
548 * @param {object} callback.apiResponse The full API response.
549 *
550 * @example
551 * ```
552 * //-
553 * // Save a single entity.
554 * //
555 * // Notice that we are providing an incomplete key. After saving, the
556 * // original Key object used to save will be updated to contain the path
557 * // with its generated ID.
558 * //-
559 * const key = datastore.key('Company');
560 * const entity = {
561 * key: key,
562 * data: {
563 * rating: '10'
564 * }
565 * };
566 *
567 * datastore.save(entity, (err) => {
568 * console.log(key.path); // [ 'Company', 5669468231434240 ]
569 * console.log(key.namespace); // undefined
570 * });
571 *
572 * //-
573 * // Save a single entity using a provided name instead of auto-generated ID.
574 * //
575 * // Here we are providing a key with name instead of an ID. After saving,
576 * // the original Key object used to save will be updated to contain the
577 * // path with the name instead of a generated ID.
578 * //-
579 * const key = datastore.key(['Company', 'donutshack']);
580 * const entity = {
581 * key: key,
582 * data: {
583 * name: 'DonutShack',
584 * rating: 8
585 * }
586 * };
587 *
588 * datastore.save(entity, (err) => {
589 * console.log(key.path); // ['Company', 'donutshack']
590 * console.log(key.namespace); // undefined
591 * });
592 *
593 * //-
594 * // Save a single entity with a provided namespace. Namespaces allow for
595 * // multitenancy. To read more about this, see
596 * // [the Datastore docs on key concepts](https://goo.gl/M1LUAu).
597 * //
598 * // Here we are providing a key with namespace.
599 * //-
600 * const key = datastore.key({
601 * namespace: 'my-namespace',
602 * path: ['Company', 'donutshack']
603 * });
604 *
605 * const entity = {
606 * key: key,
607 * data: {
608 * name: 'DonutShack',
609 * rating: 8
610 * }
611 * };
612 *
613 * datastore.save(entity, (err) => {
614 * console.log(key.path); // ['Company', 'donutshack']
615 * console.log(key.namespace); // 'my-namespace'
616 * });
617 *
618 * //-
619 * // Save different types of data, including ints, doubles, dates, booleans,
620 * // blobs, and lists.
621 * //
622 * // Notice that we are providing an incomplete key. After saving, the
623 * // original Key object used to save will be updated to contain the path
624 * // with its generated ID.
625 * //-
626 * const key = datastore.key('Company');
627 * const entity = {
628 * key: key,
629 * data: {
630 * name: 'DonutShack',
631 * rating: datastore.int(10),
632 * worth: datastore.double(123456.78),
633 * location: datastore.geoPoint({
634 * latitude: 40.6894,
635 * longitude: -74.0447
636 * }),
637 * numDonutsServed: 45,
638 * founded: new Date('Tue May 12 2015 15:30:00 GMT-0400 (EDT)'),
639 * isStartup: true,
640 * donutEmoji: Buffer.from('\uD83C\uDF69'),
641 * keywords: [
642 * 'donut',
643 * 'coffee',
644 * 'yum'
645 * ]
646 * }
647 * };
648 *
649 * datastore.save(entity, (err, apiResponse) => {});
650 *
651 * //-
652 * // Use an array, `excludeFromIndexes`, to exclude properties from indexing.
653 * // This will allow storing string values larger than 1500 bytes.
654 * //-
655 * const entity = {
656 * key: datastore.key('Company'),
657 * excludeFromIndexes: [
658 * 'description',
659 * 'embeddedEntity.description',
660 * 'arrayValue[]',
661 * 'arrayValue[].description'
662 * ],
663 * data: {
664 * description: 'Long string (...)',
665 * embeddedEntity: {
666 * description: 'Long string (...)'
667 * },
668 * arrayValue: [
669 * 'Long string (...)',
670 * {
671 * description: 'Long string (...)'
672 * }
673 * ]
674 * }
675 * };
676 *
677 * datastore.save(entity, (err, apiResponse) => {});
678 *
679 * //-
680 * // Use boolean `excludeLargeProperties`, to auto exclude Large properties from indexing.
681 * // This will allow storing string values larger than 1500 bytes.
682 * //-
683 * const entity = {
684 * key: datastore.key('Company'),
685 * data: {
686 * description: 'Long string (...)',
687 * embeddedEntity: {
688 * description: 'Long string (...)'
689 * },
690 * arrayValue: [
691 * 'Long string (...)',
692 * {
693 * description: 'Long string (...)'
694 * }
695 * ]
696 * },
697 * excludeLargeProperties: true
698 * };
699 *
700 * datastore.save(entity, (err, apiResponse) => {});
701 *
702 * //-
703 * // Save multiple entities at once.
704 * //-
705 * const companyKey = datastore.key(['Company', 123]);
706 * const productKey = datastore.key(['Product', 'Computer']);
707 * const entities = [
708 * {
709 * key: companyKey,
710 * data: {
711 * HQ: 'Dallas, TX'
712 * }
713 * },
714 * {
715 * key: productKey,
716 * data: {
717 * vendor: 'Dell'
718 * }
719 * }
720 * ];
721 *
722 * datastore.save(entities, (err, apiResponse) => {});
723 *
724 * //-
725 * // Explicitly attempt to 'insert' a specific entity.
726 * //-
727 * const userKey = datastore.key(['User', 'chilts']);
728 * const entity = {
729 * key: userKey,
730 * method: 'insert',
731 * data: {
732 * fullName: 'Andrew Chilton'
733 * }
734 * };
735 *
736 * datastore.save(entity, (err, apiResponse) => {});
737 *
738 * //-
739 * // Returns a Promise if callback is omitted.
740 * //-
741 * datastore.save(entity).then((data) => {
742 * const apiResponse = data[0];
743 * });
744 * ```
745 */
746 save(entities: Entities, gaxOptions?: CallOptions): Promise<SaveResponse>;
747 save(entities: Entities, gaxOptions: CallOptions, callback: SaveCallback): void;
748 save(entities: Entities, callback: SaveCallback): void;
749 /**
750 * Maps to {@link https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/datastore#_google_cloud_datastore_Datastore_save_member_1_|Datastore#save}, forcing the method to be `update`.
751 *
752 * @param {object|object[]} entities Datastore key object(s).
753 * @param {Key} entities.key Datastore key object.
754 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
755 * indexing using a simple JSON path notation. See the examples in
756 * {@link Datastore#save} to see how to target properties at different
757 * levels of nesting within your entity.
758 * @param {object} entities.data Data to save with the provided key.
759 * @param {function} callback The callback function.
760 * @param {?error} callback.err An error returned while making this request
761 * @param {object} callback.apiResponse The full API response.
762 */
763 update(entities: Entities): Promise<UpdateResponse>;
764 update(entities: Entities, callback: UpdateCallback): void;
765 /**
766 * Maps to {@link https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/datastore#_google_cloud_datastore_Datastore_save_member_1_|Datastore#save}, forcing the method to be `upsert`.
767 *
768 * @param {object|object[]} entities Datastore key object(s).
769 * @param {Key} entities.key Datastore key object.
770 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
771 * indexing using a simple JSON path notation. See the examples in
772 * {@link Datastore#save} to see how to target properties at different
773 * levels of nesting within your entity.
774 * @param {object} entities.data Data to save with the provided key.
775 * @param {function} callback The callback function.
776 * @param {?error} callback.err An error returned while making this request
777 * @param {object} callback.apiResponse The full API response.
778 */
779 upsert(entities: Entities): Promise<UpsertResponse>;
780 upsert(entities: Entities, callback: UpsertCallback): void;
781 /**
782 * Helper function to get a Datastore Double object.
783 *
784 * @param {number} value The double value.
785 * @returns {object}
786 *
787 * @example
788 * ```
789 * const {Datastore} = require('@google-cloud/datastore');
790 * const datastore = new Datastore();
791 * const threeDouble = datastore.double(3.0);
792 * ```
793 */
794 static double(value: number): entity.Double;
795 double(value: number): entity.Double;
796 /**
797 * Helper function to check if something is a Datastore Double object.
798 *
799 * @param {*} value
800 * @returns {boolean}
801 *
802 * @example
803 * ```
804 * const {Datastore} = require('@google-cloud/datastore');
805 * const datastore = new Datastore();
806 * datastore.isDouble(0.42); // false
807 * datastore.isDouble(datastore.double(0.42)); // true
808 * ```
809 */
810 static isDouble(value?: {}): boolean;
811 isDouble(value?: {}): boolean;
812 /**
813 * Helper function to get a Datastore Geo Point object.
814 *
815 * @param {object} coordinates Coordinate value.
816 * @param {number} coordinates.latitude Latitudinal value.
817 * @param {number} coordinates.longitude Longitudinal value.
818 * @returns {object}
819 *
820 * @example
821 * ```
822 * const {Datastore} = require('@google-cloud/datastore');
823 * const datastore = new Datastore();
824 * const coordinates = {
825 * latitude: 40.6894,
826 * longitude: -74.0447
827 * };
828 *
829 * const geoPoint = datastore.geoPoint(coordinates);
830 *
831 * //-
832 * // List all companies that are located at 40.123 latitude
833 * // and -74.0447 longitude.
834 * //-
835 * const query = datastore.createQuery('Company');
836 * const companyQuery = query
837 * .filter('geoPoint.latitude', datastore.double(40.123))
838 * .filter('geoPoint.longitude', datastore.double(-74.0447));
839 * ```
840 */
841 static geoPoint(coordinates: entity.Coordinates): entity.GeoPoint;
842 geoPoint(coordinates: entity.Coordinates): entity.GeoPoint;
843 /**
844 * Helper function to check if something is a Datastore Geo Point object.
845 *
846 * @param {*} value
847 * @returns {boolean}
848 *
849 * @example
850 * ```
851 * const {Datastore} = require('@google-cloud/datastore');
852 * const datastore = new Datastore();
853 * const coordinates = {
854 * latitude: 0,
855 * longitude: 0
856 * };
857 *
858 * datastore.isGeoPoint(coordinates); // false
859 * datastore.isGeoPoint(datastore.geoPoint(coordinates)); // true
860 * ```
861 */
862 static isGeoPoint(value?: {}): boolean;
863 isGeoPoint(value?: {}): boolean;
864 /**
865 * Helper function to get a Datastore Integer object.
866 *
867 * This is also useful when using an ID outside the bounds of a JavaScript
868 * Number object.
869 *
870 * @param {number} value The integer value.
871 * @returns {object}
872 *
873 * @example
874 * ```
875 * const {Datastore} = require('@google-cloud/datastore');
876 * const datastore = new Datastore();
877 * const sevenInteger = datastore.int(7);
878 *
879 * //-
880 * // Create an Int to support long Key IDs.
881 * //-
882 * const key = datastore.key([
883 * 'Kind',
884 * datastore.int('100000000000001234')
885 * ]);
886 * ```
887 */
888 static int(value: number | string): entity.Int;
889 int(value: number | string): entity.Int;
890 /**
891 * Helper function to check if something is a Datastore Integer object.
892 *
893 * @param {*} value
894 * @returns {boolean}
895 *
896 * @example
897 * ```
898 * const {Datastore} = require('@google-cloud/datastore');
899 * const datastore = new Datastore();
900 * datastore.isInt(42); // false
901 * datastore.isInt(datastore.int(42)); // true
902 * ```
903 */
904 static isInt(value?: {}): boolean;
905 isInt(value?: {}): boolean;
906 /**
907 * Access the Key from an Entity object.
908 *
909 * @name Datastore.KEY
910 * @type {symbol}
911 */
912 /**
913 * Access the Key from an Entity object.
914 *
915 * @name Datastore#KEY
916 * @type {symbol}
917 */
918 static KEY: typeof entity.KEY_SYMBOL;
919 KEY: typeof entity.KEY_SYMBOL;
920 /**
921 * This is one of three values which may be returned from
922 * {@link Datastore#runQuery}, {@link Transaction#runQuery}, and
923 * {@link Query#run} as `info.moreResults`.
924 *
925 * There *may* be more results after the specified end cursor.
926 *
927 * @type {string}
928 */
929 static MORE_RESULTS_AFTER_CURSOR: string;
930 MORE_RESULTS_AFTER_CURSOR: string;
931 /**
932 * This is one of three values which may be returned from
933 * {@link Datastore#runQuery}, {@link Transaction#runQuery}, and
934 * {@link Query#run} as `info.moreResults`.
935 *
936 * There *may* be more results after the specified limit.
937 *
938 * @type {string}
939 */
940 static MORE_RESULTS_AFTER_LIMIT: string;
941 MORE_RESULTS_AFTER_LIMIT: string;
942 /**
943 * This is one of three values which may be returned from
944 * {@link Datastore#runQuery}, {@link Transaction#runQuery}, and
945 * {@link Query#run} as `info.moreResults`.
946 *
947 * There are no more results left to query for.
948 *
949 * @type {string}
950 */
951 static NO_MORE_RESULTS: string;
952 NO_MORE_RESULTS: string;
953 /**
954 * Create a query for the specified kind. See {@link Query} for all
955 * of the available methods.
956 *
957 * @see {@link https://cloud.google.com/datastore/docs/concepts/queries| Datastore Queries}
958 * @see {@link Query}
959 *
960 * @param {string} [namespace] Namespace.
961 * @param {string} kind The kind to query.
962 * @returns {Query}
963 *
964 * @example
965 * ```
966 * const {Datastore} = require('@google-cloud/datastore');
967 * const datastore = new Datastore();
968 * const query = datastore.createQuery('Company');
969 * ```
970 */
971 createQuery(kind?: string): Query;
972 createQuery(kind?: string[]): Query;
973 createQuery(namespace: string, kind: string): Query;
974 createQuery(namespace: string, kind: string[]): Query;
975 /**
976 * Helper to create a Key object, scoped to the instance's namespace by
977 * default.
978 *
979 * You may also specify a configuration object to define a namespace and path.
980 *
981 * @param {object|string|array} [options] Key path. To specify or override a namespace,
982 * you must use an object here to explicitly state it.
983 * @param {string|array} [options.path] Key path.
984 * @param {string} [options.namespace] Optional namespace.
985 * @returns {Key} A newly created Key from the options given.
986 *
987 * @example
988 * ```
989 * <caption>Create an incomplete key with a kind value of `Company`.
990 * Since no Id is supplied, Datastore will generate one on save.</caption>
991 * const {Datastore} = require('@google-cloud/datastore');
992 * const datastore = new Datastore();
993 * const key = datastore.key('Company');
994 *
995 * ```
996 * @example
997 * ```
998 * <caption>Create a complete key with a kind value of `Company` and Id `123`.</caption>
999 * const {Datastore} = require('@google-cloud/datastore');
1000 * const datastore = new Datastore();
1001 * const key = datastore.key(['Company', 123]);
1002 *
1003 * ```
1004 * @example
1005 * ```
1006 * <caption>If the ID integer is outside the bounds of a JavaScript Number
1007 * object, create an Int.</caption>
1008 * const {Datastore} = require('@google-cloud/datastore');
1009 * const datastore = new Datastore();
1010 * const key = datastore.key([
1011 * 'Company',
1012 * datastore.int('100000000000001234')
1013 * ]);
1014 *
1015 * ```
1016 * @example
1017 * ```
1018 * <caption>Create a complete key with a kind value of `Company` and name `Google`.
1019 * Because the supplied Id is a string, Datastore will prefix it with "name=".
1020 * Had the supplied Id been numeric, Datastore would prefix it with the standard, "id=".</caption>
1021 * const {Datastore} = require('@google-cloud/datastore');
1022 * const datastore = new Datastore();
1023 * const key = datastore.key(['Company', 'Google']);
1024 *
1025 * ```
1026 * @example
1027 * ```
1028 * <caption>Create a complete key from a provided namespace and path.</caption>
1029 * const {Datastore} = require('@google-cloud/datastore');
1030 * const datastore = new Datastore();
1031 * const key = datastore.key({
1032 * namespace: 'My-NS',
1033 * path: ['Company', 123]
1034 * });
1035 *
1036 * ```
1037 * @example
1038 * ```
1039 * <caption>Create a complete key that specifies an ancestor. This will create a Team entity
1040 * with a name of "Datastore", which belongs to the Company with the "name=Google" key.</caption>
1041 * const {Datastore} = require('@google-cloud/datastore');
1042 * const datastore = new Datastore();
1043 * const key = datastore.key(['Company', 'Google', 'Team', 'Datastore']);
1044 *
1045 * ```
1046 * @example
1047 * ```
1048 * <caption>Create a incomplete key that specifies an ancestor. This will create an Employee entity
1049 * with an auto-generated Id, which belongs to the Company with the "name=Google" key.</caption>
1050 * const {Datastore} = require('@google-cloud/datastore');
1051 * const datastore = new Datastore();
1052 * const key = datastore.key(['Company', 'Google', 'Employee']);
1053 * ```
1054 */
1055 key(options: entity.KeyOptions): entity.Key;
1056 key(path: PathType[]): entity.Key;
1057 key(path: string): entity.Key;
1058 /**
1059 * Helper function to check if something is a Datastore Key object.
1060 *
1061 * @param {*} value
1062 * @returns {boolean}
1063 *
1064 * @example
1065 * ```
1066 * const {Datastore} = require('@google-cloud/datastore');
1067 * const datastore = new Datastore();
1068 * datastore.isKey({path: ['Company', 123]}); // false
1069 * datastore.isKey(datastore.key(['Company', 123])); // true
1070 * ```
1071 */
1072 static isKey(value?: {}): boolean;
1073 isKey(value?: {}): boolean;
1074 /**
1075 * Helper to create a URL safe key.
1076 *
1077 * This is intended to work with the "legacy" representation of a
1078 * datastore "Key" used within Google App Engine (a so-called "Reference").
1079 * The returned string can be used as the "urlsafe"
1080 * The base64 encoded values will have padding removed.
1081 *
1082 *
1083 * @param {entity.Key} key Entity key object.
1084 * @param {string} locationPrefix Optional .
1085 * The location prefix of an App Engine project ID.
1086 * Often this value is 's~', but may also be 'e~', or other location prefixes
1087 * currently unknown.
1088 * @param {function} callback The callback function.
1089 * @param {?error} callback.err An error returned while making this request
1090 * @param {string} callback.urlSafeKey A Base64-encoded URL-safe key.
1091 *
1092 * @example
1093 * ```
1094 * const {Datastore} = require('@google-cloud/datastore');
1095 * const datastore = new Datastore();
1096 * const key = datastore.key(['Company', 'Google']);
1097 *
1098 * datastore.keyToLegacyUrlSafe(key, (err, urlSafeKey) => {
1099 * if (err) {
1100 * // Error handling omitted.
1101 * }
1102 * console.log(urlSafeKey);
1103 * });
1104 *
1105 * //-
1106 * // Create a complete URL-safe key using a location prefix.
1107 * //-
1108 * const locationPrefix = 's~';
1109 *
1110 * datastore.keyToLegacyUrlSafe(key, locationPrefix, (err, urlSafeKey) => {
1111 * if (err) {
1112 * // Error handling omitted.
1113 * }
1114 * console.log(urlSafeKey);
1115 * });
1116 *
1117 * //-
1118 * // If the callback is omitted, we'll return a Promise.
1119 * //-
1120 * datastore.keyToLegacyUrlSafe(key).then((data) => {
1121 * const urlSafeKey = data[0];
1122 * console.log(urlSafeKey);
1123 * });
1124 * ```
1125 */
1126 keyToLegacyUrlSafe(key: entity.Key, locationPrefix?: string): Promise<string>;
1127 keyToLegacyUrlSafe(key: entity.Key, callback: KeyToLegacyUrlSafeCallback): void;
1128 keyToLegacyUrlSafe(key: entity.Key, locationPrefix: string, callback: KeyToLegacyUrlSafeCallback): void;
1129 /**
1130 * Helper to convert URL safe key string to entity key object
1131 *
1132 * This is intended to work with the "legacy" representation of a
1133 * datastore "Key" used within Google App Engine (a so-called "Reference").
1134 *
1135 * @param {entity.Key} key Entity key object.
1136 * @param {string} locationPrefix Optional .
1137 * The location prefix of an App Engine project ID.
1138 * Often this value is 's~', but may also be 'e~', or other location prefixes
1139 * currently unknown.
1140 * @returns {string} Created urlsafe key.
1141 *
1142 * @example
1143 * ```
1144 * const {Datastore} = require('@google-cloud/datastore');
1145 * const datastore = new Datastore();
1146 * const urlSafeKey = 'ag9ncmFzcy1jbHVtcC00NzlyEwsSB0NvbXBhbnkiBkdvb2dsZQw';
1147 *
1148 * datastore.keyFromLegacyUrlsafe(key);
1149 *
1150 * ```
1151 */
1152 keyFromLegacyUrlsafe(key: string): entity.Key;
1153 /**
1154 * Create a new Transaction object.
1155 *
1156 * @param {object} [options] Configuration object.
1157 * @param {string} [options.id] The ID of a previously run transaction.
1158 * @param {boolean} [options.readOnly=false] A read-only transaction cannot
1159 * modify entities.
1160 * @returns {Transaction}
1161 *
1162 * @example
1163 * ```
1164 * const {Datastore} = require('@google-cloud/datastore');
1165 * const datastore = new Datastore();
1166 * const transaction = datastore.transaction();
1167 * ```
1168 */
1169 transaction(options?: TransactionOptions): Transaction;
1170 /**
1171 * Determine the appropriate endpoint to use for API requests. If not
1172 * explicitly defined, check for the "DATASTORE_EMULATOR_HOST" environment
1173 * variable, used to connect to a local Datastore server.
1174 *
1175 * @private
1176 *
1177 * @param {string} customApiEndpoint Custom API endpoint.
1178 */
1179 determineBaseUrl_(customApiEndpoint?: string): void;
1180 /**
1181 * {@link DatastoreRequest} class.
1182 *
1183 * @name Datastore.DatastoreRequest
1184 * @see DatastoreRequest
1185 * @type {constructor}
1186 */
1187 DatastoreRequest: typeof DatastoreRequest;
1188 /**
1189 * {@link Query} class.
1190 *
1191 * @name Datastore.Query
1192 * @see Query
1193 * @type {constructor}
1194 */
1195 Query: typeof Query;
1196 /**
1197 * {@link Transaction} class.
1198 *
1199 * @name Datastore.Transaction
1200 * @see Transaction
1201 * @type {constructor}
1202 */
1203 Transaction: typeof Transaction;
1204}
1205export { Datastore };
1206export interface TransactionOptions {
1207 id?: string;
1208 readOnly?: boolean;
1209}
1210export { Index, DatastoreRequest, Query, Transaction };
1211export interface DatastoreOptions extends GoogleAuthOptions {
1212 namespace?: string;
1213 apiEndpoint?: string;
1214 sslCreds?: ChannelCredentials;
1215}
1216export interface KeyToLegacyUrlSafeCallback {
1217 (err?: Error | null, urlSafeKey?: string): void;
1218}
1219declare const v1: any;
1220export { v1 };
1221export { DatastoreClient, DatastoreAdminClient } from './v1';