-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I'm preparing to add yet a third action to generate sitemap files. To keep everything nice and neat, here are a few refactorings: - Collect all of the actions into the actions/ directory. - Factor out repeated S3 bucket config details into a common file. - Collect the mapCirculars function and the CircularsAction type into one file to keep the Lambda entry point itself clean as we add more imports for the actions. - Rename the mapCirculars function to forAllCirculars, because its behavior more closely resembles the .forEach() array method than the .map() method.
- Loading branch information
Showing
8 changed files
with
65 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/*! | ||
* Copyright © 2023 United States Government as represented by the | ||
* Administrator of the National Aeronautics and Space Administration. | ||
* All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { tables } from '@architect/functions' | ||
import type { DynamoDBDocument } from '@aws-sdk/lib-dynamodb' | ||
import { paginateScan } from '@aws-sdk/lib-dynamodb' | ||
|
||
import type { Circular } from '~/routes/circulars/circulars.lib' | ||
|
||
export interface CircularAction<T = any> { | ||
initialize: () => T | Promise<T> | ||
action: (circulars: Circular[], context: T) => void | Promise<void> | ||
finalize: (context: T) => void | Promise<void> | ||
} | ||
|
||
export async function forAllCirculars(...actions: CircularAction[]) { | ||
const contexts = await Promise.all( | ||
actions.map((action) => action.initialize()) | ||
) | ||
for await (const circulars of getAllRecords()) { | ||
await Promise.all( | ||
actions.map(({ action }, i) => action(circulars, contexts[i])) | ||
) | ||
} | ||
await Promise.all(actions.map(({ finalize }, i) => finalize(contexts[i]))) | ||
} | ||
|
||
async function* getAllRecords() { | ||
const db = await tables() | ||
const client = db._doc as unknown as DynamoDBDocument | ||
const TableName = db.name('circulars') | ||
const pages = paginateScan({ client }, { TableName }) | ||
|
||
for await (const page of pages) { | ||
yield page.Items as Circular[] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/*! | ||
* Copyright © 2023 United States Government as represented by the | ||
* Administrator of the National Aeronautics and Space Administration. | ||
* All Rights Reserved. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
import { S3Client } from '@aws-sdk/client-s3' | ||
|
||
export const s3 = new S3Client({}) | ||
export const keyPrefix = 'generated/circulars' |