Security News
The Dark Side of Open Source
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
@celleb/auto-query
Advanced tools
Readme
Restful query builder for mongodb with mongoose using your url query
npm i @celleb/auto-query
import { Schema } from 'mongoose';
const userSchema = new Schema({
name: String,
surname: String,
likes: [String],
car: [
{
model: String,
year: Number,
},
],
activities: [
{
name: String,
},
],
});
The dictionary is used to transform the api fields to the database fields. If the fields are the same then you can exclude it from the dictionary. The dictionary is POJO with string for both keys and values.
For example {apiField: 'databaseField' }
// shallow/flat dictionary/map
const dictionary = {
firstName: 'name',
lastName: 'surname',
'vehicle.model': 'car.model',
'vehicle.year': 'car.year',
};
The AutoQuery
takes a model and a dictionary.
import { AutoQuery } from '@celleb/auto-query';
import mongoose from 'mongoose';
const User = mongoose.model('User', userSchema);
const qb = new AutoQuery(User, dictionary);
Call the query builder's .build
method with the request query.
The build method returns a Mongoose
query and you can chain other methods before calling .exec()
.
async function routHandler(req: Request, res: Response) {
return res.json(await qb.build(req.query).exec());
}
The following are fields support on the query
interface QueryParams = {
match?: Record<string, string|number|Array<string|number>>;
sort?: string;
skip?: number;
limit?: number;
select?: string[];
}
For example: url?match[firstName]=Jonas&sort=firstName&skip=0&limit=10&select=firstName&select=lastName
.
You decide how you encode and decode your url query but the decoded query must match the Query Parameter Interface above.
Allows you to query the database using specific fields and operators.
Symbol | Description | Usage |
---|---|---|
= | Equal to or [in]. Do not add an additional equal sign | url?match[firstName]=Jonas or with array url?match[firstName]=Jonas,Jon ' |
! | Not equal to to or not in [nin]. | url?match[firstName]=!Jonas or with array url?match[likes]=Football,!Tennis ' |
>: | Greater than or equal | url?match[vehicle.year]=>:2017 |
> | Greater than | url?match[vehicle.year]=>2017 |
<: | Less than or equal | url?match[vehicle.year]=<:2017 |
< | Less than or equal | url?match[vehicle.year]=<2017 |
More operations will be added in the future
Specifies the field and the order by which to sort the results.
Use sort?=-fieldName
for descending order and sort=fieldName
for ascending order.
Specifies the number of records to skip in the database.
For example skip=10
skips the first 10 records.
Limits the number of matching records returned.
Example limit=10
returns the first 10 results.
FAQs
Restful query builder for mongodb with mongoose
The npm package @celleb/auto-query receives a total of 0 weekly downloads. As such, @celleb/auto-query popularity was classified as not popular.
We found that @celleb/auto-query demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At Node Congress, Socket CEO Feross Aboukhadijeh uncovers the darker aspects of open source, where applications that rely heavily on third-party dependencies can be exploited in supply chain attacks.
Research
Security News
The Socket Research team found this npm package includes code for collecting sensitive developer information, including your operating system username, Git username, and Git email.
Security News
OpenJS is warning of social engineering takeovers targeting open source projects after receiving a credible attempt on the foundation.