UNPKG

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