Coder Social home page Coder Social logo

rajanrastogi / poseidon Goto Github PK

View Code? Open in Web Editor NEW

This project forked from playlyfe/poseidon

0.0 2.0 0.0 104 KB

Poseidon allows you to wrap existing Node Module APIs in a simple promise layer using Bluebird for easier programming.

CoffeeScript 32.89% JavaScript 67.11%

poseidon's Introduction

Poseidon

Poseidon is a tiny module which can generate a promise layer over an existing callback API for easier programming.

The performance of the generated api is on par with simple callbacks.

Simple Callbacks x 762 ops/seca ±1.51% (85 runs sampled)
Poseidon x 757 ops/sec ±1.39% (87 runs sampled)
Fastest is Simple Callbacks,Poseidon

How to generate an API

Poseidon uses a simple configuration file to generate an api. It creates a proxy class that holds a reference to the original object on this.instance. The proxy class also has all the methods of the original object in its prototype. Each of the proxy functions wrap the original function using Bluebird promises. Once the api is generated you can output the generated javascript to a file.

Sample configuration file from poseidon-mongo:

"Collection": {
    // Files to require and their location. The paths provided should be relative to the folder that the generated files are written to.
    // Bluebird promises are required by default in every generated file. It can be accessed through 'Promise'.
    "require": {
      "Mongo": "mongodb",
      "Cursor": "./cursor"
    },
    // You can write a custom constructor to wrap the original object that is passed to it. The original object should be stored on 'this.instance' always
    "constructor": {
      "params": ["collection"],
      "body": """
      if (!(collection instanceof Mongo.Collection)) {
        throw new Error('Object must be an instance of Mongo Collection');
      }
      this.instance = collection;
      return;
      """
    },
    // The instance is a simple object
    "type": "object",
    "functions": {
      "insert": {},
      "remove": {},
      "save": {},
      "update": {},
      "distinct": {},
      "count": {},
      "drop": {},
      "find": {
        // Don't wrap the find with a promise returning function
        "wrap": false,
        // Convert the return value to a Cursor class
        "return": ["Cursor"]
      },
      "findAndModify": {},
      "findAndRemove": {},
      "findOne": {},
      "createIndex": {},
      "ensureIndex": {},
      "indexInformation": {},
      "dropIndex": {},
      "dropAllIndexes": {},
      "reIndex": {},
      "mapReduce": {},
      "group": {},
      "options": {},
      "isCapped": {},
      "indexExists": {},
      "geoNear": {},
      "geoHaystackSearch": {},
      "indexes": {},
      "aggregate": {},
      "stats": {},
      "rename": {
        "return": ["Collection"]
      }
    }
  },
  "Cursor": {
    "require": {
      "Mongo": "mongodb"
    },
    "constructor": {
      "params": ["cursor"],
      "body": """
      if (cursor.constructor.name !== 'Cursor') {
        throw Error('Object must be an instance of Mongo Cursor');
      }
      this.instance = cursor;
      return;
      """
    },
    "type": "object",
    "functions": {
      "toArray": {},
      "each": {},
      "count": {},
      "nextObject": {},
      "explain": {},
      "close": {},
      "stream": {
        "wrap": false
      },
      "isClosed": {
        "wrap": false
      },
      "rewind": {
        "wrap": false,
        // The function will return a reference to the wrapping class allowing you to chain methods. Note: Chained methods should have wrap set to 'false'
        "chain": true
      },
      "sort": {
        "wrap": false,
        "chain": true
      },
      "setReadPreference": {
        "wrap": false,
        "chain": true
      },
      "skip": {
        "wrap": false,
        "chain": true
      },
      "limit": {
        "wrap": false,
        "chain": true
      },
      "batchSize": {
        "wrap": false,
        "chain": true
      }
    }
  },
  "Database": {
    "require": {
      "Driver": "./driver",
      "Collection": "./collection",
      "Cursor": "./cursor"
    },
    "constructor": {
      "params": ["connectionName"],
      "body": """
      this.connectionName = connectionName;
      this.instance = Driver.openConnection(connectionName);
      return;
      """
    },
    // The instance is a promised object
    "type": "promise",
    "functions": {
      "db": {},
      "collectionNames": {},
      "eval": {},
      "dereference": {},
      "logout": {},
      "authenticate": {},
      "addUser": {},
      "removeUser": {},
      "command": {},
      "dropCollection": {},
      "lastError": {},
      "previousErrors": {},
      "resetErrorHistory": {},
      "createIndex": {},
      "ensureIndex": {},
      "cursorInfo": {},
      "dropIndex": {},
      "reIndex": {},
      "indexInformation": {},
      "dropDatabase": {},
      "stats": {},
      "close": {},
      "collection": {
        "return": ["Collection"]
      },
      "collections": {
        "return": [
          // The value is an array of Collection items.
          {
            "name": "Collection"
            "array": true
          }
        ]
      },
      "createCollection": {
        "return": ["Collection"]
      },
      "renameCollection": {
        "return": ["Collection"]
      },
      "collectionsInfo": {
        "return": ["Cursor"]
      },
    }
  }
}

Modules using Poseidon

If you want to get your module listed here, just let me know at [email protected].

License

The MIT License

Copyright(c) 2013-2014, Playlyfe Technologies, [email protected], http://dev.playlyfe.com/

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.