Skip to content

Commit

Permalink
fix(shared): allow unprefixed query keys (#3333)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
blacha authored Aug 26, 2024
1 parent 89e72ea commit 42c2279
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
26 changes: 13 additions & 13 deletions packages/shared/src/dynamo/__tests__/config.dynamo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/src/dynamo/dynamo.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class ConfigDynamoBase<T extends BaseConfig = BaseConfig> 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 {
Expand Down

0 comments on commit 42c2279

Please sign in to comment.