forked from tywalch/electrodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.js
200 lines (180 loc) · 4.61 KB
/
transaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const { TableIndex, TransactionMethods } = require("./types");
const { getEntityIdentifiers, matchToEntityAlias } = require("./entity");
function cleanseCanceledData(
index = TableIndex,
entities,
data = {},
config = {},
) {
if (config.raw) {
return data;
}
const identifiers = getEntityIdentifiers(entities);
const canceled = data.canceled || [];
const paramItems = config._paramItems || [];
const results = [];
for (let i = 0; i < canceled.length; i++) {
const { Item, Code, Message } = canceled[i] || {};
const paramItem = paramItems[i];
const code = Code || "None";
const rejected = code !== "None";
const result = {
rejected,
code,
message: Message,
};
if (Item) {
const entityAlias = matchToEntityAlias({
record: Item,
paramItem,
identifiers,
});
result.item = entities[entityAlias].formatResponse({ Item }, index, {
...config,
pager: false,
parse: undefined,
}).data;
} else {
result.item = null;
}
results.push(result);
}
return results;
}
function cleanseTransactionData(
index = TableIndex,
entities,
data = {},
config = {},
) {
if (config.raw) {
return data;
}
const identifiers = getEntityIdentifiers(entities);
data.Items = data.Items || [];
const paramItems = config._paramItems || [];
const results = [];
for (let i = 0; i < data.Items.length; i++) {
const record = data.Items[i];
if (!record) {
results.push(null);
continue;
}
const paramItem = paramItems[i];
const entityAlias = matchToEntityAlias({ paramItem, identifiers, record });
if (!entityAlias) {
continue;
}
// pager=false because we don't want the entity trying to parse the lastEvaluatedKey
let formatted = entities[entityAlias].formatResponse(
{ Item: record },
index,
{
...config,
pager: false,
parse: undefined,
},
);
results.push(formatted.data);
}
return results.map((item) => ({
rejected: false,
item,
}));
}
function createTransaction(options) {
const { fn, method, getEntities } = options;
const operations = {
params: (options = {}) => {
const paramItems = fn(getEntities());
const params = {
TransactItems: paramItems,
};
if (typeof options.token === "string" && options.token.length) {
params["ClientRequestToken"] = options.token;
}
if (options._returnParamItems) {
return { params, paramItems };
}
return params;
},
go: async (options) => {
const driver = Object.values(getEntities())[0];
if (!driver) {
throw new Error(
"At least one entity must exist to perform a transaction",
);
}
const { params, paramItems } = operations.params({
...options,
_returnParamItems: true,
});
let canceled = false;
if (paramItems.length === 0) {
return {
canceled,
data: [],
};
}
const response = await driver.go(method, params, {
...options,
parse: (options, data) => {
if (options.raw) {
return data;
} else if (data.canceled) {
canceled = true;
return cleanseCanceledData(TableIndex, getEntities(), data, {
...options,
_isTransaction: true,
_paramItems: paramItems,
});
} else if (data.Responses) {
return cleanseTransactionData(
TableIndex,
getEntities(),
{
Items: data.Responses.map((response) => response.Item),
},
{
...options,
_isTransaction: true,
_paramItems: paramItems,
},
);
} else {
return new Array(paramItems ? paramItems.length : 0).fill({
item: null,
code: "None",
rejected: false,
message: undefined,
});
}
},
});
return {
...response,
canceled,
};
},
};
return operations;
}
function createWriteTransaction(entities, fn) {
return createTransaction({
fn,
method: TransactionMethods.transactWrite,
getEntities: () => entities,
});
}
function createGetTransaction(entities, fn) {
return createTransaction({
fn,
method: TransactionMethods.transactGet,
getEntities: () => entities,
});
}
module.exports = {
createTransaction,
createWriteTransaction,
createGetTransaction,
};