@webiny/db-dynamodb 0.0.0-mt-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.
Files changed (103) hide show
  1. package/BatchProcess.d.ts +31 -0
  2. package/BatchProcess.js +196 -0
  3. package/DynamoDbDriver.d.ts +27 -0
  4. package/DynamoDbDriver.js +272 -0
  5. package/LICENSE +21 -0
  6. package/QueryGenerator.d.ts +19 -0
  7. package/QueryGenerator.js +79 -0
  8. package/README.md +67 -0
  9. package/index.d.ts +2 -0
  10. package/index.js +15 -0
  11. package/operators/comparison/beginsWith.d.ts +3 -0
  12. package/operators/comparison/beginsWith.js +24 -0
  13. package/operators/comparison/between.d.ts +3 -0
  14. package/operators/comparison/between.js +30 -0
  15. package/operators/comparison/eq.d.ts +3 -0
  16. package/operators/comparison/eq.js +34 -0
  17. package/operators/comparison/gt.d.ts +3 -0
  18. package/operators/comparison/gt.js +24 -0
  19. package/operators/comparison/gte.d.ts +3 -0
  20. package/operators/comparison/gte.js +24 -0
  21. package/operators/comparison/lt.d.ts +3 -0
  22. package/operators/comparison/lt.js +24 -0
  23. package/operators/comparison/lte.d.ts +3 -0
  24. package/operators/comparison/lte.js +24 -0
  25. package/operators/index.d.ts +12 -0
  26. package/operators/index.js +39 -0
  27. package/operators/logical/and.d.ts +3 -0
  28. package/operators/logical/and.js +63 -0
  29. package/operators/logical/or.d.ts +3 -0
  30. package/operators/logical/or.js +63 -0
  31. package/package.json +45 -0
  32. package/plugins/definitions/AttributePlugin.d.ts +20 -0
  33. package/plugins/definitions/AttributePlugin.js +61 -0
  34. package/plugins/definitions/DateTimeTransformPlugin.d.ts +4 -0
  35. package/plugins/definitions/DateTimeTransformPlugin.js +54 -0
  36. package/plugins/definitions/FieldPathPlugin.d.ts +22 -0
  37. package/plugins/definitions/FieldPathPlugin.js +48 -0
  38. package/plugins/definitions/FieldPlugin.d.ts +34 -0
  39. package/plugins/definitions/FieldPlugin.js +73 -0
  40. package/plugins/definitions/NumberTransformPlugin.d.ts +4 -0
  41. package/plugins/definitions/NumberTransformPlugin.js +49 -0
  42. package/plugins/definitions/TimeTransformPlugin.d.ts +4 -0
  43. package/plugins/definitions/TimeTransformPlugin.js +65 -0
  44. package/plugins/definitions/ValueFilterPlugin.d.ts +20 -0
  45. package/plugins/definitions/ValueFilterPlugin.js +49 -0
  46. package/plugins/definitions/ValueTransformPlugin.d.ts +22 -0
  47. package/plugins/definitions/ValueTransformPlugin.js +52 -0
  48. package/plugins/definitions/assignFields.d.ts +1 -0
  49. package/plugins/definitions/assignFields.js +37 -0
  50. package/plugins/filters/andIn.d.ts +3 -0
  51. package/plugins/filters/andIn.js +35 -0
  52. package/plugins/filters/between.d.ts +3 -0
  53. package/plugins/filters/between.js +36 -0
  54. package/plugins/filters/contains.d.ts +3 -0
  55. package/plugins/filters/contains.js +32 -0
  56. package/plugins/filters/eq.d.ts +3 -0
  57. package/plugins/filters/eq.js +20 -0
  58. package/plugins/filters/fuzzy.d.ts +3 -0
  59. package/plugins/filters/fuzzy.js +30 -0
  60. package/plugins/filters/gt.d.ts +3 -0
  61. package/plugins/filters/gt.js +20 -0
  62. package/plugins/filters/gte.d.ts +3 -0
  63. package/plugins/filters/gte.js +20 -0
  64. package/plugins/filters/in.d.ts +3 -0
  65. package/plugins/filters/in.js +35 -0
  66. package/plugins/filters/index.d.ts +2 -0
  67. package/plugins/filters/index.js +32 -0
  68. package/plugins/filters/lt.d.ts +3 -0
  69. package/plugins/filters/lt.js +20 -0
  70. package/plugins/filters/lte.d.ts +3 -0
  71. package/plugins/filters/lte.js +20 -0
  72. package/plugins/index.d.ts +2 -0
  73. package/plugins/index.js +19 -0
  74. package/statements/createKeyConditionExpressionArgs.d.ts +12 -0
  75. package/statements/createKeyConditionExpressionArgs.js +48 -0
  76. package/statements/processStatement.d.ts +4 -0
  77. package/statements/processStatement.js +39 -0
  78. package/types.d.ts +29 -0
  79. package/types.js +5 -0
  80. package/utils/attributes.d.ts +10 -0
  81. package/utils/attributes.js +35 -0
  82. package/utils/batchRead.d.ts +15 -0
  83. package/utils/batchRead.js +58 -0
  84. package/utils/batchWrite.d.ts +17 -0
  85. package/utils/batchWrite.js +30 -0
  86. package/utils/cleanup.d.ts +3 -0
  87. package/utils/cleanup.js +53 -0
  88. package/utils/cursor.d.ts +2 -0
  89. package/utils/cursor.js +26 -0
  90. package/utils/documentClient.d.ts +8 -0
  91. package/utils/documentClient.js +33 -0
  92. package/utils/filter.d.ts +12 -0
  93. package/utils/filter.js +212 -0
  94. package/utils/get.d.ts +17 -0
  95. package/utils/get.js +30 -0
  96. package/utils/listResponse.d.ts +13 -0
  97. package/utils/listResponse.js +30 -0
  98. package/utils/query.d.ts +33 -0
  99. package/utils/query.js +127 -0
  100. package/utils/sort.d.ts +17 -0
  101. package/utils/sort.js +93 -0
  102. package/utils/table.d.ts +7 -0
  103. package/utils/table.js +27 -0
@@ -0,0 +1,31 @@
1
+ import { DocumentClient } from "aws-sdk/clients/dynamodb";
2
+ import { Batch } from "@webiny/db";
3
+ declare type BatchType = "batchWrite" | "batchGet";
4
+ declare class BatchProcess {
5
+ documentClient: DocumentClient;
6
+ batch: Batch;
7
+ resolveBuild: () => void;
8
+ rejectBuild: ({ message: string }: {
9
+ message: any;
10
+ }) => void;
11
+ queryBuild: Promise<void>;
12
+ resolveExecution: () => void;
13
+ rejectExecution: ({ message: string }: {
14
+ message: any;
15
+ }) => void;
16
+ queryExecution: Promise<void>;
17
+ operations: [Record<string, any>, Record<string, any>][];
18
+ batchType: BatchType;
19
+ results: Record<string, any>[];
20
+ response: Record<string, any>;
21
+ constructor(batch: Batch, documentClient: DocumentClient);
22
+ waitStartExecution(): Promise<void>;
23
+ waitExecution(): Promise<void>;
24
+ addBatchOperation(type: BatchType, args: any, meta?: {}): () => any;
25
+ addBatchWrite(args: any): () => any;
26
+ addBatchDelete(args: any): () => any;
27
+ addBatchGet(args: any): () => any;
28
+ allOperationsAdded(): boolean;
29
+ startExecution(): import("aws-sdk/lib/request").Request<DocumentClient.BatchWriteItemOutput, import("aws-sdk/lib/error").AWSError> | import("aws-sdk/lib/request").Request<DocumentClient.BatchGetItemOutput, import("aws-sdk/lib/error").AWSError>;
30
+ }
31
+ export default BatchProcess;
@@ -0,0 +1,196 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
13
+
14
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
15
+
16
+ class BatchProcess {
17
+ constructor(batch, documentClient) {
18
+ (0, _defineProperty2.default)(this, "documentClient", void 0);
19
+ (0, _defineProperty2.default)(this, "batch", void 0);
20
+ (0, _defineProperty2.default)(this, "resolveBuild", void 0);
21
+ (0, _defineProperty2.default)(this, "rejectBuild", void 0);
22
+ (0, _defineProperty2.default)(this, "queryBuild", void 0);
23
+ (0, _defineProperty2.default)(this, "resolveExecution", void 0);
24
+ (0, _defineProperty2.default)(this, "rejectExecution", void 0);
25
+ (0, _defineProperty2.default)(this, "queryExecution", void 0);
26
+ (0, _defineProperty2.default)(this, "operations", void 0);
27
+ (0, _defineProperty2.default)(this, "batchType", void 0);
28
+ (0, _defineProperty2.default)(this, "results", void 0);
29
+ (0, _defineProperty2.default)(this, "response", void 0);
30
+ this.documentClient = documentClient;
31
+ this.batch = batch;
32
+ this.resolveBuild = null;
33
+ this.rejectBuild = null;
34
+ this.queryBuild = new Promise((resolve, reject) => {
35
+ this.resolveBuild = resolve;
36
+ this.rejectBuild = reject;
37
+ });
38
+ this.resolveExecution = null;
39
+ this.rejectExecution = null;
40
+ this.queryExecution = new Promise((resolve, reject) => {
41
+ this.resolveExecution = resolve;
42
+ this.rejectExecution = reject;
43
+ });
44
+ this.operations = [];
45
+ this.results = [];
46
+ this.response = [];
47
+ this.batchType;
48
+ }
49
+
50
+ waitStartExecution() {
51
+ return this.queryBuild;
52
+ }
53
+
54
+ waitExecution() {
55
+ return this.queryExecution;
56
+ }
57
+
58
+ addBatchOperation(type, args, meta = {}) {
59
+ if (!this.batchType) {
60
+ this.batchType = type;
61
+ } else if (this.batchType !== type) {
62
+ const initial = this.batchType;
63
+ const index = this.operations.length;
64
+ this.rejectBuild({
65
+ message: `Cannot batch operations - all operations must be of the same type (the initial operation type was "${initial}", and operation type on index "${index}" is "${type}").`
66
+ });
67
+ return;
68
+ }
69
+
70
+ this.operations.push([args, meta]);
71
+ const index = this.operations.length - 1;
72
+ return () => this.results[index];
73
+ }
74
+
75
+ addBatchWrite(args) {
76
+ return this.addBatchOperation("batchWrite", args);
77
+ }
78
+
79
+ addBatchDelete(args) {
80
+ return this.addBatchOperation("batchWrite", _objectSpread({}, args), {
81
+ delete: true
82
+ });
83
+ }
84
+
85
+ addBatchGet(args) {
86
+ return this.addBatchOperation("batchGet", args);
87
+ }
88
+
89
+ allOperationsAdded() {
90
+ return this.operations.length === this.batch.operations.length;
91
+ }
92
+
93
+ startExecution() {
94
+ this.resolveBuild();
95
+ const documentClientArgs = {
96
+ ReturnConsumedCapacity: "TOTAL",
97
+ RequestItems: {}
98
+ };
99
+
100
+ const reject = e => {
101
+ e.message = `An error occurred while executing "${this.batchType}" batch operation: ${e.message}`;
102
+ return this.rejectExecution(e);
103
+ };
104
+
105
+ let resolve = response => {
106
+ this.response = response;
107
+ this.resolveExecution();
108
+ };
109
+
110
+ switch (this.batchType) {
111
+ case "batchWrite":
112
+ documentClientArgs.RequestItems = {};
113
+
114
+ for (let i = 0; i < this.operations.length; i++) {
115
+ const [args, meta] = this.operations[i];
116
+
117
+ if (!documentClientArgs.RequestItems[args.table]) {
118
+ documentClientArgs.RequestItems[args.table] = [];
119
+ }
120
+
121
+ const push = {};
122
+
123
+ if (meta.delete) {
124
+ push.DeleteRequest = {
125
+ Key: args.query
126
+ };
127
+ } else {
128
+ push.PutRequest = {
129
+ Item: args.data
130
+ };
131
+ }
132
+
133
+ documentClientArgs.RequestItems[args.table].push(push);
134
+ }
135
+
136
+ break;
137
+
138
+ case "batchGet":
139
+ documentClientArgs.RequestItems = {};
140
+
141
+ for (let i = 0; i < this.operations.length; i++) {
142
+ const [args] = this.operations[i];
143
+
144
+ if (!documentClientArgs.RequestItems[args.table]) {
145
+ documentClientArgs.RequestItems[args.table] = {
146
+ Keys: []
147
+ };
148
+ }
149
+
150
+ documentClientArgs.RequestItems[args.table].Keys.push(args.query);
151
+ }
152
+
153
+ resolve = response => {
154
+ this.response = response;
155
+ const results = []; // The results of batchGet aren't ordered so we have to figure out the order of results ourselves.
156
+
157
+ for (let i = 0; i < this.operations.length; i++) {
158
+ const [args] = this.operations[i];
159
+ const responseItems = response.Responses[args.table];
160
+ let foundResult = null;
161
+
162
+ outer: for (let j = 0; j < responseItems.length; j++) {
163
+ const responseItem = responseItems[j];
164
+
165
+ for (const queryKey in args.query) {
166
+ if (typeof responseItem[queryKey] === "undefined" || args.query[queryKey] !== responseItem[queryKey]) {
167
+ continue outer;
168
+ }
169
+ }
170
+
171
+ foundResult = responseItem;
172
+ }
173
+
174
+ results.push(foundResult);
175
+ }
176
+
177
+ this.results = results;
178
+ this.resolveExecution();
179
+ };
180
+
181
+ break;
182
+ }
183
+
184
+ return this.documentClient[this.batchType](documentClientArgs, (error, result) => {
185
+ if (error) {
186
+ reject(error);
187
+ } else {
188
+ resolve(result);
189
+ }
190
+ });
191
+ }
192
+
193
+ }
194
+
195
+ var _default = BatchProcess;
196
+ exports.default = _default;
@@ -0,0 +1,27 @@
1
+ import { DocumentClient } from "aws-sdk/clients/dynamodb";
2
+ import BatchProcess from "./BatchProcess";
3
+ import { DbDriver, Args, Result } from "@webiny/db";
4
+ declare type ConstructorArgs = {
5
+ documentClient?: DocumentClient;
6
+ };
7
+ declare class DynamoDbDriver implements DbDriver {
8
+ batchProcesses: Record<string, BatchProcess>;
9
+ documentClient: DocumentClient;
10
+ constructor({ documentClient }?: ConstructorArgs);
11
+ getClient(): DocumentClient;
12
+ create({ table, data, meta, __batch: batch }: Args): Promise<Result>;
13
+ update({ query, data, table, meta, __batch: batch }: Args): Promise<Result>;
14
+ delete({ query, table, meta, __batch: batch }: Args): Promise<Result>;
15
+ read<T>({ table, query, sort, limit, keys, meta, __batch: batch }: Args): Promise<Result<T[]>>;
16
+ createLog({ id, operation, data, table }: {
17
+ id: any;
18
+ operation: any;
19
+ data: any;
20
+ table: any;
21
+ }): Promise<Result>;
22
+ readLogs<T>({ table }: {
23
+ table: any;
24
+ }): Promise<Result<T[]>>;
25
+ getBatchProcess(__batch: any): BatchProcess;
26
+ }
27
+ export default DynamoDbDriver;
@@ -0,0 +1,272 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _dynamodb = require("aws-sdk/clients/dynamodb");
13
+
14
+ var _BatchProcess = _interopRequireDefault(require("./BatchProcess"));
15
+
16
+ var _QueryGenerator = _interopRequireDefault(require("./QueryGenerator"));
17
+
18
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
19
+
20
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
21
+
22
+ const LOG_KEYS = [{
23
+ primary: true,
24
+ unique: true,
25
+ name: "primary",
26
+ fields: [{
27
+ name: "PK"
28
+ }, {
29
+ name: "SK"
30
+ }]
31
+ }];
32
+
33
+ class DynamoDbDriver {
34
+ constructor({
35
+ documentClient
36
+ } = {}) {
37
+ (0, _defineProperty2.default)(this, "batchProcesses", void 0);
38
+ (0, _defineProperty2.default)(this, "documentClient", void 0);
39
+ this.batchProcesses = {};
40
+ this.documentClient = documentClient || new _dynamodb.DocumentClient();
41
+ }
42
+
43
+ getClient() {
44
+ return this.documentClient;
45
+ }
46
+
47
+ async create({
48
+ table,
49
+ data,
50
+ meta,
51
+ __batch: batch
52
+ }) {
53
+ if (!batch) {
54
+ const result = await this.documentClient.put({
55
+ TableName: table,
56
+ Item: data,
57
+ ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
58
+ }).promise();
59
+ return [true, {
60
+ response: result.$response
61
+ }];
62
+ }
63
+
64
+ const batchProcess = this.getBatchProcess(batch);
65
+ batchProcess.addBatchWrite({
66
+ table,
67
+ data
68
+ });
69
+
70
+ if (batchProcess.allOperationsAdded()) {
71
+ batchProcess.startExecution();
72
+ } else {
73
+ await batchProcess.waitStartExecution();
74
+ }
75
+
76
+ await batchProcess.waitExecution();
77
+ return [true, {
78
+ response: batchProcess.response
79
+ }];
80
+ }
81
+
82
+ async update({
83
+ query,
84
+ data,
85
+ table,
86
+ meta,
87
+ __batch: batch
88
+ }) {
89
+ if (!batch) {
90
+ const update = {
91
+ UpdateExpression: "SET ",
92
+ ExpressionAttributeNames: {},
93
+ ExpressionAttributeValues: {}
94
+ };
95
+ const updateExpression = [];
96
+
97
+ for (const key in data) {
98
+ updateExpression.push(`#${key} = :${key}`);
99
+ update.ExpressionAttributeNames[`#${key}`] = key;
100
+ update.ExpressionAttributeValues[`:${key}`] = data[key];
101
+ }
102
+
103
+ update.UpdateExpression += updateExpression.join(", ");
104
+ const result = await this.documentClient.update(_objectSpread({
105
+ TableName: table,
106
+ Key: query,
107
+ ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
108
+ }, update)).promise();
109
+ return [true, {
110
+ response: result.$response
111
+ }];
112
+ }
113
+
114
+ const batchProcess = this.getBatchProcess(batch);
115
+ batchProcess.addBatchWrite({
116
+ table,
117
+ data
118
+ });
119
+
120
+ if (batchProcess.allOperationsAdded()) {
121
+ batchProcess.startExecution();
122
+ } else {
123
+ await batchProcess.waitStartExecution();
124
+ }
125
+
126
+ await batchProcess.waitExecution();
127
+ return [true, {
128
+ response: batchProcess.response
129
+ }];
130
+ }
131
+
132
+ async delete({
133
+ query,
134
+ table,
135
+ meta,
136
+ __batch: batch
137
+ }) {
138
+ if (!batch) {
139
+ const result = await this.documentClient.delete({
140
+ TableName: table,
141
+ Key: query,
142
+ ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
143
+ }).promise();
144
+ return [true, {
145
+ response: result.$response
146
+ }];
147
+ }
148
+
149
+ const batchProcess = this.getBatchProcess(batch);
150
+ batchProcess.addBatchDelete({
151
+ table,
152
+ query
153
+ });
154
+
155
+ if (batchProcess.allOperationsAdded()) {
156
+ batchProcess.startExecution();
157
+ } else {
158
+ await batchProcess.waitStartExecution();
159
+ }
160
+
161
+ await batchProcess.waitExecution();
162
+ return [true, {
163
+ response: batchProcess.response
164
+ }];
165
+ }
166
+
167
+ async read({
168
+ table,
169
+ query,
170
+ sort,
171
+ limit,
172
+ keys,
173
+ meta,
174
+ __batch: batch
175
+ }) {
176
+ if (!batch) {
177
+ const queryGenerator = new _QueryGenerator.default();
178
+ const queryParams = queryGenerator.generate({
179
+ query,
180
+ keys,
181
+ sort,
182
+ limit,
183
+ tableName: table
184
+ });
185
+ const response = await this.documentClient.query(_objectSpread(_objectSpread({}, queryParams), {}, {
186
+ ReturnConsumedCapacity: meta ? "TOTAL" : "NONE"
187
+ })).promise();
188
+
189
+ if (Array.isArray(response.Items)) {
190
+ return [response.Items, {
191
+ response: response.$response
192
+ }];
193
+ }
194
+
195
+ return [[], {
196
+ response: response.$response
197
+ }];
198
+ } // DynamoDb doesn't support batch queries, so we can immediately assume the GetRequest operation.
199
+
200
+
201
+ const batchProcess = this.getBatchProcess(batch);
202
+ const getResult = batchProcess.addBatchGet({
203
+ table,
204
+ query
205
+ });
206
+
207
+ if (batchProcess.allOperationsAdded()) {
208
+ batchProcess.startExecution();
209
+ } else {
210
+ await batchProcess.waitStartExecution();
211
+ }
212
+
213
+ await batchProcess.waitExecution();
214
+ const result = getResult();
215
+
216
+ if (result) {
217
+ return [[result], {
218
+ response: batchProcess.response
219
+ }];
220
+ }
221
+
222
+ return [[], {
223
+ response: batchProcess.response
224
+ }];
225
+ }
226
+
227
+ async createLog({
228
+ id,
229
+ operation,
230
+ data,
231
+ table
232
+ }) {
233
+ await this.create({
234
+ table: table,
235
+ keys: LOG_KEYS,
236
+ data: _objectSpread({
237
+ PK: "log",
238
+ SK: id,
239
+ id,
240
+ operation
241
+ }, data)
242
+ });
243
+ return [true, {}];
244
+ }
245
+
246
+ async readLogs({
247
+ table
248
+ }) {
249
+ return this.read({
250
+ table,
251
+ keys: LOG_KEYS,
252
+ query: {
253
+ PK: "log",
254
+ SK: {
255
+ $gte: " "
256
+ }
257
+ }
258
+ });
259
+ }
260
+
261
+ getBatchProcess(__batch) {
262
+ if (!this.batchProcesses[__batch.instance.id]) {
263
+ this.batchProcesses[__batch.instance.id] = new _BatchProcess.default(__batch.instance, this.documentClient);
264
+ }
265
+
266
+ return this.batchProcesses[__batch.instance.id];
267
+ }
268
+
269
+ }
270
+
271
+ var _default = DynamoDbDriver;
272
+ exports.default = _default;
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,19 @@
1
+ declare class QueryGenerator {
2
+ generate({ query, keys, sort, limit, tableName }: {
3
+ query: any;
4
+ keys: any;
5
+ sort: any;
6
+ limit: any;
7
+ tableName: any;
8
+ }): {
9
+ TableName: any;
10
+ Limit: any;
11
+ KeyConditionExpression: string;
12
+ ExpressionAttributeNames: {};
13
+ ExpressionAttributeValues: {};
14
+ ScanIndexForward: boolean;
15
+ IndexName: any;
16
+ };
17
+ findQueryKey(query?: {}, keys?: any[]): any;
18
+ }
19
+ export default QueryGenerator;
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _createKeyConditionExpressionArgs = _interopRequireDefault(require("./statements/createKeyConditionExpressionArgs"));
13
+
14
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
15
+
16
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
17
+
18
+ class QueryGenerator {
19
+ generate({
20
+ query,
21
+ keys,
22
+ sort,
23
+ limit,
24
+ tableName
25
+ }) {
26
+ // 1. Which key can we use in this query operation?
27
+ const key = this.findQueryKey(query, keys);
28
+
29
+ if (!key) {
30
+ throw new Error("Cannot perform query - key not found.");
31
+ } // 2. Now that we know the key, let's separate the key attributes from the rest.
32
+
33
+
34
+ const keyAttributesValues = {},
35
+ nonKeyAttributesValues = {};
36
+
37
+ for (const queryKey in query) {
38
+ if (key.fields.find(item => item.name === queryKey)) {
39
+ keyAttributesValues[queryKey] = query[queryKey];
40
+ } else {
41
+ nonKeyAttributesValues[queryKey] = query[queryKey];
42
+ }
43
+ }
44
+
45
+ const keyConditionExpression = (0, _createKeyConditionExpressionArgs.default)({
46
+ query: keyAttributesValues,
47
+ sort,
48
+ key
49
+ });
50
+ return _objectSpread(_objectSpread({}, keyConditionExpression), {}, {
51
+ TableName: tableName,
52
+ Limit: limit
53
+ });
54
+ }
55
+
56
+ findQueryKey(query = {}, keys = []) {
57
+ for (let i = 0; i < keys.length; i++) {
58
+ const key = keys[i];
59
+ let hasAllFields = true;
60
+
61
+ for (let j = 0; j < key.fields.length; j++) {
62
+ const field = key.fields[j];
63
+
64
+ if (!query[field.name]) {
65
+ hasAllFields = false;
66
+ break;
67
+ }
68
+ }
69
+
70
+ if (hasAllFields) {
71
+ return key;
72
+ }
73
+ }
74
+ }
75
+
76
+ }
77
+
78
+ var _default = QueryGenerator;
79
+ exports.default = _default;