Parameterize View Couchbase Lite

Hi,
i’m just starting to use CouchBase Lite for my android application.
The docs said that Don't try to "parameterize" the map function by referring to an external variable doc.
My question is: if i have multiple models for my data, have i to write for every model a getView function that return the same view only with the applied type of my document changed?
My view should be like this;

view.setMap(new Mapper() {
            @Override
            public void map(Map<String, Object> document, Emitter emitter) {
                if(document.get("type").equals(the_type_of_my_model)) {
                    emitter.emit(document.get("_id"), null);
                }
            }
        }, "1");

What i’d like to do is to pass the_duty_type_of_my_model instead of string hardcoding it.
I’m asking because it would be weird that i should write for each model the same function, when using an external variable would be perfect, and i was wondering if there is a common pattern to follow when dealing with more models.

Views are static indexes used for querying. You wouldn’t normally do what you’re showing here (emit the document id) for two reasons: 1) It doesn’t improve your ability to query the data and 2) the document ID is made available for free anyway. (See the QueryRow documentation for details.)

Further, if I’m understanding your question, you don’t want to reference an external model type variable. This would make the choice of emitting contents into the View dependent on the setting of that variable at the time the document is processed. You can’t know when that will happen, so your index won’t be consistent.

Can you say more about what you’re trying to achieve?

As @hod.greeley mentioned, you would want to emit the_type_of_my_model in the map function (and therefore it will end up in the view index), and then query on it by passing the actual type you want to query on.

Check out some of these resources:

Also, if you are building a green field app, you might check out CBLite 2.0 which adds a query language that is functionally similar to SQL.

+1 to @Traun’s suggestion of using CBM 2.0

Thank you for the answers.
I have an application that have for example two models, User and Post. I’d like to implement a getAll() static method that returns all the document with type “user” in case i call User.getAll(), and type “post” on Post.getAll(). I’d like to write a superclass method getAll that changes its behaviour depending of the subclass calling it.

I’ve looked CBLite 2.0, and seems very good.

According to the docs, in the query performance’s section , it says to use a “singleton” pattern for queries.
Do you think is it possibile to implement a getAll() like written above?

Another thing: i didn’t find the api’s section( specially about “Query”) in the docs about CBLite 2.0. Where can i find it?

There is an intro blog that discusses the fundamental queries. There will be some enhancements coming in DB21 but this should give you a very good idea of where to begin. There will be follow on blogs on collections querying and FTS coming shortly.
(The documentation will also be updated in due course…)

Yes. Take a look at the blog and you should be able to figure out your query very quickly - using a where clause of type property.

1 Like

If you use Map/Reduce (CBL 1.x), another approach could be to use custom document IDs. So, for example, each User doc starts with user-, and then you tack on something unique (like a UUID). Then you can use an all-docs query and specify a key range to get the documents by type. This would save you from needing a View at all (again, if using 1.x). See the 1.x Query docs for more.