-
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-3960: pushing new table to deploy to dev
- Loading branch information
1 parent
093d889
commit 8740adb
Showing
2 changed files
with
83 additions
and
1 deletion.
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,62 @@ | ||
import { DynamoDBClient, paginateScan } from "@aws-sdk/client-dynamodb"; | ||
import { DynamoDBDocumentClient, PutCommand } from "@aws-sdk/lib-dynamodb"; | ||
|
||
const transformMeasureTable = async () => { | ||
const dbClient = buildClient(!!process.env.DYNAMODB_URL); | ||
const tableName = "local-measures"; | ||
const newTableName = "local-cs-measures"; | ||
console.log(`Processing table ${tableName}`); | ||
for await (let entry of scan(dbClient, tableName)) { | ||
add(dbClient, newTableName, 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}${entry.coreSet}`; | ||
const params = { | ||
TableName: table, | ||
Item: { | ||
compoundKey: newCompoundKey, | ||
coreSet: entry.coreSet, | ||
}, | ||
}; | ||
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, | ||
}, | ||
}) | ||
); | ||
} | ||
} |
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