Skip to content

Instantly share code, notes, and snippets.

@tywalch
Created December 12, 2024 15:14
Show Gist options
  • Save tywalch/085be2766ae22ad2644ffb3768df45cc to your computer and use it in GitHub Desktop.
Save tywalch/085be2766ae22ad2644ffb3768df45cc to your computer and use it in GitHub Desktop.
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