node-document

Many document stores, Y U NO interface?

npm install node-document
32 downloads in the last month

NODE-DOCUMENT Build Status

Many document stores, Y U NO interface?

Work in progress; see TODO.

About

Minimalistic ODM for Node.js implementing the most fundamental operations (such as GET/SET/DEL) on different kinds of "document(-ish)" stores using one unified API. Switching database should be a matter of changing a line of code.

To stick to this philosophy more advanced operations won't be supported in core, but node-document can be used along with any 3rd-party drivers.

Document

The ODM.

  • Class
    • new
    • create
    • get
    • set
    • del
    • exists
  • Instance
    • save
    • destroy
    • fetch
    • validate
    • diff
    • clone
    • inspect
  • Events

Storage

Unified interface for write/read data to/from differen kinds of storages/databases.

Validator

Unified interface for validating data based on a custom JSON Schema.

Differ

Unified interface for diffing objects to see changes between the two (additions/removals/edits).

Installation

  $ npm install node-document

Usage

Basic:

  var Document = require('node-document');

  // Some storages of choice - install via NPM
  var Redis = require('node-document-storage-redis'); // NOTE: $ npm install node-document-storage-redis
  var FileSystem = require('node-document-storage-fs'); // NOTE: $ npm install node-document-storage-fs

  // A model
  var Post = Document('Post', new Redis('redis://localhost:6379/app'));
  // ...or shortcut: var Post = Document('Post', 'redis://localhost:6379/app');

  // A record
  var post = new Post({title: "Once upon a time"});

  // Save it
  post.save(function(err, res) {
    console.log("SAVE  Persisted: %s | Storage: %s | Type: %s | ID: %s  ->  %s", post.persisted, post.storage.name, post.type, post.id, post);

    // Find it
    Post.get(post.id, function(err, res) {
      console.log("GET  Storage: %s | Type: %s | ID: %s  ->  %s", post.storage.name, post.type, post.id, JSON.stringify(res));

      // Destroy it
      post.destroy(function(err, res) {
        console.log("DESTROY  Persisted: %s | Storage: %s | Type: %s | ID: %s  ->  %s", post.persisted, post.storage.name, post.type, post.id, post);

        // Switch storage
        Post.storage = new FileSystem('file:///tmp/app');

        // Save to file instead
        post.save(function(err, res) {
          console.log("SAVE  Persisted: %s | Storage: %s | Type: %s | ID: %s  ->  %s", post.persisted, post.storage.name, post.type, post.id, post);

          // Find it again
          Post.get(post.id, function(err, res) {
            console.log("GET  Storage: %s | Type: %s | ID: %s  ->  %s", post.storage.name, post.type, post.id, JSON.stringify(res));
          });
        });
      });
    });
  });

  // etc.
})

More usage examples coming soon, unil then checkout the tests.

Test

Local tests:

  $ make test

Remote tests:

  $ make test-remote

Notes

This project is very much work-in-progress; the API will most probably change between the first couple of minor version numbers until it will be settled.

Credit

License

Released under the MIT license.

Copyright (c) Jonas Grimfelt

npm loves you