Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
prisma-query
Advanced tools
convert query params to prisma args
const req = {
query: {
id: 1,
likes_gt: 1000,
_sort: 'likes',
_expand: 'comments',
_page: 9,
_limit: 10,
},
};
console.log(processFindAllQuery(req.query));
/*
{
where: { id: 1, likes: { gt: 1000 } },
include: { comments: true },
orderBy: [ { likes: 'asc' } ],
skip: 80,
take: 10
}
*/
import { processFindOneQuery } from 'prisma-query';
const req = {
query: { _expand: 'comments', 'comments.likes_gte': '10' },
};
console.log(processFindOneQuery(req.query));
/*
{
include: {
comments: {
where: {
likes: {
gte: "10",
},
},
},
},
}
*/
when passing numeric values in query, distinction can't be made between numeric or string values, we have two ways to solve for this
First ->
in case of number, we can wrap the value in num() function like ?id=num(12)
in case of boolean, we can wrap the value in bool() function like ?vip=bool(true)
Second ->
we can define queryModifier for a the model and pass it as second argument of processFindAllQuery or processFindOneQuery
NOTE: this will work only for first level, for filters in nested models wrapping with num() and bool() are necessary
import { processFindAllQuery, QueryModifier } from 'prisma-query';
/**
* Model Guest
*
*/
export type Guest = {
id: number;
fans: number;
name: string;
vip: boolean;
createdAt: Date;
updatedAt: Date;
eventId: number;
eventSignupId: number | null;
};
const guestQueryModifier: QueryModifier<Guest> = {
numericValues: ['id', 'fans', 'eventId', 'eventSignupId'],
booleanValues: ['vip'],
};
const req = {
query: {
eventId: '1',
fans_gt: '1000',
vip: 'true',
_expand: 'comments',
'comments.likes_gt': 'num(1000)', // if num() not specified, then {gt: '1000'}
},
};
console.log(processFindAllQuery(req.query));
const routeToFindManyArgs = {
'/events?id=6&id=7': { where: { id: { in: [6, 7] } } },
// expand guests for John
'/events?_expand=guests&guests.name_like=John': {
include: {
guests: {
where: { name: { contains: 'John', mode: 'insensitive' } },
},
},
},
// expand guests whose vip is false
'/events?_expand=guests&guests.vip=bool(false)': {
include: { guests: { where: { vip: false } } },
},
// below means get all events for which host name is like Hitesh
// filter for hosts didn't got transferred since _expand is after filter
'/events?hosts.every.name_like=Hitesh&_expand=hosts': {
where: {
hosts: { every: { name: { contains: 'Hitesh', mode: 'insensitive' } } },
},
include: {
hosts: true,
},
},
// in below case we are expanding hosts (before applying the filter), so it is interpreted as get all the events
// and get all hosts whose name is like Hitesh (filter got transferred to child)
'/events?_expand=hosts&hosts.name_like=Hitesh': {
include: {
hosts: { where: { name: { contains: 'Hitesh', mode: 'insensitive' } } },
},
},
// multiple filters for nested models
'/events?_expand=guests&guests.name_like=Rahul&guests.vip=bool(true)': {
include: {
guests: {
where: {
vip: true,
name: { contains: 'Rahul', mode: 'insensitive' },
},
},
},
},
// ne -> not equal
'/guests?eventSignupId_ne=null': {
where: { eventSignupId: { not: null } },
},
// expand multiple models for nested model
'/eventCategories?_expand=events.hosts&_expand=events.eventMetadata': {
include: {
events: { include: { eventMetadata: true, hosts: true } },
},
},
'/events?_sort=startTime&_sort=id&_order=asc': {
orderBy: [{ startTime: 'asc' }, { id: 'asc' }],
},
// id by default took asc, (, separation is supported for both _sort and _order)
'/events?_sort=duration,id&_order=desc': {
orderBy: [{ duration: 'desc' }, { id: 'asc' }],
},
'/guests?_page=2&_limit=5': { skip: 5, take: 5 },
// _start and _end work like indexes
'/guests?_start=0&_end=10': {
skip: 0,
take: 10,
},
// for nested numbers wrap number inside num()
// although that is not required at first level, since that is taken care off by queryModifier see below example
'/events?_expand=guests&guests.fans_gt=num(21000)': {
include: { guests: { where: { fans: { gt: 21000 } } } },
},
// fans is treated as number already because of guestQueryModifier
/*
const guestQueryModifier: QueryModifier<Guest> = {
numericValues: ['id', 'fans', 'eventId', 'eventSignupId'],
booleanValues: ['vip'],
};
*/
// so need of writing fans_gt=num(21000)
'/guests?eventId=1&fans_gt=21000': {
where: { eventId: 1, fans: { gt: 21000 } },
},
};
FAQs
transforms rest query params to prisma model args
The npm package prisma-query receives a total of 0 weekly downloads. As such, prisma-query popularity was classified as not popular.
We found that prisma-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
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.