| Les deux révisions précédentes Révision précédente | |
| web:api:prisma-fastify:p2-b [2026/03/20 13:25] – jcheron | web:api:prisma-fastify:p2-b [2026/03/20 13:50] (Version actuelle) – [Factorisation] jcheron |
|---|
| Pour le CRUD : | Pour le CRUD : |
| <sxh ts> | <sxh ts> |
| class BaseService<T> { | type CrudModel = { |
| constructor(private model) {} | findMany: (args?: any) => any |
| | findUnique: (args: any) => any |
| | create: (args: any) => any |
| | update: (args: any) => any |
| | delete: (args: any) => any |
| | } |
| |
| findAll() { | class BaseService<T extends CrudModel> { |
| return this.model.findMany() | constructor(private model: T) {} |
| } | |
| |
| findById(id: string) { | findAll() { |
| return this.model.findUnique({ where: { id } }) | return this.model.findMany() |
| } | } |
| |
| create(data: any) { | findById(id: string) { |
| return this.model.create({ data }) | return this.model.findUnique({ where: { id } }) |
| } | } |
| |
| update(id: string, data: any) { | create(data: any) { |
| return this.model.update({ | return this.model.create({ data }) |
| where: { id }, | } |
| data | |
| }) | |
| } | |
| |
| delete(id: string) { | update(id: string, data: any) { |
| return this.model.delete({ | return this.model.update({ |
| where: { id } | where: { id }, |
| }) | data |
| } | }) |
| | } |
| | |
| | delete(id: string) { |
| | return this.model.delete({ |
| | where: { id } |
| | }) |
| | } |
| } | } |
| </sxh> | </sxh> |