Created
December 12, 2024 15:14
-
-
Save tywalch/085be2766ae22ad2644ffb3768df45cc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { Entity, ElectroError } from 'electrodb'; | |
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; | |
const table = 'electro'; | |
const client = new DynamoDBClient({ | |
region: "us-east-1", | |
endpoint: 'http://localhost:8000', | |
credentials: { | |
accessKeyId: 'fake', | |
secretAccessKey: 'fake', | |
} | |
}); | |
function isConditionalRequestFailedError(err: unknown): boolean { | |
return err instanceof ElectroError && | |
err.cause?.name === 'ConditionalCheckFailedException'; | |
} | |
const Thing = new Entity( | |
{ | |
model: { | |
entity: "thing", | |
version: "1", | |
service: "store" | |
}, | |
attributes: { | |
id: { | |
type: "string", | |
}, | |
}, | |
indexes: { | |
id: { | |
pk: { | |
field: "pk", | |
composite: ["id"] | |
}, | |
sk: { | |
field: "sk", | |
composite: [], | |
} | |
}, | |
} | |
}, | |
{ table, client } | |
); | |
(async function main() { | |
try { | |
await Thing.remove({ id: "123" }).go(); | |
} catch(err: unknown) { | |
if (isConditionalRequestFailedError(err)) { | |
console.log('task already exists'); | |
} else { | |
console.log('something went wrong', err); | |
} | |
} | |
})().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment