Using with CouchDB
Starting with SproutCore 0.9.18 you’ll be able to talk directly with a CouchDB server.
This is great for being able to focus on your front end, and not worry how your data is being stored. Then if you’re brave, you can keep using it in production.
Getting started is pretty simple, there are just a couple of steps.
NOTE installing this will override the current server implementations. The new versions expect different response from the server. If you just want to use couchDB, then don’t worry about it, otherwise, read up, and enjoy the goodness that is the updated server code.
Installation is easy, From your SproutCore directory:
$ sc-install geoffreyd-couchdb
Edit sc-config:
c[:required] = [:sproutcore, :'geoffreyd-couchdb']
Then restart sc-server.
To get it to work, once you have it installed:
1) edit sc-config, and add this as the last line of the fine:
proxy '/data', :to => 'localhost:5984', :url => "/database-name"
‘/data’ can be whatever you want, this is just the path that SC will
look for your data.
2) Setup the server for your application ie. in core.js:
Contacts = SC.Object.create({ server: SC.CouchdbServer.create({ prefix: ['Contacts'] }), // This is the important part FIXTURES: [] }) ;
3) Set the dataSource for your model, and set a ‘type’ property. ie.:
Contacts.Person = SC.Record.extend( /** @scope Contacts.Person.prototype */ { dataSource: Contacts.server, // This is the same as you set in the proxy call (note there is not slash at the start) resourceURL: "data", // Make sure that you have a 'type' property, this is how we will separate // your different types of data, and pull them out again. properties: ['type', 'firstName', 'lastName', 'created', 'modified'], }) ;
4) if you have a view on your couchDB (if you have more than just test data,
You really should have views), then call your view with:
Contacts.server.listFor(Contacts.Person, {view: "_view/people/by_lastname"}) ;
5) code like you normally would. This is mostly all there is that is
different to using SC.Server or SC.RestServer.
Of course, this is not finished so there are things that won’t work,
like offset, limit and conditions.
Also it is only using couchdb’s temp_views at the moment, the next
version will have support for predefined views.
Disclaimer: this is a work in progress, and does not yet support all
couchDB functions.
