prisma-query
Advanced tools
Comparing version 1.1.2 to 1.1.3
@@ -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", |
108
README.md
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 6 instances in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4492971
60
1617
205
12