wirejs-deploy-amplify-basic 0.0.65-table-resource → 0.0.67-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.
@@ -7,14 +7,15 @@ import { Bucket, BlockPublicAccess } from 'aws-cdk-lib/aws-s3';
7
7
  import { Table, AttributeType, BillingMode } from 'aws-cdk-lib/aws-dynamodb';
8
8
  import { api } from './functions/api/resource';
9
9
  import { auth } from './auth/resource';
10
+ import { KeyFieldDefinition } from 'wirejs-resources';
11
+
12
+ // @ts-ignore
13
+ import generated from './generated-resources';
10
14
 
11
15
  const APP_ID = process.env.AWS_APP_ID ?? process.env.PWD?.replace(/[^a-zA-Z0-9-_]/g, '_');
12
16
  const BRANCH_ID = process.env.AWS_BRANCH ?? process.env.USER ?? 'anonymous';
13
17
  const TABLE_NAME_PREFIX = `${APP_ID}-${BRANCH_ID}-`;
14
18
 
15
- // @ts-ignore
16
- import generated from './generated-resources';
17
-
18
19
  /**
19
20
  * Amplify resources
20
21
  */
@@ -59,25 +60,30 @@ function isDistributedTable(resource: any): resource is {
59
60
  type: 'DistributedTable';
60
61
  options: {
61
62
  absoluteId: string;
62
- partitionKey: string;
63
- sortKey: string[];
63
+ partitionKey: KeyFieldDefinition<any, any, any>;
64
+ sortKey: KeyFieldDefinition<any, any, any> | undefined;
64
65
  }
65
66
  } {
66
67
  return resource.type === 'DistributedTable';
67
68
  }
68
69
 
70
+ const PKFieldTypes = {
71
+ 'string': AttributeType.STRING,
72
+ 'number': AttributeType.NUMBER,
73
+ }
74
+
69
75
  // TODO: Need to get AttributeType from customer definition.
70
76
  for (const resource of generated) {
71
77
  if (isDistributedTable(resource)) {
72
78
  const sanitizedId = resource.options.absoluteId.replace(/[^a-zA-Z0-9-_]/g, '_');
73
79
  const table = new Table(backend.stack, sanitizedId, {
74
80
  partitionKey: {
75
- name: resource.options.partitionKey,
76
- type: AttributeType.STRING,
81
+ name: resource.options.partitionKey.field,
82
+ type: PKFieldTypes[resource.options.partitionKey.type],
77
83
  },
78
84
  sortKey: resource.options.sortKey ? {
79
- name: resource.options.sortKey[0],
80
- type: AttributeType.STRING,
85
+ name: resource.options.sortKey.field,
86
+ type: PKFieldTypes[resource.options.sortKey.type],
81
87
  } : undefined,
82
88
  removalPolicy: RemovalPolicy.RETAIN,
83
89
  tableName: `${TABLE_NAME_PREFIX}${sanitizedId}`,
@@ -3,6 +3,6 @@
3
3
  "dependencies": {
4
4
  "jsdom": "^25.0.1",
5
5
  "wirejs-dom": "^1.0.38",
6
- "wirejs-resources": "^0.1.33-table-resource"
6
+ "wirejs-resources": "^0.1.35-table-resource"
7
7
  }
8
8
  }
@@ -1,5 +1,5 @@
1
1
  import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
2
- import { Filter, Parser, RecordKey, Resource } from 'wirejs-resources';
2
+ import { AnyKeyFieldOption, Filter, Parser, RecordKey, Resource } from 'wirejs-resources';
3
3
  export declare function PassThruParser<T>(record: Record<string, any>): T;
4
4
  /**
5
5
  * A table of records that favors very high *overall* scalability at the expense of
@@ -12,7 +12,7 @@ export declare function PassThruParser<T>(record: Record<string, any>): T;
12
12
  *
13
13
  * High cardinality, non-sequential partition keys allow for the best overall scaling.
14
14
  */
15
- export declare class DistributedTable<const P extends Parser<any>, const T extends ReturnType<P>, const PK extends keyof T & string, const SK extends (keyof T & string)[] | undefined> extends Resource {
15
+ export declare class DistributedTable<const P extends Parser<any>, const T extends ReturnType<P>, const PK extends AnyKeyFieldOption<T>, const SK extends AnyKeyFieldOption<T> | undefined> extends Resource {
16
16
  #private;
17
17
  parse: P;
18
18
  partitionKey: PK;
@@ -26,6 +26,8 @@ export declare class DistributedTable<const P extends Parser<any>, const T exten
26
26
  sort?: SK;
27
27
  };
28
28
  });
29
+ get partitionKeyName(): PK['field'];
30
+ get sortKeyName(): 'field' extends (string & keyof SK) ? SK['field'] : undefined;
29
31
  save(item: T): Promise<void>;
30
32
  saveMany(items: T[]): Promise<void>;
31
33
  delete(item: RecordKey<T, PK, SK>): Promise<void>;
@@ -34,7 +36,7 @@ export declare class DistributedTable<const P extends Parser<any>, const T exten
34
36
  scan(options?: {
35
37
  filter?: Filter<T>;
36
38
  }): AsyncGenerator<T>;
37
- query(partition: Pick<T, PK>, options?: {
39
+ query(partition: Pick<T, PK['field']>, options?: {
38
40
  filter?: Filter<T>;
39
41
  }): AsyncGenerator<T>;
40
42
  }
@@ -1,6 +1,6 @@
1
1
  import { env } from 'process';
2
- import { DynamoDBClient, DeleteItemCommand, } from '@aws-sdk/client-dynamodb';
3
- import { PutCommand, GetCommand, QueryCommand, ScanCommand, } from '@aws-sdk/lib-dynamodb';
2
+ import { DynamoDBClient, } from '@aws-sdk/client-dynamodb';
3
+ import { PutCommand, GetCommand, QueryCommand, ScanCommand, DeleteCommand, } from '@aws-sdk/lib-dynamodb';
4
4
  import { Resource, } from 'wirejs-resources';
5
5
  import { addResource } from '../resource-collector.js';
6
6
  function isFieldComparison(filter) {
@@ -111,14 +111,20 @@ export class DistributedTable extends Resource {
111
111
  sortKey: this.sort,
112
112
  });
113
113
  }
114
+ get partitionKeyName() {
115
+ const [pkField] = Object.keys(this.partitionKey);
116
+ return pkField;
117
+ }
118
+ get sortKeyName() {
119
+ const [skField] = Object.keys(this.sort ?? {});
120
+ return skField;
121
+ }
114
122
  #getDDBKey(key) {
115
123
  const ddbKey = {
116
- [this.partitionKey]: key[this.partitionKey]
124
+ [this.partitionKeyName]: key[this.partitionKeyName]
117
125
  };
118
- if (this.sort) {
119
- for (const sk of this.sort) {
120
- ddbKey[sk] = key[sk];
121
- }
126
+ if (typeof this.sortKeyName === 'string') {
127
+ ddbKey[this.sortKeyName] = key[this.sortKeyName];
122
128
  }
123
129
  return ddbKey;
124
130
  }
@@ -141,7 +147,7 @@ export class DistributedTable extends Resource {
141
147
  async delete(item) {
142
148
  const key = this.#getDDBKey(item);
143
149
  console.log('Deleting item from DynamoDB:', key);
144
- await this.ddbClient.send(new DeleteItemCommand({
150
+ await this.ddbClient.send(new DeleteCommand({
145
151
  TableName: this.table,
146
152
  Key: key,
147
153
  }));
@@ -191,9 +197,9 @@ export class DistributedTable extends Resource {
191
197
  const expressionAttributeValues = options.filter ? buildExpressionAttributeValues(options.filter) : undefined;
192
198
  const result = await this.ddbClient.send(new QueryCommand({
193
199
  TableName: this.table,
194
- KeyConditionExpression: `${this.partitionKey} = :partitionKey`,
200
+ KeyConditionExpression: `${this.partitionKeyName} = :partitionKey`,
195
201
  ExpressionAttributeValues: {
196
- ':partitionKey': ddbKey[this.partitionKey],
202
+ ':partitionKey': ddbKey[this.partitionKeyName],
197
203
  ...expressionAttributeValues
198
204
  },
199
205
  FilterExpression: filterExpression,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-deploy-amplify-basic",
3
- "version": "0.0.65-table-resource",
3
+ "version": "0.0.67-table-resource",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -32,7 +32,7 @@
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.33-table-resource"
35
+ "wirejs-resources": "^0.1.35-table-resource"
36
36
  },
37
37
  "devDependencies": {
38
38
  "@aws-amplify/backend": "^1.14.0",