node-document

Many document stores, Y U NO interface?

NODE-DOCUMENT

Many document stores, Y U NO interface?

Work in progress; see TODO.

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.

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).

  $ npm install node-document

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(errres) {
    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(errres) {
      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(errres) {
        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(errres) {
          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(errres) {
            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.

Local tests:

  $ make test

Remote tests:

  $ make test-remote

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.

Released under the MIT license.

Copyright (c) Jonas Grimfelt