electrodb 2.9.3 → 2.10.1

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