@tsdiapi/prisma-rest
A TSDIAPI plugin that provides dynamic REST API access to your Prisma models with built-in security controls.
📌 About
This is a TSDIAPI plugin that provides a single dynamic endpoint to access your Prisma models through REST API. It includes built-in security features and access control.
🔗 TSDIAPI CLI: @tsdiapi/cli
📦 Installation
You can install this plugin using npm:
tsdiapi add prisma-rest
Then, register the plugin in your TSDIAPI project:
import { createApp } from "@tsdiapi/server";
import createPlugin from "@tsdiapi/prisma-rest";
createApp({
plugins: [createPlugin({ enabled: true })],
});
🚀 Features
- 🔄 Dynamic Endpoint - Single endpoint for all Prisma operations
- 🔒 JWT Authentication - Built-in JWT guard support
- 🌐 IP Restrictions - Control access based on IP addresses
- 🎯 Method Control - Enable/disable specific Prisma methods
- 🏷️ Model Selection - Choose which models to expose through the API
- 🔍 Request Filtering - Modify or validate request data before processing
- 🎛️ Model-Specific Configuration - Fine-grained control over methods and IPs per model
📱 Prisma REST Client
A TypeScript client for working with Prisma REST API generated by this plugin.
🔗 NPM Package: @tsdiapi/prisma-rest-client
🔗 Demo: https://tsdiapi.com/prisma-rest-client
Client Features
- Full type safety using Prisma types
- Automatic model and method validation
- Support for all Prisma methods (findMany, findUnique, create, update, delete, etc.)
- JWT authentication
- Error handling
- Minified bundle for browser usage
Installation
npm install prisma @prisma/client @tsdiapi/prisma-rest-client
Usage Example
// Get the Prisma client (types only)
import { PrismaClient } from '@prisma/client';
import { PrismaRestClient } from '@tsdiapi/prisma-rest-client';
// Create client instance with the Prisma client
const instance = new PrismaRestClient<PrismaClient>({
apiUrl: "http://localhost:3100/api/prisma/v1",
token: "your-jwt-token"
});
// Get typed client
const client = instance.useClient();
// Use like a regular Prisma client
async function main() {
// Get list of users
const users = await client.user.findMany({
where: {
email: {
contains: '@example.com'
}
},
select: {
id: true,
email: true,
name: true
}
});
// Create new user
const newUser = await client.user.create({
data: {
email: 'user@example.com',
name: 'John Doe'
}
});
// Update user
const updatedUser = await client.user.update({
where: { id: 'user-id' },
data: { name: 'Jane Doe' }
});
// Delete user
await client.user.delete({
where: { id: 'user-id' }
});
}
main().catch(console.error);
Security
The client works with the following security mechanisms provided by the plugin:
- Model access restrictions
- Method access restrictions
- IP address restrictions
- JWT authentication
It is recommended to configure these restrictions according to your security requirements.
Development
For development and testing:
- Clone the repository
- Install dependencies:
npm install
- Run the build:
npm run build
- For development with auto-rebuild:
npm run dev
The repository includes an example HTML page for testing the client. Open https://tsdiapi.com/prisma-rest-client in your browser after building.
🔧 Configuration
This plugin can be configured through environment variables or during initialization:
createPlugin({
enabled: true,
// Global filter function
filter: async (model, method, request, req) => {
// Modify or validate request data
return request;
},
// Model-specific configurations
access: {
User: {
allowedMethods: ['findMany', 'findUnique'],
allowedIps: ['127.0.0.1'],
filter: async (model, method, request, req) => {
// Model-specific request filtering
return request;
}
}
}
});
Configuration Options
Variable | Type | Default | Description |
---|
PRISMA_REST_ENABLED | boolean | true | Enable/disable the REST API |
PRISMA_REST_METHODS | string | "*" | Comma-separated list of allowed Prisma methods |
PRISMA_REST_MODELS | string | "*" | Comma-separated list of models to expose |
PRISMA_REST_ALLOWED_IPS | string | "127.0.0.1,::1" | Comma-separated list of allowed IP addresses |
PRISMA_REST_GUARD | string | "admin" | Guard name for JWT authentication |
Advanced Configuration
Request Filtering
You can add a global filter function or model-specific filters to modify or validate request data:
createPlugin({
// Global filter for all models
filter: async (model, method, request, req) => {
// Add timestamp to all requests
return {
...request,
timestamp: new Date().toISOString()
};
},
access: {
User: {
// Model-specific filter
filter: async (model, method, request, req) => {
// Validate user creation
if (method === 'create' && !request.email) {
throw new Error('Email is required for user creation');
}
return request;
}
}
}
});
Model-Specific Configuration
You can configure allowed methods and IP addresses for specific models:
createPlugin({
access: {
User: {
allowedMethods: ['findMany', 'findUnique'], // Only allow read operations
allowedIps: ['127.0.0.1', '192.168.1.1'] // Restrict access to specific IPs
},
Post: {
allowedMethods: ['create', 'update'], // Only allow write operations
allowedIps: ['*'] // Allow all IPs
}
}
});
Filter Function Parameters
The filter function receives the following parameters:
model
: The Prisma model name
method
: The Prisma method being called
request
: The request body data
req
: The full request object with state
The filter function can:
- Modify the request data
- Validate the request data
- Throw errors to prevent invalid operations
- Access request state and headers
📌 How to Use
The plugin provides a single dynamic endpoint:
POST /api/v1/prisma/:method/:model
Example Usage:
// Create a new user
POST /api/v1/prisma/create/user
{
"name": "John Doe",
"email": "john@example.com"
}
// Find users
POST /api/v1/prisma/findMany/user
{
"where": {
"email": "john@example.com"
}
}
// Update user
POST /api/v1/prisma/update/user
{
"where": {
"id": 1
},
"data": {
"name": "John Smith"
}
}
Authentication
All requests require JWT authentication with the specified guard.
Error Responses
- 400: Invalid method, model, or request data
- 403: IP not allowed or authentication failed
- 500: Server error
🔗 Related Plugins
You can find more TSDIAPI plugins here:
🔗 Available Plugins
👨💻 Contributing
Contributions are always welcome! If you have ideas for improving this plugin, feel free to open a pull request.
Author: unbywyd
GitHub Repository: https://github.com/unbywyd/tsdiapi-prisma-rest
📧 Contact: unbywyd@gmail.com
🚀 Happy coding with TSDIAPI! 🎉