UNPKG

10.9 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';
22/**
23 * A transaction is a set of Datastore operations on one or more entities. Each
24 * transaction is guaranteed to be atomic, which means that transactions are
25 * never partially applied. Either all of the operations in the transaction are
26 * applied, or none of them are applied.
27 *
28 * @see [Transactions Reference]{@link https://cloud.google.com/datastore/docs/concepts/transactions}
29 *
30 * @class
31 * @extends {Request}
32 * @param {Datastore} datastore A Datastore instance.
33 * @mixes module:datastore/request
34 *
35 * @example
36 * const {Datastore} = require('@google-cloud/datastore');
37 * const datastore = new Datastore();
38 * const transaction = datastore.transaction();
39 */
40declare class Transaction extends DatastoreRequest {
41 namespace?: string;
42 readOnly: boolean;
43 request: Function;
44 modifiedEntities_: ModifiedEntities;
45 skipCommit?: boolean;
46 constructor(datastore: Datastore, options?: TransactionOptions);
47 /*! Developer Documentation
48 *
49 * Below, we override two methods that we inherit from DatastoreRequest:
50 * `delete` and `save`. This is done because:
51 *
52 * A) the documentation needs to be different for a transactional save, and
53 * B) we build up a "modifiedEntities_" array on this object, used to build
54 * the final commit request with.
55 */
56 commit(gaxOptions?: CallOptions): Promise<CommitResponse>;
57 commit(callback: CommitCallback): void;
58 commit(gaxOptions: CallOptions, callback: CommitCallback): void;
59 createQuery(kind?: string): Query;
60 createQuery(kind?: string[]): Query;
61 createQuery(namespace: string, kind: string): Query;
62 createQuery(namespace: string, kind: string[]): Query;
63 /**
64 * Delete all entities identified with the specified key(s) in the current
65 * transaction.
66 *
67 * @param {Key|Key[]} key Datastore key object(s).
68 *
69 * @example
70 * const {Datastore} = require('@google-cloud/datastore');
71 * const datastore = new Datastore();
72 * const transaction = datastore.transaction();
73 *
74 * transaction.run((err) => {
75 * if (err) {
76 * // Error handling omitted.
77 * }
78 *
79 * // Delete a single entity.
80 * transaction.delete(datastore.key(['Company', 123]));
81 *
82 * // Delete multiple entities at once.
83 * transaction.delete([
84 * datastore.key(['Company', 123]),
85 * datastore.key(['Product', 'Computer'])
86 * ]);
87 *
88 * transaction.commit((err) => {
89 * if (!err) {
90 * // Transaction committed successfully.
91 * }
92 * });
93 * });
94 */
95 delete(entities?: Entities): any;
96 /**
97 * Maps to {@link Datastore#save}, forcing the method to be `insert`.
98 *
99 * @param {object|object[]} entities Datastore key object(s).
100 * @param {Key} entities.key Datastore key object.
101 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
102 * indexing using a simple JSON path notation. See the examples in
103 * {@link Datastore#save} to see how to target properties at different
104 * levels of nesting within your entity.
105 * @param {object} entities.data Data to save with the provided key.
106 */
107 insert(entities: Entities): void;
108 rollback(callback: RollbackCallback): void;
109 rollback(gaxOptions?: CallOptions): Promise<RollbackResponse>;
110 rollback(gaxOptions: CallOptions, callback: RollbackCallback): void;
111 run(options?: RunOptions): Promise<RunResponse>;
112 run(callback: RunCallback): void;
113 run(options: RunOptions, callback: RunCallback): void;
114 /**
115 * Insert or update the specified object(s) in the current transaction. If a
116 * key is incomplete, its associated object is inserted and the original Key
117 * object is updated to contain the generated ID.
118 *
119 * This method will determine the correct Datastore method to execute
120 * (`upsert`, `insert`, or `update`) by using the key(s) provided. For
121 * example, if you provide an incomplete key (one without an ID), the request
122 * will create a new entity and have its ID automatically assigned. If you
123 * provide a complete key, the entity will be updated with the data specified.
124 *
125 * By default, all properties are indexed. To prevent a property from being
126 * included in *all* indexes, you must supply an `excludeFromIndexes` array.
127 * See below for an example.
128 *
129 * @param {object|object[]} entities Datastore key object(s).
130 * @param {Key} entities.key Datastore key object.
131 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
132 * indexing using a simple JSON path notation. See the example below to
133 * see how to target properties at different levels of nesting within your
134 * entity.
135 * @param {object} entities.data Data to save with the provided key.
136 *
137 * @example
138 * <caption>Save a single entity.</caption>
139 * const {Datastore} = require('@google-cloud/datastore');
140 * const datastore = new Datastore();
141 * const transaction = datastore.transaction();
142 *
143 * // Notice that we are providing an incomplete key. After the transaction is
144 * // committed, the Key object held by the `key` variable will be populated
145 * // with a path containing its generated ID.
146 * //-
147 * const key = datastore.key('Company');
148 *
149 * transaction.run((err) => {
150 * if (err) {
151 * // Error handling omitted.
152 * }
153 *
154 * transaction.save({
155 * key: key,
156 * data: {
157 * rating: '10'
158 * }
159 * });
160 *
161 * transaction.commit((err) => {
162 * if (!err) {
163 * // Data saved successfully.
164 * }
165 * });
166 * });
167 *
168 * @example
169 * const {Datastore} = require('@google-cloud/datastore');
170 * const datastore = new Datastore();
171 * const transaction = datastore.transaction();
172 *
173 * // Use an array, `excludeFromIndexes`, to exclude properties from indexing.
174 * // This will allow storing string values larger than 1500 bytes.
175 *
176 * transaction.run((err) => {
177 * if (err) {
178 * // Error handling omitted.
179 * }
180 *
181 * transaction.save({
182 * key: key,
183 * excludeFromIndexes: [
184 * 'description',
185 * 'embeddedEntity.description',
186 * 'arrayValue[].description'
187 * ],
188 * data: {
189 * description: 'Long string (...)',
190 * embeddedEntity: {
191 * description: 'Long string (...)'
192 * },
193 * arrayValue: [
194 * {
195 * description: 'Long string (...)'
196 * }
197 * ]
198 * }
199 * });
200 *
201 * transaction.commit((err) => {
202 * if (!err) {
203 * // Data saved successfully.
204 * }
205 * });
206 * });
207 *
208 * @example
209 * <caption>Save multiple entities at once.</caption>
210 * const {Datastore} = require('@google-cloud/datastore');
211 * const datastore = new Datastore();
212 * const transaction = datastore.transaction();
213 * const companyKey = datastore.key(['Company', 123]);
214 * const productKey = datastore.key(['Product', 'Computer']);
215 *
216 * transaction.run((err) => {
217 * if (err) {
218 * // Error handling omitted.
219 * }
220 *
221 * transaction.save([
222 * {
223 * key: companyKey,
224 * data: {
225 * HQ: 'Dallas, TX'
226 * }
227 * },
228 * {
229 * key: productKey,
230 * data: {
231 * vendor: 'Dell'
232 * }
233 * }
234 * ]);
235 *
236 * transaction.commit((err) => {
237 * if (!err) {
238 * // Data saved successfully.
239 * }
240 * });
241 * });
242 */
243 save(entities: Entities): void;
244 /**
245 * Maps to {@link Datastore#save}, forcing the method to be `update`.
246 *
247 * @param {object|object[]} entities Datastore key object(s).
248 * @param {Key} entities.key Datastore key object.
249 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
250 * indexing using a simple JSON path notation. See the examples in
251 * {@link Datastore#save} to see how to target properties at different
252 * levels of nesting within your entity.
253 * @param {object} entities.data Data to save with the provided key.
254 */
255 update(entities: Entities): void;
256 /**
257 * Maps to {@link Datastore#save}, forcing the method to be `upsert`.
258 *
259 * @param {object|object[]} entities Datastore key object(s).
260 * @param {Key} entities.key Datastore key object.
261 * @param {string[]} [entities.excludeFromIndexes] Exclude properties from
262 * indexing using a simple JSON path notation. See the examples in
263 * {@link Datastore#save} to see how to target properties at different
264 * levels of nesting within your entity.
265 * @param {object} entities.data Data to save with the provided key.
266 */
267 upsert(entities: Entities): void;
268}
269export declare type ModifiedEntities = Array<{
270 entity: {
271 key: Entity;
272 };
273 method: string;
274 args: Entity[];
275}>;
276export declare type RunResponse = [Transaction, google.datastore.v1.IBeginTransactionResponse];
277export interface RunCallback {
278 (error: Error | null, transaction: Transaction | null, response?: google.datastore.v1.IBeginTransactionResponse): void;
279}
280export interface RollbackCallback {
281 (error: Error | null, response?: google.datastore.v1.IRollbackResponse): void;
282}
283export declare type RollbackResponse = [google.datastore.v1.IRollbackResponse];
284export interface RunOptions {
285 readOnly?: boolean;
286 transactionId?: string;
287 transactionOptions?: TransactionOptions;
288 gaxOptions?: CallOptions;
289}
290/**
291 * Reference to the {@link Transaction} class.
292 * @name module:@google-cloud/datastore.Transaction
293 * @see Transaction
294 */
295export { Transaction };