You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
details: feathers-utils is the successor of feathers-hooks-common, providing a modern and improved set of hooks and utilities for FeathersJS applications.
21
+
link: /why
22
+
- title: Pluggable hooks and utils
23
+
details: feathers-utils provides a set of common hooks and utilities that can be used across FeathersJS applications.
24
+
- title: TypeScript support
25
+
details:
26
+
feathers-utils is written in TypeScript, providing type definitions and support for better development
27
+
experience.
28
+
- title: Community-driven
29
+
details: feathers-utils is maintained by the FeathersJS community, ensuring that it stays up-to-date with the latest FeathersJS features and best practices.
@@ -6,7 +6,7 @@ This document provides a guide for migrating from `feathers-hooks-common` to `fe
6
6
7
7
The migration from 'feathers-hooks-common' to 'feathers-utils' is not a 1:1 mapping. Some hooks have been removed, some have been replaced with new hooks, and some utilities have been added or modified. This document will help you understand the changes and how to adapt your code accordingly. We recommend to migrate gradually hook by hook and utility by utility.
8
8
9
-
In the following sections, we will cover the changes in detail. You can browse this migration guide and search for the hooks and utilities you are using in your codebase. If you have any questions or need help with the migration, please reach out to us in this [github issue](https://github.com/feathersjs/feathers-utils/issues/1).
9
+
In the following sections, we will cover the changes in detail. You can browse this migration guide and search for the hooks and utilities from 'feathers-hooks-common' you are using in your codebase. If you have any questions or need help with the migration, please reach out in this [github issue](https://github.com/feathersjs/feathers-utils/issues/1).
10
10
11
11
## `actOn`, `actOnDefault`, and `actOnDispatch`
12
12
@@ -16,6 +16,28 @@ The `actOn`, `actOnDefault`, and `actOnDispatch` hooks have been removed. But we
16
16
17
17
The hook `alterItems` was removed and replaced with the more explicit [`transformData`](/hooks/transform-data.html) and [`transformResult`](/hooks/transform-result.html) hooks. These hooks allow you to transform the data or result of a hook call, respectively.
18
18
19
+
The signature of the new hooks differ a little bit. The second argument of the transformer function is now an `options` object instead of the `context`.
In [`transformResult`](/hooks/transform-result.html) you have a third argument of type `MutateResultOptions` in which you set the `{ dispatch: true }` (transform items in `context.dispatch`) or `{ dispatch: 'both' }` option (transform items in `context.result` and `context.dispatch`).
40
+
19
41
## `cache`
20
42
21
43
The `cache` hook is completely rewritten. Its implementation is heavily inspired by [`contextCache` from 'feathers-fletching'](https://daddywarbucks.github.io/feathers-fletching/#/./hooks?id=contextcache) but has a few differences.
@@ -42,6 +64,26 @@ Nowadays it's recommended to use [resolvers](https://feathersjs.com/api/schema/r
42
64
43
65
The `disallow` hook has been updated to use a more explicit syntax. Instead of using a spread argument, you can now pass a single string or an array of field names to specify which fields should not be allowed in the data or result of a hook call. This change improves clarity and consistency in how disallowed fields are specified.
44
66
67
+
```ts
68
+
// old
69
+
import { disallow } from"feathers-hooks-common";
70
+
71
+
app.service("my-service").hooks({
72
+
before: {
73
+
all: [disallow("field1", "field2")],
74
+
},
75
+
});
76
+
77
+
// new
78
+
import { disallow } from"feathers-utils/hooks";
79
+
80
+
app.service("my-service").hooks({
81
+
before: {
82
+
all: [disallow(["field1", "field2"])],
83
+
},
84
+
});
85
+
```
86
+
45
87
## `discard`
46
88
47
89
The `discard` hook has been removed. Instead you can use [`transformData`](/hooks/transform-data.html) or [`transformResult`](/hooks/transform-result.html) with the [`omit transformer`](/transformers/omit.html).
The `discardQuery` hook has been removed. Instead you can use `transformQuery` with the `omit` transformer.
68
110
@@ -186,6 +228,16 @@ Nowadays it's recommended to use [resolvers](https://feathersjs.com/api/schema/r
186
228
187
229
The arguments for `preventChanges` have been simplified. Instead of using a boolean as the first argument, you can now pass an options object as the second argument. This allows for more flexibility and clarity in how you specify the fields to prevent changes on.
The `replaceItems` utility has been removed. It had too much magic. Based on `context.type` it replaced `context.data` or `context.result`. This does not play well with `around` hooks or for example when you early return `context.result` in a `before` hook and want to transform `context.result` in a `before` hook. Also it returned an object or an array, which adds unnecessary complexity.
@@ -227,10 +279,9 @@ import { setNow } from "feathers-utils/transformers";
@@ -249,38 +300,56 @@ The `traverse` utility has been updated to require an explicit options object. T
249
300
250
301
## `validate`
251
302
252
-
The `validate` hook has been removed. If you need it please reach out to us in this [github issue](https://github.com/feathersjs/feathers-utils/issues/1).
303
+
The `validate` hook has been removed. We plan on bringing it back into another library like `feathers-validation` or something similar. If you need it please reach out to us in this [github issue](https://github.com/feathersjs/feathers-utils/issues/1).
253
304
254
305
## `validateSchema`
255
306
256
-
The `validateSchema` hook has been removed. If you need it please reach out to us in this [github issue](https://github.com/feathersjs/feathers-utils/issues/1).
307
+
The `validateSchema` hook has been removed. We plan on bringing it back into another library like `feathers-validation` or something similar. If you need it please reach out to us in this [github issue](https://github.com/feathersjs/feathers-utils/issues/1).
257
308
258
309
## new hooks
259
310
260
311
### [`createRelated`](/hooks/create-related.html)
261
312
313
+
The new [`createRelated`](/hooks/create-related.html) hook allows you to create related items in a single operation. This can be useful for creating related items in a `create` operation without having to manually create them in a separate step.
314
+
262
315
### [`checkMulti`](/hooks/check-multi.html)
263
316
317
+
Early throw an error if the request is `.patch(null)` or `.remove(null)` and the service is defined as `multi: false`. This is useful to prevent accidental multi operations on services that are not configured for it.
318
+
264
319
### [`onDelete`](/hooks/on-delete.html)
265
320
321
+
The new [`onDelete`](/hooks/on-delete.html) hook allows you to perform actions when an item is deleted. This can be useful for cleaning up related data or performing other actions when an item is deleted.
322
+
266
323
### [`skippable`](/hooks/skippable.html)
267
324
268
-
Also see [`shouldSkip`](/predicates/should-skip.html)
325
+
Also see [`shouldSkip`](/predicates/should-skip.html) and [`addSkip`](/utils/add-skip.html).
269
326
270
327
### [`throwIf`](/hooks/throw-if.html)
271
328
329
+
The new [`throwIf`](/hooks/throw-if.html) hook allows you to throw an error if a certain condition is met. This can be useful for validating conditions before proceeding with the hook chain.
The new [`transformData`](/hooks/transform-data.html) hook allows you to transform the data of a hook call. It's very flexible and is meant to be used with the new [transformers](/transformers/). This can be useful for modifying the data before it is processed by the service.
The new [`transformQuery`](/hooks/transform-query.html) hook allows you to transform the query of a hook call. It's very flexible and is meant to be used with the new [transformers](/transformers/). This can be useful for modifying the query before it is processed by the service.
The new [`transformResult`](/hooks/transform-result.html) hook allows you to transform the result of a hook call. It's very flexible and is meant to be used with the new [transformers](/transformers/). This can be useful for modifying the result before it is returned to the client.
346
+
282
347
## new utils
283
348
349
+
### [`addSkip`](/utils/add-skip.html)
350
+
351
+
In conjunction with the [`skippable` hook](/hooks/skippable.html) and [`shouldSkip` predicate](/predicates/should-skip.html) the new [`addSkip` utility](/utils/add-skip.html) allows you to add a skip flag to the context. This can be useful for conditionally skipping hooks based on certain criteria.
The new [`getDataIsArray`](/utils/get-data-is-array.html) utility returns `context.data` as an array, even if it is not an array. This can be useful for ensuring that you always work with an array in your hooks, regardless of the input type.
@@ -289,22 +358,52 @@ The new [`getDataIsArray`](/utils/get-data-is-array.html) utility returns `conte
289
358
290
359
The new [`getResultIsArray`](/utils/get-result-is-array.html) utility returns `context.result` as an array, even if it is not an array. This can be useful for ensuring that you always work with an array in your hooks, regardless of the input type.
291
360
361
+
### [`iterateFind`](/utils/iterate-find.html)
362
+
363
+
The new [`iterateFind`](/utils/iterate-find.html) utility allows you to iterate over the results of a `find` operation. This can be useful for processing each item in the result set individually, without having to worry about the original result structure (array, object, paginated).
364
+
292
365
### [`mutateData`](/utils/mutate-data.html)
293
366
294
367
The new [`mutateData`](/utils/mutate-data.html) utility mutates `context.data` item by item. This can be useful for transforming the data in your hooks without having to worry about the original data structure (array, object).
295
368
296
-
## [`mutateResult`](/utils/mutate-result.html)
369
+
###[`mutateResult`](/utils/mutate-result.html)
297
370
298
371
The new [`mutateResult`](/utils/mutate-result.html) utility mutates `context.result` item by item. This can be useful for transforming the result in your hooks without having to worry about the original result structure (array, object, paginated).
299
372
300
-
## [`getPaginate`](/utils/get-paginate.html)
373
+
###[`getPaginate`](/utils/get-paginate.html)
301
374
302
375
The new [`getPaginate`](/utils/get-paginate.html) utility returns the pagination information from `context.params` or `service.options.paginate` or `context.params.adapter`.
303
376
377
+
### [`patchBatch`](/utils/patch-batch.html)
378
+
379
+
The new [`patchBatch`](/utils/patch-batch.html) utility allows you to batch patch operations in your hooks. This can be useful for optimizing performance when you need to update multiple items with varying data.
380
+
304
381
## [`skipResult`](/utils/skip-result.html)
305
382
306
383
The new [`skipResult`](/utils/skip-result.html) utility allows you to skip the result of a hook call. This can be useful for early returns in your hooks without having to modify the `context.result` directly. It knows when to set an array, a paginated result or `null`.
307
384
385
+
## Transformers
386
+
387
+
Transformers are functions that modify the data or result of a Feathers service call. They can be used to apply transformations such as trimming strings, converting dates, or omitting fields from the data or result.
388
+
389
+
Instead of using the old `discard`, `keep`, `lowerCase`, `setNow`, and other similar hooks, you can now use the new [`transformData`](/hooks/transform-data.html) and [`transformResult`](/hooks/transform-result.html) hooks with the appropriate transformers.
390
+
391
+
For example, instead of using the old `discard` hook, you can now use the new [`omit` transformer](/transformers/omit.html) with the `transformData` or `transformResult` hooks:
Copy file name to clipboardExpand all lines: docs/predicates/index.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,4 +3,28 @@
3
3
4
4
# Predicates
5
5
6
+
Predicates are functions that return a boolean value based on the context of a Feathers service call. They can be used to conditionally execute hooks or logic based on the state of the request, such as whether the user is authenticated, if the data is an array, or any other custom condition you define.
7
+
8
+
Hooks that are meant to be used with predicates:
9
+
10
+
-[iff](/hooks/iff.html): Executes a hook if the predicate returns true.
11
+
-[iffElse](/hooks/iff-else.html): Executes one hook if the predicate returns true, and another if it returns false.
12
+
-[unless](/hooks/unless.html): Executes a series of hooks if the predicate returns false.
13
+
-[skippable](/hooks/skippable.html): Allows you to skip the execution of a hook based on a predicate.
14
+
-[throwIf](/hooks/throw-if.html): Throws an error if the predicate returns true.
Copy file name to clipboardExpand all lines: docs/transformers/index.md
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,4 +3,17 @@
3
3
4
4
# Transformers
5
5
6
+
Transformers are functions that modify the data or result of a Feathers service call. They can be used to apply transformations such as trimming strings, converting dates, or omitting fields from the data or result.
7
+
8
+
Hooks that are meant to be used with transformers:
9
+
10
+
-[transformData](/hooks/transform-data.html): Transforms the data before it is sent to the database.
11
+
-[transformResult](/hooks/transform-result.html): Transforms the result after it has been retrieved from the database.
12
+
-[transformQuery](/hooks/transform-query.html): Transforms the query parameters before they are sent to the database.
13
+
14
+
Utils that are meant to be used with transformers:
15
+
16
+
-[mutateData](/utils/mutate-data.html): Utility functions to mutate the data.
17
+
-[mutateResult](/utils/mutate-result.html): Utility functions to mutate the result.
0 commit comments