Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

prisma-query

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

prisma-query - npm Package Compare versions

Comparing version 1.1.2 to 1.1.3

.parcel-cache/4b4e63ff349d6a2b

4

.parcel-cache/748e64b1ec39ac27.txt

@@ -1,2 +0,2 @@

28959825
1667532955508666000
32653770
1667680254631936000

@@ -15,3 +15,7 @@ type TQueryModifier = {

};
export type QueryModifier<T> = {
numericValues?: (keyof T)[] | undefined;
booleanValues?: (keyof T)[] | undefined;
};
//# sourceMappingURL=types.d.ts.map
{
"name": "prisma-query",
"version": "1.1.2",
"version": "1.1.4",
"description": "transforms rest query params to prisma model args",

@@ -5,0 +5,0 @@ "source": "src/index.ts",

@@ -5,2 +5,103 @@ # prisma-query

## Usage
### processFindAllQuery
```typescript
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
}
*/
```
### processFindOneQuery
```typescript
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",
},
},
},
},
}
*/
```
### QueryModifier
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
```typescript
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));
```
## Examples

@@ -93,2 +194,9 @@

// 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': {

@@ -95,0 +203,0 @@ where: { eventId: 1, fans: { gt: 21000 } },

@@ -5,1 +5,6 @@ export {

} from 'utils/processQueryUtils';
export type QueryModifier<T> = {
numericValues?: (keyof T)[] | undefined;
booleanValues?: (keyof T)[] | undefined;
};

@@ -1,5 +0,2 @@

export type QueryModifier<T> = {
numericValues?: (keyof T)[] | undefined;
booleanValues?: (keyof T)[] | undefined;
};
import { QueryModifier } from 'index';

@@ -6,0 +3,0 @@ /**

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc