From 37780f0d44f32a06c68eae45ee8f92f49ffdd43f Mon Sep 17 00:00:00 2001 From: KOMIYA Atsushi Date: Sun, 28 Jan 2024 15:23:57 +0900 Subject: [PATCH] Introduce fetchMultiple() method --- .../src/DynamoDbInstallationStore.ts | 7 +++- .../src/InstallationStoreBase.ts | 38 ++++++++++++++++--- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/packages/bolt-dynamodb/src/DynamoDbInstallationStore.ts b/packages/bolt-dynamodb/src/DynamoDbInstallationStore.ts index 82b10a2..100e840 100644 --- a/packages/bolt-dynamodb/src/DynamoDbInstallationStore.ts +++ b/packages/bolt-dynamodb/src/DynamoDbInstallationStore.ts @@ -13,6 +13,7 @@ import { KeyGenerator, KeyGeneratorArgs, Storage, + StorageBase, } from './InstallationStoreBase'; type DynamoDbKey = Record; @@ -116,14 +117,16 @@ export class SimpleKeyGenerator implements DynamoDbKeyGenerator { } } -class DynamoDbStorage implements Storage { +class DynamoDbStorage extends StorageBase { 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, diff --git a/packages/bolt-dynamodb/src/InstallationStoreBase.ts b/packages/bolt-dynamodb/src/InstallationStoreBase.ts index 73cf744..184ebe1 100644 --- a/packages/bolt-dynamodb/src/InstallationStoreBase.ts +++ b/packages/bolt-dynamodb/src/InstallationStoreBase.ts @@ -22,9 +22,39 @@ export interface Storage { logger: Logger | undefined ): Promise; fetch(key: KEY, logger: Logger | undefined): Promise; + fetchMultiple( + keys: KEY[], + logger: Logger | undefined + ): Promise<(Buffer | undefined)[]>; delete(key: KEY_FOR_DELETION, logger: Logger | undefined): Promise; } +export abstract class StorageBase + implements Storage +{ + abstract store( + key: KEY, + data: Buffer, + isBotToken: boolean, + logger: Logger | undefined + ): Promise; + abstract fetch( + key: KEY, + logger: Logger | undefined + ): Promise; + abstract delete( + key: KEY_FOR_DELETION, + logger: Logger | undefined + ): Promise; + + async fetchMultiple( + keys: KEY[], + logger: Logger | undefined + ): Promise<(Buffer | undefined)[]> { + return await Promise.all(keys.map(key => this.fetch(key, logger))); + } +} + export class InstallationStoreBase implements InstallationStore { @@ -102,12 +132,8 @@ export class InstallationStoreBase } 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) {