https://github.com/marshallswain/feathers-prisma
A working, minimal Prisma adapter for Feathers.
https://github.com/marshallswain/feathers-prisma
Last synced: 4 months ago
JSON representation
A working, minimal Prisma adapter for Feathers.
- Host: GitHub
- URL: https://github.com/marshallswain/feathers-prisma
- Owner: marshallswain
- Created: 2022-06-22T21:03:35.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-22T21:10:13.000Z (almost 3 years ago)
- Last Synced: 2025-01-01T00:19:22.140Z (6 months ago)
- Language: TypeScript
- Size: 203 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# feathers-prisma
[](https://github.com/feathersjs-ecosystem/feathers-prisma/actions?query=workflow%3ACI)
[](https://david-dm.org/feathersjs-ecosystem/feathers-prisma)
[](https://www.npmjs.com/package/feathers-prisma)A [Feathers](https://feathersjs.com) database adapter for [Prisma](https://www.prisma.org/) using the Prisma Client.
> Note: There may be better options than this adapter, by now. My goal was to build a clean adapter for Prisma to try out Prisma 2. I have since determined that I will not be using Prisma. I've published it as a reference and to not lose the code.
## NOT PUBLISHED TO npm
This was never published to npm, so it can't be installed. The feathers-prisma package already on npm is not this one.
```bash
$ npm install --save prisma feathers-prisma
```> __Important:__ `feathers-prisma` implements the [Feathers Common database adapter API](https://docs.feathersjs.com/api/databases/common.html) and [querying syntax](https://docs.feathersjs.com/api/databases/querying.html).
## API
### `service(options)`
Returns a new service instance initialized with the given options. `Model` has to be a Prisma collection.
```js
const MongoClient = require('prisma').MongoClient;
const service = require('feathers-prisma');MongoClient.connect('prisma://localhost:27017/feathers').then(client => {
app.use('/messages', service({
Model: client.db('feathers').collection('messages')
}));
app.use('/messages', service({ Model, id, events, paginate }));
});
```__Options:__
- `Model` (**required**) - The Prisma Client instance
### params.prisma
When making a [service method](https://docs.feathersjs.com/api/services.html) call, `params` can contain an `prisma` property (for example, `{upsert: true}`) which allows to modify the options used to run the Prisma query.
#### Transactions
## Example
Here is an example of a Feathers server with a `messages` endpoint that writes to the `feathers` database and the `messages` collection.
```cli
npm install @feathersjs/feathers @feathersjs/errors @feathersjs/express @feathersjs/socketio feathers-prisma prisma
```In `app.js`:
```js
const feathers = require('@feathersjs/feathers');
const express = require('@feathersjs/express');
const socketio = require('@feathersjs/socketio');const MongoClient = require('prisma').MongoClient;
const service = require('feathers-prisma');// Create an Express compatible Feathers application instance.
const app = express(feathers());
// Turn on JSON parser for REST services
app.use(express.json());
// Turn on URL-encoded parser for REST services
app.use(express.urlencoded({extended: true}));
// Enable REST services
app.configure(express.rest());
// Enable Socket.io
app.configure(socketio());// Connect to the db, create and register a Feathers service.
app.use('/messages', service({
paginate: {
default: 2,
max: 4
}
}));// A basic error handler, just like Express
app.use(express.errorHandler());// Connect to your Prisma instance(s)
MongoClient.connect('prisma://localhost:27017/feathers')
.then(function(client){
// Set the model now that we are connected
app.service('messages').Model = client.db('feathers').collection('messages');// Now that we are connected, create a dummy Message
app.service('messages').create({
text: 'Message created on server'
}).then(message => console.log('Created message', message));
}).catch(error => console.error(error));// Start the server.
const port = 3030;app.listen(port, () => {
console.log(`Feathers server listening on port ${port}`);
});
```Run the example with `node app` and go to [localhost:3030/messages](http://localhost:3030/messages).
## Querying
### Example: Find records with a case-insensitive search
## License
Copyright (c) 2019
Licensed under the [MIT license](LICENSE).