Replies: 1 comment
-
You can do something like this: const store = createStore().setTable("items", {
i1: { name: "item1", order: 1 },
i2: { name: "item2", order: 2 },
i3: { name: "item3", order: 3 },
});
// Use a transaction when swapping 2 items, to keep the change atomic...
store.transaction(() => {
store.setCell("items", "i2", "order", 3);
store.setCell("items", "i3", "order", 2);
});
// Get items sorted by the `order` field...
console.log(
"items",
store
.getSortedRowIds("items", "order")
.map((id) => store.getRow("items", id)),
);
// Prints:
items [
{ name: 'item1', order: 1 },
{ name: 'item3', order: 2 },
{ name: 'item2', order: 3 }
] To use a query to get the sorted items, create a query and then call the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
I'm currently evaluating TinyBase for a collaborative application and ran into a problem: many of my shared data items are lists (more precisely: ordered sets) whose contents and ordering may be changed by any collaborator at any time.
But how do I map such lists onto TinyBase values and tables? Since values are atomic, I'll presumably have to use tables. The examples seem to implement dynamic lists as linked lists - do I really have to go that way, too? I would then have to make sure myself, that "concurrent" changes do not destroy the sequence of links
Or is there a better way?
Thanks in advance for any response!
Beta Was this translation helpful? Give feedback.
All reactions