From cfa0f2bec8afd4950c81b24749d226d5cb3b96b1 Mon Sep 17 00:00:00 2001 From: Martin Ramm Date: Sat, 9 Dec 2023 00:54:10 +0100 Subject: [PATCH] Allow TTL attribute to also be a partition or sort key --- src/config/pragmas/tables.js | 8 ++++++ src/lib/is.js | 4 +-- test/unit/src/config/pragmas/tables-test.js | 27 ++++++++++++++++++++- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/config/pragmas/tables.js b/src/config/pragmas/tables.js index c49a76f..4a47073 100644 --- a/src/config/pragmas/tables.js +++ b/src/config/pragmas/tables.js @@ -38,11 +38,19 @@ module.exports = function configureTables ({ arc, inventory, errors }) { if (is.sortKey(value)) { t.sortKey = key t.sortKeyType = value.replace('**', '').toLowerCase() + if (t.sortKeyType === 'ttl') { + t.sortKeyType = 'number' + t.ttl = key + } if (!t.sortKeyType) t.sortKeyType = 'string' } else if (is.primaryKey(value)) { t.partitionKey = key t.partitionKeyType = value.replace('*', '').toLowerCase() + if (t.partitionKeyType === 'ttl') { + t.partitionKeyType = 'number' + t.ttl = key + } if (!t.partitionKeyType) t.partitionKeyType = 'string' } if (key === 'stream') t.stream = value diff --git a/src/lib/is.js b/src/lib/is.js index 91af513..eb06219 100644 --- a/src/lib/is.js +++ b/src/lib/is.js @@ -13,8 +13,8 @@ let string = item => typeof item === 'string' let exists = path => existsSync(path) let folder = path => existsSync(path) && lstatSync(path).isDirectory() // Pragma-specific stuff -let primaryKey = val => string(val) && [ '*', '*string', '*number' ].includes(val.toLowerCase()) -let sortKey = val => string(val) && [ '**', '**string', '**number' ].includes(val.toLowerCase()) +let primaryKey = val => string(val) && [ '*', '*string', '*number', '*ttl' ].includes(val.toLowerCase()) +let sortKey = val => string(val) && [ '**', '**string', '**number', '**ttl' ].includes(val.toLowerCase()) module.exports = { array, diff --git a/test/unit/src/config/pragmas/tables-test.js b/test/unit/src/config/pragmas/tables-test.js index 8e79181..8b2c0f8 100644 --- a/test/unit/src/config/pragmas/tables-test.js +++ b/test/unit/src/config/pragmas/tables-test.js @@ -53,7 +53,7 @@ number-keys }) test('@tables population (extra params)', t => { - t.plan(20) + t.plan(28) let arc = parse(` @tables @@ -130,6 +130,31 @@ string-keys tables = populateTables({ arc, inventory }) t.ok(tables.length === 1, 'Got correct number of tables back') t.equal(tables[0].ttl, '_ttl', 'Got back correct TTL value') + + arc = parse(` +@tables +time-to-live-primary + expiresAt *ttl +`) + inventory = inventoryDefaults() + tables = populateTables({ arc, inventory }) + t.ok(tables.length === 1, 'Got correct number of tables back') + t.equal(tables[0].ttl, 'expiresAt', 'Got back correct TTL value') + t.equal(tables[0].partitionKey, 'expiresAt', 'Got back correct partition key') + t.equal(tables[0].partitionKeyType, 'Number', 'Got back correct partition key type') + + arc = parse(` +@tables +time-to-live-secondary + pk * + expiresAt **ttl +`) + inventory = inventoryDefaults() + tables = populateTables({ arc, inventory }) + t.ok(tables.length === 1, 'Got correct number of tables back') + t.equal(tables[0].ttl, 'expiresAt', 'Got back correct TTL value') + t.equal(tables[0].sortKey, 'expiresAt', 'Got back correct sort key') + t.equal(tables[0].sortKeyType, 'Number', 'Got back correct sort key type') }) test('@tables population: plugin setter', t => {