Skip to content

Commit

Permalink
Introduce fetchMultiple() method
Browse files Browse the repository at this point in the history
  • Loading branch information
komiya-atsushi committed Jan 28, 2024
1 parent de16d75 commit 37780f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
7 changes: 5 additions & 2 deletions packages/bolt-dynamodb/src/DynamoDbInstallationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
KeyGenerator,
KeyGeneratorArgs,
Storage,
StorageBase,
} from './InstallationStoreBase';

type DynamoDbKey = Record<string, AttributeValue>;
Expand Down Expand Up @@ -116,14 +117,16 @@ export class SimpleKeyGenerator implements DynamoDbKeyGenerator {
}
}

class DynamoDbStorage implements Storage<DynamoDbKey, DynamoDbDeletionKey> {
class DynamoDbStorage extends StorageBase<DynamoDbKey, DynamoDbDeletionKey> {
constructor(
private readonly client: DynamoDB,
private readonly tableName: string,
private readonly keyGenerator: DynamoDbKeyGenerator,
private readonly attributeName: string,
private readonly deletionOption: DeletionOption
) {}
) {
super();
}

static async create(
client: DynamoDB | Promise<DynamoDB>,
Expand Down
38 changes: 32 additions & 6 deletions packages/bolt-dynamodb/src/InstallationStoreBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,39 @@ export interface Storage<KEY, KEY_FOR_DELETION> {
logger: Logger | undefined
): Promise<void>;
fetch(key: KEY, logger: Logger | undefined): Promise<Buffer | undefined>;
fetchMultiple(
keys: KEY[],
logger: Logger | undefined
): Promise<(Buffer | undefined)[]>;
delete(key: KEY_FOR_DELETION, logger: Logger | undefined): Promise<void>;
}

export abstract class StorageBase<KEY, KEY_FOR_DELETION>
implements Storage<KEY, KEY_FOR_DELETION>
{
abstract store(
key: KEY,
data: Buffer,
isBotToken: boolean,
logger: Logger | undefined
): Promise<void>;
abstract fetch(
key: KEY,
logger: Logger | undefined
): Promise<Buffer | undefined>;
abstract delete(
key: KEY_FOR_DELETION,
logger: Logger | undefined
): Promise<void>;

async fetchMultiple(
keys: KEY[],
logger: Logger | undefined
): Promise<(Buffer | undefined)[]> {
return await Promise.all(keys.map(key => this.fetch(key, logger)));
}
}

export class InstallationStoreBase<KEY, KEY_FOR_DELETION>
implements InstallationStore
{
Expand Down Expand Up @@ -102,12 +132,8 @@ export class InstallationStoreBase<KEY, KEY_FOR_DELETION>
}

const storage = await this.storage;
const [app, user] = await Promise.all(
keys.map(key =>
storage
.fetch(key, logger)
.then(data => (data ? this.codec.decode(data) : undefined))
)
const [app, user] = (await storage.fetchMultiple(keys, logger)).map(data =>
data ? this.codec.decode(data) : undefined
);

if (app !== undefined) {
Expand Down

0 comments on commit 37780f0

Please sign in to comment.