diff --git a/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap b/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap index 20cda2065f2..faf01a6546b 100644 --- a/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap +++ b/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap @@ -13656,3 +13656,51 @@ $util.qr($update.put(\\"expression\\", \\"$expression\\")) $util.toJson($UpdateItem) ## [End] Mutation Update resolver. **" `; + +exports[`ModelTransformer: the datastore table should be configured 1`] = ` +"## [Start] Sync Request template. ** +#if( !$util.isNullOrEmpty($ctx.stash.authFilter) ) + #set( $filter = $ctx.stash.authFilter ) + #if( !$util.isNullOrEmpty($ctx.args.filter) ) + #set( $filter = { + \\"and\\": [$filter, $ctx.args.filter] +} ) + #end +#else + #if( !$util.isNullOrEmpty($ctx.args.filter) ) + #set( $filter = $ctx.args.filter ) + #end +#end +#if( !$util.isNullOrEmpty($filter) ) + #set( $filterExpression = $util.parseJson($util.transform.toDynamoDBFilterExpression($filter)) ) + #if( !$util.isNullOrBlank($filterExpression.expression) ) + #if( $filterEpression.expressionValues.size() == 0 ) + $util.qr($filterEpression.remove(\\"expressionValues\\")) + #end + #set( $filter = $filterExpression ) + #end +#end +{ + \\"version\\": \\"2018-05-29\\", + \\"operation\\": \\"Sync\\", + \\"filter\\": #if( $filter ) +$util.toJson($filter) + #else +null + #end, + \\"limit\\": $util.defaultIfNull($ctx.args.limit, 100), + \\"lastSync\\": $util.toJson($util.defaultIfNull($ctx.args.lastSync, null)), + \\"nextToken\\": $util.toJson($util.defaultIfNull($ctx.args.nextToken, null)) +} +## [End] Sync Request template. **" +`; + +exports[`ModelTransformer: the datastore table should be configured 2`] = ` +"## [Start] ResponseTemplate. ** +#if( $ctx.error ) + $util.error($ctx.error.message, $ctx.error.type, $ctx.result) +#else + $util.toJson($ctx.result) +#end +## [End] ResponseTemplate. **" +`; diff --git a/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts b/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts index 673b3ae9c45..7598acbc0bd 100644 --- a/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts +++ b/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts @@ -1089,4 +1089,81 @@ describe('ModelTransformer: ', () => { expectFieldsOnInputType(updateTodoInput!, ['name']); }); + it('the datastore table should be configured', () => { + const validSchema = ` + type Todo @model { + name: String + }`; + + const transformer = new GraphQLTransform({ + transformConfig: { + ResolverConfig: { + project: { + ConflictDetection: 'VERSION', + ConflictHandler: ConflictHandlerType.AUTOMERGE, + }, + }, + }, + sandboxModeEnabled: true, + transformers: [new ModelTransformer()], + }); + const out = transformer.transform(validSchema); + expect(out).toBeDefined(); + const schema = parse(out.schema); + validateModelSchema(schema); + // sync operation + const queryObject = getObjectType(schema, 'Query'); + expectFields(queryObject!, ['syncTodos']); + // sync resolvers + expect(out.pipelineFunctions['Query.syncTodos.req.vtl']).toMatchSnapshot(); + expect(out.pipelineFunctions['Query.syncTodos.res.vtl']).toMatchSnapshot(); + // ds table + cdkExpect(out.rootStack).to( + haveResource('AWS::DynamoDB::Table', { + KeySchema: [ + { + AttributeName: 'ds_pk', + KeyType: 'HASH', + }, + { + AttributeName: 'ds_sk', + KeyType: 'RANGE', + }, + ], + AttributeDefinitions: [ + { + AttributeName: 'ds_pk', + AttributeType: 'S', + }, + { + AttributeName: 'ds_sk', + AttributeType: 'S', + }, + ], + BillingMode: 'PAY_PER_REQUEST', + StreamSpecification: { + StreamViewType: 'NEW_AND_OLD_IMAGES', + }, + TableName: { + 'Fn::Join': [ + '', + [ + 'AmplifyDataStore-', + { + 'Fn::GetAtt': ['GraphQLAPI', 'ApiId'], + }, + '-', + { + Ref: 'env', + }, + ], + ], + }, + TimeToLiveSpecification: { + AttributeName: '_ttl', + Enabled: true, + }, + }), + ); + }); }); diff --git a/packages/amplify-graphql-transformer-core/src/transformation/sync-utils.ts b/packages/amplify-graphql-transformer-core/src/transformation/sync-utils.ts index a703a99e82b..3e656f14cc4 100644 --- a/packages/amplify-graphql-transformer-core/src/transformation/sync-utils.ts +++ b/packages/amplify-graphql-transformer-core/src/transformation/sync-utils.ts @@ -1,4 +1,4 @@ -import { AttributeType, BillingMode, StreamViewType, Table, TableEncryption } from '@aws-cdk/aws-dynamodb'; +import { AttributeType, BillingMode, StreamViewType, Table } from '@aws-cdk/aws-dynamodb'; import * as cdk from '@aws-cdk/core'; import * as iam from '@aws-cdk/aws-iam'; import { ResourceConstants, SyncResourceIDs } from 'graphql-transformer-common'; @@ -19,7 +19,7 @@ export function createSyncTable(context: TransformerContext) { const stack = context.stackManager.getStackFor(SyncResourceIDs.syncTableName); const tableName = context.resourceHelper.generateResourceName(SyncResourceIDs.syncTableName); // eslint-disable-next-line no-new - new Table(stack, SyncResourceIDs.syncTableName, { + new Table(stack, SyncResourceIDs.syncDataSourceID, { tableName, partitionKey: { name: SyncResourceIDs.syncPrimaryKey, @@ -30,7 +30,6 @@ export function createSyncTable(context: TransformerContext) { type: AttributeType.STRING, }, stream: StreamViewType.NEW_AND_OLD_IMAGES, - encryption: TableEncryption.DEFAULT, removalPolicy: cdk.RemovalPolicy.DESTROY, billingMode: BillingMode.PAY_PER_REQUEST, timeToLiveAttribute: '_ttl',