Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shared): allow unprefixed query keys #3333

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading