@webiny/db-dynamodb 0.0.0-unstable.fcdad0bc61 → 0.0.0-unstable.fdd9228b5d

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.
Files changed (234) hide show
  1. package/DynamoDbDriver.d.ts +22 -32
  2. package/DynamoDbDriver.js +232 -242
  3. package/DynamoDbDriver.js.map +1 -1
  4. package/README.md +81 -1
  5. package/index.d.ts +2 -0
  6. package/index.js +17 -2
  7. package/index.js.map +1 -1
  8. package/package.json +18 -22
  9. package/plugins/definitions/AttributePlugin.d.ts +3 -5
  10. package/plugins/definitions/AttributePlugin.js +4 -19
  11. package/plugins/definitions/AttributePlugin.js.map +1 -1
  12. package/plugins/definitions/DateTimeTransformPlugin.d.ts +2 -1
  13. package/plugins/definitions/DateTimeTransformPlugin.js +14 -26
  14. package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -1
  15. package/plugins/definitions/FieldPathPlugin.js +6 -18
  16. package/plugins/definitions/FieldPathPlugin.js.map +1 -1
  17. package/plugins/definitions/FieldPlugin.d.ts +2 -2
  18. package/plugins/definitions/FieldPlugin.js +2 -24
  19. package/plugins/definitions/FieldPlugin.js.map +1 -1
  20. package/plugins/definitions/TimeTransformPlugin.d.ts +2 -1
  21. package/plugins/definitions/TimeTransformPlugin.js +18 -25
  22. package/plugins/definitions/TimeTransformPlugin.js.map +1 -1
  23. package/plugins/definitions/ValueFilterPlugin.d.ts +3 -1
  24. package/plugins/definitions/ValueFilterPlugin.js +9 -15
  25. package/plugins/definitions/ValueFilterPlugin.js.map +1 -1
  26. package/plugins/definitions/ValueTransformPlugin.d.ts +1 -1
  27. package/plugins/definitions/ValueTransformPlugin.js +6 -19
  28. package/plugins/definitions/ValueTransformPlugin.js.map +1 -1
  29. package/plugins/definitions/assignFields.js +2 -9
  30. package/plugins/definitions/assignFields.js.map +1 -1
  31. package/plugins/filters/andIn.d.ts +2 -2
  32. package/plugins/filters/andIn.js +4 -8
  33. package/plugins/filters/andIn.js.map +1 -1
  34. package/plugins/filters/between.d.ts +2 -2
  35. package/plugins/filters/between.js +4 -8
  36. package/plugins/filters/between.js.map +1 -1
  37. package/plugins/filters/contains.d.ts +2 -2
  38. package/plugins/filters/contains.js +44 -17
  39. package/plugins/filters/contains.js.map +1 -1
  40. package/plugins/filters/eq.d.ts +2 -2
  41. package/plugins/filters/eq.js +6 -7
  42. package/plugins/filters/eq.js.map +1 -1
  43. package/plugins/filters/fuzzy.d.ts +2 -2
  44. package/plugins/filters/fuzzy.js +17 -11
  45. package/plugins/filters/fuzzy.js.map +1 -1
  46. package/plugins/filters/gt.d.ts +2 -2
  47. package/plugins/filters/gt.js +4 -4
  48. package/plugins/filters/gt.js.map +1 -1
  49. package/plugins/filters/gte.d.ts +2 -2
  50. package/plugins/filters/gte.js +4 -4
  51. package/plugins/filters/gte.js.map +1 -1
  52. package/plugins/filters/in.d.ts +2 -2
  53. package/plugins/filters/in.js +4 -8
  54. package/plugins/filters/in.js.map +1 -1
  55. package/plugins/filters/index.d.ts +1 -1
  56. package/plugins/filters/index.js +2 -14
  57. package/plugins/filters/index.js.map +1 -1
  58. package/plugins/filters/lt.d.ts +2 -2
  59. package/plugins/filters/lt.js +4 -4
  60. package/plugins/filters/lt.js.map +1 -1
  61. package/plugins/filters/lte.d.ts +2 -2
  62. package/plugins/filters/lte.js +4 -4
  63. package/plugins/filters/lte.js.map +1 -1
  64. package/plugins/filters/startsWith.d.ts +2 -2
  65. package/plugins/filters/startsWith.js +15 -8
  66. package/plugins/filters/startsWith.js.map +1 -1
  67. package/plugins/index.d.ts +1 -1
  68. package/plugins/index.js +2 -4
  69. package/plugins/index.js.map +1 -1
  70. package/store/entity.d.ts +78 -0
  71. package/store/entity.js +41 -0
  72. package/store/entity.js.map +1 -0
  73. package/store/keys.d.ts +6 -0
  74. package/store/keys.js +22 -0
  75. package/store/keys.js.map +1 -0
  76. package/store/types.d.ts +7 -0
  77. package/store/types.js +7 -0
  78. package/store/types.js.map +1 -0
  79. package/toolbox.d.ts +3 -0
  80. package/toolbox.js +20 -0
  81. package/toolbox.js.map +1 -0
  82. package/types.d.ts +10 -23
  83. package/types.js +3 -1
  84. package/types.js.map +1 -1
  85. package/utils/attributes.d.ts +5 -5
  86. package/utils/attributes.js +6 -10
  87. package/utils/attributes.js.map +1 -1
  88. package/utils/batch/batchRead.d.ts +15 -0
  89. package/utils/{batchRead.js → batch/batchRead.js} +5 -22
  90. package/utils/batch/batchRead.js.map +1 -0
  91. package/utils/batch/batchWrite.d.ts +13 -0
  92. package/utils/batch/batchWrite.js +65 -0
  93. package/utils/batch/batchWrite.js.map +1 -0
  94. package/utils/batch/index.d.ts +3 -0
  95. package/utils/batch/index.js +40 -0
  96. package/utils/batch/index.js.map +1 -0
  97. package/utils/batch/types.d.ts +25 -0
  98. package/utils/batch/types.js +7 -0
  99. package/utils/batch/types.js.map +1 -0
  100. package/utils/cleanup.d.ts +3 -3
  101. package/utils/cleanup.js +7 -15
  102. package/utils/cleanup.js.map +1 -1
  103. package/utils/count.d.ts +2 -0
  104. package/utils/count.js +23 -0
  105. package/utils/count.js.map +1 -0
  106. package/utils/createEntity.d.ts +54 -0
  107. package/utils/createEntity.js +61 -0
  108. package/utils/createEntity.js.map +1 -0
  109. package/utils/createTable.d.ts +8 -0
  110. package/utils/createTable.js +29 -0
  111. package/utils/createTable.js.map +1 -0
  112. package/utils/cursor.js +2 -6
  113. package/utils/cursor.js.map +1 -1
  114. package/utils/delete.d.ts +10 -0
  115. package/utils/delete.js +18 -0
  116. package/utils/delete.js.map +1 -0
  117. package/utils/entity/Entity.d.ts +26 -0
  118. package/utils/entity/Entity.js +77 -0
  119. package/utils/entity/Entity.js.map +1 -0
  120. package/utils/entity/EntityReadBatch.d.ts +17 -0
  121. package/utils/entity/EntityReadBatch.js +41 -0
  122. package/utils/entity/EntityReadBatch.js.map +1 -0
  123. package/utils/entity/EntityReadBatchBuilder.d.ts +10 -0
  124. package/utils/entity/EntityReadBatchBuilder.js +29 -0
  125. package/utils/entity/EntityReadBatchBuilder.js.map +1 -0
  126. package/utils/entity/EntityWriteBatch.d.ts +22 -0
  127. package/utils/entity/EntityWriteBatch.js +59 -0
  128. package/utils/entity/EntityWriteBatch.js.map +1 -0
  129. package/utils/entity/EntityWriteBatchBuilder.d.ts +11 -0
  130. package/utils/entity/EntityWriteBatchBuilder.js +28 -0
  131. package/utils/entity/EntityWriteBatchBuilder.js.map +1 -0
  132. package/utils/entity/getEntity.d.ts +4 -0
  133. package/utils/entity/getEntity.js +19 -0
  134. package/utils/entity/getEntity.js.map +1 -0
  135. package/utils/entity/index.d.ts +7 -0
  136. package/utils/entity/index.js +84 -0
  137. package/utils/entity/index.js.map +1 -0
  138. package/utils/entity/types.d.ts +51 -0
  139. package/utils/entity/types.js +7 -0
  140. package/utils/entity/types.js.map +1 -0
  141. package/utils/filter.d.ts +2 -2
  142. package/utils/filter.js +7 -39
  143. package/utils/filter.js.map +1 -1
  144. package/utils/get.d.ts +8 -6
  145. package/utils/get.js +16 -7
  146. package/utils/get.js.map +1 -1
  147. package/utils/index.d.ts +17 -0
  148. package/utils/index.js +194 -0
  149. package/utils/index.js.map +1 -0
  150. package/utils/listResponse.js +3 -5
  151. package/utils/listResponse.js.map +1 -1
  152. package/utils/put.d.ts +12 -0
  153. package/utils/put.js +19 -0
  154. package/utils/put.js.map +1 -0
  155. package/utils/query.d.ts +19 -6
  156. package/utils/query.js +70 -29
  157. package/utils/query.js.map +1 -1
  158. package/utils/scan.d.ts +38 -0
  159. package/utils/scan.js +90 -0
  160. package/utils/scan.js.map +1 -0
  161. package/utils/sort.d.ts +1 -1
  162. package/utils/sort.js +3 -18
  163. package/utils/sort.js.map +1 -1
  164. package/utils/table/Table.d.ts +11 -0
  165. package/utils/table/Table.js +38 -0
  166. package/utils/table/Table.js.map +1 -0
  167. package/utils/table/TableReadBatch.d.ts +19 -0
  168. package/utils/table/TableReadBatch.js +62 -0
  169. package/utils/table/TableReadBatch.js.map +1 -0
  170. package/utils/table/TableWriteBatch.d.ts +21 -0
  171. package/utils/table/TableWriteBatch.js +69 -0
  172. package/utils/table/TableWriteBatch.js.map +1 -0
  173. package/utils/table/index.d.ts +4 -0
  174. package/utils/table/index.js +51 -0
  175. package/utils/table/index.js.map +1 -0
  176. package/utils/table/types.d.ts +39 -0
  177. package/utils/table/types.js +7 -0
  178. package/utils/table/types.js.map +1 -0
  179. package/utils/update.d.ts +12 -0
  180. package/utils/update.js +19 -0
  181. package/utils/update.js.map +1 -0
  182. package/BatchProcess.d.ts +0 -47
  183. package/BatchProcess.js +0 -198
  184. package/BatchProcess.js.map +0 -1
  185. package/QueryGenerator.d.ts +0 -21
  186. package/QueryGenerator.js +0 -78
  187. package/QueryGenerator.js.map +0 -1
  188. package/operators/comparison/beginsWith.d.ts +0 -3
  189. package/operators/comparison/beginsWith.js +0 -24
  190. package/operators/comparison/beginsWith.js.map +0 -1
  191. package/operators/comparison/between.d.ts +0 -3
  192. package/operators/comparison/between.js +0 -30
  193. package/operators/comparison/between.js.map +0 -1
  194. package/operators/comparison/eq.d.ts +0 -3
  195. package/operators/comparison/eq.js +0 -34
  196. package/operators/comparison/eq.js.map +0 -1
  197. package/operators/comparison/gt.d.ts +0 -3
  198. package/operators/comparison/gt.js +0 -24
  199. package/operators/comparison/gt.js.map +0 -1
  200. package/operators/comparison/gte.d.ts +0 -3
  201. package/operators/comparison/gte.js +0 -24
  202. package/operators/comparison/gte.js.map +0 -1
  203. package/operators/comparison/lt.d.ts +0 -3
  204. package/operators/comparison/lt.js +0 -24
  205. package/operators/comparison/lt.js.map +0 -1
  206. package/operators/comparison/lte.d.ts +0 -3
  207. package/operators/comparison/lte.js +0 -24
  208. package/operators/comparison/lte.js.map +0 -1
  209. package/operators/index.d.ts +0 -12
  210. package/operators/index.js +0 -39
  211. package/operators/index.js.map +0 -1
  212. package/operators/logical/and.d.ts +0 -3
  213. package/operators/logical/and.js +0 -63
  214. package/operators/logical/and.js.map +0 -1
  215. package/operators/logical/or.d.ts +0 -3
  216. package/operators/logical/or.js +0 -63
  217. package/operators/logical/or.js.map +0 -1
  218. package/plugins/definitions/NumberTransformPlugin.d.ts +0 -4
  219. package/plugins/definitions/NumberTransformPlugin.js +0 -45
  220. package/plugins/definitions/NumberTransformPlugin.js.map +0 -1
  221. package/statements/createKeyConditionExpressionArgs.d.ts +0 -15
  222. package/statements/createKeyConditionExpressionArgs.js +0 -52
  223. package/statements/createKeyConditionExpressionArgs.js.map +0 -1
  224. package/statements/processStatement.d.ts +0 -3
  225. package/statements/processStatement.js +0 -42
  226. package/statements/processStatement.js.map +0 -1
  227. package/utils/batchRead.d.ts +0 -15
  228. package/utils/batchRead.js.map +0 -1
  229. package/utils/batchWrite.d.ts +0 -17
  230. package/utils/batchWrite.js +0 -30
  231. package/utils/batchWrite.js.map +0 -1
  232. package/utils/table.d.ts +0 -7
  233. package/utils/table.js +0 -27
  234. package/utils/table.js.map +0 -1
@@ -1,35 +1,25 @@
1
- /**
2
- * Remove this when no apps are using our internal db drivers anymore
3
- */
4
- import { DocumentClient } from "aws-sdk/clients/dynamodb";
5
- import BatchProcess from "./BatchProcess";
6
- import { DbDriver, Args, Result, ArgsBatch } from "@webiny/db";
7
- declare type ConstructorArgs = {
8
- documentClient?: DocumentClient;
9
- };
10
- interface ReadLogsParams {
11
- table: string;
1
+ import type { DynamoDBDocument } from "@webiny/aws-sdk/client-dynamodb";
2
+ import type { DbDriver, GetValueResult, GetValuesResult, IListValuesParams, ListValuesResult, RemoveValueResult, RemoveValuesResult, StorageKey, StoreValueResult, StoreValuesResult } from "@webiny/db";
3
+ import type { Entity } from "dynamodb-toolbox";
4
+ import type { Table } from "./utils/createTable";
5
+ import type { GenericRecord } from "@webiny/api/types";
6
+ interface ConstructorArgs {
7
+ documentClient: DynamoDBDocument;
12
8
  }
13
- interface CreateLogParams {
14
- id: string;
15
- operation: string;
16
- /**
17
- * TODO: determine the data type.
18
- */
19
- data: any;
20
- table: string;
21
- }
22
- declare class DynamoDbDriver implements DbDriver {
23
- batchProcesses: Record<string, BatchProcess>;
24
- documentClient: DocumentClient;
25
- constructor({ documentClient }?: ConstructorArgs);
26
- getClient(): DocumentClient;
27
- create({ table, data, meta, __batch: batch }: Args): Promise<Result>;
28
- update({ query, data, table, meta, __batch: batch }: Args): Promise<Result>;
29
- delete({ query, table, meta, __batch: batch }: Args): Promise<Result>;
30
- read<T>({ table, query, sort, limit, keys, meta, __batch: batch }: Args): Promise<Result<T[]>>;
31
- createLog({ id, operation, data, table }: CreateLogParams): Promise<Result>;
32
- readLogs<T>({ table }: ReadLogsParams): Promise<Result<T[]>>;
33
- getBatchProcess(__batch: ArgsBatch): BatchProcess;
9
+ declare class DynamoDbDriver implements DbDriver<DynamoDBDocument> {
10
+ readonly documentClient: DynamoDBDocument;
11
+ private _table;
12
+ private _entity;
13
+ table(): Table;
14
+ entity(): Entity;
15
+ constructor({ documentClient }: ConstructorArgs);
16
+ getClient(): DynamoDBDocument;
17
+ storeValue<V>(key: string, input: V): Promise<StoreValueResult<V>>;
18
+ storeValues<V extends GenericRecord<StorageKey>>(values: V): Promise<StoreValuesResult<V>>;
19
+ getValue<V>(key: StorageKey): Promise<GetValueResult<V>>;
20
+ getValues<V extends GenericRecord<StorageKey>>(input: (keyof V)[]): Promise<GetValuesResult<V>>;
21
+ listValues<V extends GenericRecord<StorageKey>>(params?: IListValuesParams): Promise<ListValuesResult<V>>;
22
+ removeValue<V>(key: StorageKey): Promise<RemoveValueResult<V>>;
23
+ removeValues<V extends GenericRecord<StorageKey>>(input: (keyof V)[]): Promise<RemoveValuesResult<V>>;
34
24
  }
35
25
  export default DynamoDbDriver;
package/DynamoDbDriver.js CHANGED
@@ -1,274 +1,264 @@
1
1
  "use strict";
2
2
 
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
-
5
3
  Object.defineProperty(exports, "__esModule", {
6
4
  value: true
7
5
  });
8
6
  exports.default = void 0;
9
-
10
- var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
11
-
12
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
13
-
14
- var _dynamodb = require("aws-sdk/clients/dynamodb");
15
-
16
- var _BatchProcess = _interopRequireDefault(require("./BatchProcess"));
17
-
18
- var _QueryGenerator = _interopRequireDefault(require("./QueryGenerator"));
19
-
20
- /**
21
- * Remove this when no apps are using our internal db drivers anymore
22
- */
23
- // @ts-nocheck
24
- const LOG_KEYS = [{
25
- primary: true,
26
- unique: true,
27
- name: "primary",
28
- fields: [{
29
- name: "PK"
30
- }, {
31
- name: "SK"
32
- }]
33
- }];
34
-
7
+ var _createTable = require("./utils/createTable");
8
+ var _entity = require("./store/entity");
9
+ var _utils = require("./utils");
10
+ var _keys = require("./store/keys");
35
11
  class DynamoDbDriver {
12
+ _table = undefined;
13
+ _entity = undefined;
14
+ table() {
15
+ if (this._table) {
16
+ return this._table;
17
+ }
18
+ this._table = (0, _createTable.createTable)({
19
+ documentClient: this.documentClient
20
+ });
21
+ return this._table;
22
+ }
23
+ entity() {
24
+ if (this._entity) {
25
+ return this._entity;
26
+ }
27
+ this._entity = (0, _entity.createEntity)({
28
+ table: this.table()
29
+ });
30
+ return this._entity;
31
+ }
36
32
  constructor({
37
33
  documentClient
38
- } = {}) {
39
- (0, _defineProperty2.default)(this, "batchProcesses", void 0);
40
- (0, _defineProperty2.default)(this, "documentClient", void 0);
41
- this.batchProcesses = {};
42
- this.documentClient = documentClient || new _dynamodb.DocumentClient();
34
+ }) {
35
+ this.documentClient = documentClient;
43
36
  }
44
-
45
37
  getClient() {
46
38
  return this.documentClient;
47
39
  }
48
-
49
- async create({
50
- table,
51
- data,
52
- meta,
53
- __batch: batch
54
- }) {
55
- if (!batch) {
56
- const result = await this.documentClient.put({
57
- TableName: table,
58
- Item: data,
59
- ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
60
- }).promise();
61
- return [true, {
62
- response: result.$response
63
- }];
40
+ async storeValue(key, input) {
41
+ let value;
42
+ try {
43
+ value = JSON.stringify(input);
44
+ } catch (ex) {
45
+ return {
46
+ key,
47
+ error: ex
48
+ };
64
49
  }
65
-
66
- const batchProcess = this.getBatchProcess(batch);
67
- batchProcess.addBatchWrite({
68
- table,
69
- data
70
- });
71
-
72
- if (batchProcess.allOperationsAdded()) {
73
- batchProcess.startExecution();
74
- } else {
75
- await batchProcess.waitStartExecution();
50
+ try {
51
+ await (0, _utils.put)({
52
+ entity: this.entity(),
53
+ item: {
54
+ PK: (0, _keys.createPartitionKey)(),
55
+ SK: (0, _keys.createSortKey)({
56
+ key
57
+ }),
58
+ TYPE: (0, _keys.createType)(),
59
+ key,
60
+ value
61
+ }
62
+ });
63
+ return {
64
+ key,
65
+ data: input
66
+ };
67
+ } catch (ex) {
68
+ return {
69
+ key,
70
+ error: ex
71
+ };
76
72
  }
77
-
78
- await batchProcess.waitExecution();
79
- return [true, {
80
- response: batchProcess.response
81
- }];
82
73
  }
83
-
84
- async update({
85
- query,
86
- data,
87
- table,
88
- meta,
89
- __batch: batch
90
- }) {
91
- if (!batch) {
92
- const update = {
93
- UpdateExpression: "SET ",
94
- ExpressionAttributeNames: {},
95
- ExpressionAttributeValues: {}
74
+ async storeValues(values) {
75
+ const keys = Object.keys(values);
76
+ try {
77
+ const batch = keys.map(key => {
78
+ const input = values[key];
79
+ let value;
80
+ try {
81
+ value = JSON.stringify(input);
82
+ } catch (ex) {
83
+ throw ex;
84
+ }
85
+ const item = {
86
+ PK: (0, _keys.createPartitionKey)(),
87
+ SK: (0, _keys.createSortKey)({
88
+ key
89
+ }),
90
+ TYPE: (0, _keys.createType)(),
91
+ key,
92
+ value
93
+ };
94
+ return this.entity().putBatch(item);
95
+ });
96
+ await (0, _utils.batchWriteAll)({
97
+ table: this.table(),
98
+ items: batch
99
+ });
100
+ return {
101
+ keys,
102
+ data: values
103
+ };
104
+ } catch (ex) {
105
+ return {
106
+ keys,
107
+ error: ex
96
108
  };
97
- const updateExpression = [];
98
-
99
- for (const key in data) {
100
- updateExpression.push(`#${key} = :${key}`);
101
- update.ExpressionAttributeNames[`#${key}`] = key;
102
- update.ExpressionAttributeValues[`:${key}`] = data[key];
103
- }
104
-
105
- update.UpdateExpression += updateExpression.join(", ");
106
- const result = await this.documentClient.update((0, _objectSpread2.default)({
107
- TableName: table,
108
- Key: query,
109
- ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
110
- }, update)).promise();
111
- return [true, {
112
- response: result.$response
113
- }];
114
- }
115
-
116
- const batchProcess = this.getBatchProcess(batch);
117
- batchProcess.addBatchWrite({
118
- table,
119
- data
120
- });
121
-
122
- if (batchProcess.allOperationsAdded()) {
123
- batchProcess.startExecution();
124
- } else {
125
- await batchProcess.waitStartExecution();
126
109
  }
127
-
128
- await batchProcess.waitExecution();
129
- return [true, {
130
- response: batchProcess.response
131
- }];
132
110
  }
133
-
134
- async delete({
135
- query,
136
- table,
137
- meta,
138
- __batch: batch
139
- }) {
140
- if (!batch) {
141
- const result = await this.documentClient.delete({
142
- TableName: table,
143
- Key: query,
144
- ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
145
- }).promise();
146
- return [true, {
147
- response: result.$response
148
- }];
149
- }
150
-
151
- const batchProcess = this.getBatchProcess(batch);
152
- batchProcess.addBatchDelete({
153
- table,
154
- query
155
- });
156
-
157
- if (batchProcess.allOperationsAdded()) {
158
- batchProcess.startExecution();
159
- } else {
160
- await batchProcess.waitStartExecution();
111
+ async getValue(key) {
112
+ try {
113
+ const result = await (0, _utils.get)({
114
+ entity: this.entity(),
115
+ keys: {
116
+ PK: (0, _keys.createPartitionKey)(),
117
+ SK: (0, _keys.createSortKey)({
118
+ key
119
+ })
120
+ }
121
+ });
122
+ return {
123
+ key,
124
+ data: result ? JSON.parse(result.value) : null
125
+ };
126
+ } catch (ex) {
127
+ return {
128
+ key,
129
+ error: ex
130
+ };
161
131
  }
162
-
163
- await batchProcess.waitExecution();
164
- return [true, {
165
- response: batchProcess.response
166
- }];
167
132
  }
168
-
169
- async read({
170
- table,
171
- query,
172
- sort,
173
- limit,
174
- keys,
175
- meta,
176
- __batch: batch
177
- }) {
178
- if (!batch) {
179
- const queryGenerator = new _QueryGenerator.default();
180
- const queryParams = queryGenerator.generate({
181
- query,
182
- keys,
183
- sort,
184
- limit,
185
- tableName: table
133
+ async getValues(input) {
134
+ const keys = [...input];
135
+ const batch = keys.map(key => {
136
+ return this.entity().getBatch({
137
+ PK: (0, _keys.createPartitionKey)(),
138
+ SK: (0, _keys.createSortKey)({
139
+ key
140
+ })
186
141
  });
187
- const response = await this.documentClient.query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, queryParams), {}, {
188
- ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
189
- })).promise();
190
-
191
- if (Array.isArray(response.Items)) {
192
- return [response.Items, {
193
- response: response.$response
194
- }];
195
- }
196
-
197
- return [[], {
198
- response: response.$response
199
- }];
200
- } // DynamoDb doesn't support batch queries, so we can immediately assume the GetRequest operation.
201
-
202
-
203
- const batchProcess = this.getBatchProcess(batch);
204
- const getResult = batchProcess.addBatchGet({
205
- table,
206
- query
207
142
  });
208
-
209
- if (batchProcess.allOperationsAdded()) {
210
- batchProcess.startExecution();
211
- } else {
212
- await batchProcess.waitStartExecution();
143
+ try {
144
+ const results = await (0, _utils.batchReadAll)({
145
+ table: this.table(),
146
+ items: batch
147
+ });
148
+ const data = keys.reduce((collection, key) => {
149
+ const result = results.find(item => {
150
+ return item.PK === (0, _keys.createPartitionKey)() && item.SK === (0, _keys.createSortKey)({
151
+ key
152
+ });
153
+ });
154
+ if (!result?.value) {
155
+ // @ts-expect-error
156
+ collection[key] = null;
157
+ return collection;
158
+ }
159
+ try {
160
+ // @ts-expect-error
161
+ collection[key] = JSON.parse(result.value);
162
+ } catch {
163
+ // @ts-expect-error
164
+ collection[key] = null;
165
+ }
166
+ return collection;
167
+ }, {});
168
+ return {
169
+ keys,
170
+ data
171
+ };
172
+ } catch (ex) {
173
+ return {
174
+ keys,
175
+ error: ex
176
+ };
213
177
  }
214
-
215
- await batchProcess.waitExecution();
216
- const result = getResult();
217
-
218
- if (result) {
219
- return [[result], {
220
- response: batchProcess.response
221
- }];
178
+ }
179
+ async listValues(params) {
180
+ try {
181
+ const partitionKey = (0, _keys.createPartitionKey)();
182
+ const options = {
183
+ ...params
184
+ };
185
+ const results = await (0, _utils.queryAll)({
186
+ entity: this.entity(),
187
+ partitionKey,
188
+ options
189
+ });
190
+ const data = results.reduce((collection, item) => {
191
+ try {
192
+ // @ts-expect-error
193
+ collection[item.key] = JSON.parse(item.value);
194
+ } catch (ex) {
195
+ // @ts-expect-error
196
+ collection[item.key] = null;
197
+ }
198
+ return collection;
199
+ }, {});
200
+ return {
201
+ keys: Object.keys(data),
202
+ data
203
+ };
204
+ } catch (ex) {
205
+ return {
206
+ error: ex
207
+ };
222
208
  }
223
-
224
- return [[], {
225
- response: batchProcess.response
226
- }];
227
209
  }
228
-
229
- async createLog({
230
- id,
231
- operation,
232
- data,
233
- table
234
- }) {
235
- await this.create({
236
- table: table,
237
- keys: LOG_KEYS,
238
- data: (0, _objectSpread2.default)({
239
- PK: "log",
240
- SK: id,
241
- id,
242
- operation
243
- }, data)
244
- });
245
- return [true, {}];
210
+ async removeValue(key) {
211
+ const result = await this.getValue(key);
212
+ if (result.error) {
213
+ return {
214
+ key,
215
+ error: result.error
216
+ };
217
+ }
218
+ try {
219
+ await this.entity().delete({
220
+ PK: (0, _keys.createPartitionKey)(),
221
+ SK: (0, _keys.createSortKey)({
222
+ key
223
+ })
224
+ });
225
+ return {
226
+ key,
227
+ data: result.data
228
+ };
229
+ } catch (ex) {
230
+ return {
231
+ key,
232
+ error: ex
233
+ };
234
+ }
246
235
  }
247
-
248
- async readLogs({
249
- table
250
- }) {
251
- return this.read({
252
- table,
253
- keys: LOG_KEYS,
254
- query: {
255
- PK: "log",
256
- SK: {
257
- $gte: " "
258
- }
259
- }
236
+ async removeValues(input) {
237
+ const keys = [...input];
238
+ const batch = keys.map(key => {
239
+ return this.entity().deleteBatch({
240
+ PK: (0, _keys.createPartitionKey)(),
241
+ SK: (0, _keys.createSortKey)({
242
+ key
243
+ })
244
+ });
260
245
  });
261
- }
262
-
263
- getBatchProcess(__batch) {
264
- if (!this.batchProcesses[__batch.instance.id]) {
265
- this.batchProcesses[__batch.instance.id] = new _BatchProcess.default(__batch.instance, this.documentClient);
246
+ try {
247
+ await (0, _utils.batchWriteAll)({
248
+ table: this.table(),
249
+ items: batch
250
+ });
251
+ return {
252
+ keys
253
+ };
254
+ } catch (ex) {
255
+ return {
256
+ keys,
257
+ error: ex
258
+ };
266
259
  }
267
-
268
- return this.batchProcesses[__batch.instance.id];
269
260
  }
270
-
271
261
  }
262
+ var _default = exports.default = DynamoDbDriver;
272
263
 
273
- var _default = DynamoDbDriver;
274
- exports.default = _default;
264
+ //# sourceMappingURL=DynamoDbDriver.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["LOG_KEYS","primary","unique","name","fields","DynamoDbDriver","constructor","documentClient","batchProcesses","DocumentClient","getClient","create","table","data","meta","__batch","batch","result","put","TableName","Item","ReturnConsumedCapacity","promise","response","$response","batchProcess","getBatchProcess","addBatchWrite","allOperationsAdded","startExecution","waitStartExecution","waitExecution","update","query","UpdateExpression","ExpressionAttributeNames","ExpressionAttributeValues","updateExpression","key","push","join","Key","delete","addBatchDelete","read","sort","limit","keys","queryGenerator","QueryGenerator","queryParams","generate","tableName","Array","isArray","Items","getResult","addBatchGet","createLog","id","operation","PK","SK","readLogs","$gte","instance","BatchProcess"],"sources":["DynamoDbDriver.ts"],"sourcesContent":["/**\n * Remove this when no apps are using our internal db drivers anymore\n */\n// @ts-nocheck\nimport { DocumentClient } from \"aws-sdk/clients/dynamodb\";\nimport BatchProcess from \"./BatchProcess\";\nimport QueryGenerator from \"./QueryGenerator\";\nimport { DbDriver, Args, Result, ArgsBatch } from \"@webiny/db\";\nimport { QueryKeys } from \"~/types\";\n\ntype ConstructorArgs = {\n documentClient?: DocumentClient;\n};\n\nconst LOG_KEYS: QueryKeys = [\n {\n primary: true,\n unique: true,\n name: \"primary\",\n fields: [{ name: \"PK\" }, { name: \"SK\" }]\n }\n];\n\ninterface Update {\n UpdateExpression: string;\n ExpressionAttributeNames: Record<string, any>;\n ExpressionAttributeValues: Record<string, any>;\n}\n\ninterface ReadLogsParams {\n table: string;\n}\n\ninterface CreateLogParams {\n id: string;\n operation: string;\n /**\n * TODO: determine the data type.\n */\n data: any;\n table: string;\n}\n\nclass DynamoDbDriver implements DbDriver {\n batchProcesses: Record<string, BatchProcess>;\n documentClient: DocumentClient;\n constructor({ documentClient }: ConstructorArgs = {}) {\n this.batchProcesses = {};\n this.documentClient = documentClient || new DocumentClient();\n }\n\n getClient() {\n return this.documentClient;\n }\n\n async create({ table, data, meta, __batch: batch }: Args): Promise<Result> {\n if (!batch) {\n const result = await this.documentClient\n .put({\n TableName: table,\n Item: data,\n ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\"\n })\n .promise();\n return [true, { response: result.$response }];\n }\n\n const batchProcess = this.getBatchProcess(batch);\n batchProcess.addBatchWrite({ table, data });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n return [true, { response: batchProcess.response }];\n }\n\n async update({ query, data, table, meta, __batch: batch }: Args): Promise<Result> {\n if (!batch) {\n const update: Update = {\n UpdateExpression: \"SET \",\n ExpressionAttributeNames: {},\n ExpressionAttributeValues: {}\n };\n\n const updateExpression = [];\n for (const key in data) {\n updateExpression.push(`#${key} = :${key}`);\n update.ExpressionAttributeNames[`#${key}`] = key;\n update.ExpressionAttributeValues[`:${key}`] = data[key];\n }\n\n update.UpdateExpression += updateExpression.join(\", \");\n\n const result = await this.documentClient\n .update({\n TableName: table,\n Key: query,\n ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\",\n ...update\n })\n .promise();\n\n return [true, { response: result.$response }];\n }\n\n const batchProcess = this.getBatchProcess(batch);\n\n batchProcess.addBatchWrite({\n table,\n data\n });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n return [true, { response: batchProcess.response }];\n }\n\n async delete({ query, table, meta, __batch: batch }: Args): Promise<Result> {\n if (!batch) {\n const result = await this.documentClient\n .delete({\n TableName: table,\n Key: query,\n ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\"\n })\n .promise();\n\n return [true, { response: result.$response }];\n }\n\n const batchProcess = this.getBatchProcess(batch);\n batchProcess.addBatchDelete({\n table,\n query\n });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n return [true, { response: batchProcess.response }];\n }\n\n async read<T>({\n table,\n query,\n sort,\n limit,\n keys,\n meta,\n __batch: batch\n }: Args): Promise<Result<T[]>> {\n if (!batch) {\n const queryGenerator = new QueryGenerator();\n const queryParams = queryGenerator.generate({\n query,\n keys,\n sort,\n limit,\n tableName: table\n });\n\n const response = await this.documentClient\n .query({ ...queryParams, ReturnConsumedCapacity: meta ? \"TOTAL\" : \"NONE\" })\n .promise();\n\n if (Array.isArray(response.Items)) {\n return [response.Items as T[], { response: response.$response }];\n }\n return [[], { response: response.$response }];\n }\n\n // DynamoDb doesn't support batch queries, so we can immediately assume the GetRequest operation.\n const batchProcess = this.getBatchProcess(batch);\n const getResult = batchProcess.addBatchGet({\n table,\n query\n });\n\n if (batchProcess.allOperationsAdded()) {\n batchProcess.startExecution();\n } else {\n await batchProcess.waitStartExecution();\n }\n\n await batchProcess.waitExecution();\n\n const result = getResult() as T;\n if (result) {\n return [[result], { response: batchProcess.response }];\n }\n\n return [[], { response: batchProcess.response }];\n }\n\n async createLog({ id, operation, data, table }: CreateLogParams): Promise<Result> {\n await this.create({\n table: table,\n keys: LOG_KEYS,\n data: {\n PK: \"log\",\n SK: id,\n id,\n operation,\n ...data\n }\n });\n\n return [true, {}];\n }\n\n async readLogs<T>({ table }: ReadLogsParams) {\n return this.read<T>({\n table,\n keys: LOG_KEYS,\n query: {\n PK: \"log\",\n SK: { $gte: \" \" }\n }\n });\n }\n\n getBatchProcess(__batch: ArgsBatch): BatchProcess {\n if (!this.batchProcesses[__batch.instance.id]) {\n this.batchProcesses[__batch.instance.id] = new BatchProcess(\n __batch.instance,\n this.documentClient\n );\n }\n\n return this.batchProcesses[__batch.instance.id];\n }\n}\n\nexport default DynamoDbDriver;\n"],"mappings":";;;;;;;;;;;;;AAIA;;AACA;;AACA;;AANA;AACA;AACA;AACA;AAWA,MAAMA,QAAmB,GAAG,CACxB;EACIC,OAAO,EAAE,IADb;EAEIC,MAAM,EAAE,IAFZ;EAGIC,IAAI,EAAE,SAHV;EAIIC,MAAM,EAAE,CAAC;IAAED,IAAI,EAAE;EAAR,CAAD,EAAiB;IAAEA,IAAI,EAAE;EAAR,CAAjB;AAJZ,CADwB,CAA5B;;AA6BA,MAAME,cAAN,CAAyC;EAGrCC,WAAW,CAAC;IAAEC;EAAF,IAAsC,EAAvC,EAA2C;IAAA;IAAA;IAClD,KAAKC,cAAL,GAAsB,EAAtB;IACA,KAAKD,cAAL,GAAsBA,cAAc,IAAI,IAAIE,wBAAJ,EAAxC;EACH;;EAEDC,SAAS,GAAG;IACR,OAAO,KAAKH,cAAZ;EACH;;EAEW,MAANI,MAAM,CAAC;IAAEC,KAAF;IAASC,IAAT;IAAeC,IAAf;IAAqBC,OAAO,EAAEC;EAA9B,CAAD,EAA+D;IACvE,IAAI,CAACA,KAAL,EAAY;MACR,MAAMC,MAAM,GAAG,MAAM,KAAKV,cAAL,CAChBW,GADgB,CACZ;QACDC,SAAS,EAAEP,KADV;QAEDQ,IAAI,EAAEP,IAFL;QAGDQ,sBAAsB,EAAEP,IAAI,GAAG,OAAH,GAAa;MAHxC,CADY,EAMhBQ,OANgB,EAArB;MAOA,OAAO,CAAC,IAAD,EAAO;QAAEC,Q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EAArC,CAAP;EACH;;AA3MoC;;eA8M1BtD,c"}
1
+ {"version":3,"names":["_createTable","require","_entity","_utils","_keys","DynamoDbDriver","_table","undefined","table","createTable","documentClient","entity","createEntity","constructor","getClient","storeValue","key","input","value","JSON","stringify","ex","error","put","item","PK","createPartitionKey","SK","createSortKey","TYPE","createType","data","storeValues","values","keys","Object","batch","map","putBatch","batchWriteAll","items","getValue","result","get","parse","getValues","getBatch","results","batchReadAll","reduce","collection","find","listValues","params","partitionKey","options","queryAll","removeValue","delete","removeValues","deleteBatch","_default","exports","default"],"sources":["DynamoDbDriver.ts"],"sourcesContent":["import type { DynamoDBDocument } from \"@webiny/aws-sdk/client-dynamodb\";\nimport type {\n DbDriver,\n GetValueResult,\n GetValuesResult,\n IListValuesParams,\n ListValuesResult,\n RemoveValueResult,\n RemoveValuesResult,\n StorageKey,\n StoreValueResult,\n StoreValuesResult\n} from \"@webiny/db\";\nimport type { Entity } from \"dynamodb-toolbox\";\nimport type { Table } from \"~/utils/createTable\";\nimport { createTable } from \"~/utils/createTable\";\nimport type { GenericRecord } from \"@webiny/api/types\";\nimport { createEntity } from \"~/store/entity\";\nimport { batchReadAll, batchWriteAll, get, put, queryAll } from \"~/utils\";\nimport { createPartitionKey, createSortKey, createType } from \"~/store/keys\";\nimport type { IStoreItem } from \"~/store/types\";\n\ninterface ConstructorArgs {\n documentClient: DynamoDBDocument;\n}\n\nclass DynamoDbDriver implements DbDriver<DynamoDBDocument> {\n public readonly documentClient: DynamoDBDocument;\n\n private _table: Table | undefined = undefined;\n private _entity: Entity | undefined = undefined;\n\n public table(): Table {\n if (this._table) {\n return this._table;\n }\n this._table = createTable({\n documentClient: this.documentClient\n });\n return this._table;\n }\n\n public entity(): Entity {\n if (this._entity) {\n return this._entity;\n }\n this._entity = createEntity({\n table: this.table()\n });\n return this._entity;\n }\n\n constructor({ documentClient }: ConstructorArgs) {\n this.documentClient = documentClient;\n }\n\n public getClient() {\n return this.documentClient;\n }\n\n public async storeValue<V>(key: string, input: V): Promise<StoreValueResult<V>> {\n let value: string | undefined;\n try {\n value = JSON.stringify(input);\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n\n try {\n await put<IStoreItem>({\n entity: this.entity(),\n item: {\n PK: createPartitionKey(),\n SK: createSortKey({ key }),\n TYPE: createType(),\n key,\n value\n }\n });\n\n return {\n key,\n data: input\n };\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n }\n public async storeValues<V extends GenericRecord<StorageKey>>(\n values: V\n ): Promise<StoreValuesResult<V>> {\n const keys = Object.keys(values);\n try {\n const batch = keys.map(key => {\n const input = values[key];\n let value: string | undefined;\n try {\n value = JSON.stringify(input);\n } catch (ex) {\n throw ex;\n }\n const item: IStoreItem = {\n PK: createPartitionKey(),\n SK: createSortKey({ key }),\n TYPE: createType(),\n key,\n value\n };\n return this.entity().putBatch(item);\n });\n\n await batchWriteAll({\n table: this.table(),\n items: batch\n });\n return {\n keys,\n data: values\n };\n } catch (ex) {\n return {\n keys,\n error: ex\n };\n }\n }\n public async getValue<V>(key: StorageKey): Promise<GetValueResult<V>> {\n try {\n const result = await get<IStoreItem>({\n entity: this.entity(),\n keys: {\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n }\n });\n return {\n key,\n data: result ? JSON.parse(result.value) : null\n };\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n }\n public async getValues<V extends GenericRecord<StorageKey>>(\n input: (keyof V)[]\n ): Promise<GetValuesResult<V>> {\n const keys = [...input] as string[];\n const batch = keys.map(key => {\n return this.entity().getBatch({\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n });\n });\n\n try {\n const results = await batchReadAll<IStoreItem>({\n table: this.table(),\n items: batch\n });\n const data = keys.reduce((collection, key) => {\n const result = results.find(item => {\n return item.PK === createPartitionKey() && item.SK === createSortKey({ key });\n });\n if (!result?.value) {\n // @ts-expect-error\n collection[key] = null;\n return collection;\n }\n try {\n // @ts-expect-error\n collection[key] = JSON.parse(result.value);\n } catch {\n // @ts-expect-error\n collection[key] = null;\n }\n\n return collection;\n }, {} as V);\n return {\n keys,\n data\n };\n } catch (ex) {\n return {\n keys,\n error: ex\n };\n }\n }\n public async listValues<V extends GenericRecord<StorageKey>>(\n params?: IListValuesParams\n ): Promise<ListValuesResult<V>> {\n try {\n const partitionKey = createPartitionKey();\n const options = {\n ...params\n };\n const results = await queryAll<IStoreItem>({\n entity: this.entity(),\n partitionKey,\n options\n });\n\n const data = results.reduce((collection, item) => {\n try {\n // @ts-expect-error\n collection[item.key] = JSON.parse(item.value);\n } catch (ex) {\n // @ts-expect-error\n collection[item.key] = null;\n }\n\n return collection;\n }, {} as V);\n\n return {\n keys: Object.keys(data),\n data\n };\n } catch (ex) {\n return {\n error: ex\n };\n }\n }\n\n public async removeValue<V>(key: StorageKey): Promise<RemoveValueResult<V>> {\n const result = await this.getValue<V>(key);\n if (result.error) {\n return {\n key,\n error: result.error\n };\n }\n try {\n await this.entity().delete({\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n });\n return {\n key,\n data: result.data\n };\n } catch (ex) {\n return {\n key,\n error: ex\n };\n }\n }\n\n public async removeValues<V extends GenericRecord<StorageKey>>(\n input: (keyof V)[]\n ): Promise<RemoveValuesResult<V>> {\n const keys = [...input] as string[];\n const batch = keys.map(key => {\n return this.entity().deleteBatch({\n PK: createPartitionKey(),\n SK: createSortKey({ key })\n });\n });\n\n try {\n await batchWriteAll({\n table: this.table(),\n items: batch\n });\n return {\n keys\n };\n } catch (ex) {\n return {\n keys,\n error: ex\n };\n }\n }\n}\n\nexport default DynamoDbDriver;\n"],"mappings":";;;;;;AAeA,IAAAA,YAAA,GAAAC,OAAA;AAEA,IAAAC,OAAA,GAAAD,OAAA;AACA,IAAAE,MAAA,GAAAF,OAAA;AACA,IAAAG,KAAA,GAAAH,OAAA;AAOA,MAAMI,cAAc,CAAuC;EAG/CC,MAAM,GAAsBC,SAAS;EACrCL,OAAO,GAAuBK,SAAS;EAExCC,KAAKA,CAAA,EAAU;I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