Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Carlos
/
query-to-mongo
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Wiki
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
48a2d9a2
authored
Aug 26, 2016
by
Jose Carlos López
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add criteria
parent
3e4f68b0
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
6 deletions
index.js
test/query.js
index.js
View file @
48a2d9a2
...
@@ -142,8 +142,9 @@ function hasOrdinalKeys(obj) {
...
@@ -142,8 +142,9 @@ function hasOrdinalKeys(obj) {
// Convert query parameters to a mongo query criteria.
// Convert query parameters to a mongo query criteria.
// for example {field1:"red","field2>2":""} becomes {field1:"red",field2:{$gt:2}}
// for example {field1:"red","field2>2":""} becomes {field1:"red",field2:{$gt:2}}
function
queryCriteriaToMongo
(
query
,
options
)
{
function
queryCriteriaToMongo
(
query
,
options
)
{
var
hash
=
{},
p
,
v
,
deep
var
options
=
options
||
{}
options
=
options
||
{}
var
p
,
v
,
deep
var
hash
=
query
.
isOr
?
[]
:
{}
for
(
var
key
in
query
)
{
for
(
var
key
in
query
)
{
if
(
Object
.
prototype
.
hasOwnProperty
.
call
(
query
,
key
)
&&
(
!
options
.
ignore
||
options
.
ignore
.
indexOf
(
key
)
==
-
1
))
{
if
(
Object
.
prototype
.
hasOwnProperty
.
call
(
query
,
key
)
&&
(
!
options
.
ignore
||
options
.
ignore
.
indexOf
(
key
)
==
-
1
))
{
deep
=
(
typeof
query
[
key
]
===
'object'
&&
!
hasOrdinalKeys
(
query
[
key
]))
deep
=
(
typeof
query
[
key
]
===
'object'
&&
!
hasOrdinalKeys
(
query
[
key
]))
...
@@ -158,10 +159,16 @@ function queryCriteriaToMongo(query, options) {
...
@@ -158,10 +159,16 @@ function queryCriteriaToMongo(query, options) {
}
}
if
(
p
)
{
if
(
p
)
{
if
(
query
.
isOr
)
{
var
orQuery
=
{};
orQuery
[
p
.
key
]
=
p
.
value
hash
.
push
(
orQuery
)
}
else
{
hash
[
p
.
key
]
=
p
.
value
hash
[
p
.
key
]
=
p
.
value
}
}
}
}
}
}
}
return
hash
return
hash
}
}
...
@@ -201,13 +208,15 @@ module.exports = function(query, options) {
...
@@ -201,13 +208,15 @@ module.exports = function(query, options) {
}
else
{
}
else
{
options
.
ignore
=
(
typeof
options
.
ignore
===
'string'
)
?
[
options
.
ignore
]
:
options
.
ignore
options
.
ignore
=
(
typeof
options
.
ignore
===
'string'
)
?
[
options
.
ignore
]
:
options
.
ignore
}
}
options
.
ignore
=
options
.
ignore
.
concat
([
'fields'
,
'omit'
,
'sort'
,
'offset'
,
'limit'
])
options
.
ignore
=
options
.
ignore
.
concat
([
'fields'
,
'omit'
,
'sort'
,
'offset'
,
'limit'
,
'isOr'
])
if
(
!
options
.
parser
)
options
.
parser
=
querystring
if
(
!
options
.
parser
)
options
.
parser
=
querystring
if
(
typeof
query
===
'string'
)
query
=
options
.
parser
.
parse
(
query
)
if
(
typeof
query
===
'string'
)
query
=
options
.
parser
.
parse
(
query
)
var
criteria
=
queryCriteriaToMongo
(
query
,
options
);
if
(
query
.
isOr
)
{
criteria
=
{
$or
:
criteria
};
}
return
{
return
{
criteria
:
queryCriteriaToMongo
(
query
,
options
)
,
criteria
:
criteria
,
options
:
queryOptionsToMongo
(
query
,
options
),
options
:
queryOptionsToMongo
(
query
,
options
),
links
:
function
(
url
,
totalCount
)
{
links
:
function
(
url
,
totalCount
)
{
...
...
test/query.js
View file @
48a2d9a2
...
@@ -156,6 +156,11 @@ describe("query-to-mongo(query) =>", function () {
...
@@ -156,6 +156,11 @@ describe("query-to-mongo(query) =>", function () {
assert
.
ok
(
results
.
criteria
)
assert
.
ok
(
results
.
criteria
)
assert
.
deepEqual
(
results
.
criteria
,
{
d
:
ObjectID
(
'574553013b58d8582cdb1d5f'
)})
assert
.
deepEqual
(
results
.
criteria
,
{
d
:
ObjectID
(
'574553013b58d8582cdb1d5f'
)})
})
})
it
(
"should create or criteria"
,
function
()
{
var
results
=
q2m
(
"isOr=true&a>3&a<8"
)
assert
.
ok
(
results
.
criteria
)
assert
.
deepEqual
(
results
.
criteria
,
{
$or
:
[{
a
:
{
$gt
:
3
}},
{
a
:
{
$lt
:
8
}
}]})
})
})
})
describe
(
".options"
,
function
()
{
describe
(
".options"
,
function
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment