Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable id query with no date #4481

Merged
merged 2 commits into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/server/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ function create (params, opts) {
var query = finder && finder.find ? finder.find : { };

// Ensure some kind of sane date constraint tied to an index is expressed in the query.
enforceDateFilter(query, opts);
// unless an ID is provided, in which case assume the user knows what they are doing.
if (! query._id ) {
enforceDateFilter(query, opts);
}

// Help queries for _id.
updateIdQuery(query);

Expand Down
38 changes: 38 additions & 0 deletions tests/query.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

require('should');

var moment = require('moment');

describe('query', function ( ) {
var query = require('../lib/server/query');

it('should provide default options', function ( ) {
var opts = query();

var low = moment().utc().subtract(4, 'days').subtract(1, 'minutes').format();
var high = moment().utc().subtract(4, 'days').add(1, 'minutes').format();

opts.date['$gte'].should.be.greaterThan(low);
opts.date['$gte'].should.be.lessThan(high);
});

it('should not override non default options', function ( ) {
var opts = query({}, {
deltaAgo: 2 * 24 * 60 * 60000,
dateField: 'created_at'
});

var low = moment().utc().subtract(2, 'days').subtract(1, 'minutes').format();
var high = moment().utc().subtract(2, 'days').add(1, 'minutes').format();

opts.created_at['$gte'].should.greaterThan(low);
opts.created_at['$gte'].should.lessThan(high);
});

it('should not enforce date filter if query includes id', function ( ) {
var opts = query({ find: { _id: 1234 } });

(typeof opts.date).should.equal('undefined')
});
});