creating a view that returns non-deleted documents

39 views
Skip to first unread message

Niels Egberts

unread,
Apr 22, 2010, 3:44:24 PM4/22/10
to Desktop CouchDB
I'm creating an application to manage projects. The idea is that you
can add todo-lists, files, notes, etc. I found out that deleted items
were not really deleted, but only marked as such. So I want to create
a view that returns only non-deleted objects.

What I tried is this:
function(doc) {
if (doc.application_annotations["Ubuntu One"]
["private_application_annotations"]["deleted"]) {
emit(null, doc);
}
}
This returns the deleted items as expected.

Then I tried this:
function(doc) {
if (!doc.application_annotations["Ubuntu One"]
["private_application_annotations"]["deleted"]) {
emit(null, doc);
}
}
But this returns nothing... why?

Then I tried to be smart and tried this:
function(doc) {
if (doc.application_annotations["Ubuntu One"]
["private_application_annotations"]["deleted"]) {
} else {
emit(null, doc);
}
}
But this does not return anything too. Can anyone explain why?

Niels.

--
You received this message because you are subscribed to the Google Groups "Desktop CouchDB" group.
To post to this group, send email to desktop...@googlegroups.com.
To unsubscribe from this group, send email to desktop-couch...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/desktop-couchdb?hl=en.

Eric Casteleijn

unread,
Apr 22, 2010, 4:20:22 PM4/22/10
to desktop...@googlegroups.com
On 04/22/2010 03:44 PM, Niels Egberts wrote:

> I'm creating an application to manage projects. The idea is that you
> can add todo-lists, files, notes, etc.

Hi Niels, that sounds awesome! Is the project hosted on launchpad or
somewhere else, or is it too early to ask that question yet? :)

> I found out that deleted items
> were not really deleted, but only marked as such. So I want to create
> a view that returns only non-deleted objects.
>
> What I tried is this:
> function(doc) {
> if (doc.application_annotations["Ubuntu One"]
> ["private_application_annotations"]["deleted"]) {
> emit(null, doc);
> }
> }
> This returns the deleted items as expected.
>
> Then I tried this:
> function(doc) {
> if (!doc.application_annotations["Ubuntu One"]
> ["private_application_annotations"]["deleted"]) {
> emit(null, doc);
> }
> }
> But this returns nothing... why?
>
> Then I tried to be smart and tried this:
> function(doc) {
> if (doc.application_annotations["Ubuntu One"]
> ["private_application_annotations"]["deleted"]) {
> } else {
> emit(null, doc);
> }
> }
> But this does not return anything too. Can anyone explain why?

You're almost there, but the problem is that your code checks the
value of the deleted field, and considers the document "not deleted"
when that value is false. This is correct but incomplete, since most
documents that are not deleted will not have this field at all.

In desktopcouch itself, this is handled as follows:

function(doc) {
try {
if (! doc['application_annotations']['Ubuntu One']

['private_application_annotations']['deleted']) {
emit(doc.record_type, doc);
}
} catch (e) {
emit(doc.record_type, doc);
}
}

getting the value of a property that does not exist will throw an
error, at which point the couchdb view emits nothing for that
record. In this code, we catch that error, and say: if the field is
not even there, that must mean the record is not deleted.

Hope that helps,

--
eric casteleijn
https://code.launchpad.net/~thisfred
Canonical Ltd.

niels....@gmail.com

unread,
Apr 22, 2010, 4:42:50 PM4/22/10
to desktop...@googlegroups.com
On Thu, Apr 22, 2010 at 10:20 PM, Eric Casteleijn
<eric.ca...@canonical.com> wrote:
> Hi Niels, that sounds awesome! Is the project hosted on launchpad or
> somewhere else, or is it too early to ask that question yet? :)

When it get's in some sort of usable state I'll publish it on
launchpad. At the moment its just a list where you can add (but not
remove) items (soon to be projects).

> getting the value of a property that does not exist will throw an
> error, at which point the couchdb view emits nothing for that
> record. In this code, we catch that error, and say: if the field is
> not even there, that must mean the record is not deleted.
>
> Hope that helps,

That helped a lot! Thank you!

Niels.

Duane Hinnen

unread,
Apr 22, 2010, 6:36:48 PM4/22/10
to desktop...@googlegroups.com
On Thu, Apr 22, 2010 at 2:44 PM, Niels Egberts <niels....@gmail.com> wrote:
I'm creating an application to manage projects. The idea is that you
can add todo-lists, files, notes, etc. I found out that deleted items
were not really deleted, but only marked as such. So I want to create
a view that returns only non-deleted objects.


niels,
Sounds like a great project! If you want I have started a page on the Ubuntu One wiki where people can put there desktopcouch, couchdb, Ubuntu One projects. I am hoping it will serve as a repository for projects where people can find one another and share code and ideas. Take a look and see what you think. I would love it if you would add your project. Good luck I can not wait to see how your application progresses.


https://wiki.ubuntu.com/UbuntuOne/ThirdPartyProjects

--
Duane Hinnen
duane...@ubuntu.com
sip:duane...@ekiga.net
Reply all
Reply to author
Forward
0 new messages