Skip to content

Instantly share code, notes, and snippets.

@sonjisov
Created March 22, 2020 04:25
Show Gist options
  • Save sonjisov/39bffe923ed39c2729560f80d2c80488 to your computer and use it in GitHub Desktop.
Save sonjisov/39bffe923ed39c2729560f80d2c80488 to your computer and use it in GitHub Desktop.
const aws = require('aws-sdk');
const dynamodb = new aws.DynamoDB.DocumentClient();
const uuid = require('uuid/v4');
const addToDynamoDb = async ({ butterflyName, bornTime }) => {
const butterflyId = uuid();
const nowTimestamp = new Date().getTime(); // Date when a butterfly is born as an egg
const becomesCaterpilarAt = nowTimestamp + 7 * 24 * 60 * 60 * 1000; // Time when an egg becomes a caterpilar (update terminology in the blog)
const becomesCocoonAt = becomesCaterpilarAt + 7 * 24 * 60 * 60 * 1000; // Time when a caterpilar becomes a cocoon
const becomesButterflyAt = becomesCocoonAt + 7 * 24 * 60 * 60 * 1000; // Time when a cocoon becomes a butterfly
const butterflyTableName = 'Butterfly';
await dynamodb.transactWrite({
TransactItems: [
{
// Scheduling transition into a caterpilar
Put: {
TableName: butterflyTableName,
Item: {
partitionId: butterflyId,
id: becomesCaterpilarAt,
status: 'CATERPILAR',
recordType: 'STATUS_TASK',
ttl: becomesCaterpilarAt,
}
},
},
{
// Scheduling transition into a cocoon
Put: {
TableName: butterflyTableName, // TODO : Make a const for the table name
Item: {
partitionId: butterflyId,
id: becomesCocoonAt,
status: 'COCOON',
recordType: 'STATUS_TASK',
ttl: becomesCocoonAt,
}
},
},
{
// Scheduling transition into a buttterfly
Put: {
TableName: butterflyTableName,
Item: {
partitionId: butterflyId,
id: becomesButterflyAt,
status: 'BUTTERFLY',
recordType: 'STATUS_TASK',
ttl: becomesButterflyAt,
}
},
},
{
// Adding a new butterfly in the "egg" state
Put: {
TableName: butterflyTableName,
Item: {
partitionId: butterflyId,
id: butterflyId,
name: butterflyName,
status: 'EGG',
recordType: 'BUTTERFLY',
}
},
}
],
}).promise();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment