Git Product home page Git Product logo

path-to-mongodb's Introduction

path-to-mongodb

Parse URL to MongoDB query.

Install

npm i path-to-mongodb --save

Usage

pathToMongodb(path, realPath, options);
  • path: {String} Express style path like: /users/:uid.
  • realPath: {String} Actual path like: /users/123?vip=true.
  • options: {Object}
    • queryOptions: {Object|Array} Options to preserve, see test.
    • alias: {Object} alias for query or options, eg: p=2 -> skip=20&limit=10.
    • others see path-to-regexp.

Example

pathToMongodb(
  '/posts/:year',
  '/posts/2015?__skip=20&__limit=10&(comments[$size]=10||praise>=5)',
  {queryOptions: {__skip: 'skip', __limit: 'limit'}}
);

// output

{
  "query": {
    "$or": [
      {"comments": {"$size": 10}},
      {"praise": {"$gte": 5}}
    ],
    "year": 2015
  },
  "options": {
    "skip": 20,
    "limit": 10
  }
}
function aliasLimitAndSkip(p) {
  return 'limit=10&skip=' + (p * 10);
}

pathToMongodb(
  '/posts/:year',
  '/posts/2015?((user=nswbmw||user=zk)&&(star=true||comments[$size]>=100))&p=2',
  {
    depth: Infinity,
    alias: {
      p: aliasLimitAndSkip
    }
  }
);

//output

{
    "query": {
        "$and": [{
            "$or": [{
                "user": "nswbmw"
            },
            {
                "user": "zk"
            }]
        },
        {
            "$or": [{
                "star": true
            },
            {
                "comments": {
                    "$size": {
                        "$gte": 100
                    }
                }
            }]
        }],
        "year": 2015
    },
    "options": {
        "limit": 10,
        "skip": 20
    }
}

Next, you may write like this:

mongoclient
  .db('test')
  .collection('users')
  .find(obj.query, obj.options)

see test for more details.

Test

npm test

Benchmark

node benchmark

License

MIT

path-to-mongodb's People

Contributors

dasithkuruppu avatar nswbmw avatar

Stargazers

 avatar

Watchers

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