From dcbec3cf5e1eae36be849cdcc52a6f099e8625c3 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Mon, 26 Aug 2024 14:43:40 +1200 Subject: [PATCH 1/2] fix(shared): allow unprefixed query keys --- packages/shared/src/dynamo/dynamo.config.base.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 { From bf86fe6899c3e975aaeea3a901bc8019383e5da2 Mon Sep 17 00:00:00 2001 From: Blayne Chard Date: Mon, 26 Aug 2024 14:52:11 +1200 Subject: [PATCH 2/2] refactor: fixup unit tests --- .../dynamo/__tests__/config.dynamo.test.ts | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 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', () => {