Skip to content

Commit 6666bb3

Browse files
author
Dane Pilcher
committed
feat: add grantStreamRead to amplify managed table
1 parent f9a2e2c commit 6666bb3

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

packages/amplify-graphql-model-transformer/src/__tests__/amplify-dynamodb-table-generator.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
} from '@aws-amplify/graphql-transformer-core';
77
import { parse } from 'graphql';
88
import { ModelTransformer } from '../graphql-model-transformer';
9+
import { SearchableModelTransformer } from '@aws-amplify/graphql-searchable-transformer';
910
import { CUSTOM_DDB_CFN_TYPE, CUSTOM_IMPORTED_DDB_CFN_TYPE } from '../resources/amplify-dynamodb-table/amplify-dynamodb-table-construct';
1011
import { ITERATIVE_TABLE_STACK_NAME } from '../resources/amplify-dynamodb-table/amplify-dynamo-model-resource-generator';
1112

@@ -149,4 +150,26 @@ describe('ModelTransformer:', () => {
149150
};
150151
expect(() => testTransform(transformOption)).toThrow('No resource generator assigned for Post with dbType DYNAMODB');
151152
});
153+
154+
it('should allow searchable on amplify managed table', async () => {
155+
const validSchema = `
156+
type Post @model @searchable {
157+
id: ID!
158+
title: String!
159+
}
160+
`;
161+
162+
const out = testTransform({
163+
schema: validSchema,
164+
transformers: [new ModelTransformer(), new SearchableModelTransformer()],
165+
dataSourceStrategies: {
166+
Post: DDB_AMPLIFY_MANAGED_DATASOURCE_STRATEGY,
167+
},
168+
});
169+
expect(out).toBeDefined();
170+
expect(out.stacks.SearchableStack).toBeDefined();
171+
172+
validateModelSchema(parse(out.schema));
173+
parse(out.schema);
174+
});
152175
});

packages/amplify-graphql-model-transformer/src/resources/amplify-dynamodb-table/amplify-dynamodb-table-construct/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
TableEncryption,
1515
TableProps,
1616
} from 'aws-cdk-lib/aws-dynamodb';
17+
import { IGrantable, Grant } from 'aws-cdk-lib/aws-iam';
1718
import { Construct } from 'constructs';
1819

1920
const HASH_KEY_TYPE = 'HASH';
@@ -202,6 +203,20 @@ export class AmplifyDynamoDBTable extends Resource {
202203
return schema;
203204
}
204205

206+
public grantStreamRead(grantee: IGrantable): Grant {
207+
if (!this.tableStreamArn) {
208+
throw new Error(`No stream ARNs found on the table ${this.node.path}`);
209+
}
210+
if (this.encryptionKey) {
211+
this.encryptionKey.grant(grantee, 'kms:Decrypt', 'kms:DescribeKey');
212+
}
213+
return Grant.addToPrincipal({
214+
grantee,
215+
actions: ['dynamodb:ListStreams', 'dynamodb:DescribeStream', 'dynamodb:GetRecords', 'dynamodb:GetShardIterator'],
216+
resourceArns: [this.tableStreamArn],
217+
});
218+
}
219+
205220
private addKey(attribute: Attribute, keyType: string) {
206221
const existingProp = this.findKey(keyType);
207222
if (existingProp) {

0 commit comments

Comments
 (0)