@zola_do/crud 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +12 -7
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -156,7 +156,9 @@ Delete hook compatibility is preserved:
156
156
  - New alias: `beforeDelete` / `afterDelete`
157
157
  - If both are configured, both run (legacy first, then alias)
158
158
 
159
- Hooks receive operation context with request and mutable payload/filter data:
159
+ Hooks receive operation context with request and mutable payload/filter data.
160
+ `service` is the injected CRUD service instance (for example, your subclass of
161
+ `EntityCrudService`), so hook code can call custom service methods directly:
160
162
 
161
163
  ```typescript
162
164
  const options: EntityCrudOptions = {
@@ -166,30 +168,33 @@ const options: EntityCrudOptions = {
166
168
  if (!req?.user && operation !== 'findAll') return false;
167
169
  },
168
170
 
169
- beforeCreate: async ({ req, params, itemData }) => {
171
+ beforeCreate: async ({ service, req, params, itemData }) => {
170
172
  const userId = req?.user?.sub ?? req?.user?.id;
171
173
  const eventId = params?.eventId;
172
174
  if (!userId || !eventId) throw new UnauthorizedException();
173
175
 
174
- await policies.assertCanEditEvent(eventId, userId);
176
+ await service.assertCanEditEvent(eventId, userId);
175
177
  itemData.eventId = eventId;
176
178
  itemData.status ??= 'pending';
177
179
  },
178
180
 
179
- beforeFindAll: async ({ req, params, where }) => {
181
+ beforeFindAll: async ({ service, req, params, where }) => {
180
182
  const userId = req?.user?.sub ?? req?.user?.id;
181
- await policies.assertCanAccessEvent(params?.eventId, userId);
183
+ await service.assertCanAccessEvent(params?.eventId, userId);
182
184
  where.eventId = params?.eventId;
183
185
  },
184
186
 
185
- canUpdate: async ({ req, params }) => {
187
+ canUpdate: async ({ service, req, params }) => {
186
188
  const userId = req?.user?.sub ?? req?.user?.id;
187
- await policies.assertCanEditEvent(params?.eventId, userId);
189
+ await service.assertCanEditEvent(params?.eventId, userId);
188
190
  return true;
189
191
  },
190
192
  };
191
193
  ```
192
194
 
195
+ When using this pattern, define those methods in your app service class that
196
+ extends `EntityCrudService<T>` (or `ExtraCrudService<T>`).
197
+
193
198
  Hook context includes:
194
199
 
195
200
  - `req`, `params`, `query`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zola_do/crud",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Generic CRUD controllers and services for NestJS",
5
5
  "author": "zolaDO",
6
6
  "license": "ISC",