From 42c22794350e5d0685bcace61ae50fcbf6d8e7aa Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Mon, 26 Aug 2024 15:06:39 +1200 Subject: [PATCH] fix(shared): allow unprefixed query keys (#3333) ### Motivation all of our unit tests use the memory config provider which ensures the keys start with the correct prefix ### Modifications In dynamodb configs force the prefix to be added if its not present. ### Verification updated unit tests --- .../dynamo/__tests__/config.dynamo.test.ts | 26 +++++++++---------- .../shared/src/dynamo/dynamo.config.base.ts | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/packages/shared/src/dynamo/__tests__/config.dynamo.test.ts b/packages/shared/src/dynamo/__tests__/config.dynamo.test.ts index b004c67f9..5c3b161a9 100644 --- a/packages/shared/src/dynamo/__tests__/config.dynamo.test.ts +++ b/packages/shared/src/dynamo/__tests__/config.dynamo.test.ts @@ -89,10 +89,8 @@ describe('ConfigDynamo', () => { it('should throw without prefix', async () => { fakeDynamo.values.set('ts_abc123', { id: 'ts_abc123' }); - const ret = await provider.TileSet.get('abc123').catch((e) => String(e)); - - assert.deepEqual(fakeDynamo.get, []); - assert.deepEqual(String(ret), 'Error: Trying to query "abc123" expected prefix of ts'); + const ret = await provider.TileSet.get('abc123'); + assert.equal(ret?.id, 'ts_abc123'); }); it('should get-all partial', async () => { @@ -105,19 +103,21 @@ describe('ConfigDynamo', () => { assert.deepEqual([...ret.values()], [...fakeDynamo.values.values()] as any); }); - it('should throw if on wrong prefix', async () => { - const ret = await provider.TileSet.get('im_abc123').catch((e) => String(e)); - assert.deepEqual(fakeDynamo.get, []); - assert.deepEqual(String(ret), 'Error: Trying to query "im_abc123" expected prefix of ts'); + it('should not throw if on wrong prefix', async () => { + fakeDynamo.values.set('im_abc123', { id: 'im_abc123' }); + + const ret = await provider.TileSet.get('im_abc123'); + assert.equal(ret, undefined); // Query will be for ts_im_abc123 }); - it('should throw on prefixed and un-prefixed', async () => { + it('should not on prefixed and un-prefixed', async () => { fakeDynamo.values.set('ts_abc123', { id: 'ts_abc123' }); - const ret = provider.TileSet.getAll(new Set(['abc123', 'ts_abc123'])); - const err = await ret.then(() => null).catch((e) => String(e)); - assert.equal(String(err), 'Error: Trying to query "abc123" expected prefix of ts'); - assert.deepEqual(fakeDynamo.getAll, []); + await provider.TileSet.getAll(new Set(['abc123', 'ts_abc123'])); + assert.deepEqual(fakeDynamo.getAll[0].RequestItems.Foo.Keys, [ + { id: { S: 'ts_abc123' } }, + { id: { S: 'ts_abc123' } }, + ]); }); describe('DynamoCached', () => { diff --git a/packages/shared/src/dynamo/dynamo.config.base.ts b/packages/shared/src/dynamo/dynamo.config.base.ts index d880c2be7..2abfa95bf 100644 --- a/packages/shared/src/dynamo/dynamo.config.base.ts +++ b/packages/shared/src/dynamo/dynamo.config.base.ts @@ -27,7 +27,7 @@ export class ConfigDynamoBase extends Basemap /** Ensure the ID is prefixed before querying */ ensureId(id: string): string { if (id.startsWith(this.prefix + '_')) return id; - throw new Error(`Trying to query "${id}" expected prefix of ${this.prefix}`); + return `${this.prefix}_${id}`; } private get db(): DynamoDB {