-
QuestionHi, I'm writing a system where my entities are closely coupled to entities in another external system which I can access through API calls. I need to partially mirror the data in my system to the external system. So for every created entity in my db I need to make a creation call, for every deletion the same etc. Thanks a lot for you time! How to reproduce (optional)No response Expected behavior (optional)No response Information about Prisma Schema, Client Queries and Environment (optional)No response |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hi @m1212e Prisma does not directly support side effects or hooks that would allow you to automatically trigger API calls on database operations like create, update, or delete. One way to do this is by using the repository pattern, as you mentioned. In this pattern, you would create a repository for each entity in your system. Each repository would contain the logic for interacting with the database using Prisma, as well as the logic for making the necessary API calls to the external system. For example: class UserRepository {
constructor(prismaClient, externalApiClient) {
this.prisma = prismaClient;
this.externalApi = externalApiClient;
}
async createUser(data) {
// Create user in your database
const user = await this.prisma.user.create({ data });
// Make API call to external system
await this.externalApi.createUser(user);
return user;
}
async deleteUser(id) {
// Delete user from your database
await this.prisma.user.delete({ where: { id } });
// Make API call to external system
await this.externalApi.deleteUser(id);
}
// ...other methods for updating, finding, etc...
} The |
Beta Was this translation helpful? Give feedback.
-
Hey @m1212e 👋 Just adding to what @ludralph has already mentioned, I think there are two other approaches that you can consider for this:
|
Beta Was this translation helpful? Give feedback.
Hi @m1212e
Prisma does not directly support side effects or hooks that would allow you to automatically trigger API calls on database operations like create, update, or delete.
One way to do this is by using the repository pattern, as you mentioned. In this pattern, you would create a repository for each entity in your system. Each repository would contain the logic for interacting with the database using Prisma, as well as the logic for making the necessary API calls to the external system. For example: