Skip to content

Instantly share code, notes, and snippets.

@tywalch
Created October 19, 2024 18:25
Show Gist options
  • Save tywalch/fa43890fa0c7beb84804811b7f95bf46 to your computer and use it in GitHub Desktop.
Save tywalch/fa43890fa0c7beb84804811b7f95bf46 to your computer and use it in GitHub Desktop.
electrodb-issue-435.ts
import { Entity, Service } from 'electrodb';
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
const client = new DynamoDBClient({
region: "us-east-1",
endpoint: 'http://localhost:8000',
});
const table = 'electro';
const config = {
client,
table,
};
const tasks = new Entity(
{
model: {
entity: "tasks",
version: "1",
service: "taskapp"
},
attributes: {
id: {
type: "string",
},
replayedAt: {
type: 'list',
items: {
type: 'number',
required: true,
},
required: true,
default: [],
// Add a getter for accessing existing items that may not have a `replayedAt` attribute
get: (value: unknown) => {
console.log('replayedAt', {value})
if (Array.isArray(value)) {
return value
}
return []
},
},
isReplay: {
type: 'boolean',
required: true,
watch: ['replayedAt'],
get: (_, item: any) => {
const { replayedAt } = item;
console.log('isReplay', { replayedAt, item });
// Note: the sibling attribute values passed into the `get` method
// do not invoke their own get methods, so we must check
// if the attribute is an array before we can check it has a length > 0.
// The below will throw an error of "Cannot read properties of undefined (reading 'length')"
return replayedAt.length > 0
},
set: () => undefined,
},
},
indexes: {
id: {
pk: {
field: "pk",
composite: ["id"]
},
sk: {
field: 'sk',
composite: [],
}
},
}
},
config,
);
async function main() {
const id = '123';
await tasks.put({id, isReplay: false}).go();
const item = await tasks.get({id}).go();
console.log(item);
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment