jgaskins / perpetuity

Persistence gem for Ruby objects using the Data Mapper pattern
http://jgaskins.org/blog/2012/04/20/data-mapper-vs-active-record/
MIT License
250 stars 14 forks source link

Enumerable predicates on query attributes #32

Closed jgaskins closed 10 years ago

jgaskins commented 10 years ago

One requested feature was adding Enumerable predicates to query attributes.

We could add integration specs to test for enumerable predicates on query attributes to make sure they're implemented in the database adapters (if possible).

I'll add separate issues in the Postgres and MongoDB adapter repos for DB-specific discussion, as well.

jcostello commented 10 years ago

In the case of any? or none? for mongodb, we should delegate them to ruby instead of the DB. Better this way that without those methods. It make sense?

jgaskins commented 10 years ago

The problem with doing it outside the database is that it we'd have to retrieve everything first, which wastes bandwidth, processor cycles and memory. It'll be much better if we can do them in the DB.

For example, here's what the mapper.select { |user| user.friends.any? } query would look like in MongoDB:

db.User.find({ friends: { $ne: [] } })

For Postgres:

SELECT * FROM "User" WHERE json_array_length(friends) > 0`

Both of these should be easily doable from their respective adapters.

jgaskins commented 10 years ago

Closing. We have specs for any? and none? on query attributes and both are implemented in both the MongoDB and Postgres adapters.