graphql-ttl-transformer-v2-beta
Version:
Enable DynamoDB's time-to-live feature to auto-delete old entries in your AWS Amplify API!
46 lines • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TtlTransformer = void 0;
const graphql_transformer_core_1 = require("@aws-amplify/graphql-transformer-core");
const graphql_transformer_common_1 = require("graphql-transformer-common");
class TtlTransformer extends graphql_transformer_core_1.TransformerPluginBase {
constructor() {
super("TtlTransformer", "directive @ttl on FIELD_DEFINITION");
this.ttlFields = new Map();
this.field = (parent, definition, directive, acc) => {
if (!["AWSTimestamp", "Int"].includes((0, graphql_transformer_common_1.getBaseType)(definition.type))) {
throw new graphql_transformer_core_1.InvalidDirectiveError('Directive "@ttl" must be used only on AWSTimestamp or Int type fields.');
}
let numberOfTtlDirectivesInsideParentType = 0;
if (parent.fields) {
parent.fields.forEach((field) => {
if (field.directives) {
numberOfTtlDirectivesInsideParentType += field.directives.filter((directive) => directive.name.value === "ttl").length;
}
});
}
if (numberOfTtlDirectivesInsideParentType > 1) {
throw new graphql_transformer_core_1.InvalidDirectiveError('Directive "@ttl" must be used only once in the same type.');
}
const fieldName = definition.name.value;
this.ttlFields.set(parent, fieldName);
};
this.generateResolvers = (ctx) => {
this.ttlFields.forEach((fieldName, parent) => {
const ddbTable = this.getTable(ctx, parent);
ddbTable["table"].timeToLiveSpecification = {
attributeName: fieldName,
enabled: true,
};
});
};
this.getTable = (context, definition) => {
const ddbDataSource = context.dataSources.get(definition);
const tableName = graphql_transformer_common_1.ModelResourceIDs.ModelTableResourceID(definition.name.value);
const table = ddbDataSource.ds.stack.node.findChild(tableName);
return table;
};
}
}
exports.TtlTransformer = TtlTransformer;
//# sourceMappingURL=ttl-transformer.js.map