staticbackendhq / core

Backend server API handling user mgmt, database, storage and real-time component
https://staticbackend.com
MIT License
682 stars 67 forks source link

Add ability to update and delete many entries by criteria #27

Closed dstpierre closed 1 year ago

dstpierre commented 2 years ago

This will require 2 new database endpoints.

This should take the same query logic as the query function in db.go:167 and use the UpdateMany and DeleteMany functions respectively to update or delete more than one entries based on a filter criteria.

szymonnogiec commented 1 year ago

Hey, I want to work on this issue;

dstpierre commented 1 year ago

hey @szymonnogiec great. I just read the description, this issue was created prior to having the Persister interface, so let me give an updated guide line here:

Starting from the database sub-packages, you'd have 3 implementation to do, the postgresql, mongo, and memory.

I'd start by implementing the memory one by creating a test and your implementation.

You may test only the memory implementation with make test-mem at the project root.

I'd create two new functions in the base.go file UpdateDocuments and DeleteDocuments matching their non pluralized counter part.

Look at how the QueryDocuments function uses criteria to be a bit inspired, you may look at the TestQueryDocuments function in memory/base_test.go to start writing the TestUpdateDocuments and TestDeleteDocuments tests.

Once you have a working implementation, you might want to add those two function in the internal/persister.go Persister interface and go write the implementation and tests for both postgresql and mongo packages.

The last step will be to modify the update and delete endpoints of the API to handle receiving criteria.

You'll noticed in the db.go:26 the following:

} else if r.Method == http.MethodPut {
  database.update(w, r)
} else if r.Method == http.MethodDelete {
  database.del(w, r)
}

For the update, one simple idea to handle the criteria would be to have a query string parameter in the URL and if present call a new handler that will be able to accept a more complex data object for instance:

{
  "criteria": [],
  "update": {}
}

Where the criteria is the [][]interface{} as seen in the query function of db.go:152. The code that parses the criteria / filters should be extracted from there since it will be re-used by the query function and the two new function that you'll be creating.

Those are just suggestions, feel free to explore and use different ways etc.

Let me know if you have any questions.

rostikts commented 1 year ago

Hello! @dstpierre @szymonnogiec don't you mind If I'll take a look as well?

dstpierre commented 1 year ago

Hey @rostikts it's awesome you want to work on this. @szymonnogiec started a couple of weeks ago, maybe you could reach them and see if there's way to collaborate on this. The issue is kind of big so I suppose it could be broken down into smaller tasks.

If you're not on Discord - join link yet, I'd suggest you join us there

Let me know how I can help with anything.

dstpierre commented 1 year ago

Hey @rostikts - @szymonnogiec replied in Discord and this issue could be split into two. One of you take the update part and the other the deletion part.

dstpierre commented 1 year ago

@szymonnogiec hey how things are going? Can I help with anything?

For the delete part, you may take a look at @rostikts changes for the update and reuse a similar approach / reuse the filtering codee.

Let us know if you have any question, I'd be happy to help you get started and/or unblock for this :wave:

dstpierre commented 1 year ago

Hey @szymonnogiec last time you confirmed via Discord that you'd still want to tackle this one. Are you still interested? I'm planning a new release and I'd like that one to be part of the next version.

Let me know where things are please, thanks.

dstpierre commented 1 year ago

implemented the remaining bulk delete in #79 completing this bulk update/delete feature.