Getting Started

Flatpack is designed to feel similar to working with other ORM layers, but at the same time not hide away the RESTfulness of CouchDB.

Defining a Model

To get started, the first thing you must do is define a model. In this case we will simply let Flatpack know that we want a model of type customer.

var flatpack = require('flatpack');
// define the customer model
    'http://localhost:5984', // the couch url
    'test', // the target database
    'customer', // the model name
    function(err) {
        console.log('defined customer model');

This model is quite possibly too simple to be useful, but it will do for now. We’ll cover Initializing Views soon which will create us something genuinely helpful.

Saving Data

Once a model is defined, its simple to save an object to the db. In flatpack, you don’t need to pass in a special kind of object to save it - just a plain old Javascript object.

var flatpack = require('flatpack');

// include our module definitions

// get a reference to the customer "db"
flatpack.use('customer', function(err, customerdb) {
    var customer = {
            firstName: 'Donald',
            lastName: 'Duck',
            company: 'Walt Disney'

    // now create a new customer, function(err, id) {
        console.log('saved customer, id is: ' + id);

Table Of Contents

Previous topic


Next topic

Initializing Views

This Page