UNPKG

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