UNPKG

11.3 kBTypeScriptView Raw
1import { Database } from "./database.js";
2/**
3 * Indicates whether the given value represents a {@link Transaction}.
4 *
5 * @param transaction - A value that might be a transaction.
6 */
7export declare function isArangoTransaction(transaction: any): transaction is Transaction;
8/**
9 * Options for how the transaction should be committed.
10 */
11export type TransactionCommitOptions = {
12 /**
13 * If set to `true`, the request will explicitly permit ArangoDB to return a
14 * potentially dirty or stale result and arangojs will load balance the
15 * request without distinguishing between leaders and followers.
16 */
17 allowDirtyRead?: boolean;
18};
19/**
20 * Options for how the transaction should be aborted.
21 */
22export type TransactionAbortOptions = {
23 /**
24 * If set to `true`, the request will explicitly permit ArangoDB to return a
25 * potentially dirty or stale result and arangojs will load balance the
26 * request without distinguishing between leaders and followers.
27 */
28 allowDirtyRead?: boolean;
29};
30/**
31 * Status of a given transaction.
32 *
33 * See also {@link database.TransactionDetails}.
34 */
35export type TransactionStatus = {
36 /**
37 * Unique identifier of the transaction.
38 */
39 id: string;
40 /**
41 * Status of the transaction.
42 */
43 status: "running" | "committed" | "aborted";
44};
45/**
46 * Represents a streaming transaction in a {@link database.Database}.
47 */
48export declare class Transaction {
49 protected _db: Database;
50 protected _id: string;
51 /**
52 * @internal
53 */
54 constructor(db: Database, id: string);
55 /**
56 * @internal
57 *
58 * Indicates that this object represents an ArangoDB transaction.
59 */
60 get isArangoTransaction(): true;
61 /**
62 * Unique identifier of this transaction.
63 *
64 * See {@link database.Database#transaction}.
65 */
66 get id(): string;
67 /**
68 * Checks whether the transaction exists.
69 *
70 * @example
71 * ```js
72 * const db = new Database();
73 * const trx = db.transaction("some-transaction");
74 * const result = await trx.exists();
75 * // result indicates whether the transaction exists
76 * ```
77 */
78 exists(): Promise<boolean>;
79 /**
80 * Retrieves general information about the transaction.
81 *
82 * @example
83 * ```js
84 * const db = new Database();
85 * const col = db.collection("some-collection");
86 * const trx = db.beginTransaction(col);
87 * await trx.step(() => col.save({ hello: "world" }));
88 * const info = await trx.get();
89 * // the transaction exists
90 * ```
91 */
92 get(): Promise<TransactionStatus>;
93 /**
94 * Attempts to commit the transaction to the databases.
95 *
96 * @param options - Options for comitting the transaction.
97 *
98 * @example
99 * ```js
100 * const db = new Database();
101 * const col = db.collection("some-collection");
102 * const trx = db.beginTransaction(col);
103 * await trx.step(() => col.save({ hello: "world" }));
104 * const result = await trx.commit();
105 * // result indicates the updated transaction status
106 * ```
107 */
108 commit(options?: TransactionCommitOptions): Promise<TransactionStatus>;
109 /**
110 * Attempts to abort the transaction to the databases.
111 *
112 * @param options - Options for aborting the transaction.
113 *
114 * @example
115 * ```js
116 * const db = new Database();
117 * const col = db.collection("some-collection");
118 * const trx = db.beginTransaction(col);
119 * await trx.step(() => col.save({ hello: "world" }));
120 * const result = await trx.abort();
121 * // result indicates the updated transaction status
122 * ```
123 */
124 abort(options?: TransactionAbortOptions): Promise<TransactionStatus>;
125 /**
126 * Executes the given function locally as a single step of the transaction.
127 *
128 * @param T - Type of the callback's returned promise.
129 * @param callback - Callback function returning a promise.
130 *
131 * **Warning**: The callback function should wrap a single call of an async
132 * arangojs method (e.g. a method on a `Collection` object of a collection
133 * that is involved in the transaction or the `db.query` method).
134 * If the callback function is async, only the first promise-returning (or
135 * async) method call will be executed as part of the transaction. See the
136 * examples below for how to avoid common mistakes when using this method.
137 *
138 * **Note**: Avoid defining the callback as an async function if possible
139 * as arangojs will throw an error if the callback did not return a promise.
140 * Async functions will return an empty promise by default, making it harder
141 * to notice if you forgot to return something from the callback.
142 *
143 * **Note**: Although almost anything can be wrapped in a callback and passed
144 * to this method, that does not guarantee ArangoDB can actually do it in a
145 * transaction. Refer to the ArangoDB documentation if you are unsure whether
146 * a given operation can be executed as part of a transaction. Generally any
147 * modification or retrieval of data is eligible but modifications of
148 * collections or databases are not.
149 *
150 * @example
151 * ```js
152 * const db = new Database();
153 * const vertices = db.collection("vertices");
154 * const edges = db.collection("edges");
155 * const trx = await db.beginTransaction({ write: [vertices, edges] });
156 *
157 * // The following code will be part of the transaction
158 * const left = await trx.step(() => vertices.save({ label: "left" }));
159 * const right = await trx.step(() => vertices.save({ label: "right" }));
160 *
161 * // Results from preceding actions can be used normally
162 * await trx.step(() => edges.save({
163 * _from: left._id,
164 * _to: right._id,
165 * data: "potato"
166 * }));
167 *
168 * // Transaction must be committed for changes to take effected
169 * // Always call either trx.commit or trx.abort to end a transaction
170 * await trx.commit();
171 * ```
172 *
173 * @example
174 * ```js
175 * // BAD! If the callback is an async function it must only use await once!
176 * await trx.step(async () => {
177 * await collection.save(data);
178 * await collection.save(moreData); // WRONG
179 * });
180 *
181 * // BAD! Callback function must use only one arangojs call!
182 * await trx.step(() => {
183 * return collection.save(data)
184 * .then(() => collection.save(moreData)); // WRONG
185 * });
186 *
187 * // BETTER: Wrap every arangojs method call that should be part of the
188 * // transaction in a separate `trx.step` call
189 * await trx.step(() => collection.save(data));
190 * await trx.step(() => collection.save(moreData));
191 * ```
192 *
193 * @example
194 * ```js
195 * // BAD! If the callback is an async function it must not await before
196 * // calling an arangojs method!
197 * await trx.step(async () => {
198 * await doSomethingElse();
199 * return collection.save(data); // WRONG
200 * });
201 *
202 * // BAD! Any arangojs inside the callback must not happen inside a promise
203 * // method!
204 * await trx.step(() => {
205 * return doSomethingElse()
206 * .then(() => collection.save(data)); // WRONG
207 * });
208 *
209 * // BETTER: Perform any async logic needed outside the `trx.step` call
210 * await doSomethingElse();
211 * await trx.step(() => collection.save(data));
212 *
213 * // OKAY: You can perform async logic in the callback after the arangojs
214 * // method call as long as it does not involve additional arangojs method
215 * // calls, but this makes it easy to make mistakes later
216 * await trx.step(async () => {
217 * await collection.save(data);
218 * await doSomethingDifferent(); // no arangojs method calls allowed
219 * });
220 * ```
221 *
222 * @example
223 * ```js
224 * // BAD! The callback should not use any functions that themselves use any
225 * // arangojs methods!
226 * async function saveSomeData() {
227 * await collection.save(data);
228 * await collection.save(moreData);
229 * }
230 * await trx.step(() => saveSomeData()); // WRONG
231 *
232 * // BETTER: Pass the transaction to functions that need to call arangojs
233 * // methods inside a transaction
234 * async function saveSomeData(trx) {
235 * await trx.step(() => collection.save(data));
236 * await trx.step(() => collection.save(moreData));
237 * }
238 * await saveSomeData(); // no `trx.step` call needed
239 * ```
240 *
241 * @example
242 * ```js
243 * // BAD! You must wait for the promise to resolve (or await on the
244 * // `trx.step` call) before calling `trx.step` again!
245 * trx.step(() => collection.save(data)); // WRONG
246 * await trx.step(() => collection.save(moreData));
247 *
248 * // BAD! The trx.step callback can not make multiple calls to async arangojs
249 * // methods, not even using Promise.all!
250 * await trx.step(() => Promise.all([ // WRONG
251 * collection.save(data),
252 * collection.save(moreData),
253 * ]));
254 *
255 * // BAD! Multiple `trx.step` calls can not run in parallel!
256 * await Promise.all([ // WRONG
257 * trx.step(() => collection.save(data)),
258 * trx.step(() => collection.save(moreData)),
259 * ]));
260 *
261 * // BETTER: Always call `trx.step` sequentially, one after the other
262 * await trx.step(() => collection.save(data));
263 * await trx.step(() => collection.save(moreData));
264 *
265 * // OKAY: The then callback can be used if async/await is not available
266 * trx.step(() => collection.save(data))
267 * .then(() => trx.step(() => collection.save(moreData)));
268 * ```
269 *
270 * @example
271 * ```js
272 * // BAD! The callback will return an empty promise that resolves before
273 * // the inner arangojs method call has even talked to ArangoDB!
274 * await trx.step(async () => {
275 * collection.save(data); // WRONG
276 * });
277 *
278 * // BETTER: Use an arrow function so you don't forget to return
279 * await trx.step(() => collection.save(data));
280 *
281 * // OKAY: Remember to always return when using a function body
282 * await trx.step(() => {
283 * return collection.save(data); // easy to forget!
284 * });
285 *
286 * // OKAY: You do not have to use arrow functions but it helps
287 * await trx.step(function () {
288 * return collection.save(data);
289 * });
290 * ```
291 *
292 * @example
293 * ```js
294 * // BAD! You can not pass promises instead of a callback!
295 * await trx.step(collection.save(data)); // WRONG
296 *
297 * // BETTER: Wrap the code in a function and pass the function instead
298 * await trx.step(() => collection.save(data));
299 * ```
300 *
301 * @example
302 * ```js
303 * // WORSE: Calls to non-async arangojs methods don't need to be performed
304 * // as part of a transaction
305 * const collection = await trx.step(() => db.collection("my-documents"));
306 *
307 * // BETTER: If an arangojs method is not async and doesn't return promises,
308 * // call it without `trx.step`
309 * const collection = db.collection("my-documents");
310 * ```
311 */
312 step<T>(callback: () => Promise<T>): Promise<T>;
313}
314//# sourceMappingURL=transaction.d.ts.map
\No newline at end of file