@tsdiapi/prisma-rest-client
TypeScript client for working with Prisma REST API generated by the @tsdiapi/prisma-rest plugin for the @tsdiapi/server framework.
Demo
https://tsdiapi.com/prisma-rest-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 (server)
See @tsdiapi/server for installation instructions.
tsdiapi create app
cd app
tsdiapi add prisma-rest
Client
- Install the client
npm install prisma @prisma/client @tsdiapi/prisma-rest-client
- Copy prisma.schema from server project to client project
- Generate the Prisma client
npx prisma generate
Usage Example
// Get the Prisma client (types only)
import { PrismaClient } from '@prisma/client';
// Get the Prisma REST 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 @tsdiapi/prisma-rest plugin provides the following security mechanisms:
- 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
Testing
The repository includes an example HTML page for testing the client. Open https://tsdiapi.com/prisma-rest-client in your browser after building.
License
MIT License (see LICENSE)