UNPKG

20.5 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 */
16import { CallOptions } from 'google-gax';
17import { google } from '../protos/protos';
18import { Datastore, TransactionOptions } from '.';
19import { Entities, Entity, entity } from './entity';
20import { Query, RunQueryCallback, RunQueryOptions, RunQueryResponse } from './query';
21import { CommitCallback, CommitResponse, CreateReadStreamOptions, DatastoreRequest, GetCallback, GetResponse, RequestCallback } from './request';
22import { AggregateQuery } from './aggregate';
23/**
24 * A transaction is a set of Datastore operations on one or more entities. Each
25 * transaction is guaranteed to be atomic, which means that transactions are
26 * never partially applied. Either all of the operations in the transaction are
27 * applied, or none of them are applied.
28 *
29 * @see {@link https://cloud.google.com/datastore/docs/concepts/transactions| Transactions Reference}
30 *
31 * @class
32 * @extends {Request}
33 * @param {Datastore} datastore A Datastore instance.
34 * @mixes module:datastore/request
35 *
36 * @example
37 * ```
38 * const {Datastore} = require('@google-cloud/datastore');
39 * const datastore = new Datastore();
40 * const transaction = datastore.transaction();
41 * ```
42 */
43declare class Transaction extends DatastoreRequest {
44 #private;
45 namespace?: string;
46 readOnly: boolean;
47 request: Function;
48 modifiedEntities_: ModifiedEntities;
49 skipCommit?: boolean;
50 constructor(datastore: Datastore, options?: TransactionOptions);
51 /*! Developer Documentation
52 *
53 * Below, we override two methods that we inherit from DatastoreRequest:
54 * `delete` and `save`. This is done because:
55 *
56 * A) the documentation needs to be different for a transactional save, and
57 * B) we build up a "modifiedEntities_" array on this object, used to build
58 * the final commit request with.
59 */
60 /**
61 * Commit the remote transaction and finalize the current transaction
62 * instance.
63 *
64 * If the commit request fails, we will automatically rollback the
65 * transaction.
66 *
67 * @param {object} [gaxOptions] Request configuration options, outlined here:
68 * https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
69 * @param {function} callback The callback function.
70 * @param {?error} callback.err An error returned while making this request.
71 * If the commit fails, we automatically try to rollback the transaction
72 * (see {module:datastore/transaction#rollback}).
73 * @param {object} callback.apiResponse The full API response.
74 *
75 * @example
76 * ```
77 * const {Datastore} = require('@google-cloud/datastore');
78 * const datastore = new Datastore();
79 * const transaction = datastore.transaction();
80 *
81 * transaction.commit((err, apiResponse) => {
82 * if (err) {
83 * // Transaction could not be committed.
84 * }
85 * });
86 *
87 * //-
88 * // If the callback is omitted, we'll return a Promise.
89 * //-
90 * transaction.commit().then((data) => {
91 * const apiResponse = data[0];
92 * });
93 * ```
94 */
95 commit(gaxOptions?: CallOptions): Promise<CommitResponse>;
96 /**
97 * @param {object} [gaxOptions] Request configuration options, outlined here:
98 * https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
99 * @param {function} callback The callback function.
100 * @param {?error} callback.err An error returned while making this request.
101 * If the commit fails, we automatically try to rollback the transaction
102 * (see {module:datastore/transaction#rollback}).
103 * @param {object} callback.apiResponse The full API response.
104 */
105 commit(callback: CommitCallback): void;
106 commit(gaxOptions: CallOptions, callback: CommitCallback): void;
107 /**
108 * Create a query for the specified kind. See {module:datastore/query} for all
109 * of the available methods.
110 *
111 * @see {@link https://cloud.google.com/datastore/docs/concepts/queries| Datastore Queries}
112 *
113 * @see {@link Query}
114 *
115 * @param {string} [namespace] Namespace.
116 * @param {string} kind The kind to query.
117 * @returns {Query}
118 *
119 * @example
120 * ```
121 * const {Datastore} = require('@google-cloud/datastore');
122 * const datastore = new Datastore();
123 * const transaction = datastore.transaction();
124 *
125 * // Run the query inside the transaction.
126 * transaction.run((err) => {
127 * if (err) {
128 * // Error handling omitted.
129 * }
130 * const ancestorKey = datastore.key(['ParentCompany', 'Alphabet']);
131 *
132 * const query = transaction.createQuery('Company')
133 * .hasAncestor(ancestorKey);
134 *
135 * query.run((err, entities) => {
136 * if (err) {
137 * // Error handling omitted.
138 * }
139 *
140 * transaction.commit((err) => {
141 * if (!err) {
142 * // Transaction committed successfully.
143 * }
144 * });
145 * });
146 * });
147 *
148 * // Run the query inside the transaction.with namespace
149 * transaction.run((err) => {
150 * if (err) {
151 * // Error handling omitted.
152 * }
153 * const ancestorKey = datastore.key(['ParentCompany', 'Alphabet']);
154 *
155 * const query = transaction.createQuery('CompanyNamespace', 'Company')
156 * .hasAncestor(ancestorKey);
157 *
158 * query.run((err, entities) => {
159 * if (err) {
160 * // Error handling omitted.
161 * }
162 *
163 * transaction.commit((err) => {
164 * if (!err) {
165 * // Transaction committed successfully.
166 * }
167 * });
168 * });
169 * });
170 * ```
171 */
172 createQuery(kind?: string): Query;
173 createQuery(kind?: string[]): Query;
174 createQuery(namespace: string, kind: string): Query;
175 createQuery(namespace: string, kind: string[]): Query;
176 /**
177 * Create an aggregation query from the query specified. See {module:datastore/query} for all
178 * of the available methods.
179 *
180 */
181 createAggregationQuery(query: Query): AggregateQuery;
182 /**
183 * Delete all entities identified with the specified key(s) in the current
184 * transaction.
185 *
186 * @param {Key|Key[]} key Datastore key object(s).
187 *
188 * @example
189 * ```
190 * const {Datastore} = require('@google-cloud/datastore');
191 * const datastore = new Datastore();
192 * const transaction = datastore.transaction();
193 *
194 * transaction.run((err) => {
195 * if (err) {
196 * // Error handling omitted.
197 * }
198 *
199 * // Delete a single entity.
200 * transaction.delete(datastore.key(['Company', 123]));
201 *
202 * // Delete multiple entities at once.
203 * transaction.delete([
204 * datastore.key(['Company', 123]),
205 * datastore.key(['Product', 'Computer'])
206 * ]);
207 *
208 * transaction.commit((err) => {
209 * if (!err) {
210 * // Transaction committed successfully.
211 * }
212 * });
213 * });
214 * ```
215 */
216 delete(entities?: Entities): any;
217 /**
218 * This function calls get on the super class. If the transaction
219 * has not been started yet then the transaction is started before the
220 * get call is made.
221 *
222 * @param {Key|Key[]} keys Datastore key object(s).
223 * @param {object} [options] Optional configuration.
224 * @param {function} callback The callback function.
225 *
226 */
227 get(keys: entity.Key | entity.Key[], options?: CreateReadStreamOptions): Promise<GetResponse>;
228 get(keys: entity.Key | entity.Key[], callback: GetCallback): void;
229 get(keys: entity.Key | entity.Key[], options: CreateReadStreamOptions, callback: GetCallback): void;
230 /**
231 * Maps to {@link https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/transaction#_google_cloud_datastore_Transaction_save_member_1_|Datastore#save}, forcing the method to be `insert`.
232 *
233 * @param {object|object[]} entities Datastore key object(s).
234 * @param {Key} entities.key Datastore key object.
235 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
236 * indexing using a simple JSON path notation. See the examples in
237 * {@link Datastore#save} to see how to target properties at different
238 * levels of nesting within your entity.
239 * @param {object} entities.data Data to save with the provided key.
240 */
241 insert(entities: Entities): void;
242 /**
243 * Reverse a transaction remotely and finalize the current transaction
244 * instance.
245 *
246 * @param {object} [gaxOptions] Request configuration options, outlined here:
247 * https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
248 * @param {function} callback The callback function.
249 * @param {?error} callback.err An error returned while making this request.
250 * @param {object} callback.apiResponse The full API response.
251 *
252 * @example
253 * ```
254 * const {Datastore} = require('@google-cloud/datastore');
255 * const datastore = new Datastore();
256 * const transaction = datastore.transaction();
257 *
258 * transaction.run((err) => {
259 * if (err) {
260 * // Error handling omitted.
261 * }
262 *
263 * transaction.rollback((err) => {
264 * if (!err) {
265 * // Transaction rolled back successfully.
266 * }
267 * });
268 * });
269 *
270 * //-
271 * // If the callback is omitted, we'll return a Promise.
272 * //-
273 * transaction.rollback().then((data) => {
274 * const apiResponse = data[0];
275 * });
276 * ```
277 */
278 rollback(callback: RollbackCallback): void;
279 rollback(gaxOptions?: CallOptions): Promise<RollbackResponse>;
280 rollback(gaxOptions: CallOptions, callback: RollbackCallback): void;
281 /**
282 * Begin a remote transaction. In the callback provided, run your
283 * transactional commands.
284 *
285 * @param {object} [options] Configuration object.
286 * @param {object} [options.gaxOptions] Request configuration options, outlined
287 * here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
288 * @param {boolean} [options.readOnly=false] A read-only transaction cannot
289 * modify entities.
290 * @param {string} [options.transactionId] The ID of a previous transaction.
291 * @param {function} callback The function to execute within the context of
292 * a transaction.
293 * @param {?error} callback.err An error returned while making this request.
294 * @param {Transaction} callback.transaction This transaction
295 * instance.
296 * @param {object} callback.apiResponse The full API response.
297 *
298 * @example
299 * ```
300 * const {Datastore} = require('@google-cloud/datastore');
301 * const datastore = new Datastore();
302 * const transaction = datastore.transaction();
303 *
304 * transaction.run((err, transaction) => {
305 * // Perform Datastore transactional operations.
306 * const key = datastore.key(['Company', 123]);
307 *
308 * transaction.get(key, (err, entity) => {
309 * entity.name = 'Google';
310 *
311 * transaction.save({
312 * key: key,
313 * data: entity
314 * });
315 *
316 * transaction.commit((err) => {
317 * if (!err) {
318 * // Data saved successfully.
319 * }
320 * });
321 * });
322 * });
323 *
324 * //-
325 * // If the callback is omitted, we'll return a Promise.
326 * //-
327 * transaction.run().then((data) => {
328 * const transaction = data[0];
329 * const apiResponse = data[1];
330 * });
331 * ```
332 */
333 run(options?: RunOptions): Promise<RunResponse>;
334 run(callback: RunCallback): void;
335 run(options: RunOptions, callback: RunCallback): void;
336 /**
337 *
338 * This function calls runAggregationQuery on the super class. If the transaction
339 * has not been started yet then the transaction is started before the
340 * runAggregationQuery call is made.
341 *
342 * @param {AggregateQuery} [query] AggregateQuery object.
343 * @param {RunQueryOptions} [options] Optional configuration
344 * @param {function} [callback] The callback function. If omitted, a promise is
345 * returned.
346 *
347 **/
348 runAggregationQuery(query: AggregateQuery, options?: RunQueryOptions): Promise<RunQueryResponse>;
349 runAggregationQuery(query: AggregateQuery, options: RunQueryOptions, callback: RequestCallback): void;
350 runAggregationQuery(query: AggregateQuery, callback: RequestCallback): void;
351 /**
352 * This function calls runQuery on the super class. If the transaction
353 * has not been started yet then the transaction is started before the
354 * runQuery call is made.
355 *
356 * @param {Query} query A Query object
357 * @param {object} [options] Optional configuration.
358 * @param {function} [callback] The callback function. If omitted, a readable
359 * stream instance is returned.
360 *
361 */
362 runQuery(query: Query, options?: RunQueryOptions): Promise<RunQueryResponse>;
363 runQuery(query: Query, options: RunQueryOptions, callback: RunQueryCallback): void;
364 runQuery(query: Query, callback: RunQueryCallback): void;
365 /**
366 * Insert or update the specified object(s) in the current transaction. If a
367 * key is incomplete, its associated object is inserted and the original Key
368 * object is updated to contain the generated ID.
369 *
370 * This method will determine the correct Datastore method to execute
371 * (`upsert`, `insert`, or `update`) by using the key(s) provided. For
372 * example, if you provide an incomplete key (one without an ID), the request
373 * will create a new entity and have its ID automatically assigned. If you
374 * provide a complete key, the entity will be updated with the data specified.
375 *
376 * By default, all properties are indexed. To prevent a property from being
377 * included in *all* indexes, you must supply an `excludeFromIndexes` array.
378 * See below for an example.
379 *
380 * @param {object|object[]} entities Datastore key object(s).
381 * @param {Key} entities.key Datastore key object.
382 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
383 * indexing using a simple JSON path notation. See the example below to
384 * see how to target properties at different levels of nesting within your
385 * entity.
386 * @param {object} entities.data Data to save with the provided key.
387 *
388 * @example
389 * ```
390 * <caption>Save a single entity.</caption>
391 * const {Datastore} = require('@google-cloud/datastore');
392 * const datastore = new Datastore();
393 * const transaction = datastore.transaction();
394 *
395 * // Notice that we are providing an incomplete key. After the transaction is
396 * // committed, the Key object held by the `key` variable will be populated
397 * // with a path containing its generated ID.
398 * //-
399 * const key = datastore.key('Company');
400 *
401 * transaction.run((err) => {
402 * if (err) {
403 * // Error handling omitted.
404 * }
405 *
406 * transaction.save({
407 * key: key,
408 * data: {
409 * rating: '10'
410 * }
411 * });
412 *
413 * transaction.commit((err) => {
414 * if (!err) {
415 * // Data saved successfully.
416 * }
417 * });
418 * });
419 *
420 * ```
421 * @example
422 * ```
423 * const {Datastore} = require('@google-cloud/datastore');
424 * const datastore = new Datastore();
425 * const transaction = datastore.transaction();
426 *
427 * // Use an array, `excludeFromIndexes`, to exclude properties from indexing.
428 * // This will allow storing string values larger than 1500 bytes.
429 *
430 * transaction.run((err) => {
431 * if (err) {
432 * // Error handling omitted.
433 * }
434 *
435 * transaction.save({
436 * key: key,
437 * excludeFromIndexes: [
438 * 'description',
439 * 'embeddedEntity.description',
440 * 'arrayValue[].description'
441 * ],
442 * data: {
443 * description: 'Long string (...)',
444 * embeddedEntity: {
445 * description: 'Long string (...)'
446 * },
447 * arrayValue: [
448 * {
449 * description: 'Long string (...)'
450 * }
451 * ]
452 * }
453 * });
454 *
455 * transaction.commit((err) => {
456 * if (!err) {
457 * // Data saved successfully.
458 * }
459 * });
460 * });
461 *
462 * ```
463 * @example
464 * ```
465 * <caption>Save multiple entities at once.</caption>
466 * const {Datastore} = require('@google-cloud/datastore');
467 * const datastore = new Datastore();
468 * const transaction = datastore.transaction();
469 * const companyKey = datastore.key(['Company', 123]);
470 * const productKey = datastore.key(['Product', 'Computer']);
471 *
472 * transaction.run((err) => {
473 * if (err) {
474 * // Error handling omitted.
475 * }
476 *
477 * transaction.save([
478 * {
479 * key: companyKey,
480 * data: {
481 * HQ: 'Dallas, TX'
482 * }
483 * },
484 * {
485 * key: productKey,
486 * data: {
487 * vendor: 'Dell'
488 * }
489 * }
490 * ]);
491 *
492 * transaction.commit((err) => {
493 * if (!err) {
494 * // Data saved successfully.
495 * }
496 * });
497 * });
498 * ```
499 */
500 save(entities: Entities): void;
501 /**
502 * Maps to {@link https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/transaction#_google_cloud_datastore_Transaction_save_member_1_|Datastore#save}, forcing the method to be `update`.
503 *
504 * @param {object|object[]} entities Datastore key object(s).
505 * @param {Key} entities.key Datastore key object.
506 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
507 * indexing using a simple JSON path notation. See the examples in
508 * {@link Datastore#save} to see how to target properties at different
509 * levels of nesting within your entity.
510 * @param {object} entities.data Data to save with the provided key.
511 */
512 update(entities: Entities): void;
513 /**
514 * Maps to {@link https://cloud.google.com/nodejs/docs/reference/datastore/latest/datastore/transaction#_google_cloud_datastore_Transaction_save_member_1_|Datastore#save}, forcing the method to be `upsert`.
515 *
516 * @param {object|object[]} entities Datastore key object(s).
517 * @param {Key} entities.key Datastore key object.
518 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
519 * indexing using a simple JSON path notation. See the examples in
520 * {@link Datastore#save} to see how to target properties at different
521 * levels of nesting within your entity.
522 * @param {object} entities.data Data to save with the provided key.
523 */
524 upsert(entities: Entities): void;
525}
526export type ModifiedEntities = Array<{
527 entity: {
528 key: Entity;
529 };
530 method: string;
531 args: Entity[];
532}>;
533export type RunResponse = [
534 Transaction,
535 google.datastore.v1.IBeginTransactionResponse
536];
537export interface RunCallback {
538 (error: Error | null, transaction: Transaction | null, response?: google.datastore.v1.IBeginTransactionResponse): void;
539}
540export interface RollbackCallback {
541 (error: Error | null, response?: google.datastore.v1.IRollbackResponse): void;
542}
543export type RollbackResponse = [google.datastore.v1.IRollbackResponse];
544export interface RunOptions {
545 readOnly?: boolean;
546 transactionId?: string;
547 transactionOptions?: TransactionOptions;
548 gaxOptions?: CallOptions;
549}
550/**
551 * Reference to the {@link Transaction} class.
552 * @name module:@google-cloud/datastore.Transaction
553 * @see Transaction
554 */
555export { Transaction };