Skip to content

Commit 31c4794

Browse files
committed
there are no entity methods creators in ba sing se
1 parent 5f5fe79 commit 31c4794

File tree

9 files changed

+37
-621
lines changed

9 files changed

+37
-621
lines changed

docs/api/createEntityAdapter.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ In other words, they accept a state that looks like `{ids: [], entities: {}}`, a
239239

240240
These CRUD methods may be used in multiple ways:
241241

242-
- 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)
242+
- They may be passed as case reducers directly to `createReducer` and `createSlice`.
243243
- 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.
244244
- They may be used as immutable update methods when called manually, if the `state` argument is actually a plain JS object or array.
245245

docs/api/createSlice.mdx

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -845,105 +845,6 @@ reducers: (create) => {
845845
846846
:::
847847
848-
##### `create.entityMethods` (`entityMethodsCreator`)
849-
850-
Creates a set of reducers for managing a normalized entity state, based on a provided [adapter](./createEntityAdapter).
851-
852-
```ts
853-
import {
854-
createEntityAdapter,
855-
buildCreateSlice,
856-
entityMethodsCreator,
857-
} from '@reduxjs/toolkit'
858-
859-
const createAppSlice = buildCreateSlice({
860-
creators: { entityMethods: entityMethodsCreator },
861-
})
862-
863-
interface Post {
864-
id: string
865-
text: string
866-
}
867-
868-
const postsAdapter = createEntityAdapter<Post>()
869-
870-
const postsSlice = createAppSlice({
871-
name: 'posts',
872-
initialState: postsAdapter.getInitialState(),
873-
reducers: (create) => ({
874-
...create.entityMethods(postsAdapter),
875-
}),
876-
})
877-
878-
export const { setOne, upsertMany, removeAll, ...etc } = postsSlice.actions
879-
```
880-
881-
:::caution
882-
883-
Because this creator returns an object of multiple reducer definitions, it should be spread into the final object returned by the `reducers` callback.
884-
885-
:::
886-
887-
**Parameters**
888-
889-
- **adapter** The [adapter](./createEntityAdapter) to use.
890-
- **config** The configuration object. (optional)
891-
892-
The configuration object can contain some of the following options:
893-
894-
**`selectEntityState`**
895-
896-
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.
897-
898-
```ts no-transpile
899-
const postsSlice = createAppSlice({
900-
name: 'posts',
901-
initialState: { posts: postsAdapter.getInitialState() },
902-
reducers: (create) => ({
903-
...create.entityMethods(postsAdapter, {
904-
selectEntityState: (state) => state.posts,
905-
}),
906-
}),
907-
})
908-
```
909-
910-
**`name`, `pluralName`**
911-
912-
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.
913-
914-
```ts no-transpile
915-
const postsSlice = createAppSlice({
916-
name: 'posts',
917-
initialState: postsAdapter.getInitialState(),
918-
reducers: (create) => ({
919-
...create.entityMethods(postsAdapter, {
920-
name: 'post',
921-
}),
922-
}),
923-
})
924-
925-
const { addOnePost, upsertManyPosts, removeAllPosts, ...etc } =
926-
postsSlice.actions
927-
```
928-
929-
`pluralName` defaults to `name + 's'`, but can be provided if this isn't desired.
930-
931-
```ts no-transpile
932-
const gooseSlice = createAppSlice({
933-
name: 'geese',
934-
initialState: gooseAdapter.getInitialState(),
935-
reducers: (create) => ({
936-
...create.entityMethods(gooseAdapter, {
937-
name: 'goose',
938-
pluralName: 'geese',
939-
}),
940-
}),
941-
})
942-
943-
const { addOneGoose, upsertManyGeese, removeAllGeese, ...etc } =
944-
gooseSlice.actions
945-
```
946-
947848
### Writing your own creators
948849
949850
In version v2.2.0 (TODO), we introduced a system for including your own creators.

packages/toolkit/src/entities/models.ts

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ import type { Draft } from 'immer'
33
import type { PayloadAction } from '../createAction'
44
import type { GetSelectorsOptions } from './state_selectors'
55
import type { CastAny, Id as Compute } from '../tsHelpers'
6-
import type { CaseReducerDefinition } from '../createSlice'
7-
import type { CaseReducer } from '../createReducer'
86

97
/**
108
* @public
@@ -160,51 +158,12 @@ export interface EntityStateAdapter<T, Id extends EntityId> {
160158
/**
161159
* @public
162160
*/
163-
export type EntitySelectors<
164-
T,
165-
V,
166-
Id extends EntityId,
167-
Single extends string = '',
168-
Plural extends string = DefaultPlural<Single>,
169-
> = Compute<
170-
{
171-
[K in `select${Capitalize<Single>}Ids`]: (state: V) => Id[]
172-
} & {
173-
[K in `select${Capitalize<Single>}Entities`]: (state: V) => Record<Id, T>
174-
} & {
175-
[K in `selectAll${Capitalize<Plural>}`]: (state: V) => T[]
176-
} & {
177-
[K in `selectTotal${Capitalize<Plural>}`]: (state: V) => number
178-
} & {
179-
[K in `select${Capitalize<Single>}ById`]: (
180-
state: V,
181-
id: Id,
182-
) => Compute<UncheckedIndexedAccess<T>>
183-
}
184-
>
185-
186-
export type DefaultPlural<Single extends string> = Single extends ''
187-
? ''
188-
: `${Single}s`
189-
190-
export type EntityReducers<
191-
T,
192-
Id extends EntityId,
193-
State = EntityState<T, Id>,
194-
Single extends string = '',
195-
Plural extends string = DefaultPlural<Single>,
196-
> = {
197-
[K in keyof EntityStateAdapter<
198-
T,
199-
Id
200-
> as `${K}${Capitalize<K extends `${string}One` ? Single : Plural>}`]: EntityStateAdapter<
201-
T,
202-
Id
203-
>[K] extends (state: any) => any
204-
? CaseReducerDefinition<State, PayloadAction>
205-
: EntityStateAdapter<T, Id>[K] extends CaseReducer<any, infer A>
206-
? CaseReducerDefinition<State, A>
207-
: never
161+
export interface EntitySelectors<T, V, Id extends EntityId> {
162+
selectIds: (state: V) => Id[]
163+
selectEntities: (state: V) => Record<Id, T>
164+
selectAll: (state: V) => T[]
165+
selectTotal: (state: V) => number
166+
selectById: (state: V, id: Id) => Compute<UncheckedIndexedAccess<T>>
208167
}
209168

210169
/**
@@ -228,19 +187,12 @@ export interface EntityAdapter<T, Id extends EntityId>
228187
extends EntityStateAdapter<T, Id>,
229188
EntityStateFactory<T, Id>,
230189
Required<EntityAdapterOptions<T, Id>> {
231-
getSelectors<
232-
Single extends string = '',
233-
Plural extends string = DefaultPlural<Single>,
234-
>(
190+
getSelectors(
235191
selectState?: undefined,
236-
options?: GetSelectorsOptions<Single, Plural>,
237-
): EntitySelectors<T, EntityState<T, Id>, Id, Single, Plural>
238-
getSelectors<
239-
V,
240-
Single extends string = '',
241-
Plural extends string = DefaultPlural<Single>,
242-
>(
192+
options?: GetSelectorsOptions,
193+
): EntitySelectors<T, EntityState<T, Id>, Id>
194+
getSelectors<V>(
243195
selectState: (state: V) => EntityState<T, Id>,
244-
options?: GetSelectorsOptions<Single, Plural>,
245-
): EntitySelectors<T, V, Id, Single, Plural>
196+
options?: GetSelectorsOptions,
197+
): EntitySelectors<T, V, Id>
246198
}

packages/toolkit/src/entities/slice_creator.ts

Lines changed: 0 additions & 161 deletions
This file was deleted.

0 commit comments

Comments
 (0)