Skip to content

Commit

Permalink
Courey/create synonym server only (nasa-gcn#1933)
Browse files Browse the repository at this point in the history
  • Loading branch information
Courey authored Feb 22, 2024
1 parent 409cf4f commit 1d7bb05
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions app/routes/_gcn.synonyms/synonyms.server.ts
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
}

0 comments on commit 1d7bb05

Please sign in to comment.