Socket
Socket
Sign inDemoInstall

mr.q

Package Overview
Dependencies
72
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    mr.q

JSON AST to RethinkDB query


Version published
Weekly downloads
2
Maintainers
1
Install size
1.76 MB
Created
Weekly downloads
 

Readme

Source

mr.q

JSON AST to RethinkDB query

Build Status

Usage

  • expose flexible query as API
  • store & exchange query as data

Installation

npm install --save mr.q

Expressions

  • date {$date: 'YYYY-MM-DD HH:MM:SS'}
  • between {$between: [START, END]}
  • in {$in: [foo, bar]}
  • not in {$nin: [foo, bar]}
  • contains {$contains: value}
  • greater than {$gt: value}
  • greater or equals to {$ge: value}
  • less than {$lt: value}
  • less or equal to {$le: value}

Example

// Sample data

r.table('mr').insert([
  {
    id: 1,
    type: 'foo',
    name: 'foo1',
    value: 1,
    list: [],
    dt: new Date('2014-08-17 12:00:00')
  }, {
    id: 2,
    type: 'foo',
    name: 'foo2',
    value: 2,
    list: ['A'],
    dt: new Date('2014-08-17 13:00:00')
  }, {
    id: 3,
    type: 'foo',
    name: 'foo3',
    value: 3,
    list: ['A', 'B'],
    dt: new Date('2014-08-17 14:00:00')
  }, {
    id: 4,
    type: 'bar',
    name: 'bar1',
    value: 1,
    list: ['A', 'C'],
    dt: new Date('2014-08-17 12:00:00')
  }, {
    id: 5,
    type: 'bar',
    name: 'bar2',
    value: 2,
    list: ['B', 'C'],
    dt: new Date('2014-08-17 13:00:00')
  }
]);

var r  = require('rethinkdb');
var mr = require('mr.q');
var co = require('co');


co(function*() {
  // documents that have type of 'foo' and name in ['foo1', 'bar1']
  var query = mr.match({
    type: 'foo',
    name: {
      $in: ['foo1', 'bar1']
    }
  });

  var result = yield run(query(table));

  // {
  //   id: 1,
  //   type: 'foo',
  //   name: 'foo1',
  //   value: 1,
  //   list: [],
  //   dt: new Date('2014-08-17 12:00:00')
  // }
})();

co(function*() {
  // Distinct types of documents that have type in ['foo', 'bar']
  // and dt between '2014-08-17 12:00:00' and '2014-08-17 13:30:00'

  var q1 = mr.match({
    type: {
      $in: ['foo', 'bar']
    },
    dt: {
      $between: [
        {
          $date: '2014-08-17 12:00:00'
        }, {
          $date: '2014-08-17 13:30:00'
        }
      ]
    }
  });
  var q2 = mr.aggregate({
    $distinct: 'type'
  });
  var result = yield run(q2(q1(table)));

  // [
  //   {type: 'foo'},
  //   {type: 'bar'}
  // ]
})();

more examples can be found in the test suite

Keywords

FAQs

Last updated on 17 Aug 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc