tywalch / electrodb

A DynamoDB library to ease the use of modeling complex hierarchical relationships and implementing a Single Table Design while keeping your query code readable.
MIT License
1.03k stars 67 forks source link

Feature Request: BatchWrite across entities #312

Open yamatatsu opened 1 year ago

yamatatsu commented 1 year ago

I know ElectroDB have a feature transaction.write(). But when using put() and delete(), we want to use BatchWriteItem because it is less cost and higher performance.

The interface will be like following:

// create a user and attach to a tenant
await service.batchWrite
  .user.put({ userId: "", registrationCode: "" })
  .userTenantMap.put({ userId: "", tenantId: "" })
  .registrationCode.delete({ registrationCode: "" })
  .go()

or

// create a user and attach to a tenant
await service.batchWrite([
  service.entities.user.put({ userId: "", registrationCode: "" }),
  service.entities.userTenantMap.put({ userId: "", tenantId: "" }),
  service.entities.registrationCode.delete({ registrationCode: "" }),
]).go()

Actually, BatchWriteItem can effect to multiple table, but if following ElectroDB service concept, it is enough to provide the feature BatchWriteItem only for entities closed in service I think.

yamatatsu commented 1 year ago

The interface of transaction.write() is so cool! BatchWriteItem feature can follow this interface.

https://electrodb.dev/en/mutations/transact-write/#performing-write-transactions

await yourService.batch
  .write(({ entity1, entity2 }) => [
    entity1
      .put({ prop1: "value1", prop2: "value2" })
      .commit(),

    entity2
      .delete({ prop1: "value1", prop2: "value2" })
      .commit(),
  ])
  .go();
tywalch commented 1 year ago

Hey @yamatatsu 👋

Thanks for putting this together! It might be a little while before I can address this, but it makes sense and would be a great addition 👍