relevance / diametric

Diametric is a library for building schemas, queries, and transactions for Datomic from Ruby objects.
MIT License
169 stars 28 forks source link

save should be transitive #49

Closed bokmann closed 10 years ago

bokmann commented 10 years ago

Right now if I do something like:

t = Terrarium.new
t.turtles << Turtle.new(name: "Speedy")
t.save

the turtle is not also saved with the terrarium... I have to create and save the turtle first.

It would be much better if saves were transitive to all new objects, and they were all part of the same datomic transaction.

yokolet commented 10 years ago

Thanks for reporting.

Related issue: https://github.com/relevance/diametric/issues/37

bokmann commented 10 years ago

I have a solution to this, but at the moment, it only works on trees. If the objects being persisted form a graph, the ID resolution after the save stack overflows. The test with Book/Author catches it.

In my app, this isn't a big deal, but I don't want to submit this as a pullup until I have a solution that marks as visited.

bokmann commented 10 years ago

In fact, the above code snippet has two issues... I have a solution for trnsitive saves, but the above code example also relies on the set's '<<' method setting the parent as dirty. That appears to be a deeper problem I'll open an issue for as soon as I understand it.

yokolet commented 10 years ago

I misunderstood this a bit, but the problem was fixed in the master.

Now, diametric's reference type behaves consistent to:

http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_one http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/has_many

Although some methods are missing, we can use Diametric's ref types like ActiveRecord.

However, improvements are welcome. If you have better code, give us a pull request.