View/LiveQuery for getting document Creation, Updates, and Deletes?


#1

I’m trying to get a view/livequery created that will allow me to capture documents of a certain type being created, updated and deleted. I’m wondering if I need to do something special for the deletes part, here’s what I’ve tried (which seems to work fine for creation/update)

public void CreateView(string viewName, string objectType, EventHandler<QueryChangeEventArgs> changed)
{
    if (manager == null)
        InitCouchBase();
    var view = database.GetView(viewName);
    MapDelegate map = (doc, emit) => 
    {
        object key;
        var hasType = doc.TryGetValue("type", out key);
        if (!hasType)
        {
            object deletvalue;
            var hasdelete = doc.TryGetValue("_deleted", out deletvalue);
            if (hasdelete && deletvalue.Equals(true))
            {
                emit(doc["_id"], null);
            }
        }
        if (hasType && key.Equals(objectType))
        {
            emit(doc["_id"], null);
        }
    };

    view.SetMap(map, "2.0");
    var query = view.CreateQuery().ToLiveQuery();
    query.Changed += changed;
    query.Start();
}

#2

I’m not sure if it’s necessary to check for the _deleted flag, I found an example that seemed like it was supposed to be handling deletes that didn’t include that (but possibly it wasn’t handling them?). The reason I added that section was I was noticing that deleted documents on the couchbase server didn’t have the type field anymore so I figured possibly that might be causing me problems?