# flatpack

> Simple mapping library for CouchDB

Latest version **0.1.2** (published 2012-02-02) · 0 weekly downloads

## Install

```sh
npm install flatpack
pnpm add flatpack
yarn add flatpack
bun add flatpack
```

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2012-02-02 |
| First published | 2012-01-30 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >= 0.4.x < 0.7.0 |
| Dependencies | 7 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Nathan Oehlman |
| Maintainers | nathanoehlman |

## Links

- npm: https://www.npmjs.com/package/flatpack
- Repository: https://github.com/sidelab/flatpack
- Issues: http://github.com/steelmesh/flatpack/issues
- npm.io page: https://npm.io/package/flatpack

## Dependencies (7)

- [async](https://npm.io/package/async.md) 0.1.x
- [debug](https://npm.io/package/debug.md) *
- [piper](https://npm.io/package/piper.md) >= 0.1.1
- [config](https://npm.io/package/config.md) 0.4.x
- [supercomfy](https://npm.io/package/supercomfy.md) >= 0.0.2
- [underscore](https://npm.io/package/underscore.md) 1.2.x
- [underscore.string](https://npm.io/package/underscore.string.md) 2.0.0

## Recent versions

- 0.1.2 (latest) — 2012-02-02
- 0.1.1 — 2012-02-01
- 0.0.3 — 2012-01-31
- 0.0.2 — 2012-01-30
- 0.0.1 — 2012-01-30

## README

# Flatpack

Flatpack provides some simple document modelling for CouchDB built on top of [supercomfy](https://github.com/sidelab/supercomfy) CouchDB client. Flatpack will allow you to define a model, and will then create appropriate permanent views in CouchDB to allow for easy searching and retrieval of these documents, as well as creating a helper object which will allow you to easily manipulate documents of that type.  

## Goals

- Provide alternate client implementations (use either supercomfy, cradle or nano)
- Use Travis-CI for continuous integration

## Example

### Defining a model

``` js
    // Define a model with just the default view (getByType)
    flatpack.define(couchurl, db, 'customer', null, callback);

    // Define a model with custom views
    flatpack.define(couchurl, db, 'customer', {views: {byName: ['firstName', 'lastName'], byCompany: ['company']}}, callback);
```

### Saving an object

``` js
    // Create a document
    var customer = {firstName: 'Nathan', lastName: 'Oehlman', company: 'Sidelab'},
        customerdb = flatpack.use('customer');
    
    // Save for the first time
    customerdb.save(customer, function(err, id) {
        // id is the id assigned by couch to the document
    });

```

### Getting and updating object

``` js
    // Create a document
    var customerdb = flatpack.use('customer');
    
    // Get the existing object
    customerdb.get(id, function(err, object) {
        if (!err) {
            // Update the details
            object.firstName = 'Thomas';
            customerdb.save(object, function(err, id)) {
                if (!err) {
                    // All ok!
                }
            });
        }
    });
```

### Using a view ###

By default, flatpack will create a default view that can access all documents of the type.

``` js
	// Get all customer documents
	var customerdb = flatpack.use('customer');
	customerdb.findByType(function(err, results) {
		// returns a CouchDB results
		// ie. {total_rows: x, offset: x, rows: [documents]}
	});
	
	// Can also be accessed using the all alias
	customerdb.all(function(err, results) {
		// etc
	});
	
```

Using a custom view is also very similar. 

``` js
	// Get all customer documents belonging to the Sidelab company
	var customerdb = flatpack.use('customer');
	// An argumented view takes an opts object, that gets converted into Couch DB view parameters (so you can use startKey, endKey, etc)
	customerdb.findByCompany({key: 'Sidelab'}, function(err, results) {
		// same as before - a CouchDB result set
	});
	
```

---
_Source: https://npm.io/package/flatpack · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
