Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
@celleb/auto-query
Advanced tools
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
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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.