# lian

> Simple object persistence with MongoDB

Latest version **0.11.1** (published 2013-02-15) · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.11.1 |
| Published | 2013-02-15 |
| First published | 2012-05-21 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.4.7 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Richard Hodgson |
| Maintainers | richardhodgson |

## Links

- npm: https://www.npmjs.com/package/lian
- Repository: https://github.com/richardhodgson/lian
- npm.io page: https://npm.io/package/lian

## Dependencies (2)

- [monk](https://npm.io/package/monk.md) >=0.1.9
- [promised-io](https://npm.io/package/promised-io.md) >=0.3.0

## Recent versions

- 0.11.1 (latest) — 2013-02-15
- 0.11.0 — 2013-02-04
- 0.10.2 — 2013-01-15
- 0.10.1 — 2013-01-08
- 0.10.0 — 2013-01-08
- 0.9.0 — 2013-01-06
- 0.8.0 — 2013-01-06
- 0.7.0 — 2013-01-03
- 0.6.0 — 2013-01-01
- 0.5.0 — 2012-12-31
- 0.4.0 — 2012-12-29
- 0.3.0 — 2012-12-29
- 0.2.0 — 2012-12-28
- 0.1.0 — 2012-05-21

## README

# Lian

Simple object persistence for node.js with MongoDB.

## tl;dr

I wanted something more involved than just a DB driver but I didn't want to define a schema or similar meta object.

I also only want to work with the objects I define - if I've saved a `Person` object, when I invoke a find operation it should return an array of `Person` objects.

Finally, writing tests for projects that use MongoDB should be easy and not rely on a connection to a DB instance.

See the other [goals](#goals).

## Examples

    var lian = require('lian')('localhost/mydb');

    function Person (name) {
        lian(this, 'person');
        this.name = name;
    }

    Person.prototype.getGender = function () {
        return this.gender;
    }

    var john = new Person('John Smith');
    john.gender = "male";

    john.save();

Create a projection of `Person` to find the instance saved above.

    var john = new Person('John Smith');
    john.find().then(function (results) {

        john = results[0];
        john.name; // "John Smith"
        john.getGender(); // "male"
    });

Make some changes to persist.

    john.name = "John Anthony Smith";
    john.save();

Decoupled, lian's `Store` object can be used directly.

    var lian  = require('lian'),
        Store = lian.Store;

    function Person (name) {
        lian(this, 'person');
        this.name = name;
    }

    var steve = new Person('steve');

    typeof steve.insert // "undefined"

    var store = new Store('localhost/mydb');
    store.insert(steve);

Easy to mock with, for testing. Require the `lian/lib/mock` module path instead of `lian`.

    var lian = require('lian/lib/mock')('localhost/mydb');

    function Person (name) {
        lian(this, 'person');
        this.name = name;
    }

    var john = new Person('John Smith');
    john.gender = "male";

    // saved in memory
    john.save().then(function () {

        var john2 = new Person('John Smith');
        john2.findOne().then(function (result) {
            result.gender; // "male"
        });
    });

Hooks for validation.

    var lian = require('lian')('localhost/mydb');

    function Person (name) {
        lian(this, 'person', {
            before: {
                'insert': function (person) {
                    // check the person has a gender set
                    return (person.gender);
                }
            }
        });

        this.name = name;
    }

    var john = new Person('John Smith');

    john.insert().then(
        function () {
            // promise is rejected, see next callback
        },
        function () {
            throw new Error("failed to pass validation");
        }
    );

Learn more about [validation](https://github.com/richardhodgson/lian/blob/master/documentation.md#validation) or [read more documentation](https://github.com/richardhodgson/lian/blob/master/documentation.md).

## Goals

- Avoid writing result to object mapping code over and over.
- Instance based connections, multiple connections within the same process.
- All asynchronous operations should return a promise.
- Store provides an in-memory alternative, for testing.

## Install

Install with [npm](https://npmjs.org/package/lian).

    npm install lian

## Development [![Build Status](https://secure.travis-ci.org/richardhodgson/lian.png)](http://travis-ci.org/richardhodgson/lian)

Lian uses [monk](https://github.com/LearnBoost/monk) to talk to MongoDB and [promised-io](https://github.com/kriszyp/promised-io) for futures.

Clone the repo...

    git clone git://github.com/richardhodgson/lian.git

Use [npm](http://npmjs.org) to install dependencies.

    cd lian && \
    npm install --dev

Run the tests.

    make test

The tests mock out monk, there are integration tests expecting a MongoDB instance running on `localhost:27017`. They will create a `lian-integration` database.

    make integration-test

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