Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add view option to specify a custom emit value for views #209

Merged
merged 2 commits into from
Aug 20, 2016

Conversation

ellneal
Copy link
Contributor

@ellneal ellneal commented Jun 29, 2016

The current implementation of generated views is to always emit(keys, 1), which allows a simple count to be gathered using the default _sum reduce function. This PR adds the option for the emit value to be specified without having to write the entire map function in javascript.

The example below would create a view that can be queried for total amount taken per client per day.

class Invoice < CouchRest::Model::Base
  property :client_id
  property :date
  property :total_amount

  design do
    view :by_client_id_and_date, emit: :total_amount
  end
end

Generated javascript:

function(doc) {
  if ((doc['type'] == 'Invoice') && (doc['client_id'] != null) && (doc['date'] != null)) {
    emit([doc['client_id'], doc['date']], doc['total_amount']);
  }
}

@samlown samlown merged commit a955c99 into couchrest:master Aug 20, 2016
@samlown
Copy link
Member

samlown commented Aug 20, 2016

Very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants