ngrx / platform

Reactive State for Angular
https://ngrx.io
Other
7.96k stars 1.95k forks source link

feat(signals): add entityConfig function #4399

Closed markostanimirovic closed 1 week ago

markostanimirovic commented 3 weeks ago

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

const selectId: SelectEntityId<Todo> = (todo) => todo.key;

const Store = signalStore(
  withEntities({ entity: type<Todo>(), collection: 'todo' }),
  withEntities({ entity: type<User>(), collection: 'user' }),
  withMethods((store) => ({
    addTodo(todo: Todo): void {
      patchState(store, addEntity(todo, { collection: 'todo', selectId }));
    },
    addUser(user: User): void {
      patchState(store, addEntity(user, { collection: 'user' }));
    },
  }))
);

Closes #4393

What is the new behavior?

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

const userConfig = entityConfig({
  entity: type<User>(),
  collection: 'user',
});

const Store = signalStore(
  withEntities(todoConfig),
  withEntities(userConfig),
  withMethods((store) => ({
    addTodo(todo: Todo): void {
      patchState(store, addEntity(todo, todoConfig));
    },
    addUser(user: User): void {
      patchState(store, addEntity(user, userConfig));
    },
  }))
);

Does this PR introduce a breaking change?

[ ] Yes
[x] No
netlify[bot] commented 3 weeks ago

Deploy Preview for ngrx-io ready!

Name Link
Latest commit 49b0ca0414126f3aea897e24c900a3fb84ea9144
Latest deploy log https://app.netlify.com/sites/ngrx-io/deploys/667fefe5bd48fb00083254fc
Deploy Preview https://deploy-preview-4399--ngrx-io.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.