Giter Club home page Giter Club logo

feathers-prisma's Introduction

feathers-prisma

libraries.io Code Climate Test Coverage npm

A Feathers service adapter for Prisma ORM.

Installation

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).

Setup

import feathers from "@feathersjs/feathers";
import { service } from "feathers-prisma";
import { PrismaClient } from "@prisma/client";

// Initialize the application
const app = feathers();

// Initialize the plugin
const prismaClient = new PrismaClient();
prismaClient.$connect();
app.set("prisma", prismaClient);

const paginate = {
  default: 10,
  max: 50,
};

app.use(
  "/messages",
  service(
    {
      model: "messages",
      paginate,
      multi: ["create", "patch", "remove"],
      whitelist: ["$eager"],
    },
    prismaClient
  )
);

Eager Loading / Relation Queries

Relations can be resolved via $eager property in your query. It supports also deep relations. The $eager property has to be set in the whitelist option parameter. Otherwise the service will throw an error.

app.use(
  "/messages",
  service(
    {
      model: "message",
      whitelist: ["$eager"],
    },
    prismaClient
  )
);
// will load the recipients with the related user
// as well as all attachments  of the messages
app.service("messages").find({
  query: {
    $eager: [["recipients", ["user"]], "attachments"],
  },
});
// selecting specific fields is also supported since 0.4.0
app.service("messages").find({
  query: {
    $eager: {
      recipients: ["receivedAt", "user"],
    },
  },
});

Filter with default prisma filters

Since 0.5.0 it is possible to use default prisma filters. This makes it possible to filter JSON fields or to filter relations.

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

app.use(
  "/messages",
  service(
    {
      model: "message",
      whitelist: ["$rawWhere"],
    },
    prismaClient
  )
);
// will load all messages where at least one of the recipients userIds is equal 1
app.service("messages").find({
  query: {
    recipients: {
      $rawWhere: {
        some: {
          userId: 1,
        },
      },
    },
  },
});

Batch requests

This adapter supports batch requests. This is possible by allowing this in the multi property in the service options. Supported methods are create, patch and delete.

app.use(
  "/messages",
  service(
    {
      model: "messages",
      multi: ["create", "patch", "delete"],
    },
    prismaClient
  )
);

app.service("messages").create([{ body: "Lorem" }, { body: "Ipsum" }]);

Full-Text Search

Prisma supports a full-text search which is currently in preview mode. Find out more how to activate it here. If you activated it through your schema you have to allow it in the whitelist property:

app.use(
  "/messages",
  service(
    {
      model: "messages",
      whitelist: ["$search"],
    },
    prismaClient
  )
);

app.service("messages").find({
  query: {
    body: {
      $search: "hello | hola",
    },
  },
});

Complete Example

Here's an example of a Feathers server that uses feathers-prisma.

import feathers from "@feathersjs/feathers";
import { service } from "feathers-prisma";

// Initialize the application
const app = feathers();

// Initialize the plugin
const prismaClient = new PrismaClient();
prismaClient.$connect();
app.set("prisma", prismaClient);

const paginate = {
  default: 10,
  max: 50,
};

app.use(
  "/messages",
  service(
    {
      model: "messages",
      paginate,
      multi: ["create", "patch", "remove"],
      whitelist: ["$eager"],
    },
    prismaClient
  )
);
// Or if you want to extend the service class
import { PrismaService } from "feathers-prisma";

License

Copyright (c) 2021.

Licensed under the MIT license.

feathers-prisma's People

Contributors

dependabot[bot] avatar ps73 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.