vwkd / denokv-graphql

GraphQL bindings for Deno KV
https://deno.land/x/denokv_graphql
MIT License
11 stars 0 forks source link

Insert across tables #5

Closed vwkd closed 1 year ago

vwkd commented 1 year ago

Insert across tables in one atomically consistent set. Receives deep object as input, walks from deepest leaves to root, inserts each object into corresponding table.

But how should report the ids and versionstamps of the nested rows? Array of Result with additional table name entry?

Deep insert is complex to implement, needs to pick apart deep input object, generate the ids and match them up, just to stitch it back together at query time later. It largely defeats the purpose of relational data storage, since can't link new data to existing data, must always insert together, could almost just store the whole data as JSON if not for ability to query only part of it. Also cumbersome for user to define duplicate type hierarchy for input types and output types.

Better to let user build the relations. Just somehow ensure consistency... Would need to give up generating ids for user, instead let the user generate random UUIDs in advance, allow to insert multiple at same time, throw if any id already exists.

Could accept multiple rows in a mutation, and multiple mutations in a request. The mutations must all be committed in one atomic transaction. Order doesn't matter since there are no foreign key constraints in Deno KV, and atomicity makes either all succeed or all fail. Inserting valid references is up to the user?

mutation {
  createBooks(data: [ { id: "xxx", ..., author: "bbb" }, { id: "yyy", ..., author: "aaa"} ]) Result @insert(table: "Book")
  createAuthors(data: [ { id: "aaa", ... }, { id: "bbb", ... } ]) Result @insert(table: "Author")
}
vwkd commented 1 year ago

It's now possible to insert consistently in one atomic transaction with https://github.com/vwkd/graphql-denokv/commit/bb3eccd91d8f89a577b048c88210425976b21c77

We leave it to the user to separate nested data into the tables to not take away control and also keep complexity down.