Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion modules/data/src/entity-metadata/entity-definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export function createEntityDefinition<T, S extends object>(
const selectId = metadata.selectId || defaultSelectId;
const sortComparer = (metadata.sortComparer = metadata.sortComparer || false);

const entityAdapter = createEntityAdapter<T>({ selectId, sortComparer });
const entityAdapter = createEntityAdapter<T>({
selectId: selectId as any,
sortComparer,
});

const entityDispatcherOptions: Partial<EntityDispatcherDefaultOptions> =
metadata.entityDispatcherOptions || {};
Expand Down
68 changes: 68 additions & 0 deletions modules/entity/spec/types/entity_adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { expecter } from 'ts-snippet';
import { compilerOptions } from './utils';

describe('EntityAdapter Types', () => {
const expectSnippet = expecter(
(code) => `
import { EntityState, createEntityAdapter, EntityAdapter } from '@ngrx/entity';

interface EntityWithStringId {
id: string;
}

interface EntityWithNumberId {
id: number;
}

interface EntityWithoutId {
key: number;
}


${code}
`,
compilerOptions()
);

it('sets the id type to string when the entity has a string id', () => {
expectSnippet(`
export const adapter: EntityAdapter<EntityWithStringId, string> = createEntityAdapter<EntityWithStringId>();
`).toSucceed();
});

it('sets the id type to number when the entity has a number id', () => {
expectSnippet(`
export const adapter: EntityAdapter<EntityWithNumberId, number> = createEntityAdapter<EntityWithNumberId>();
`).toSucceed();
});

it('sets the id type to string when selectId returns a string', () => {
expectSnippet(`
export const adapter: EntityAdapter<EntityWithNumberId, string> = createEntityAdapter<EntityWithNumberId>({
selectId: (entity) => entity.id.toString(),
});
`).toSucceed();
});

it('sets the id type to string | number when the entity has no id and no selectId is provided', () => {
expectSnippet(`
export const adapter: EntityAdapter<EntityWithoutId, string | number> = createEntityAdapter<EntityWithoutId>();
`).toSucceed();
});

it('sets the id type to correct type if selectId is provided', () => {
expectSnippet(`
export const adapter: EntityAdapter<EntityWithoutId, string> = createEntityAdapter<EntityWithoutId>({
selectId: (entity) => entity.key.toString(),
});
`).toSucceed();
});

it('sets the id type to string when selectId returns a string', () => {
expectSnippet(`
export const adapter: EntityAdapter<EntityWithNumberId, string> = createEntityAdapter<EntityWithNumberId>({
selectId: (entity) => entity.id.toString(),
});
`).toSucceed();
});
}, 8_000);
59 changes: 59 additions & 0 deletions modules/entity/src/create_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,71 @@ import {
Comparer,
IdSelector,
EntityAdapter,
IdSelectorStr,
IdSelectorNum,
} from './models';
import { createInitialStateFactory } from './entity_state';
import { createSelectorsFactory } from './state_selectors';
import { createSortedStateAdapter } from './sorted_state_adapter';
import { createUnsortedStateAdapter } from './unsorted_state_adapter';

/**
* Creates an entity adapter with methods for managing collections of entities in state.
*
* @description
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: add breaking change note to commit

* The entity adapter provides a set of predefined reducers and selectors for managing
* normalized entity collections. It includes methods for adding, updating, removing,
* and selecting entities from state.
*
* @template T - The entity type to be managed by the adapter
*
* @param options - Configuration options for the adapter
* @param options.selectId - A function that selects the unique identifier from an entity.
* Defaults to `(entity) => entity.id` if not provided.
* @param options.sortComparer - A comparer function for sorting entities in state.
* Set to `false` (default) to maintain insertion order, or provide a comparer function
* to keep entities sorted.
*
* @returns An entity adapter containing methods for managing and selecting entity collections in state.
*
* @example
* ```typescript
* // Entity with default 'id' property
* interface User {
* id: number;
* name: string;
* }
* const userAdapter = createEntityAdapter<User>();
*
* // Entity with custom id field
* interface Book {
* isbn: string;
* title: string;
* }
* const bookAdapter = createEntityAdapter<Book>({
* selectId: (book) => book.isbn,
* });
*
* // Entity with sorting
* const sortedBookAdapter = createEntityAdapter<Book>({
* sortComparer: (a, b) => a.title.localeCompare(b.title),
* });
* ```
*/
export function createEntityAdapter<
T extends { id: string | number },
>(options?: {
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T, T extends { id: infer U } ? U : never>;
export function createEntityAdapter<T>(options: {
selectId: IdSelectorStr<T>;
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T, string>;
export function createEntityAdapter<T>(options: {
selectId: IdSelectorNum<T>;
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T, number>;
export function createEntityAdapter<T>(): EntityAdapter<T>;
export function createEntityAdapter<T>(
options: {
selectId?: IdSelector<T>;
Expand Down
11 changes: 9 additions & 2 deletions modules/entity/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,15 @@ export type MemoizedEntitySelectors<T, V> = {
>;
};

export interface EntityAdapter<T> extends EntityStateAdapter<T> {
selectId: IdSelector<T>;
export interface EntityAdapter<
T,
IdType = string | number,
> extends EntityStateAdapter<T> {
selectId: IdType extends string
? IdSelectorStr<T>
: IdType extends number
? IdSelectorNum<T>
: unknown;
sortComparer: false | Comparer<T>;
getInitialState(): EntityState<T>;
getInitialState<S extends EntityState<T>>(
Expand Down
44 changes: 38 additions & 6 deletions projects/www/src/app/reference/entity/EntityAdapter.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
"excerptTokens": [
{
"kind": "Content",
"text": "interface EntityAdapter<T> extends "
"text": "interface EntityAdapter<T, IdType = "
},
{
"kind": "Content",
"text": "string | number"
},
{
"kind": "Content",
"text": "> extends "
},
{
"kind": "Reference",
Expand Down Expand Up @@ -41,6 +49,17 @@
"startIndex": 0,
"endIndex": 0
}
},
{
"typeParameterName": "IdType",
"constraintTokenRange": {
"startIndex": 0,
"endIndex": 0
},
"defaultTypeTokenRange": {
"startIndex": 1,
"endIndex": 2
}
}
],
"name": "EntityAdapter",
Expand Down Expand Up @@ -355,10 +374,23 @@
"kind": "Content",
"text": "selectId: "
},
{
"kind": "Content",
"text": "IdType extends string ? "
},
{
"kind": "Reference",
"text": "IdSelectorStr",
"canonicalReference": "@ngrx/entity!~IdSelectorStr:type"
},
{
"kind": "Content",
"text": "<T> : "
},
{
"kind": "Reference",
"text": "IdSelector",
"canonicalReference": "@ngrx/entity!IdSelector:type"
"text": "IdSelectorNum",
"canonicalReference": "@ngrx/entity!~IdSelectorNum:type"
},
{
"kind": "Content",
Expand All @@ -375,7 +407,7 @@
"name": "selectId",
"propertyTypeTokenRange": {
"startIndex": 1,
"endIndex": 3
"endIndex": 6
},
"docs": {
"modifiers": {
Expand Down Expand Up @@ -451,8 +483,8 @@
],
"extendsTokenRanges": [
{
"startIndex": 1,
"endIndex": 3
"startIndex": 3,
"endIndex": 5
}
],
"docs": {
Expand Down
Loading