0.1.2 • Published 5 years ago

mongo-query-shape v0.1.2

Weekly downloads
69
License
Apache-2.0
Repository
github
Last release
5 years ago

mongo-query-shape

Build Status Coverage Status

Determine the "shape" of a mongo query, to help identify queries that are the same.

Queries have the same shape if they make the same type of tests on the same properties of the documents in the collection. The test types can be EXACT (test for equality), RANGE (test for bounds), or TEST (test an indirect or computed attribute).

Summary

const queryShape = require('mongo-query-shape');

var query1 = { a: 1, b: {$gt: 1, $lt: 11}, c: {$not: { $eq: 1 } } };
var query2 = { a: 2, b: {$eq: 2}, c: {$gt: 100} };
var shape1, shape2;

shape1 = queryShape(query1);
// => { a: 'EXACT', b: 'RANGE', c: 'TEST' }
shape2 = queryShape(query2);
// => { a: 'EXACT', b: 'EXACT', c: 'RANGE' }
queryShape.isSame(shape1, shape2)
// => false

var shapeOptions = { shapeNames: { EXACT: '*', RANGE: '*', TEST: '*' } };
shape1 = queryShape(query1, shapeOptions);
// => { a: '*', b: '*', c: '*' }
shape2 = queryShape(query2, shapeOptions);
// => { a: '*', b: '*', c: '*' }
queryShape.isSame(shape1, shape2)
// => true

API

queryShape( query, options )

Return an object corresponding to the shape of the given mongo query. Queries that differ only in the specific values being compared to will have the same shape.

Options:

  • shapeNames - hash of shape names to use, must contain entries for 'EXACT', 'RANGE' and 'TEST'

queryShape.isSame( shape1, shape2 )

Return true if the two queries are structurally the same, else false.

ChangeLog

  • 0.1.2 - added license file
  • 0.1.1 - fix: use the user-specified shape name for scalar queries, too
  • 0.1.0 - first tagged version