Skip to content

Commit 6d7b883

Browse files
committed
docs: add page why migrating to feathers-utils
1 parent e1553e4 commit 6d7b883

File tree

100 files changed

+638
-137
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+638
-137
lines changed

docs/.vitepress/config.mts

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,45 +53,40 @@ export default defineConfig({
5353
],
5454
logo: '/feathers-utils-logo.png',
5555
sidebar: [
56+
{ text: 'Overview', link: '/overview' },
57+
{ text: 'Why moving from feathers-hooks-common', link: '/why' },
58+
{ text: 'Migrating', link: '/migrating-from-feathers-hooks-common' },
5659
{
57-
text: 'Guide',
58-
items: [
59-
{ text: 'Overview', link: '/overview' },
60-
{ text: 'Migrating', link: '/migrating-from-feathers-hooks-common' },
61-
{
62-
text: 'Hooks',
63-
link: '/hooks',
64-
collapsed: false,
65-
items: utilities
66-
.filter((x) => x.category === 'hooks')
67-
.map((x) => ({
68-
text: x.title,
69-
link: x.path,
70-
})),
71-
},
72-
{
73-
text: 'Utilities',
74-
link: '/utils',
75-
collapsed: false,
76-
items: utilities
77-
.filter((x) => x.category === 'utils')
78-
.map((x) => ({
79-
text: x.title,
80-
link: x.path,
81-
})),
82-
},
83-
{
84-
text: 'Predicates',
85-
link: '/predicates',
86-
},
87-
{
88-
text: 'Transformers',
89-
link: '/transformers',
90-
},
91-
{ text: 'Migrating', link: '/migrating' },
92-
{ text: 'Guides', link: '/guides' },
93-
],
60+
text: 'Hooks',
61+
link: '/hooks',
62+
collapsed: false,
63+
items: utilities
64+
.filter((x) => x.category === 'hooks')
65+
.map((x) => ({
66+
text: x.title,
67+
link: x.path,
68+
})),
69+
},
70+
{
71+
text: 'Utilities',
72+
link: '/utils',
73+
collapsed: false,
74+
items: utilities
75+
.filter((x) => x.category === 'utils')
76+
.map((x) => ({
77+
text: x.title,
78+
link: x.path,
79+
})),
80+
},
81+
{
82+
text: 'Predicates',
83+
link: '/predicates',
84+
},
85+
{
86+
text: 'Transformers',
87+
link: '/transformers',
9488
},
89+
{ text: 'Utility Types', link: '/utility-types' },
9590
],
9691
nav: [
9792
{
@@ -112,6 +107,9 @@ export default defineConfig({
112107
message: 'Released under the MIT License.',
113108
copyright: 'Copyright © 2016-present Feathers contributors',
114109
},
110+
search: {
111+
provider: 'local',
112+
},
115113
},
116114
markdown: {
117115
codeTransformers: [

docs/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,17 @@ hero:
1414
- theme: alt
1515
text: View on GitHub
1616
link: https://github.com/feathersjs/feathers-utils
17+
18+
features:
19+
- title: successor of feathers-hooks-common
20+
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.
1730
---

docs/migrating-from-feathers-hooks-common.md

Lines changed: 110 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This document provides a guide for migrating from `feathers-hooks-common` to `fe
66

77
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.
88

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).
1010

1111
## `actOn`, `actOnDefault`, and `actOnDispatch`
1212

@@ -16,6 +16,28 @@ The `actOn`, `actOnDefault`, and `actOnDispatch` hooks have been removed. But we
1616

1717
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.
1818

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`.
20+
21+
```ts
22+
// old
23+
import { alterItems } from "feathers-hooks-common";
24+
25+
alterItems((item, context) => {
26+
// transform item
27+
return item;
28+
});
29+
30+
// new
31+
import { transformData } from "feathers-utils/hooks";
32+
33+
transformData((item, { context }) => {
34+
// transform item
35+
return item;
36+
});
37+
```
38+
39+
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+
1941
## `cache`
2042

2143
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
4264

4365
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.
4466

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+
4587
## `discard`
4688

4789
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).
@@ -62,7 +104,7 @@ app.service("my-service").hooks({
62104
});
63105
```
64106

65-
## `discardQuery
107+
## `discardQuery`
66108

67109
The `discardQuery` hook has been removed. Instead you can use `transformQuery` with the `omit` transformer.
68110

@@ -186,6 +228,16 @@ Nowadays it's recommended to use [resolvers](https://feathersjs.com/api/schema/r
186228

187229
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.
188230

231+
```ts
232+
// old
233+
import { preventChanges } from "feathers-hooks-common";
234+
preventChanges(true, "security.badge");
235+
236+
// new
237+
import { preventChanges } from "feathers-utils/hooks";
238+
preventChanges("security.badge", { error: true });
239+
```
240+
189241
## `replaceItems`
190242

191243
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";
227279

228280
app.service("my-service").hooks({
229281
before: {
230-
all: [transformData(setNow(["createdAt", "updatedAt"]))],
231-
},
232-
after: {
233-
all: [transformResult(setNow(["createdAt", "updatedAt"]))],
282+
createdAt: [transformData(setNow(["createdAt", "updatedAt"]))],
283+
updatedAt: [transformData(setNow(["updatedAt"]))],
284+
patchedAt: [transformData(setNow(["patchedAt"]))],
234285
},
235286
});
236287
```
@@ -249,38 +300,56 @@ The `traverse` utility has been updated to require an explicit options object. T
249300

250301
## `validate`
251302

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).
253304

254305
## `validateSchema`
255306

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).
257308

258309
## new hooks
259310

260311
### [`createRelated`](/hooks/create-related.html)
261312

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+
262315
### [`checkMulti`](/hooks/check-multi.html)
263316

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+
264319
### [`onDelete`](/hooks/on-delete.html)
265320

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+
266323
### [`skippable`](/hooks/skippable.html)
267324

268-
Also see [`shouldSkip`](/predicates/should-skip.html)
325+
Also see [`shouldSkip`](/predicates/should-skip.html) and [`addSkip`](/utils/add-skip.html).
269326

270327
### [`throwIf`](/hooks/throw-if.html)
271328

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.
330+
272331
### [`throwIfIsProvider`](/hooks/throw-if-is-provider.html)
273332

274333
### [`throwIfIsMulti`](/hooks/throw-if-is-multi.html)
275334

276335
### [`transformData`](/hooks/transform-data.html)
277336

337+
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.
338+
278339
### [`transformQuery`](/hooks/transform-query.html)
279340

341+
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.
342+
280343
### [`transformResult`](/hooks/transform-result.html)
281344

345+
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+
282347
## new utils
283348

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.
352+
284353
### [`getDataIsArray`](/utils/get-data-is-array.html)
285354

286355
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
289358

290359
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.
291360

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+
292365
### [`mutateData`](/utils/mutate-data.html)
293366

294367
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).
295368

296-
## [`mutateResult`](/utils/mutate-result.html)
369+
### [`mutateResult`](/utils/mutate-result.html)
297370

298371
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).
299372

300-
## [`getPaginate`](/utils/get-paginate.html)
373+
### [`getPaginate`](/utils/get-paginate.html)
301374

302375
The new [`getPaginate`](/utils/get-paginate.html) utility returns the pagination information from `context.params` or `service.options.paginate` or `context.params.adapter`.
303376

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+
304381
## [`skipResult`](/utils/skip-result.html)
305382

306383
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`.
307384

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:
392+
393+
```ts
394+
import { transformData, transformResult } from "feathers-utils/hooks";
395+
import { omit } from "feathers-utils/transformers";
396+
397+
app.service("my-service").hooks({
398+
before: {
399+
all: [transformData(omit(["field1", "field2"]))],
400+
},
401+
after: {
402+
all: [transformResult(omit(["field1", "field2"]))],
403+
},
404+
});
405+
```
406+
308407
## new predicates
309408

310409
### [`isContext`](/predicates/is-context.html)

docs/migrating.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

docs/predicates/index.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,28 @@
33

44
# Predicates
55

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.
15+
616
<PredicatesList />
17+
18+
## Custom Predicates
19+
20+
You can easily create custom predicates like:
21+
22+
```ts
23+
const isSingleData = (context) => !Array.isArray(context.data);
24+
```
25+
26+
or
27+
28+
```ts
29+
const isAuthenticated = (context) => !!context.params?.authenticated;
30+
```

docs/transformers/index.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,17 @@
33

44
# Transformers
55

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.
18+
619
<TransformersList />

0 commit comments

Comments
 (0)