forked from nasa-gcn/gcn.nasa.gov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Courey/create synonym server only (nasa-gcn#1933)
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*! | ||
* 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/dist-types/DynamoDBDocument' | ||
import crypto from 'crypto' | ||
|
||
/* | ||
* If an eventId already has a synonym and is passed in, it will unlink the | ||
* eventId from the old synonym and the only remaining link will be to the | ||
* new synonym. | ||
* | ||
* BatchWriteItem has a limit of 25 items, so the user may not add more than | ||
* 25 synonyms at a time. | ||
*/ | ||
export async function createSynonyms(...synonymousEventIds: string[]) { | ||
const uuid = crypto.randomUUID() | ||
const db = await tables() | ||
const client = db._doc as unknown as DynamoDBDocument | ||
|
||
const writeRequests = synonymousEventIds | ||
.filter((eventId) => Boolean(eventId)) | ||
.map((eventId) => ({ | ||
PutRequest: { | ||
Item: { uuid, eventId }, | ||
}, | ||
})) | ||
|
||
const params = { | ||
RequestItems: { | ||
synonyms: writeRequests, | ||
}, | ||
} | ||
|
||
await client.batchWrite(params) | ||
|
||
return uuid | ||
} |