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

@ngrx/signals/entity: Introduce entityConfig function #4393

Closed
1 of 2 tasks
markostanimirovic opened this issue Jun 14, 2024 · 0 comments · Fixed by #4399
Closed
1 of 2 tasks

@ngrx/signals/entity: Introduce entityConfig function #4393

markostanimirovic opened this issue Jun 14, 2024 · 0 comments · Fixed by #4399
Assignees

Comments

@markostanimirovic
Copy link
Member

markostanimirovic commented Jun 14, 2024

Which @ngrx/* package(s) are relevant/related to the feature request?

signals

Information

To use @ngrx/signals/entities plugin with custom configuration, we usually repeat the same properties multiple times:

const selectTodoId = (todo: Todo) => todo._id;

const TodosStore = signalStore(
  withEntities({ entity: type<Todo>(), collection: 'todo' }),
  withMethods((store) => ({
    addTodo(todo: Todo): void {
      patchState(
        store,
        addEntity(todo, { collection: 'todo', selectId: selectTodoId })
      );
    },
    setTodos(todo: Todo[]): void {
      patchState(
        store,
        setEntities(todo, { collection: 'todo', selectId: selectTodoId })
      );
    },
  })
);

With the entityConfig function, the same store will look like this:

const todoConfig = entityConfig({
  entity: type<Todo>(),
  collection: 'todo',
  selectId: (todo) => todo._id,
});

const TodosStore = signalStore(
  withEntities(todoConfig),
  withMethods((store) => ({
    addTodo(todo: Todo): void {
      patchState(store, addEntity(todo, todoConfig));
    },
    setTodos(todo: Todo[]): void {
      patchState(store, setEntities(todo, todoConfig));
    },
  })
);

Describe any alternatives/workarounds you're currently using

The todoConfig object can be defined as follows:

const todoConfig = {
  entity: type<Todo>(),
  collection: 'todo',
  selectId: (todo) => todo._id,
} as const;

However, with dedicated function, type safety will be ensured.

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@markostanimirovic markostanimirovic self-assigned this Jun 14, 2024
markostanimirovic added a commit that referenced this issue Jun 15, 2024
markostanimirovic added a commit that referenced this issue Jun 16, 2024
markostanimirovic added a commit that referenced this issue Jun 17, 2024
markostanimirovic added a commit that referenced this issue Jun 18, 2024
@markostanimirovic markostanimirovic changed the title @ngrx/signals/entity: Introduce entityMeta function @ngrx/signals/entity: Introduce entityConfig function Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant