UNPKG

3.79 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TransactionReturnOptions = void 0;
4const ddb = require("./aws/ddb/internal");
5const utils = require("./utils");
6const Error = require("./Error");
7const ModelStore = require("./ModelStore");
8var TransactionReturnOptions;
9(function (TransactionReturnOptions) {
10 TransactionReturnOptions["request"] = "request";
11 TransactionReturnOptions["documents"] = "documents";
12})(TransactionReturnOptions = exports.TransactionReturnOptions || (exports.TransactionReturnOptions = {}));
13var TransactionType;
14(function (TransactionType) {
15 TransactionType["get"] = "get";
16 TransactionType["write"] = "write";
17})(TransactionType || (TransactionType = {}));
18function Transaction(transactions, settings, callback) {
19 settings = settings !== null && settings !== void 0 ? settings : { "return": TransactionReturnOptions.documents };
20 if (typeof settings === "function") {
21 callback = settings;
22 settings = { "return": TransactionReturnOptions.documents };
23 }
24 if (typeof transactions === "function") {
25 callback = transactions;
26 transactions = null;
27 }
28 const promise = (async () => {
29 if (!Array.isArray(transactions) || transactions.length <= 0) {
30 throw new Error.InvalidParameter("You must pass in an array with items for the transactions parameter.");
31 }
32 const transactionObjects = await Promise.all(transactions);
33 const transactionParams = {
34 "TransactItems": transactionObjects
35 };
36 if (settings.return === TransactionReturnOptions.request) {
37 return transactionParams;
38 }
39 let transactionType;
40 if (settings.type) {
41 switch (settings.type) {
42 case TransactionType.get:
43 transactionType = "transactGetItems";
44 break;
45 case TransactionType.write:
46 transactionType = "transactWriteItems";
47 break;
48 default:
49 throw new Error.InvalidParameter("Invalid type option, please pass in \"get\" or \"write\".");
50 }
51 }
52 else {
53 transactionType = transactionObjects.map((a) => Object.keys(a)[0]).every((key) => key === "Get") ? "transactGetItems" : "transactWriteItems";
54 }
55 const modelNames = transactionObjects.map((a) => Object.values(a)[0].TableName);
56 const uniqueModelNames = utils.unique_array_elements(modelNames);
57 const models = uniqueModelNames.map((name) => ModelStore(name));
58 models.forEach((model, index) => {
59 if (!model) {
60 throw new Error.InvalidParameter(`Model "${uniqueModelNames[index]}" not found. Please register the model with dynamoose before using it in transactions.`);
61 }
62 });
63 await Promise.all(models.map((model) => model.pendingTaskPromise()));
64 // TODO: remove `as any` here (https://stackoverflow.com/q/61111476/894067)
65 const result = await ddb(transactionType, transactionParams);
66 return result.Responses ? await Promise.all(result.Responses.map((item, index) => {
67 const modelName = modelNames[index];
68 const model = models.find((model) => model.name === modelName);
69 return new model.Document(item.Item, { "type": "fromDynamo" }).conformToSchema({ "customTypesDynamo": true, "checkExpiredItem": true, "saveUnknown": true, "type": "fromDynamo" });
70 })) : null;
71 })();
72 if (callback) {
73 promise.then((result) => callback(null, result)).catch((error) => callback(error));
74 }
75 else {
76 return promise;
77 }
78}
79exports.default = Transaction;
80//# sourceMappingURL=Transaction.js.map
\No newline at end of file