Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity methods creator #4223

Draft
wants to merge 18 commits into
base: create-slice-creators
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
f03c2d9
Revert "there are no entity methods creators in ba sing se"
EskiMojo14 Feb 18, 2024
248892a
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Feb 18, 2024
a6b8727
Revert "reset more entity files"
EskiMojo14 Feb 18, 2024
e012149
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Feb 18, 2024
063f248
Revert "again"
EskiMojo14 Feb 18, 2024
c5f65f5
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Feb 19, 2024
28a200b
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Feb 19, 2024
a687ebf
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Feb 21, 2024
7379cc2
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Apr 6, 2024
4a95c26
update type to match
EskiMojo14 Apr 6, 2024
2fb97e5
add entity methods creator back to docs
EskiMojo14 Apr 8, 2024
17cad3c
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Apr 8, 2024
8d61a90
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 Apr 8, 2024
7ab0f2e
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 May 22, 2024
9febdfe
move async thunk creator module augmentation
EskiMojo14 May 22, 2024
9d9c568
Merge branch 'create-slice-creators' into entity-methods-creator
EskiMojo14 May 22, 2024
b1dd066
create util to cut down on repetitive code
EskiMojo14 May 22, 2024
e888b32
prevent implicit return
EskiMojo14 May 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/createEntityAdapter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ In other words, they accept a state that looks like `{ids: [], entities: {}}`, a

These CRUD methods may be used in multiple ways:

- They may be passed as case reducers directly to `createReducer` and `createSlice`.
- They may be passed as case reducers directly to `createReducer` and `createSlice`. (also see the [`create.entityMethods`](./createSlice#createentitymethods-entitymethodscreator) slice creator which can assist with this)
- They may be used as "mutating" helper methods when called manually, such as a separate hand-written call to `addOne()` inside of an existing case reducer, if the `state` argument is actually an Immer `Draft` value.
- They may be used as immutable update methods when called manually, if the `state` argument is actually a plain JS object or array.

Expand Down
101 changes: 100 additions & 1 deletion docs/usage/custom-slice-creators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,105 @@ reducers: (create) => {

:::

##### `create.entityMethods` (`entityMethodsCreator`)

Creates a set of reducers for managing a normalized entity state, based on a provided [adapter](./createEntityAdapter).

```ts
import {
createEntityAdapter,
buildCreateSlice,
entityMethodsCreator,
} from '@reduxjs/toolkit'

const createAppSlice = buildCreateSlice({
creators: { entityMethods: entityMethodsCreator },
})

interface Post {
id: string
text: string
}

const postsAdapter = createEntityAdapter<Post>()

const postsSlice = createAppSlice({
name: 'posts',
initialState: postsAdapter.getInitialState(),
reducers: (create) => ({
...create.entityMethods(postsAdapter),
}),
})

export const { setOne, upsertMany, removeAll, ...etc } = postsSlice.actions
```

:::caution

Because this creator returns an object of multiple reducer definitions, it should be spread into the final object returned by the `reducers` callback.

:::

**Parameters**

- **adapter** The [adapter](../api/createEntityAdapter) to use.
- **config** The configuration object. (optional)

The configuration object can contain some of the following options:

**`selectEntityState`**

A selector to retrieve the entity state from the slice state. Defaults to `state => state`, but should be provided if the entity state is nested.

```ts no-transpile
const postsSlice = createAppSlice({
name: 'posts',
initialState: { posts: postsAdapter.getInitialState() },
reducers: (create) => ({
...create.entityMethods(postsAdapter, {
selectEntityState: (state) => state.posts,
}),
}),
})
```

**`name`, `pluralName`**

It's often desirable to modify the reducer names to be specific to the data type being used. These options allow you to do that simply.

```ts no-transpile
const postsSlice = createAppSlice({
name: 'posts',
initialState: postsAdapter.getInitialState(),
reducers: (create) => ({
...create.entityMethods(postsAdapter, {
name: 'post',
}),
}),
})

const { addOnePost, upsertManyPosts, removeAllPosts, ...etc } =
postsSlice.actions
```

`pluralName` defaults to `name + 's'`, but can be provided if this isn't desired.

```ts no-transpile
const gooseSlice = createAppSlice({
name: 'geese',
initialState: gooseAdapter.getInitialState(),
reducers: (create) => ({
...create.entityMethods(gooseAdapter, {
name: 'goose',
pluralName: 'geese',
}),
}),
})

const { addOneGoose, upsertManyGeese, removeAllGeese, ...etc } =
gooseSlice.actions
```

## Writing your own creators

In version v2.3.0, we introduced a system for including your own creators.
Expand All @@ -338,7 +437,7 @@ For example, the `create.preparedReducer` creator uses a definition that looks l

The callback form of `reducers` should return an object of reducer definitions, by calling creators and nesting the result of each under a key.

```js no-transpile
```js
reducers: (create) => ({
addTodo: create.preparedReducer(
(todo) => ({ payload: { id: nanoid(), ...todo } }),
Expand Down
8 changes: 0 additions & 8 deletions packages/toolkit/src/entities/index.ts

This file was deleted.