A from-scratch fork of simply_couch's model layer that runs on Couchbase natively: `include SimplyBased::Model` instead of `include SimplyCouch::Model`. - SimplyBased::Store replaces the CouchDB layer: KV documents, N1QL/SQL++ finders + lazily-created GSIs, CAS mapped onto _rev. No design documents or JavaScript map/reduce — the hand-written association views become N1QL (belongs_to/has_many/has_one scalar FK; habtm via ANY ... SATISFIES + array index, or KV multi-get on the storing side). - Redis replacements on the same cluster: Counter (atomic increment/decrement), Cache (ActiveSupport::Cache::Store over KV+TTL, drop-in Rails.cache/ActiveJob), Lock (insert-if-absent + TTL + CAS-checked release). - Runs the simply_couch test suite against live Couchbase: 378/432 applicable examples green (CouchDB-mechanic specs tagged :couchdb_only and excluded). - Remaining: ancestry (tree) + embedded_in reverse-lookups (recursive/embedded N1QL) and a few pagination/lifecycle edges. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
54 lines
1.3 KiB
Ruby
54 lines
1.3 KiB
Ruby
module SimplyBased
|
|
module Model
|
|
module View
|
|
# A view to return model instances with only some properties populated by searching its properties, e.g. for very large documents where you are only interested in some of their data
|
|
#
|
|
# example:
|
|
# view :my_view, :key => :name, :properties => [:name, :author], :type => :properties
|
|
class PropertiesViewSpec < ModelViewSpec
|
|
def map_function
|
|
map_body do
|
|
"emit(#{formatted_key}, #{properties_for_map(properties)});"
|
|
end
|
|
end
|
|
|
|
def reduce_function
|
|
<<-JS
|
|
function(key, values, rereduce) {
|
|
if(rereduce) {
|
|
return sum(values);
|
|
} else {
|
|
return values.length;
|
|
}
|
|
}
|
|
JS
|
|
end
|
|
|
|
def process_results(results)
|
|
results['rows'].map do |row|
|
|
klass.json_create row['value'].merge(:_id => row['id'])
|
|
end
|
|
end
|
|
|
|
def view_parameters
|
|
{:include_docs => false}.merge(super)
|
|
end
|
|
|
|
def language
|
|
:javascript
|
|
end
|
|
|
|
private
|
|
|
|
def properties
|
|
options[:properties]
|
|
end
|
|
|
|
def properties_for_map(properties)
|
|
'{' + properties.map{|p| "#{p}: doc.#{p}"}.join(', ') + '}'
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|