ostafen / clover

A lightweight document-oriented NoSQL database written in pure Golang.
MIT License
701 stars 55 forks source link

Support struct in Save() method #116

Closed joyhope closed 1 year ago

joyhope commented 1 year ago

As I understand the clover gives a way, to add any struct object to the collection, through NewDocumentOf. It converts the struct to a map struct. It is very easy to use.

But I am unable to use this object to make updates and deletes. If I want to do this, I had to let the collection find the object, and then use the document.Set(..) to make an update.

I think there is a way to add _id to the structure or require the struct with "_id', so the structure is clover supported structure. And then find and update is easy to process.

ostafen commented 1 year ago

What is exactly your question?

joyhope commented 1 year ago
  1. whether my understanding is correct. The structure is unable to make direct update by db, must use querey to find the document for the structure object, and then update and delete is possible.
  2. Whether user add _id to such the customer structure will solved the problem. The clover db api could adjusted to the customer structure object is easy to use.
ostafen commented 1 year ago

Yes, your understanding is correct. I think the Save() method could be updated to take an any object instead of a document to achieve what you suggest, but currently, you have to do as you said. Let me know if you would like to work on that

joyhope commented 1 year ago

I really like to have such a feature. It could be used easily. The user just defines Clover-supported structure, and such structure obj could be directly used by the DB.

Otherwise, the usage is a little awkward.

  1. Update memory structure object.
  2. Map the object to the document.
  3. Update the document
  4. Db save the document object. At least, the structure object and documents are the same meaning, but with different forms.
ostafen commented 1 year ago

You can work on it, If you like. Unfortunately, I don't have the time to work on this, currently

letterzp commented 1 year ago

@joyhope As I understand it, are you hope to have an ORM?

ostafen commented 1 year ago

As per my understanding, the Save() method should take an x parameter of any type instead of a Document object. Then, if x is already a Document, it just uses that one, otherwise we call NewDocumentOf() and attempt to create a new document from the passed object