-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CMDCT-4019: commiting new coreSet table infrastructure setup
- Loading branch information
1 parent
366cd75
commit 2cdb4b6
Showing
5 changed files
with
109 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
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,81 @@ | ||
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; | ||
import { | ||
DynamoDBDocumentClient, | ||
paginateScan, | ||
PutCommand, | ||
} from "@aws-sdk/lib-dynamodb"; | ||
import prompt from "prompt-sync"; | ||
|
||
/*** | ||
* Run with `npx tsx transformCoreSetTable.ts` | ||
*/ | ||
const transformMeasureTable = async () => { | ||
let stage = "local"; | ||
const p = prompt(); | ||
const runLocally = p("Do you want to run this script locally? Y/N: "); | ||
const isLocal = runLocally === "Y" ? true : false; | ||
if (!isLocal) { | ||
stage = p("What environment are we running on (e.g. master, val, prod)? "); | ||
} | ||
|
||
const dbClient = buildClient(isLocal); | ||
|
||
const oldTable = `${stage}-coreSets`; | ||
const newTable = `${stage}-coreSet`; | ||
console.log(`Processing table ${oldTable}`); | ||
for await (let entry of scan(dbClient, oldTable)) { | ||
add(dbClient, newTable, entry); | ||
} | ||
}; | ||
|
||
async function* scan(client: DynamoDBDocumentClient, table: string) { | ||
const query = { | ||
TableName: table, | ||
}; | ||
for await (const result of paginateScan({ client }, query)) { | ||
yield* result.Items ?? []; | ||
} | ||
} | ||
|
||
async function add(client: DynamoDBDocumentClient, table: string, entry: any) { | ||
const newCompoundKey = `${entry.state}${entry.year}`; | ||
const params = { | ||
TableName: table, | ||
Item: { | ||
...entry, | ||
compoundKey: newCompoundKey, | ||
}, | ||
}; | ||
await client.send(new PutCommand(params)); | ||
} | ||
|
||
function buildClient(isLocal: boolean) { | ||
if (isLocal) { | ||
return DynamoDBDocumentClient.from( | ||
new DynamoDBClient({ | ||
region: "localhost", | ||
endpoint: "http://localhost:8000", | ||
credentials: { | ||
accessKeyId: "LOCALFAKEKEY", // pragma: allowlist secret | ||
secretAccessKey: "LOCALFAKESECRET", // pragma: allowlist secret | ||
}, | ||
}) | ||
); | ||
} else { | ||
return DynamoDBDocumentClient.from( | ||
new DynamoDBClient({ | ||
region: "us-east-1", | ||
logger: { | ||
debug: () => { | ||
/* Dynamo's debug logs are extremely noisy */ | ||
}, | ||
info: console.info, | ||
warn: console.warn, | ||
error: console.error, | ||
}, | ||
}) | ||
); | ||
} | ||
} | ||
|
||
transformMeasureTable(); |
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