UNPKG

@aws-amplify/cli-internal

Version:
128 lines • 7.1 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DynamoDBRenderer = void 0; const typescript_1 = __importDefault(require("typescript")); const ts_1 = require("../../ts"); const resource_types_1 = require("../../../_common/resource-types"); const factory = typescript_1.default.factory; class DynamoDBRenderer { constructor(resourceName) { this.resourceName = resourceName; this.capitalizedName = resourceName.charAt(0).toUpperCase() + resourceName.slice(1); this.functionName = `defineStorage${this.capitalizedName}`; } render(table) { return factory.createNodeArray([ this.renderBackendTypeImport(), this.renderCdkImports(), this.renderCfnResourceImport(), ts_1.newLineIdentifier, this.renderDefineStorage(table), ts_1.newLineIdentifier, this.renderPostRefactor(table), ]); } renderBackendTypeImport() { return ts_1.TS.typeImport('../../backend', 'Backend'); } renderCdkImports() { return ts_1.TS.namedImport('aws-cdk-lib/aws-dynamodb', 'Table', 'AttributeType', 'BillingMode', 'StreamViewType', 'CfnTable'); } renderCfnResourceImport() { return ts_1.TS.namedImport('aws-cdk-lib', 'CfnResource'); } renderDefineStorage(table) { const scopeVarName = `storage${this.capitalizedName}Stack`; const sanitizedName = sanitizeVariableName(table.tableName.replace(/-[^-]+$/, '')); const hasGSIs = table.gsis && table.gsis.length > 0; const bodyStatements = []; bodyStatements.push(ts_1.TS.declareConst(scopeVarName, factory.createCallExpression(ts_1.TS.propAccess('backend', 'createStack'), undefined, [ factory.createStringLiteral('storage' + this.resourceName), ]))); bodyStatements.push(...this.buildTableStatements(table, scopeVarName)); bodyStatements.push(ts_1.TS.retentionLoop(ts_1.TS.propAccess(scopeVarName, 'node'), resource_types_1.STORAGE_DYNAMO_RESOURCES_TO_RETAIN)); if (hasGSIs) { bodyStatements.push(factory.createReturnStatement(factory.createIdentifier(sanitizedName))); } return ts_1.TS.exportedFunction(this.functionName, bodyStatements); } renderPostRefactor(table) { const sanitizedName = sanitizeVariableName(table.tableName.replace(/-[^-]+$/, '')); const body = ts_1.TS.castAssign(ts_1.TS.propAccess(sanitizedName, 'node', 'defaultChild'), 'CfnTable', 'tableName', factory.createStringLiteral(table.tableName)); return factory.createFunctionDeclaration([factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], undefined, 'postRefactor', undefined, [factory.createParameterDeclaration(undefined, undefined, sanitizedName, undefined, factory.createTypeReferenceNode('Table'))], undefined, factory.createBlock([body], true)); } buildTableStatements(table, scopeVarName) { const statements = []; const baseTableName = table.tableName.replace(/-[^-]+$/, ''); const sanitizedName = sanitizeVariableName(baseTableName); const tableProps = [ factory.createPropertyAssignment('partitionKey', factory.createObjectLiteralExpression([ ts_1.TS.stringProp('name', table.partitionKey.name), ts_1.TS.enumProp('type', 'AttributeType', table.partitionKey.type), ])), ts_1.TS.enumProp('billingMode', 'BillingMode', table.billingMode || 'PROVISIONED'), ]; if (table.billingMode !== 'PAY_PER_REQUEST') { tableProps.push(factory.createPropertyAssignment('readCapacity', factory.createNumericLiteral(String(table.readCapacity || 5)))); tableProps.push(factory.createPropertyAssignment('writeCapacity', factory.createNumericLiteral(String(table.writeCapacity || 5)))); } if (table.streamEnabled && table.streamViewType) { tableProps.push(ts_1.TS.enumProp('stream', 'StreamViewType', table.streamViewType)); } if (table.sortKey) { tableProps.push(factory.createPropertyAssignment('sortKey', factory.createObjectLiteralExpression([ ts_1.TS.stringProp('name', table.sortKey.name), ts_1.TS.enumProp('type', 'AttributeType', table.sortKey.type), ]))); } const hasGSIs = table.gsis && table.gsis.length > 0; if (hasGSIs) { statements.push(factory.createVariableStatement([], factory.createVariableDeclarationList([ factory.createVariableDeclaration(sanitizedName, undefined, undefined, factory.createNewExpression(factory.createIdentifier('Table'), undefined, [ factory.createIdentifier(scopeVarName), factory.createStringLiteral(sanitizedName), factory.createObjectLiteralExpression(tableProps), ])), ], typescript_1.default.NodeFlags.Const))); } else { statements.push(factory.createExpressionStatement(factory.createNewExpression(factory.createIdentifier('Table'), undefined, [ factory.createIdentifier(scopeVarName), factory.createStringLiteral(sanitizedName), factory.createObjectLiteralExpression(tableProps), ]))); } if (table.gsis) { for (const gsi of table.gsis) { statements.push(this.renderGSI(sanitizedName, gsi)); } } return statements; } renderGSI(tableVarName, gsi) { const gsiProps = [ ts_1.TS.stringProp('indexName', gsi.indexName), factory.createPropertyAssignment('partitionKey', factory.createObjectLiteralExpression([ ts_1.TS.stringProp('name', gsi.partitionKey.name), ts_1.TS.enumProp('type', 'AttributeType', gsi.partitionKey.type), ])), ]; if (gsi.sortKey) { gsiProps.push(factory.createPropertyAssignment('sortKey', factory.createObjectLiteralExpression([ ts_1.TS.stringProp('name', gsi.sortKey.name), ts_1.TS.enumProp('type', 'AttributeType', gsi.sortKey.type), ]))); } gsiProps.push(factory.createPropertyAssignment('readCapacity', factory.createNumericLiteral('5'))); gsiProps.push(factory.createPropertyAssignment('writeCapacity', factory.createNumericLiteral('5'))); return factory.createExpressionStatement(factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier(tableVarName), factory.createIdentifier('addGlobalSecondaryIndex')), undefined, [factory.createObjectLiteralExpression(gsiProps)])); } } exports.DynamoDBRenderer = DynamoDBRenderer; function sanitizeVariableName(name) { return name.replace(/[^a-zA-Z0-9_$]/g, '_'); } //# sourceMappingURL=dynamodb.renderer.js.map