### Entities Store Setup Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Instructions on how to install and set up the Elf Entities store. ```APIDOC ## Installation Install the package using the CLI command `elf-cli install` and selecting the entities package, or via npm: ```bash npm i @ngneat/elf-entities ``` ## Store Creation To use this feature, provide the `withEntities` props factory function in the `createStore` call: ```ts import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; interface Todo { id: number; label: string; } const todosStore = createStore({ name: 'todos' }, withEntities()); ``` ``` -------------------------------- ### Installation Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-status.mdx Install the @ngneat/elf-requests package either via the CLI or npm. ```APIDOC ## Installation Install the package by using the CLI command `elf-cli install` and selecting the requests package, or via npm: ```bash npm i @ngneat/elf-requests ``` ``` -------------------------------- ### Start Local Development Server Source: https://github.com/ngneat/elf/blob/master/docs/README.md Starts a local development server for live previewing changes. Changes are reflected without server restarts. ```bash $ yarn start ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/ngneat/elf/blob/master/docs/README.md Run this command to install all necessary project dependencies using Yarn. ```bash $ yarn ``` -------------------------------- ### Install elf-sync-state Source: https://github.com/ngneat/elf/blob/master/docs/docs/third-party/sync-state.mdx Install the package via npm before using it. ```bash npm i elf-sync-state ``` -------------------------------- ### Install and Use elf CLI Source: https://github.com/ngneat/elf/blob/master/packages/cli/README.md Install the elf CLI globally using npm and run commands. Shows version and help output. ```sh-session $ npm install -g @ngneat/elf-cli $ elf COMMAND running command... $ elf (--version) @ngneat/elf-cli/3.1.0 darwin-arm64 node-v24.3.0 $ elf --help [COMMAND] USAGE $ elf COMMAND ... ``` -------------------------------- ### Pagination Setup Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/pagination.mdx This snippet shows how to set up the Elf store with pagination capabilities. ```APIDOC ## Pagination Setup ### Description This snippet shows how to set up the Elf store with pagination capabilities. ### Method `createStore` with `withPagination` factory ### Endpoint N/A ### Parameters #### Request Body - **withEntities()** (function) - Required - Factory for entity management. - **withPagination()** (function) - Required - Factory for pagination management. ### Request Example ```ts import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; import { withPagination } from '@ngneat/elf-pagination'; interface Todo { id: number; label: string; } const todosStore = createStore( { name: 'todos' }, withEntities(), withPagination(), ); ``` ### Response N/A ``` -------------------------------- ### Install Elf Packages Source: https://context7.com/ngneat/elf/llms.txt Install Elf packages using the CLI or npm. The CLI is recommended for a streamlined installation process. ```bash npx @ngneat/elf-cli install ``` ```bash npm i @ngneat/elf npm i @ngneat/elf-entities npm i @ngneat/elf-requests npm i @ngneat/elf-persist-state npm i @ngneat/elf-pagination npm i @ngneat/elf-state-history npm i @ngneat/elf-devtools ``` -------------------------------- ### Install persist-state package Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/persist-state.mdx Install the persist-state package using npm. ```bash npm i @ngneat/elf-persist-state ``` -------------------------------- ### Install @ngneat/elf-pagination Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/pagination.mdx Install the pagination package using npm. This is a prerequisite for using the pagination features. ```bash npm i @ngneat/elf-pagination ``` -------------------------------- ### Elf Install Command Source: https://github.com/ngneat/elf/blob/master/packages/cli/README.md Details on the `elf install` command, used for installing Elf packages. ```APIDOC ## `elf install` ### Description Installs Elf packages. ### Method CLI Command ### Endpoint N/A (CLI command) ### Parameters #### Flags - **-h, --help** (boolean) - Optional - Show CLI help. ### Request Example ```sh-session $ elf install ``` ### Response (No specific response details provided for success or error in the source text.) ``` -------------------------------- ### Install Elf Packages with elf CLI Source: https://github.com/ngneat/elf/blob/master/packages/cli/README.md Use the 'install' command to add Elf packages to your project. Shows usage and help flag. ```sh-session USAGE $ elf install [-h] FLAGS -h, --help Show CLI help. DESCRIPTION Install Elf packages ``` -------------------------------- ### Install @ngneat/elf-state-history Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/history/entities-history.mdx Install the state-history package using npm. This is a prerequisite for using the entitiesStateHistory function. ```bash npm i @ngneat/elf-state-history ``` -------------------------------- ### Elf CLI Usage Source: https://github.com/ngneat/elf/blob/master/packages/cli/README.md Instructions on how to install and use the Elf CLI, including basic commands and version checking. ```APIDOC ## Elf CLI Usage ### Description This section provides information on how to install and use the Elf command-line interface (CLI). ### Installation ```sh-session $ npm install -g @ngneat/elf-cli ``` ### Basic Usage ```sh-session $ elf COMMAND ``` ### Version Check ```sh-session $ elf --version ``` ### Help ```sh-session $ elf --help [COMMAND] ``` ``` -------------------------------- ### Install Elf Packages Source: https://github.com/ngneat/elf/blob/master/docs/docs/cli.mdx Use this command to install Elf packages. The CLI detects and uses your package manager. ```bash npx @ngneat/elf-cli install ``` -------------------------------- ### Install Elf Requests Package Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-status.mdx Install the requests package for Elf using npm. ```bash npm i @ngneat/elf-requests ``` -------------------------------- ### Create a Simple Store with Props Source: https://context7.com/ngneat/elf/llms.txt Initialize a store with a name and properties using `createStore` and `withProps`. This example demonstrates creating an authentication store. ```typescript import { createStore, withProps } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; // Simple store with props interface AuthProps { user: { id: string } | null; } const authStore = createStore( { name: 'auth' }, withProps({ user: null }) ); // Store with multiple features interface Todo { id: number; label: string; } interface TodosProps { filter: 'ALL' | 'ACTIVE' | 'COMPLETED'; } const todosStore = createStore( { name: 'todos' }, withEntities(), withProps({ filter: 'ALL' }) ); // Subscribe to store changes authStore.subscribe((state) => { console.log(state); }); ``` -------------------------------- ### Install Elf Entities Package Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Install the Elf entities package using npm. This is a prerequisite for using the entities feature. ```bash npm i @ngneat/elf-entities ``` -------------------------------- ### Configure CLI Repository Options Source: https://github.com/ngneat/elf/blob/master/docs/docs/cli.mdx Configure CLI behavior by setting options in the 'elf.cli' section of your package.json. This example shows settings for repository template, inline store, ID key, and plugins. ```json { "elf": { "cli": { "repoTemplate": "class", "inlineStoreInClass": true, "idKey": "_id", "repoLibrary": "state", "plugins": [] } } } ``` -------------------------------- ### Get Store by Name Source: https://github.com/ngneat/elf/blob/master/docs/docs/miscellaneous/registry.mdx Obtain a reference to a specific store using its name. Ensure you import `getStore` from '@ngneat/elf'. ```typescript import { getStore } from '@ngneat/elf'; const todosStore = getStore('name'); ``` -------------------------------- ### Get Stores Snapshot Source: https://github.com/ngneat/elf/blob/master/docs/docs/miscellaneous/registry.mdx Fetch a snapshot of all current store values. Import `getStoresSnapshot` from '@ngneat/elf' to use this function. ```typescript import { getStoresSnapshot } from '@ngneat/elf'; const storesValues = getStoresSnapshot(); ``` -------------------------------- ### Integrate Actions with [@ngneat/effects](https://github.com/ngneat/effects) Source: https://github.com/ngneat/elf/blob/master/docs/docs/dev-tools.mdx Configure Elf DevTools to dispatch actions from [@ngneat/effects](https://github.com/ngneat/effects) by providing the `actionsDispatcher` option. This example shows how to set it up within an Angular application using `APP_INITIALIZER`. ```typescript import { EffectsNgModule, Actions } from '@ngneat/effects-ng'; import { SampleEffects } from 'sample/sample.effect.ts'; import { devTools } from '@ngneat/elf-devtools'; export function initElfDevTools(actions: Actions) { return () => { devTools({ name: 'Sample Application', actionsDispatcher: actions, }); }; } @NgModule({ imports: [ // other modules EffectsNgModule.forRoot([SampleEffects]), ], providers: [ { provide: APP_INITIALIZER, multi: true, useFactory: initElfDevTools, deps: [Actions], }, ], }) export class AppModule {} ``` -------------------------------- ### Enable Redux DevTools Source: https://github.com/ngneat/elf/blob/master/docs/docs/dev-tools.mdx Call the `devTools()` function to enable integration with the Redux DevTools extension. Ensure the extension is installed. ```typescript import { devTools } from '@ngneat/elf-devtools'; devTools(); ``` -------------------------------- ### Select All Entities Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Use `selectAllEntities` to get an observable of the entire store's entity collection. Ensure the `entities` package is installed and configured. ```typescript import { selectAllEntities } from '@ngneat/elf-entities'; const todos$ = todosStore.pipe(selectAllEntities()); ``` -------------------------------- ### Commit Message Format Example Source: https://github.com/ngneat/elf/blob/master/CONTRIBUTING.md Illustrates the required format for commit messages, including type, scope, subject, body, and footer. Adhering to this format ensures readability and aids in automated changelog generation. ```git commit docs(changelog): update changelog to beta.5 ``` ```git commit fix(release): need to depend on latest rxjs and zone.js The version in our package.json gets copied to the one we publish, and users need the latest of these. ``` -------------------------------- ### Create a Simple Elf Store Source: https://github.com/ngneat/elf/blob/master/docs/docs/store.mdx Initialize a store with a name and initial properties. Use `withProps` to define the shape of your state. ```typescript import { createStore, withProps } from '@ngneat/elf'; interface AuthProps { user: { id: string } | null; } const authStore = createStore( { name: 'auth' }, withProps({ user: null }), ); ``` -------------------------------- ### Create a Products Store with `withEntities` Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities-props-factory.mdx Initializes a store for products using the `withEntities` prop. This is a prerequisite for adding other entity types to the same store. ```typescript import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; interface Product { id: number; title: string; price: number; } export const productsStore = createStore( { name: 'products' }, withEntities(), ); ``` -------------------------------- ### Create Repository with elf CLI Source: https://github.com/ngneat/elf/blob/master/packages/cli/README.md Use the 'repo' command to create a new repository. Supports a dry-run option. ```sh-session USAGE $ elf repo [--dry-run] [-h] FLAGS -h, --help Show CLI help. --dry-run DESCRIPTION Create a repository ``` -------------------------------- ### Create and Manage a Todo Store with Elf Source: https://github.com/ngneat/elf/blob/master/README.md Demonstrates how to create a store with properties and entities, select specific properties, and update the store using provided actions. This is useful for managing lists of items with associated metadata. ```typescript import { createStore, withProps, select, setProp } from '@ngneat/elf'; import { withEntities, selectAllEntities, setEntities } from '@ngneat/elf-entities'; interface TodosProps { filter: 'ALL' | 'ACTIVE' | 'COMPLETED'; } interface Todo { id: string; title: string; status: string; } const store = createStore({ name: 'todos' }, withProps({ filter: 'ALL' }), withEntities()); export const filter$ = store.pipe(select(({ filter }) => filter)); export const todos$ = store.pipe(selectAllEntities()); export function setTodos(todos: Todo[]) { store.update(setEntities(todos)); } export function updateFilter(filter: TodosProps['filter']) { store.update(setProp('filter', filter)); } ``` -------------------------------- ### Queries for Request Status Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-status.mdx Utilities to select and get the status of requests. ```APIDOC ## Queries You can monitor and change the request status for your APIs using the following queries and mutations: ### `selectRequestStatus` Select the status of the provided request key: ```ts import { selectRequestStatus } from '@ngneat/elf-requests'; todosStatus$ = store.pipe(selectRequestStatus('todos')); // This will return success when either the `todos` key or the `todo-1` key is succeeded todoStatus$ = store.pipe(selectRequestStatus('todo-1', { groupKey: 'todos' })); ``` ### `getRequestStatus` Get the status of the provided request key: ```ts import { getRequestStatus } from '@ngneat/elf-requests'; todosStatus = store.query(getRequestStatus('todos')); ``` ### `selectIsRequestPending` Select whether the status of the provided request key is `pending`: ```ts import { selectIsRequestPending } from '@ngneat/elf-requests'; pending$ = store.pipe(selectIsRequestPending('todos')); ``` ``` -------------------------------- ### Get Past History Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/history/history.mdx Retrieve an array containing all past states. ```typescript propsStateHistory.getPast(); ``` -------------------------------- ### Active IDs Configuration and Usage Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Demonstrates how to configure a store with `withActiveIds` and use its associated queries and mutations. ```APIDOC ## Active Ids ### Description This feature allows a store to track multiple active entity IDs. It requires the `withEntities` and `withActiveIds` configurations. ### Setup ```typescript import { createStore } from '@ngneat/elf'; import { withEntities, withActiveIds } from '@ngneat/elf-entities'; interface Todo { id: number; label: string; } const todosStore = createStore( { name: 'todos' }, withEntities(), withActiveIds(), ); ``` ### Queries - **`selectActiveEntities()`**: Selects the active entities as an observable. ```typescript import { selectActiveEntities } from '@ngneat/elf-entities'; const actives$ = todosStore.pipe(selectActiveEntities()); ``` - **`selectActiveIds()`**: Selects the active IDs as an observable. ```typescript import { selectActiveIds } from '@ngneat/elf-entities'; const activeIds$ = todosStore.pipe(selectActiveIds()); ``` - **`getActiveEntities()`**: Gets the current active entities. ```typescript import { getActiveEntities } from '@ngneat/elf-entities'; const actives = todosStore.query(getActiveEntities()); ``` - **`getActiveIds()`**: Gets the current active IDs. ```typescript import { getActiveIds } from '@ngneat/elf-entities'; const activeIds = todosStore.query(getActiveIds); ``` ### Mutations - **`setActiveIds(ids)`**: Sets the active entity IDs. ```typescript import { setActiveIds } from '@ngneat/elf-entities'; todosStore.update(setActiveIds([id, id])); ``` - **`addActiveIds(ids)`**: Adds IDs to the set of active IDs. ```typescript import { addActiveIds } from '@ngneat/elf-entities'; todosStore.update(addActiveIds([id, id])); ``` - **`toggleActiveIds(ids)`**: Toggles the active state of the provided IDs. ```typescript import { toggleActiveIds } from '@ngneat/elf-entities'; todosStore.update(toggleActiveIds([id, id])); ``` - **`removeActiveIds(ids)`**: Removes IDs from the set of active IDs. ```typescript import { removeActiveIds } from '@ngneat/elf-entities'; todosStore.update(removeActiveIds([id, id])); ``` - **`resetActiveIds()`**: Resets all active entity IDs. ```typescript import { resetActiveIds } from '@ngneat/elf-entities'; todosStore.update(resetActiveIds()); ``` ``` -------------------------------- ### Active ID Configuration and Usage Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Demonstrates how to configure a store with `withActiveId` and use its associated queries and mutations. ```APIDOC ## Active ID ### Description This feature allows a store to track a single active entity ID. It requires the `withEntities` and `withActiveId` configurations. ### Setup ```typescript import { createStore } from '@ngneat/elf'; import { withEntities, withActiveId } from '@ngneat/elf-entities'; interface Todo { id: number; label: string; } const todosStore = createStore( { name: 'todos' }, withEntities(), withActiveId(), ); ``` ### Queries - **`selectActiveEntity()`**: Selects the active entity as an observable. ```typescript import { selectActiveEntity } from '@ngneat/elf-entities'; const active$ = todosStore.pipe(selectActiveEntity()); ``` - **`selectActiveId()`**: Selects the active ID as an observable. ```typescript import { selectActiveId } from '@ngneat/elf-entities'; const activeId$ = todosStore.pipe(selectActiveId()); ``` - **`getActiveEntity()`**: Gets the current active entity. ```typescript import { getActiveEntity } from '@ngneat/elf-entities'; const active = todosStore.query(getActiveEntity()); ``` - **`getActiveId()`**: Gets the current active ID. ```typescript import { getActiveId } from '@ngneat/elf-entities'; const activeId = todosStore.query(getActiveId); ``` ### Mutations - **`setActiveId(id)`**: Sets the active entity ID. ```typescript import { setActiveId } from '@ngneat/elf-entities'; todosStore.update(setActiveId(id)); ``` - **`resetActiveId()`**: Resets the active entity ID. ```typescript import { resetActiveId } from '@ngneat/elf-entities'; todosStore.update(resetActiveId()); ``` ``` -------------------------------- ### Get Active Entities Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `getActiveEntities` to synchronously retrieve all currently active entities. ```typescript import { getActiveEntities } from '@ngneat/elf-entities'; const actives = todosStore.query(getActiveEntities()); ``` -------------------------------- ### Select Active Entities Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `selectActiveEntities` to get an observable of all currently active entities. ```typescript import { selectActiveEntities } from '@ngneat/elf-entities'; const actives$ = todosStore.pipe(selectActiveEntities()); ``` -------------------------------- ### Get Future History Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/history/history.mdx Retrieve an array containing all future states (states that were undone). ```typescript propsStateHistory.getFuture(); ``` -------------------------------- ### Deploy Website to GitHub Pages Source: https://github.com/ngneat/elf/blob/master/docs/README.md Builds the website and pushes it to the 'gh-pages' branch for GitHub Pages hosting. Ensure your GitHub username is set. ```bash $ GIT_USER= USE_SSH=true yarn deploy ``` -------------------------------- ### Get Active Entity Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `getActiveEntity` to synchronously retrieve the currently active entity. ```typescript import { getActiveEntity } from '@ngneat/elf-entities'; const active = todosStore.query(getActiveEntity()); ``` -------------------------------- ### Create Repository File Source: https://github.com/ngneat/elf/blob/master/docs/docs/cli.mdx Generates a repository file with boilerplate code. You can choose features to customize the output. Use --dry-run to preview changes without applying them. ```bash npx @ngneat/elf-cli repo ``` ```bash npx @ngneat/elf-cli repo --dry-run ``` -------------------------------- ### Select Active Entity Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `selectActiveEntity` to get an observable of the currently active entity. ```typescript import { selectActiveEntity } from '@ngneat/elf-entities'; const active$ = todosStore.pipe(selectActiveEntity()); ``` -------------------------------- ### Build Static Website Content Source: https://github.com/ngneat/elf/blob/master/docs/README.md Generates the static content for the website into the 'build' directory, ready for hosting. ```bash $ yarn build ``` -------------------------------- ### Get Entities Count Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Returns the total number of entities currently stored. No arguments are needed. ```typescript import { getEntitiesCount } from '@ngneat/elf-entities'; const count = todosStore.query(getEntitiesCount()); ``` -------------------------------- ### Get Active IDs Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `getActiveIds` to synchronously retrieve all currently active entity IDs. ```typescript import { getActiveIds } from '@ngneat/elf-entities'; const activeIds = todosStore.query(getActiveIds); ``` -------------------------------- ### Create Store with Entities Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Initialize an Elf store with the `withEntities` configuration. Define the structure of your entities using an interface. ```typescript import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; interface Todo { id: number; label: string; } const todosStore = createStore({ name: 'todos' }, withEntities()); ``` -------------------------------- ### Select Active IDs Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `selectActiveIds` to get an observable of all currently active entity IDs. ```typescript import { selectActiveIds } from '@ngneat/elf-entities'; const activeIds$ = todosStore.pipe(selectActiveIds()); ``` -------------------------------- ### Select All Entities in Elf Store Source: https://github.com/ngneat/elf/blob/master/docs/docs/recipes.mdx This snippet shows how to select all entities from a store. It's recommended to use `shareReplay` to avoid redundant computations when multiple subscribers exist. ```typescript export const todos$ = store.pipe(selectAllEntities()); ``` ```typescript import { shareReplay } from 'rxjs/operators'; export const todos$ = store.pipe( selectAllEntities(), shareReplay({ refCount: true }), ); ``` -------------------------------- ### Get Request Status Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-status.mdx Use `getRequestStatus` to retrieve the current status of a request key synchronously. ```typescript import { getRequestStatus } from '@ngneat/elf-requests'; todosStatus = store.query(getRequestStatus('todos')); ``` -------------------------------- ### Get Current Store Value Source: https://github.com/ngneat/elf/blob/master/docs/docs/store.mdx Retrieve the current state of the store without subscribing using `getValue()`. ```typescript const state = authstore.getValue(); ``` -------------------------------- ### Integrate Version Props into a Store Source: https://github.com/ngneat/elf/blob/master/docs/docs/miscellaneous/props-factory.mdx Demonstrates how to use the functions generated by propsFactory to set an initial version, create a store with version properties, update the version, select the version, and query the version. ```typescript import { withVersion, updateVersion, selectVersion, setVersionInitialValue, } from '@app/store-props.ts'; setVersionInitialValue(1.1); const store = createStore({ name: 'todos' }, withVersion()); store.update(updateVersion(2)); store.pipe(selectVersion()); store.query(getVersion); ``` -------------------------------- ### Create Product Facade with Elf Source: https://github.com/ngneat/elf/blob/master/docs/docs/facade.mdx Defines a facade for managing product data using Elf stores, entities, and request status tracking. It includes functions to set products and an effect to fetch them from a JSON file. ```typescript import { createEffectFn } from '@ngneat/effects'; import { createStore } from '@ngneat/elf'; import { withEntities, selectAllEntities, setEntities, } from '@ngneat/elf-entities'; import { createRequestDataSource, withRequestsStatus, } from '@ngneat/elf-requests'; import { mergeMap, Observable, tap } from 'rxjs'; import { http } from '../http'; export interface Product { id: number; name: string; price: number; image: string; category: 'vegetables' | 'fruits' | 'nuts'; } const store = createStore( { name: 'products' }, withEntities(), withRequestsStatus(), ); const { setSuccess, trackRequestStatus, data$ } = createRequestDataSource({ data$: () => store.pipe(selectAllEntities()), requestKey: 'products', dataKey: 'products', store, }); export const productsDataSource = data$(); function setProducts(products: Product[]) { store.update(setEntities(products), setSuccess()); } export const getProductsEffect = createEffectFn(($: Observable) => { return $.pipe( trackRequestStatus(), mergeMap(() => http('assets/products.json', { selector: (res) => res.json(), }), ), tap(setProducts), ); }); ``` -------------------------------- ### Get Elf Registry Source: https://github.com/ngneat/elf/blob/master/docs/docs/miscellaneous/registry.mdx Retrieve the entire registry of stores. This function requires importing `getRegistry` from '@ngneat/elf'. ```typescript import { getRegistry } from '@ngneat/elf'; const stores = getRegistry(); ``` -------------------------------- ### Get Active ID Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `getActiveId` to synchronously retrieve the currently active entity's ID. ```typescript import { getActiveId } from '@ngneat/elf-entities'; const activeId = todosStore.query(getActiveId); ``` -------------------------------- ### Initialize Sync State for a Store Source: https://github.com/ngneat/elf/blob/master/docs/docs/third-party/sync-state.mdx Call the `syncState` function, passing the Elf store to synchronize. Ensure necessary imports are included. ```typescript import { createStore, withProps } from '@ngneat/elf'; import { syncState } from 'elf-sync-state'; interface AuthProps { user: { id: string } | null; token: string | null; } const authStore = createStore( { name: 'auth' }, withProps({ user: null, token: null }), ); syncState(authStore); ``` -------------------------------- ### Create Store with Pagination Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/pagination.mdx Configure a new store to include pagination capabilities by using the `withPagination` factory function. Ensure `withEntities` is also included if managing entities. ```typescript import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; import { withPagination } from '@ngneat/elf-pagination'; interface Todo { id: number; label: string; } const todosStore = createStore( { name: 'todos' }, withEntities(), withPagination(), ); ``` -------------------------------- ### Select Active ID Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/active-ids.mdx Use `selectActiveId` to get an observable of the currently active entity's ID. ```typescript import { selectActiveId } from '@ngneat/elf-entities'; const activeId$ = todosStore.pipe(selectActiveId()); ``` -------------------------------- ### Iterating with @for Loop Source: https://github.com/ngneat/elf/blob/master/apps/ng/src/app/movies/movies.component.html Use @for to iterate over collections and render content for each item. This example iterates over movies and their properties. ```html @for (movie of movies; track movie) { } Title Actors Genres {{ movie.title }} @for (actor of movie.actors; track actor) { {{ actor }} } @for (genre of movie.genres; track genre) { {{ genre }} } ``` -------------------------------- ### Create Store with UI Entities Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/ui-entities.mdx Initialize an Elf store with both standard entities and UI-specific entities. Define interfaces for both your main entity and its UI representation. ```typescript import { createStore } from '@ngneat/elf'; import { withEntities, withUIEntities } from '@ngneat/elf-entities'; interface TodoUI { id: number; open: boolean; } interface Todo { id: number; name: string; } const todosStore = createStore( { name: 'todos' }, withEntities(), withUIEntities(), ); ``` -------------------------------- ### State History Initialization Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/history/history.mdx Initialize state history for a store, optionally with configuration options. ```APIDOC ## State History Initialization ### Description Initializes the state history tracking for a given store. You can optionally provide configuration options to customize its behavior, such as setting the maximum age of history entries or defining a custom state comparator. ### Method `stateHistory` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters for `stateHistory` function: - **store** (object) - Required - The Elf store to track history for. - **options** (StateHistoryOptions) - Optional - Configuration options for state history. - **maxAge** (number) - Optional - The maximum number of history entries to keep. - **resetFutureOnNewState** (boolean) - Optional - If true, future states are cleared when a new state is added after undoing. Defaults to false. ### Request Example ```ts import { createStore } from '@ngneat/elf'; import { stateHistory } from '@ngneat/elf-state-history'; const myStore = createStore({ name: 'myStore' }); const history = stateHistory(myStore); // With options const historyWithOptions = stateHistory(myStore, { maxAge: 100, resetFutureOnNewState: true }); ``` ### Response Returns an object with methods to control and query the state history. #### Success Response (200) N/A (This is a function call, not an API endpoint) #### Response Example N/A ``` -------------------------------- ### Create Normalized Movies Store with Actors and Genres Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities-props-factory.mdx Sets up a store for movies, including normalized entities for actors and genres. This demonstrates a more complex state shape using multiple custom entity types. ```typescript interface Actor { id: string; name: string; } interface Genre { id: string; name: string; } interface Movie { id: string; title: string; genres: Array; actors: Array; } const { actorsEntitiesRef, withActorsEntities } = entitiesPropsFactory('actors'); const { genresEntitiesRef, withGenresEntities } = entitiesPropsFactory('genres'); const store = createStore( { name: 'movies' }, withEntities(), withGenresEntities(), withActorsEntities(), ); store.update( addEntities({ id: '1', name: 'Nicolas cage' }, { ref: actorsEntitiesRef }), addEntities({ id: '1', name: 'Action' }, { ref: genresEntitiesRef }), addEntities({ id: '1', title: 'Gone in 60 Seconds', genres: ['1'], actors: ['1'], }), ); ``` -------------------------------- ### Get All Entities Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Use `getAllEntities` to synchronously retrieve the entire collection of entities from the store. This returns an array of all entities. ```typescript import { getAllEntities } from '@ngneat/elf-entities'; const todos = todosStore.query(getAllEntities()); ``` -------------------------------- ### Initializing Request as Pending Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-status.mdx Initialize a request with a 'pending' status using `initializeAsPending`. ```APIDOC ## Initializing Request as Pending The default `status` of any request is `idle`. You can use the `initializeAsPending` function to initialize a request as `pending`: ```ts import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; import { withRequestsStatus, initializeAsPending } from '@ngneat/elf-requests'; const todosStore = createStore( { name: 'todos' }, withEntities(), withRequestsStatus( // highlight-next-line initializeAsPending('todos'), ), ); ``` ``` -------------------------------- ### Conditional Rendering with @if Source: https://github.com/ngneat/elf/blob/master/apps/ng/src/app/movies/movies.component.html Use @if to conditionally render content based on a boolean expression. This example shows a loading indicator. ```html @if (isLoading) { Loading... } ``` -------------------------------- ### Store Configuration Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-cache.mdx Configure your store to use request caching by providing the `withRequestsCache` factory function. ```typescript import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; import { withRequestsCache } from '@ngneat/elf-requests'; interface Todo { id: number; label: string; } const todosStore = createStore( { name: 'todos' }, withEntities(), withRequestsCache<'todo' | `todo-${string}`>(), ); ``` -------------------------------- ### Get Entities Count by Predicate Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Returns the number of entities that satisfy a given condition. The predicate function filters the entities. ```typescript import { getEntitiesCountByPredicate } from '@ngneat/elf-entities'; const count = todosStore.query( getEntitiesCountByPredicate((entity) => entity.completed), ); ``` -------------------------------- ### Use PropsFactory in Stores and Apply Mutations Source: https://context7.com/ngneat/elf/llms.txt Demonstrates using the created reusable properties (version, loading, tags) in stores and applying mutations like setVersion, setLoading, and addTags. ```typescript // Use in stores const todosStore = createStore( { name: 'todos' }, withVersion(), withLoading(), withTags() ); const usersStore = createStore( { name: 'users' }, withVersion(), withLoading() ); // Use queries and mutations todosStore.pipe(selectVersion()).subscribe(console.log); todosStore.pipe(selectLoading()).subscribe(console.log); todosStore.pipe(selectTags()).subscribe(console.log); todosStore.update(setVersion(2)); todosStore.update(setLoading(true)); todosStore.update(setTags(['important', 'urgent'])); todosStore.update(addTags(['new-tag'])); todosStore.update(removeTags(['urgent'])); todosStore.update(toggleTags(['important'])); const hasTag = todosStore.query(inTags('important')); ``` -------------------------------- ### Get Entity by Predicate Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Retrieves the first entity that matches a given condition. The predicate function receives the entity as an argument. ```typescript import { getEntityByPredicate } from '@ngneat/elf-entities'; const todo = todosStore.query( getEntityByPredicate(({ title }) => title === 'Elf'), ); ``` -------------------------------- ### Store Configuration Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-status.mdx Configure your Elf store to include request status management using `withRequestsStatus`. ```APIDOC ## Store Configuration To use this feature, provide the `withRequestsStatus` props factory function in the `createStore` call: ```ts title="todos.repository" import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; import { withRequestsStatus, createRequestsStatusOperator, } from '@ngneat/elf-requests'; interface Todo { id: number; label: string; } const todosStore = createStore( { name: 'todos' }, withEntities(), // You can pass the keys type withRequestsStatus<`todos` | `todo-${string}`>(), ); ``` ``` -------------------------------- ### Get Entity by ID Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Retrieves a single entity from the store using its unique identifier. Requires the entity's ID. ```typescript import { getEntity } from '@ngneat/elf-entities'; const todo = todosStore.query(getEntity(id)); ``` -------------------------------- ### Elf Repo Command Source: https://github.com/ngneat/elf/blob/master/packages/cli/README.md Details on the `elf repo` command, used for creating a repository. ```APIDOC ## `elf repo` ### Description Creates a repository. ### Method CLI Command ### Endpoint N/A (CLI command) ### Parameters #### Flags - **--dry-run** (boolean) - Optional - Performs a dry run without making actual changes. - **-h, --help** (boolean) - Optional - Show CLI help. ### Request Example ```sh-session $ elf repo --dry-run ``` ### Response (No specific response details provided for success or error in the source text.) ``` -------------------------------- ### Subscribe to Request Result Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests-result.mdx Subscribe to the selector to get request status (isLoading, isError, isSuccess), data, and error information. ```typescript entities$.subscribe( ({ isLoading, isError, isSuccess, data, error, status }) => { console.log( isLoading, isError, isSuccess, status, successfulRequestsCount, data, // typed as Todo[] error, ); }, ); ``` -------------------------------- ### Select Entities as Object Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Use `selectEntities` to get an observable of the store's entities as an object. This is useful for accessing entities by their IDs. ```typescript import { selectEntities } from '@ngneat/elf-entities'; const todos$ = todosStore.pipe(selectEntities()); ``` -------------------------------- ### Configure Initial Value Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Allows specifying an initial value for the entities state when creating a store. ```APIDOC ## INITIAL VALUE CONFIGURATION ### Description In case that you need to start the `entities` state with a value, you can specify it in the `initialValue` configuration. ### Method N/A (Configuration option for `withEntities`) ### Endpoint N/A ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```ts import { createStore } from '@ngneat/elf'; import { withEntities } from '@ngneat/elf-entities'; interface Widget { id: number; name: string; } const store = createStore( { name: 'widgets' }, withEntities({ initialValue: [{ id: 1, name: '' }] }) ); ``` ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Configure CLI with NG Plugin Source: https://github.com/ngneat/elf/blob/master/docs/docs/cli.mdx Integrate the '@ngneat/elf-cli-ng' plugin by adding it to the 'plugins' array in your package.json configuration. This plugin adds the 'Injectable' decorator to repository classes. ```json { "elf": { "cli": { "repoTemplate": "class", "plugins": ["@ngneat/elf-cli-ng"] } } } ``` -------------------------------- ### Get Whether a Page Exists Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/pagination.mdx Synchronously query the store to check if a specific page number exists. Returns a boolean value. ```typescript import { hasPage } from '@ngneat/elf-pagination'; hasPage = store.query(hasPage(1)); ``` -------------------------------- ### Get First Item with head Source: https://github.com/ngneat/elf/blob/master/docs/docs/miscellaneous/operators.mdx The head operator retrieves the first element from an array emitted by an observable. Import 'head' from '@ngneat/elf'. ```typescript import { head } from '@ngneat/elf'; skills$.pipe(head()); ``` -------------------------------- ### Select Last Entity Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Use `selectLast` to get an observable of the last entity in the store's collection. This is useful when the order of entities is important. ```typescript import { selectLast } from '@ngneat/elf-entities'; const last$ = todosStore.pipe(selectLast()); ``` -------------------------------- ### Initialize Elf Store with Initial Entity Values Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Provide an `initialValue` to the `withEntities` configuration to pre-populate the entities state when the store is created. This is useful for setting up default data. ```typescript import { createStore } from '@ngneat/elf'; const store = createStore( { name }, withEntities({ initialValue: [{ id: 1, name: '' }] }), ); ``` -------------------------------- ### Entities Store Queries Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Documentation for various queries available for the Entities store. ```APIDOC ## `selectAllEntities` Select the entire store's entity collection: ```ts import { selectAllEntities } from '@ngneat/elf-entities'; const todos$ = todosStore.pipe(selectAllEntities()); ``` ## `selectAllEntitiesApply` Select the entire store's entity collection, and apply a `filter/map`: ```ts import { selectAllEntitiesApply } from '@ngneat/elf-entities'; const titles$ = todosStore.pipe( selectAllEntitiesApply({ mapEntity: (e) => e.title, filterEntity: (e) => e.completed, }), ); ``` ## `getAllEntitiesApply` Get the entire store's entity collection, and apply a `filter/map`: ```ts import { getAllEntitiesApply } from '@ngneat/elf-entities'; const titles = todosStore.query( getAllEntitiesApply({ mapEntity: (e) => e.title, filterEntity: (e) => e.completed, }), ); ``` ## `selectEntities` Select the entire store's entity collection as object: ```ts import { selectEntities } from '@ngneat/elf-entities'; const todos$ = todosStore.pipe(selectEntities()); ``` ## `selectEntity` Select an entity or a slice of an entity: ```ts import { selectEntity } from '@ngneat/elf-entities'; const todo$ = todosStore.pipe(selectEntity(id)); const title$ = todosStore.pipe(selectEntity(id, { pluck: 'title' })); const title$ = todosStore.pipe(selectEntity(id, { pluck: (e) => e.title })); ``` ## `selectEntityByPredicate` Select an entity from the store by predicate: ```ts import { selectEntityByPredicate } from '@ngneat/elf-entities'; const todo$ = todosStore.pipe( selectEntityByPredicate(({ completed }) => !completed), ); const title$ = todosStore.pipe( selectEntityByPredicate(({ completed }) => !completed, { pluck: 'title', idKey: '_id', }), ); const title$ = todosStore.pipe( selectEntityByPredicate(({ completed }) => !completed, { pluck: (e) => e.title, idKey: '_id', }), ); ``` ## `selectMany` Select multiple entities from the store: ```ts import { selectMany } from '@ngneat/elf-entities'; const todos$ = todosStore.pipe(selectMany([id, id])); const titles$ = todosStore.pipe(selectMany(id, { pluck: 'title' })); const titles$ = todosStore.pipe(selectMany(id, { pluck: (e) => e.title })); ``` ## `selectManyByPredicate` Select multiple entities from the store by predicate: ```ts import { selectManyByPredicate } from '@ngneat/elf-entities'; const todos$ = todosStore.pipe( selectManyByPredicate((entity) => entity.completed === false), ); const titles$ = todosStore.pipe( selectManyByPredicate((entity) => entity.completed === false, { pluck: 'title', }), ); const titles$ = todosStore.pipe( selectManyByPredicate((entity) => entity.completed === false, { pluck: (e) => e.title, }), ); ``` ## `selectFirst` Select the first entity from the store: ```ts import { selectFirst } from '@ngneat/elf-entities'; const first$ = todosStore.pipe(selectFirst()); ``` ## `selectLast` Select the last entity from the store: ```ts import { selectLast } from '@ngneat/elf-entities'; const last$ = todosStore.pipe(selectLast()); ``` ## `selectEntitiesCount` Select the store's entity collection size: ```ts import { selectEntitiesCount } from '@ngneat/elf-entities'; const count$ = todosStore.pipe(selectEntitiesCount()); ``` ## `selectEntitiesCountByPredicate` Select the store's entity collection size: ```ts import { selectEntitiesCountByPredicate } from '@ngneat/elf-entities'; const count$ = todosStore.pipe( selectEntitiesCountByPredicate((entity) => entity.completed), ); ``` ## `getAllEntities` Get the entity collection: ```ts import { getAllEntities } from '@ngneat/elf-entities'; const todos = todosStore.query(getAllEntities()); ``` ## `getEntitiesIds` Get the entities ids: ```ts import { getEntitiesIds } from '@ngneat/elf-entities'; const todosIds = todosStore.query(getEntitiesIds()); ``` ``` -------------------------------- ### Select First Entity Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Use `selectFirst` to get an observable of the first entity in the store's collection. This is useful when the order of entities is important. ```typescript import { selectFirst } from '@ngneat/elf-entities'; const first$ = todosStore.pipe(selectFirst()); ``` -------------------------------- ### Object-Oriented Repository Implementation Source: https://github.com/ngneat/elf/blob/master/docs/docs/repository.mdx Uses a class to encapsulate store queries and mutations. This OOP approach can be beneficial for more complex state management scenarios. ```typescript import { createStore, withProps, select } from '@ngneat/elf'; interface AuthProps { user: { id: string } | null; } const authStore = createStore( { name: 'auth' }, withProps({ user: null }), ); export class AuthRepository { user$ = authStore.pipe(select((state) => state.user)); updateUser(user: AuthProps['user']) { authStore.update((state) => ({ ...state, user, })); } } ``` -------------------------------- ### Get All Entities with Apply (Filter/Map) Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/entities-management/entities.mdx Use `getAllEntitiesApply` to retrieve entities and apply `filter` and `map` functions synchronously. The filter is applied before the map. ```typescript import { getAllEntitiesApply } from '@ngneat/elf-entities'; const titles = todosStore.query( getAllEntitiesApply({ mapEntity: (e) => e.title, filterEntity: (e) => e.completed, }), ); ``` -------------------------------- ### Select Request Cache Status Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests/requests-cache.mdx Use the selectRequestCache query to get the cache status of a specific request key from the store. This returns an observable. ```typescript import { selectRequestCache } from '@ngneat/elf-requests'; todosCacheStatus$ = store.pipe(selectRequestCache('todos')); ``` -------------------------------- ### Configure Fuzzy Path Options Source: https://github.com/ngneat/elf/blob/master/docs/docs/cli.mdx Customize the fuzzy path selection prompt by exporting a configuration object from 'elf.config.js'. You can set the root path and define custom exclusion logic for paths. ```javascript module.exports = { cli: { fuzzypath: { rootPath: // defaults to process.cwd() excludePath(path) { // defaults to path.includes('node_modules') } excludeFilter(path) { // defaults to path.includes('.'); } } } } ``` -------------------------------- ### Define Elf Store with Properties Source: https://github.com/ngneat/elf/blob/master/docs/docs/troubleshooting/stale-emission.mdx Sets up the initial store with 'filter' and 'counter' properties and creates observables for them. ```typescript import { createStore, withProps, select } from '@ngneat/elf'; interface Props { filter: string | null; counter: number; } export const store = createStore( { name: 'todo' }, withProps({ filter: null, counter: 0 }), ); export const filter$ = store.pipe(select(({ filters }) => filters)); export const counter$ = store.pipe(select(({ counter }) => counter)); ``` -------------------------------- ### Cache Results for Additional Keys Source: https://github.com/ngneat/elf/blob/master/docs/docs/features/requests-result.mdx Use `additionalKeys` to cache the request result for multiple keys, for example, based on properties from the response data. ```typescript import { trackRequestResult } from '@ngneat/elf-requests'; import { setTodos } from './todos.repository'; export function fetchTodos() { return http.get(todosUrl).pipe( tap(setTodos), // highlight-next-line trackRequestResult(['todos'], { additionalKeys: (todos) => todos.map((todo) => ['todos', todo.id]), }), ); } ```