Giter Site home page Giter Site logo

nohm's Introduction

Nohm

Description

Nohm is an object relational mapper (ORM) written for node.js and redis.

Requirements

  • redis >= 2.4

Install

Installing nohm

npm install nohm

Documentation

http://maritz.github.com/nohm/

Examples

  var nohm = require('nohm').Nohm;
  var redis = require('redis').createClient();
  
  nohm.setClient(redis);

  nohm.model('User', {
    properties: {
      name: {
        type: 'string',
        unique: true,
        validations: [
          'notEmpty'
        ]
      },
      email: {
        type: 'string',
        unique: true,
        validations: [
          'email'
        ]
      },
      country: {
        type: 'string',
        defaultValue: 'Tibet',
        validations: [
          'notEmpty'
        ]
      },
      visits: {
        type: function incrVisitsBy(value, key, old) {
          return old + value;
        },
        defaultValue: 0,
        index: true
      }
    },
    methods: {
      getContryFlag: function () {
        return 'http://example.com/flag_'+this.p('country')+'.png';
      },
    }
  });

  var user = nohm.factory('User');
  user.p({
    name: 'Mark',
    email: '[email protected]',
    country: 'Mexico',
    visits: 1
  });
  user.save(function (err) {
    if (err === 'invalid') {
      console.log('properties were invalid: ', user.errors);
    } else if (err) {
      console.log(err); // database or unknown error
    } else {
      console.log('saved user! :-)');
      user.remove(function (err) {
        if (err) {
          console.log(err); // database or unknown error
        } else {
          console.log('successfully removed user');
        }
      });
    }
  });

  // try to load a user from the db
  var otherUser = nohm.factory('User', 522, function (err) {
    if (err === 'not found') {
      console.log('no user with id 522 found :-(');
    } else if (err) {
      console.log(err); // database or unknown error
    } else {
      console.log(otherUser.allProperties());
    }
  });

Do you have code that should/could be listed here? Message me!

Contribute?

Yes, please contact me or just fork and request pulls. Any help or feedback is appreciated. If you use nohm I'd also be happy if you just drop me a quick msg about it.

Running tests

To run the tests you need to have nodeunit v0.6.4. This will be installed if you installed nohm with the --dev argument. Otherwise you can run:

npm install [email protected]

Then run

node test/tests.js

Careful: This requires a running redis server. (you can configure host/port with the command line arguments --redis-host 1.1.1.1 --redis-port 1234)
The tests also create a lot of keys in your database that look something like this:

nohmtests:something:something

After the tests have run all keys that match the pattern nohmtests:* are deleted! You can prevent this by passing --no-cleanup (which will leave hundreds or thousands of test keys in your database). You may also change the prefix ("nohmtests") part doing something like

node test/tests.js --nohm-prefix YourNewPrefix

Now the keys will look like this:

YourNewPrefix:something:something

nohm's People

Contributors

maritz avatar yuchi avatar mkuklis avatar tarkus avatar karlbohlmark avatar midworld avatar index0h avatar samccone avatar

Watchers

gotomypc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.