xavierlacot / joli.js

joli.js is an Activerecord-like javascript ORM, particularly suited for being used in the Appcelerator Titanium Mobile framework.
MIT License
267 stars 60 forks source link

asDynamic() method for query results that can't be mapped to a model #21

Closed nicjansma closed 12 years ago

nicjansma commented 12 years ago

Some query results can't (and shouldn't) be mapped back to one of the models. For example, when using a GROUP BY statement, if the SELECT has a COUNT(*) AS count result to get the grouping count:

In SQL:

SELECT city, COUNT(*) as count
FROM human
GROUP BY city

In Joli, this doesn't work correctly:

var q = new joli.query()
    .select('city, COUNT(*) as count')
    .from('human')
    .groupBy('city')
    .execute();

Because the result is attempted to map to 'human', which the results are not.

I've added a .asDynamic() method that you would add before .execute(). If this is set, the results are retuned as a simple property bag instead of as a model.

The documentation was updated as well.

divadrei commented 12 years ago

Try : var q = new joli.query() .select('city, COUNT(*) as count') .from('human') .groupBy('city') .execute('array');

nicjansma commented 12 years ago

Ah, didn't notice that optional parameter. I'll close this and open a new pull request for a small documentation update.