Add like/nlike support for memory connector

This commit is contained in:
Raymond Feng 2014-06-17 23:19:28 -07:00
parent ad3af82923
commit a487eb57cd
2 changed files with 114 additions and 1 deletions

View File

@ -363,8 +363,12 @@ function applyFilter(filter) {
return pass; return pass;
} }
function replaceAll(string, find, replace) {
return string.replace(new RegExp(find, 'g'), replace);
}
function test(example, value) { function test(example, value) {
if (typeof value === 'string' && example && example.constructor.name === 'RegExp') { if (typeof value === 'string' && (example instanceof RegExp)) {
return value.match(example); return value.match(example);
} }
if (example === undefined || value === undefined) { if (example === undefined || value === undefined) {
@ -386,6 +390,22 @@ function applyFilter(filter) {
return false; return false;
} }
if (example.like || example.nlike) {
var like = example.like || example.nlike;
if (typeof like === 'string') {
like = replaceAll(like, '%', '.*');
like = replaceAll(like, '_', '.');
}
if (example.like) {
return !!new RegExp(like).test(value);
}
if (example.nlike) {
return !new RegExp(like).test(value);
}
}
if (testInEquality(example, value)) { if (testInEquality(example, value)) {
return true; return true;
} }

View File

@ -4,6 +4,7 @@ var path = require('path');
var fs = require('fs'); var fs = require('fs');
var assert = require('assert'); var assert = require('assert');
var async = require('async'); var async = require('async');
var should = require('./init.js');
describe('Memory connector', function () { describe('Memory connector', function () {
var file = path.join(__dirname, 'memory.json'); var file = path.join(__dirname, 'memory.json');
@ -91,5 +92,97 @@ describe('Memory connector', function () {
}); });
}); });
describe('Query for memory connector', function () {
var ds = new DataSource({
connector: 'memory'
});
var User = ds.define('User', {
seq: {type: Number, index: true},
name: {type: String, index: true, sort: true},
email: {type: String, index: true},
birthday: {type: Date, index: true},
role: {type: String, index: true},
order: {type: Number, index: true, sort: true},
vip: {type: Boolean}
});
before(seed);
it('should allow to find using like', function (done) {
User.find({where: {name: {like: '%St%'}}}, function (err, posts) {
should.not.exist(err);
posts.should.have.property('length', 2);
done();
});
});
it('should support like for no match', function (done) {
User.find({where: {name: {like: 'M%XY'}}}, function (err, posts) {
should.not.exist(err);
posts.should.have.property('length', 0);
done();
});
});
it('should allow to find using nlike', function (done) {
User.find({where: {name: {nlike: '%St%'}}}, function (err, posts) {
should.not.exist(err);
posts.should.have.property('length', 4);
done();
});
});
it('should support nlike for no match', function (done) {
User.find({where: {name: {nlike: 'M%XY'}}}, function (err, posts) {
should.not.exist(err);
posts.should.have.property('length', 6);
done();
});
});
function seed(done) {
var count = 0;
var beatles = [
{
seq: 0,
name: 'John Lennon',
email: 'john@b3atl3s.co.uk',
role: 'lead',
birthday: new Date('1980-12-08'),
order: 2,
vip: true
},
{
seq: 1,
name: 'Paul McCartney',
email: 'paul@b3atl3s.co.uk',
role: 'lead',
birthday: new Date('1942-06-18'),
order: 1,
vip: true
},
{seq: 2, name: 'George Harrison', order: 5, vip: false},
{seq: 3, name: 'Ringo Starr', order: 6, vip: false},
{seq: 4, name: 'Pete Best', order: 4},
{seq: 5, name: 'Stuart Sutcliffe', order: 3, vip: true}
];
User.destroyAll(function () {
beatles.forEach(function (beatle) {
User.create(beatle, ok);
});
});
function ok() {
if (++count === beatles.length) {
done();
}
}
}
});
}); });