You are looking at historical revision 22263 of this page. It may differ significantly from its current revision.

couchdb

Introduction

Apache CouchDB is a document-oriented database that can be queried and indexed in a MapReduce fashion using JavaScript. CouchDB also offers incremental replication with bi-directional conflict detection and resolution.

The couchdb egg provides a scheme interface to couchdb's RESTful JSON API.

If you are looking for a method of writing couchdb views, i.e server side code in scheme see the couchdb-view-server egg.

Requirements

http-client intarweb json uri-common defstruct

Documentation

As said above a CouchDB server will use the JSON format for encoding responses. The json egg manages the marshalling and unmarshalling. See there for the correct type conversions. As a convenience function json-ref is provided by this egg.

Querys to the database are encoded in URIs. Depending on the setup of the CouchDB server username and password may have to be encoded in the URI. This applys to database creation / deletion mostly.

Error handling

All procedures return #f on error. The last error is provided with the utility procedure last-error.

CouchDB connection procedures

For identifying connections a connection record has to be created first.

make-connection
[procedure] (make-connection DATABASE-STRING URI-STRING)

Creates a connection record for the desired DATABASE-STRING on the CouchDB located at the specified URI string.

Connection accessors / setters
[procedure] (connection-uri connection)
[procedure] (connection-database connection)

Accessors for the CouchDB egg connection record.

[procedure] (update-connection connection #!keyword database connection)

Update function for the connection record.

get-server-info
[procedure] (get-server-info connection)

Returns the couchdb version information as the json egg parses the response.

CSI> (get-server-info (make-connection "" "http://localhost:5984"))
#(("couchdb" . "Welcome") ("version" . "0.11.0"))

Database handling

[procedure] (create-database connection)

Creates a database as specified in the connection record connection. Use (make-connection database-string couchdb-uri-string) to create a connection object.

Returns #t or #f. Verbose error message can be retrieved with ''last-error'.

[procedure] (delete-database connection)

Deletes the database as specified in the connection record connection. Use (make-connection database-string couchdb-uri-string) to create a connection object.

Returns #t or #f. Verbose error message can be retrieved with ''last-error'.

[procedure] (get-database-info connection)

Returns the database information for a given connection record. Use (make-connection database-string couchdb-uri-string) to create a connection record.

Document handling

The central entity in a CouchDB is a so called document. A CouchDB document is an object that consists of named fields. Field values may be strings, numbers, dates, or even ordered lists and associative maps, all encoded in JSON.

However there are at least two mandatory fields in each document:

id
unique id within the database
rev
revision of that document
Document datatype
[procedure] (make-document id revision #!optional (body '#()))

The couchdb uses a record to represent the contents of a document. The mandatory elements have distinct slots whereas the rest of the fiels is accessible in a vector called body.

[procedure] (document? doc)

Type checking predicate for a document record.

[procedure] (document-id doc)
[procedure] (document-rev doc)
[procedure] (document-body doc)
[procedure] (update-document doc #!keyword id rev body)

Accessors for the id rev and body slots of a document record.

[procedure] (document-attribute? document name)

Predicate indicating wether document does contain a attribute name.

[procedure] (document-attribute document name)

Returns the value for name if it is contained in document.

Document retrieval / storage
[procedure] (get-document connection id)

Requests the document with id id and returns that or #f.

[procedure] (save-document connection document)

Stores the document in the database and returns it with updated id and revision attributes. If the document does not exist in the database it is created.

[procedure] (delete-document connection document)

Deletes the document form the database.

[procedure] (get-all-documents connection #!optional (query '()))

Returns all documents in the database. The output can be quite large! You can pass any valid query URI part for the optional query parameter.

View Handling

[procedure] (get-view connection view #!optional (query '()))

Returns the results of the requested view. View can be either a string or a list of strings which in the latter case is treated as a full path to the view. The query parameter can include search keys.

[procedure] (send-temp-view-request connection view method #!key query)

Utility functions

[procedure] (json-ref name object)

Retrieves a field name from a JSON object object. This is a convenience function to ease the access of the vector / hash-table mix the json egg generates.

[parameter] (last-error #f)

Will be set on errors. Returns a descriptive error string.

Author

Moritz Heidkamp

License

Apache CouchDB client library

Copyright (C) 2009 Moritz Heidkamp

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You can find a copy of the GNU Lesser General Public License at
http://www.gnu.org/licenses/

Version History

0.1
initial release