Skip to content

Commit

Permalink
Merge pull request #19 from ps73/prisma-v5
Browse files Browse the repository at this point in the history
updated codebase to support prisma v5
  • Loading branch information
ps73 authored Feb 11, 2024
2 parents 242d3c4 + b083b19 commit 018ba14
Show file tree
Hide file tree
Showing 15 changed files with 402 additions and 127 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ npm install feathers-prisma --save

## Documentation

This adapter supports all methods (`create`, `delete`, `update`, `patch`, `find`, `get`) and the common way for querying (`equality`, `$limit`, `$skip`, `$sort`, `$select`, `$in`, `$nin`, `$lt`, `$lte`, `$gt`, `$gte`, `$ne`, `$or`, `$and`). Also supports eager loading (`$eager`), full-text search (`$search`) and prisma filtering (`$rawWhere`).
This adapter supports all methods (`create`, `delete`, `update`, `patch`, `find`, `get`) and the common way for querying (`equality`, `$limit`, `$skip`, `$sort`, `$select`, `$in`, `$nin`, `$lt`, `$lte`, `$gt`, `$gte`, `$ne`, `$or`, `$and`). Also supports eager loading (`$eager`), full-text search (`$search`) and prisma filtering (from 0.7.0 on with `$prisma`, previously with `$rawWhere` which is now deprecated).

## Prisma Version

- Prisma v3 use `feathers-prisma` v0.6.0
- Prisma v5 use `feathers-prisma` v0.7.0 or higher

### Setup

Expand Down Expand Up @@ -87,15 +92,17 @@ app.service("messages").find({

Since 0.5.0 it is possible to use default prisma filters. This makes it possible to [filter JSON](https://www.prisma.io/docs/concepts/components/prisma-client/working-with-fields/working-with-json-fields) fields or to [filter relations](https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#relation-filters).

The `$rawWhere` property **has to be** set in the `whitelist` option parameter. Otherwise the service will throw an error.
The `$prisma` property **has to be** set in the `whitelist` option parameter. Otherwise the service will throw an error.

Since 0.7.0 use the $prisma property to filter instead of using the $rawWhere property.

```js
app.use(
"/messages",
service(
{
model: "message",
whitelist: ["$rawWhere"],
whitelist: ["$prisma"],
},
prismaClient
)
Expand All @@ -104,7 +111,7 @@ app.use(
app.service("messages").find({
query: {
recipients: {
$rawWhere: {
$prisma: {
some: {
userId: 1,
},
Expand Down
4 changes: 4 additions & 0 deletions dist/constants.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export declare const OPERATORS_MAP: {
$nin: string;
$ne: string;
$eager: string;
/**
* @deprecated use $prisma instead
*/
$rawWhere: string;
$prisma: string;
$contains: string;
$search: string;
$startsWith: string;
Expand Down
4 changes: 4 additions & 0 deletions dist/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ exports.OPERATORS_MAP = {
$ne: 'not',
$eager: 'includes',
// specific to prisma
/**
* @deprecated use $prisma instead
*/
$rawWhere: 'rawWhere',
$prisma: 'prisma',
$contains: 'contains',
$search: 'search',
$startsWith: 'startsWith',
Expand Down
9 changes: 5 additions & 4 deletions dist/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.errorHandler = void 0;
const errors = require("@feathersjs/errors");
const index_1 = require("@prisma/client/runtime/index");
const library_1 = require("@prisma/client/runtime/library");
function getType(v) {
let type = '';
const cases = {
common: v >= 1000 && v < 2000,
query: v >= 2000 && v < 3000,
migration: v >= 3000 && v < 4000,
introspection: v >= 4000 && v < 4000,
introspection: v >= 4000 && v < 5000,
};
Object.keys(cases).map((key) => {
// @ts-ignore
Expand All @@ -25,7 +25,7 @@ function errorHandler(error, prismaMethod) {
if (error instanceof errors.FeathersError) {
feathersError = error;
}
else if (error instanceof index_1.PrismaClientKnownRequestError) {
else if (error instanceof library_1.PrismaClientKnownRequestError) {
const { code, meta, message, clientVersion, } = error;
const errType = getType(Number(code.substring(1)));
switch (errType) {
Expand All @@ -50,11 +50,12 @@ function errorHandler(error, prismaMethod) {
break;
}
}
else if (error instanceof index_1.PrismaClientValidationError) {
else if (error instanceof library_1.PrismaClientValidationError) {
switch (prismaMethod) {
case 'findUnique':
case 'remove':
case 'update':
case 'delete':
feathersError = new errors.NotFound('Record not found.');
break;
default:
Expand Down
4 changes: 4 additions & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export interface QueryParamRecordFilters {
$gte?: string | number;
$ne?: string | boolean | number;
$eager?: EagerQuery;
/**
* @deprecated use $prisma instead
*/
$rawWhere?: Record<string, any>;
$prisma?: Record<string, any>;
$contains?: string;
$search?: string;
$startsWith?: string;
Expand Down
2 changes: 1 addition & 1 deletion dist/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const castFeathersQueryToPrismaFilters = (p, whitelist) => {
if (Array.isArray(value)) {
filters[prismaKey] = value.map((v) => (0, exports.castToNumberBooleanStringOrNull)(v));
}
else if (key === '$rawWhere' && typeof value === 'object') {
else if (key === '$rawWhere' && typeof value === 'object' || key === '$prisma' && typeof value === 'object') {
Object.keys(value).forEach((rawKey) => {
filters[rawKey] = value[rawKey];
});
Expand Down
Loading

0 comments on commit 018ba14

Please sign in to comment.