Gathering detailed insights and metrics for qs-to-mongo
Gathering detailed insights and metrics for qs-to-mongo
Gathering detailed insights and metrics for qs-to-mongo
Gathering detailed insights and metrics for qs-to-mongo
qproc-mongo-qs
Provides a query string builder class to create url encoded query strings that are compatible with qproc-mongo.
@omegion1npm/recusandae-fuga-aliquid
[MongoDB](http://mongodb.com)-backed session storage for [connect](https://www.npmjs.org/package/connect) and [Express](http://www.expressjs.com). Meant to be a well-maintained and fully-featured replacement for modules like [connect-mongo](https://www.np
npm install qs-to-mongo
Typescript
Module System
Node Version
NPM Version
70
Supply Chain
99.4
Quality
77.5
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
87,884
Last Day
6
Last Week
224
Last Month
2,615
Last Year
53,472
13 Stars
46 Commits
2 Forks
3 Watching
5 Branches
2 Contributors
Minified
Minified + Gzipped
Latest Version
4.0.1
Package Id
qs-to-mongo@4.0.1
Unpacked Size
30.53 kB
Size
9.56 kB
File Count
21
NPM Version
10.2.4
Node Version
20.11.1
Publised On
20 May 2024
Cumulative downloads
Total Downloads
Last day
-95.8%
6
Compared to previous day
Last week
-61.6%
224
Compared to previous week
Last month
-48.3%
2,615
Compared to previous month
Last year
227.6%
53,472
Compared to previous year
3
8
Thanks to this package, you can parse and convert query parameters into MongoDB query criteria and options.
npm install qs-to-mongo
1import qs2m from 'qs-to-mongo' // or const qs2m = require('qs-to-mongo') 2const result = qs2m('name=john&age>21&fields=name,age&sort=name,-age&offset=10&limit=10')
The result will be
1{ 2 criteria: { 3 name: 'john', 4 age: { $gt: 21 } 5 }, 6 options: { 7 fields: { name: true, age: true }, 8 sort: { name: 1, age: -1 }, 9 offset: 10, 10 limit: 10 11 } 12}
Resulting object props (criteria
and options
) are usable as parameters for any MongoDB driver or ODM. For example:
1import qs2m from 'qs-to-mongo' 2import { MongoClient } from 'mongodb' 3 4;(async function() { 5 const { db } = await MongoClient.connect(connectionString) 6 const result = qs2m('name=john&age>21&fields=name,age&sort=name,-age&offset=10&limit=10') 7 const documents = await db('dbName') 8 .collection('collectionName') 9 .find(result.criteria, result.options) 10})().catch(console.log)
1qs2m(query: string, options: { 2 ignoredFields?: string | string[] 3 parser?: { 4 parse(query: string, options?: any): any 5 stringify(obj: object, options?: any): string 6 } 7 parserOptions?: object 8 dateFields?: string | string[] 9 objectIdFields?: string | string[] 10 fullTextFields?: string | string[] 11 parameters?: Partial<typeof defaultParameters> 12 maxLimit?: number 13})
ignoredFields
: array of query parameters that are ignored, in addition to default ones: "fields", "omit", "sort", "offset", "limit", "q";parser
: custom query parser, must implement parse(query: string, options?: any): any
and stringify(obj: object, options?: any): string
. The default parser is qs;parserOptions
: options to pass to the query parser;dateFields
: fields that will be converted to Date
. If no fields are passed, any valid date string will be converted to ISOString;objectIdFields
: fields that will be converted to ObjectId;fullTextFields
: fields that will be used as criteria when passing the q
query parameter;parameters
: override default parameters used as query options ("fields", "omit", "sort", "offset", "limit", "q"). For example: {fields:'$fields', omit:'$omit', sort:'$sort', offset:'$offset', limit:'$limit'};maxLimit
: maximum limit that could be passed to the limit
option.1{ 2 criteria: { 3 [key: string]: any 4 } 5 options: { 6 projection: { 7 [key: string]: 0 | 1 8 } 9 sort: { 10 [key: string]: 1 | -1 11 } 12 skip: number 13 limit: number 14 } 15 links: (url: string, totalCount: number) => { 16 prev: string 17 first: string 18 next: string 19 last: string 20 } | null 21}
links
method examples1import qs2m from 'qs-to-mongo' //or const qs2m = require('qs-to-mongo') 2const query = qs2m('name=john&age>21&offset=20&limit=10') 3query.links('http://localhost/api/v1/users', 100)
This will generate an object that could be used by the Express res.links(http://expressjs.com/en/4x/api.html#res.links) method.
1{ prev: 'http://localhost/api/v1/users?name=john&age%3E21=&offset=10&limit=10', 2 first: 'http://localhost/api/v1/users?name=john&age%3E21=&offset=0&limit=10', 3 next: 'http://localhost/api/v1/users?name=john&age%3E21=&offset=30&limit=10', 4 last: 'http://localhost/api/v1/users?name=john&age%3E21=&offset=90&limit=10' }
Any query parameters other than the special parameters fields, omit, sort, offset, limit, and q are interpreted as query criteria. For example name=john&age>21
results in a criteria value of:
{
'name': 'john',
'age': { $gt: 21 }
}
Number(value) != NaN
, are compared as numbers (i.e., field=10
yields {field:10}
).{field: true}
)dateFields
is passed. If not, they will be converted to Date ISOString.null
values are compared as null
. For example bar=null
yields {bar: null}
q
query parameter could be used to perform a full-text search on fields passed in the fullTextFields
argument.$in
operator. For example, id=a&id=b
yields {id:{$in:['a','b']}}
.$nin
operator. For example, id!=a&id!=b
yields {id:{$nin:['a','b']}}
.
Comma-separated values in equals or not-equals yield an $
inor
$ninoperator. For example,
id=a,byields
{id:{$in:['a','b']}}`.name=/^john/i
yields {id: /^john/i}
.foo&bar=10
yields {foo: {$exists: true}, bar: 10}
.!foo&bar=10
yields {foo: {$exists: false}, bar: 10}
.foo:type=string
, yeilds { foo: {$type: 'string} }
.field='10'
or field="10"
) would force a string compare. Allows for a string with an embedded comma (field="a,b"
) and quotes (field="that's all folks"
).Comparisons on embedded documents should use mongo's dot notation instead of qs (Use foo.bar=value
instead of foo[bar]=value
) 'extended' syntax.
Although exact matches are handled for either method, comparisons (such as foo[bar]!=value
) are not supported because the qs
parser expects an equals sign after the nested object reference; if it's not an equals, the remainder is discarded.
You can adjust the default parameters (fields, omit, sort, offset, limit and q) by providing an alternate set as an option. For example:
1const parameters = { 2 fields:'$fields', 3 omit:'$omit', 4 sort:'$sort', 5 offset:'$offset', 6 limit:'$limit', 7 q: '$q', 8} 9 10const query = q2m(res.query, { parameters: parameters });
This will then interpret the default parameters as query parameters instead of options. For example a query of age>21&omit=false&$omit=a
results in a criteria value of:
1query.criteria = { 2 'age': { $gt: 21 }, 3 'omit': false 4}
and an option value of:
1query.option = { 2 fields: { a: false } 3}
This module also takes parsed query as input, so that it could be used by Fastify or express routes without any further addition.
1const querystring = require('querystring') 2const qs2m = require('qs-to-mongo') 3const query = 'name=john&age>21&fields=name,age&sort=name,-age&offset=10&limit=10' 4const q = q2m(querystring.parse(query))
This makes it easy to use it in fastify route:
1fastify.get('/api/v1/mycollection', (req, reply) =>{ 2 const q = q2m(req.query); 3 ... 4}
or in express one:
1router.get('/api/v1/mycollection', function(req, res, next) { 2 const q = q2m(res.query); 3 ... 4}
The format and names for query parameters were inspired by this article about best practices for RESTful APIs.
This package started as a hard fork of https://github.com/pbatey/query-to-mongo. This is a TypeScript port, with some fixes and many improvements. However, this is not a drop-in replacement because of the changes to the public API.
null
and ObjectId
hex string valuesparserOptions
parameterfullTextFields
parameter)dateFields
parameterobjectIdFields
parameterkeywords
parameter to parameters
ignore
to ignoredFields
fields
to projection
in returnd mongo optionsMIT
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
GitHub workflow tokens follow principle of least privilege
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 8
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 1/14 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More