Document store schema and methods #180
Replies: 3 comments
-
NOTE: Instead of 3 tables, I could have done this with only 2 tables by storing the document id directly in the |
Beta Was this translation helpful? Give feedback.
-
I was just trying this out to see how hard it would be and I'm going to stop here but I'll leave my notes. Using a proxy watcher such as on-change (or you could use fast-json-patch .observe()) I started working on a function to provide a watchable document that would put changes back into the store and that works fine. Lookup up However, I am not sure that I love the idea of using either of those libraries mentioned above because I'm not sure how airtight they are at capturing changes based on some issues I'm seeing on their repos. If I really want this functionality in the future I might try to use mutative / enablePatches method to watch the document and get changes into TinyBase. To make a function to get the full property-path of a deeply nested function getDocumentValuePath(
valId: string,
row: Row<DocStoreTables, "doc_vals", true>,
) {
const linked = related.getLinkedRowIds("docValsParent", valId);
const parts: string[] = [];
for (let i = 0; i < linked.length; i++) {
const id = linked[i];
if (id === valId) {
parts.push(row.k!);
} else {
parts.push(store.getCell("doc_vals", id!, "k")!);
}
}
return parts.reverse().join(".");
} One more useful note I added on the table schemas used here before I put this away: // TODO: Go back to 2 tables, but instead of putting docId in `doc_vals` just
// remove `type` from `docs` and move `doc_flds.v` to `docs` pointing to root
// object|array value... (then get rid of the doc_flds table) |
Beta Was this translation helpful? Give feedback.
-
I did a little experiment today to see if I could load up whole JSON documents into TinyBase and here's what I came up with.
It's not a big leap from this code to be able to make a JS Proxy object to listen and write changes to the correct TinyBase table using something like valtio, mutative, etc...
TLDR; I'm loading up the following original document/object into TinyBase (for editing) and pulling it back out, by mapping the whole object to tables (the code should also work with array documents)...
doc_store.ts
try_doc_store.ts
Running
npm run build && node ./dist/try_doc_store.js
outputs the following:and the final result:
Beta Was this translation helpful? Give feedback.
All reactions