Skip to content

For update/delete operators, reference items in the collection by their id (instead of an object ref) #54

Closed

Description

@KyleAMathews

This is necessary so it's easy to update or delete those objects e.g.

const { data: todos } = useLiveQuery(q => q.from({ todos: todosCollection })

// later
updateTodo(() => {
  todosCollection.update(todos[0], (draft) => draft.completed = !draft.completed)
})

Internally we'd need some way of traversing the chain of queries back to the object the collection doing the mutation knows.

Activity

  1. added this to the v0.1.0 Release milestone on May 8, 2025
  2. KyleAMathews commented on May 8, 2025

    @KyleAMathews
    CollaboratorAuthor

    Ideally the collection should just follow the reference itself directly. So maybe instead the ref path is _ref_${collection.id} so if the collection can't find an object initially, it then looks for the _ref to follow.

    Having to manually specify the ref is fairly painful — especially if you're passing in an array of items to update as you'd need to map over them to get the ref, etc. So automatic is much nicer.

  3. KyleAMathews commented on May 8, 2025

    @KyleAMathews
    CollaboratorAuthor

    Good example of terrible things you have to do right now haha 50d38b6#diff-1809398801c87d5d881c6dbcec6a38033e6097410778035ad0378e0b21b07ca4R281

  4. changed the title [-]For query results, add references to the original collection object[/-] [+]when mutating query results, automatically resolve the original collection's version of the object[/+] on May 12, 2025
  5. samwillis commented on May 23, 2025

    @samwillis
    Collaborator

    Copying my comment from #106:

    I the best solution to this is to add the getId: (item) => item.id config option to all collections that's proposed in #79. Although I would advocate for calling it getKey or keyBy, that latter is an option in the query builder and has the same effect.

    This lets the user decide on a collect-by-collection basis what their key is, this can then easily be accessible all the way through the query pipeline (with any joins, aggregates) without having to add any magic.

  6. samwillis commented on May 23, 2025

    @samwillis
    Collaborator

    Although in the context of the electric collection, would we want to make it required? There is the foot gun of not setting it and not knowing the Electric constructs a key using the table name and PK - maybe the electric collection should automatically use the PK properly rather than the electric provided key that is very opaque?

  7. KyleAMathews commented on May 23, 2025

    @KyleAMathews
    CollaboratorAuthor

    Hmm yeah so then it could be always id based e.g. collection.update(queryItem.id, draft => draft.foo = true))

    That's explicit and pretty intuitive.

  8. samwillis commented on May 23, 2025

    @samwillis
    Collaborator

    Exactly, and users are very used to the concept of keys / ids - there is nothing new here.

  9. KyleAMathews commented on May 23, 2025

    @KyleAMathews
    CollaboratorAuthor

    The electric key is weird as it has to be universal. So requiring people to come up with their own is generally a more solvable issue. It gets tricky when the source table doesn't have an id. We'd also need to retain as the actual key the one provided by the server (or perhaps just ensure the users function doesn't provide duplicate IDs).

  10. changed the title [-]when mutating query results, automatically resolve the original collection's version of the object[/-] [+]For update/delete operators, reference items in the collection by their id (instead of an object ref)[/+] on May 28, 2025
  11. KyleAMathews commented on May 28, 2025

    @KyleAMathews
    CollaboratorAuthor

    Renamed the issue for the object id approach & assigned it to myself. I have a prototype going.

  12. removed this from the v0.1.0 Release milestone on Jun 2, 2025
  13. moved this to In Progress in 0.1.0 releaseon Jun 2, 2025
  14. moved this from In Progress to Done in 0.1.0 releaseon Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions