wirejs-deploy-amplify-basic 0.0.70-table-resource → 0.0.72-table-resource
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.
|
@@ -3,7 +3,11 @@ import { DynamoDBClient, } from '@aws-sdk/client-dynamodb';
|
|
|
3
3
|
import { PutCommand, GetCommand, QueryCommand, ScanCommand, DeleteCommand, } from '@aws-sdk/lib-dynamodb';
|
|
4
4
|
import { Resource, indexName } from 'wirejs-resources';
|
|
5
5
|
import { addResource } from '../resource-collector.js';
|
|
6
|
+
function fieldAlias(name) {
|
|
7
|
+
return `#a_${name}`;
|
|
8
|
+
}
|
|
6
9
|
function isFieldComparison(filter) {
|
|
10
|
+
console.log('Checking if filter is a field comparison:', filter);
|
|
7
11
|
return !['and', 'or', 'not'].some(key => key in filter);
|
|
8
12
|
}
|
|
9
13
|
function buildFilterExpression(filter) {
|
|
@@ -20,24 +24,25 @@ function buildFilterExpression(filter) {
|
|
|
20
24
|
if (!isFieldComparison(filter)) {
|
|
21
25
|
throw new Error(`Unsupported filter: ${JSON.stringify(filter)}`);
|
|
22
26
|
}
|
|
23
|
-
const [
|
|
24
|
-
const
|
|
27
|
+
const [baseField] = Object.keys(filter);
|
|
28
|
+
const field = fieldAlias(baseField);
|
|
29
|
+
const condition = filter[baseField];
|
|
25
30
|
if ('eq' in condition)
|
|
26
|
-
return `${field} = :${
|
|
31
|
+
return `${field} = :${baseField}`;
|
|
27
32
|
if ('ne' in condition)
|
|
28
|
-
return `${field} <> :${
|
|
33
|
+
return `${field} <> :${baseField}`;
|
|
29
34
|
if ('gt' in condition)
|
|
30
|
-
return `${field} > :${
|
|
35
|
+
return `${field} > :${baseField}`;
|
|
31
36
|
if ('ge' in condition)
|
|
32
|
-
return `${field} >= :${
|
|
37
|
+
return `${field} >= :${baseField}`;
|
|
33
38
|
if ('lt' in condition)
|
|
34
|
-
return `${field} < :${
|
|
39
|
+
return `${field} < :${baseField}`;
|
|
35
40
|
if ('le' in condition)
|
|
36
|
-
return `${field} <= :${
|
|
41
|
+
return `${field} <= :${baseField}`;
|
|
37
42
|
if ('between' in condition)
|
|
38
|
-
return `${field} BETWEEN :${
|
|
43
|
+
return `${field} BETWEEN :${baseField}Low AND :${baseField}High`;
|
|
39
44
|
if ('beginsWith' in condition)
|
|
40
|
-
return `begins_with(${field}, :${
|
|
45
|
+
return `begins_with(${field}, :${baseField})`;
|
|
41
46
|
throw new Error(`Unsupported filter condition: ${JSON.stringify(condition)}`);
|
|
42
47
|
}
|
|
43
48
|
function buildExpressionAttributeValues(filter) {
|
|
@@ -78,6 +83,27 @@ function buildExpressionAttributeValues(filter) {
|
|
|
78
83
|
}
|
|
79
84
|
return values;
|
|
80
85
|
}
|
|
86
|
+
function buildFieldAliasMap(filter) {
|
|
87
|
+
const aliasMap = {};
|
|
88
|
+
if (filter.and) {
|
|
89
|
+
for (const subFilter of filter.and) {
|
|
90
|
+
Object.assign(aliasMap, buildFieldAliasMap(subFilter));
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else if (filter.or) {
|
|
94
|
+
for (const subFilter of filter.or) {
|
|
95
|
+
Object.assign(aliasMap, buildFieldAliasMap(subFilter));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else if (filter.not) {
|
|
99
|
+
Object.assign(aliasMap, buildFieldAliasMap(filter.not));
|
|
100
|
+
}
|
|
101
|
+
else if (isFieldComparison(filter)) {
|
|
102
|
+
const [field] = Object.keys(filter);
|
|
103
|
+
aliasMap[fieldAlias(field)] = field;
|
|
104
|
+
}
|
|
105
|
+
return aliasMap;
|
|
106
|
+
}
|
|
81
107
|
/**
|
|
82
108
|
* A table of records that favors very high *overall* scalability at the expense of
|
|
83
109
|
* scalability *between* partitions. Providers will distribute your data across many
|
|
@@ -168,12 +194,14 @@ export class DistributedTable extends Resource {
|
|
|
168
194
|
console.log('Scanning DynamoDB table:', this.table, options);
|
|
169
195
|
const filterExpression = options.filter ? buildFilterExpression(options.filter) : undefined;
|
|
170
196
|
const expressionAttributeValues = options.filter ? buildExpressionAttributeValues(options.filter) : undefined;
|
|
197
|
+
const expressionAttributeNames = options.filter ? buildFieldAliasMap(options.filter) : undefined;
|
|
171
198
|
let lastEvaluatedKey = undefined;
|
|
172
199
|
do {
|
|
173
200
|
const result = await this.ddbClient.send(new ScanCommand({
|
|
174
201
|
TableName: this.table,
|
|
175
202
|
FilterExpression: filterExpression,
|
|
176
203
|
ExpressionAttributeValues: expressionAttributeValues,
|
|
204
|
+
ExpressionAttributeNames: expressionAttributeNames,
|
|
177
205
|
ExclusiveStartKey: lastEvaluatedKey,
|
|
178
206
|
}));
|
|
179
207
|
for (const item of result.Items || []) {
|
|
@@ -190,7 +218,7 @@ export class DistributedTable extends Resource {
|
|
|
190
218
|
// Build the key condition expression from the `where` clause
|
|
191
219
|
const whereClauseAsFilter = {
|
|
192
220
|
// decompose each `where` clause property into a separate condition.
|
|
193
|
-
and: Object.entries(options.where).map(([k, v]) =>
|
|
221
|
+
and: Object.entries(options.where).map(([k, v]) => ({ [k]: v }))
|
|
194
222
|
};
|
|
195
223
|
const keyConditionExpression = buildFilterExpression(whereClauseAsFilter);
|
|
196
224
|
// Build the filter expression if provided
|
|
@@ -200,10 +228,22 @@ export class DistributedTable extends Resource {
|
|
|
200
228
|
...buildExpressionAttributeValues(whereClauseAsFilter),
|
|
201
229
|
...(options.filter ? buildExpressionAttributeValues(options.filter) : {}),
|
|
202
230
|
};
|
|
231
|
+
const expressionAttributeNames = {
|
|
232
|
+
...(options.filter ? buildFieldAliasMap(options.filter) : {}),
|
|
233
|
+
...buildFieldAliasMap(whereClauseAsFilter),
|
|
234
|
+
};
|
|
203
235
|
const isIndexNameTheDefault = options.by === indexName({
|
|
204
236
|
partition: this.key.partition,
|
|
205
237
|
sort: this.key.sort
|
|
206
238
|
});
|
|
239
|
+
console.log('DynamoDB query params', {
|
|
240
|
+
TableName: this.table,
|
|
241
|
+
IndexName: isIndexNameTheDefault ? undefined : options.by,
|
|
242
|
+
KeyConditionExpression: keyConditionExpression,
|
|
243
|
+
FilterExpression: filterExpression,
|
|
244
|
+
ExpressionAttributeValues: expressionAttributeValues,
|
|
245
|
+
ExpressionAttributeNames: expressionAttributeNames,
|
|
246
|
+
});
|
|
207
247
|
let lastEvaluatedKey = undefined;
|
|
208
248
|
do {
|
|
209
249
|
const result = await this.ddbClient.send(new QueryCommand({
|
|
@@ -212,6 +252,7 @@ export class DistributedTable extends Resource {
|
|
|
212
252
|
KeyConditionExpression: keyConditionExpression,
|
|
213
253
|
FilterExpression: filterExpression,
|
|
214
254
|
ExpressionAttributeValues: expressionAttributeValues,
|
|
255
|
+
ExpressionAttributeNames: expressionAttributeNames,
|
|
215
256
|
ExclusiveStartKey: lastEvaluatedKey,
|
|
216
257
|
}));
|
|
217
258
|
for (const item of result.Items || []) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "wirejs-deploy-amplify-basic",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.72-table-resource",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -32,14 +32,16 @@
|
|
|
32
32
|
"recursive-copy": "^2.0.14",
|
|
33
33
|
"rimraf": "^6.0.1",
|
|
34
34
|
"wirejs-dom": "^1.0.38",
|
|
35
|
-
"wirejs-resources": "^0.1.
|
|
35
|
+
"wirejs-resources": "^0.1.40-table-resource"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
38
|
"@aws-amplify/backend": "^1.14.0",
|
|
39
39
|
"typescript": "^5.7.3"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
|
-
"
|
|
42
|
+
"clean": "rimraf dist",
|
|
43
|
+
"build": "npm run clean && tsc",
|
|
44
|
+
"test": "node --import tsx --test test/**/*.test.ts"
|
|
43
45
|
},
|
|
44
46
|
"files": [
|
|
45
47
|
"amplify-backend-assets/*",
|