-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from PlayNetwork/develop
reworking component so that mongoose is not required except when testing...
- Loading branch information
Showing
15 changed files
with
292 additions
and
309 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,24 @@ | ||
var Query = require('mongoose').Query; | ||
|
||
|
||
Query.prototype.field = function (options) { | ||
module.exports = function(mongoose) { | ||
'use strict'; | ||
|
||
var self = this; | ||
mongoose.Query.prototype.field = function (options) { | ||
var self = this; | ||
|
||
if (options && options.filters && options.filters.field) { | ||
if (options.filters.field instanceof Array && options.filters.field.length) { | ||
options.filters.field.forEach(function (field) { | ||
if (options && options.filters && options.filters.field) { | ||
if (options.filters.field instanceof Array && options.filters.field.length) { | ||
options.filters.field.forEach(function (field) { | ||
|
||
if (self.model.schema.path(field)) { | ||
self.select(field); | ||
if (self.model.schema.path(field)) { | ||
self.select(field); | ||
} | ||
}); | ||
} else { | ||
if (self.model.schema.path(options.filters.field)) { | ||
self.select(options.filters.field); | ||
} | ||
}); | ||
} else { | ||
if (self.model.schema.path(options.filters.field)) { | ||
self.select(options.filters.field); | ||
} | ||
} | ||
} | ||
|
||
return this; | ||
}; | ||
return this; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,119 +1,116 @@ | ||
var Query = require('mongoose').Query; | ||
|
||
|
||
function sanitize(str) { | ||
module.exports = function(mongoose) { | ||
'use strict'; | ||
|
||
// santizes regex escapes | ||
return str.replace(/\W\s/ig, '\\$&'); | ||
} | ||
function sanitize(str) { | ||
// santizes regex escapes | ||
return str.replace(/\W\s/ig, '\\$&'); | ||
} | ||
|
||
|
||
Query.prototype.filter = function (options) { | ||
'use strict'; | ||
mongoose.Query.prototype.filter = function (options) { | ||
if (!options || !options.filters) { | ||
return this; | ||
} | ||
|
||
if (!options || !options.filters) { | ||
return this; | ||
} | ||
var | ||
key = null, | ||
mandatory = options.filters.mandatory || {}, | ||
optional = options.filters.optional || {}, | ||
self = this, | ||
value = null; | ||
|
||
var | ||
key = null, | ||
mandatory = options.filters.mandatory || {}, | ||
optional = options.filters.optional || {}, | ||
self = this, | ||
value = null; | ||
|
||
var applyWhere = function (spec, buildRegex) { | ||
for (var key in spec) { | ||
if (spec.hasOwnProperty(key)) { | ||
var val = spec[key]; | ||
self.where(key).regex(buildRegex(val)); | ||
var applyWhere = function (spec, buildRegex) { | ||
for (var key in spec) { | ||
if (spec.hasOwnProperty(key)) { | ||
var val = spec[key]; | ||
self.where(key).regex(buildRegex(val)); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
var applyOptionals = function(optional) { | ||
var items = []; | ||
}; | ||
|
||
var populate = function (key, value, buildRegex) { | ||
var | ||
items = [], | ||
item = {}; | ||
var applyOptionals = function(optional) { | ||
var items = []; | ||
|
||
if (value instanceof Array && value.length) { | ||
value.forEach(function (val) { | ||
var populate = function (key, value, buildRegex) { | ||
var | ||
items = [], | ||
item = {}; | ||
item[key] = buildRegex(val); | ||
items.push(item); | ||
}); | ||
return items; | ||
} else { | ||
item[key] = buildRegex(value); | ||
return item; | ||
} | ||
}; | ||
|
||
var parse = function(spec, buildRegex) { | ||
var | ||
parsedItems = null, | ||
result = null; | ||
if (value instanceof Array && value.length) { | ||
value.forEach(function (val) { | ||
item = {}; | ||
item[key] = buildRegex(val); | ||
items.push(item); | ||
}); | ||
return items; | ||
} else { | ||
item[key] = buildRegex(value); | ||
return item; | ||
} | ||
}; | ||
|
||
if (!spec) { | ||
return null; | ||
} | ||
var parse = function(spec, buildRegex) { | ||
var | ||
parsedItems = null, | ||
result = null; | ||
|
||
for (var key in spec) { | ||
if (spec.hasOwnProperty(key)) { | ||
result = populate(key, spec[key], buildRegex); | ||
if (!spec) { | ||
return null; | ||
} | ||
|
||
if (result instanceof Array && result.length) { | ||
parsedItems = result; | ||
} else { | ||
if (!parsedItems) { | ||
parsedItems = []; | ||
for (var key in spec) { | ||
if (spec.hasOwnProperty(key)) { | ||
result = populate(key, spec[key], buildRegex); | ||
|
||
if (result instanceof Array && result.length) { | ||
parsedItems = result; | ||
} else { | ||
if (!parsedItems) { | ||
parsedItems = []; | ||
} | ||
parsedItems.push(result); | ||
} | ||
parsedItems.push(result); | ||
} | ||
} | ||
} | ||
return parsedItems; | ||
}; | ||
return parsedItems; | ||
}; | ||
|
||
items = parse(optional.contains, regexContains); | ||
if (items && items instanceof Array && items.length) { | ||
self.or(items); | ||
} | ||
items = parse(optional.contains, regexContains); | ||
if (items && items instanceof Array && items.length) { | ||
self.or(items); | ||
} | ||
|
||
items = parse(optional.startsWith, regexStartsWith); | ||
if (items && items instanceof Array && items.length) { | ||
self.or(items); | ||
} | ||
items = parse(optional.startsWith, regexStartsWith); | ||
if (items && items instanceof Array && items.length) { | ||
self.or(items); | ||
} | ||
|
||
items = parse(optional.exact, regexExact); | ||
if (items && items instanceof Array && items.length) { | ||
self.or(items); | ||
} | ||
}; | ||
items = parse(optional.exact, regexExact); | ||
if (items && items instanceof Array && items.length) { | ||
self.or(items); | ||
} | ||
}; | ||
|
||
var regexContains = function (val) { | ||
return new RegExp(sanitize(val), 'i'); | ||
}; | ||
var regexContains = function (val) { | ||
return new RegExp(sanitize(val), 'i'); | ||
}; | ||
|
||
var regexStartsWith = function (val) { | ||
return new RegExp('^' + sanitize(val), 'i'); | ||
}; | ||
var regexStartsWith = function (val) { | ||
return new RegExp('^' + sanitize(val), 'i'); | ||
}; | ||
|
||
var regexExact = function (val) { | ||
return new RegExp('^' + sanitize(val) + '$', 'i'); | ||
}; | ||
var regexExact = function (val) { | ||
return new RegExp('^' + sanitize(val) + '$', 'i'); | ||
}; | ||
|
||
// MANDATORY | ||
applyWhere(mandatory.contains, regexContains); | ||
applyWhere(mandatory.startsWith, regexStartsWith); | ||
applyWhere(mandatory.exact, regexExact); | ||
// MANDATORY | ||
applyWhere(mandatory.contains, regexContains); | ||
applyWhere(mandatory.startsWith, regexStartsWith); | ||
applyWhere(mandatory.exact, regexExact); | ||
|
||
// OPTIONAL | ||
applyOptionals(optional); | ||
// OPTIONAL | ||
applyOptionals(optional); | ||
|
||
return self; | ||
}; | ||
return self; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,19 @@ | ||
var | ||
field = require('./field'), | ||
filter = require('./filter'), | ||
keyword = require('./keyword'), | ||
order = require('./order'), | ||
page = require('./page'); | ||
var mongooseLib = require('mongoose'); | ||
|
||
module.exports.initialize = function (options, mongoose) { | ||
if (!mongoose) { | ||
mongoose = options; | ||
options = null; | ||
} | ||
|
||
var | ||
field = require('./field')(mongoose), | ||
filter = require('./filter')(mongoose), | ||
keyword = require('./keyword')(mongoose), | ||
order = require('./order')(mongoose), | ||
page = require('./page')(mongoose); | ||
|
||
module.exports.initialize = function (options) { | ||
if (options && options.maxDocs) { | ||
page.initialize(options); | ||
} | ||
}; | ||
}; |
Oops, something went wrong.