@hf-chimera/store
v0.2.2
Published
Cross-end reactivity API - Core store
Maintainers
Readme
@hf-chimera/store
Core store library for Chimera Store - a cross-platform, reactive cache management library with intelligent deduplication and efficient data synchronization.
Features
- 💾 Intelligent Cache Management: Automatic deduplication and memory-efficient caching
- 🔍 Advanced Querying: Built-in filtering and sorting support
- ⚡ Real-Time Updates: Event-driven architecture with automatic cache invalidation
- 🎯 Type Safety: Full TypeScript support with comprehensive type definitions
- 🛡️ Error Handling: Robust error handling with detailed error messages
- 🌐 Universal Compatibility: Works with any data source (REST APIs, GraphQL, WebSockets, etc.)
Installation
npm install @hf-chimera/store
# or
yarn add @hf-chimera/store
# or
pnpm add @hf-chimera/storeFramework Integrations
- React: Use
@hf-chimera/reactfor React hooks - Vue: Use
@hf-chimera/vuefor Vue 3 composables - Query Builder: Use
@hf-chimera/query-builderfor fluent query building
Quick Start
Basic Setup
import { createChimeraEntityStore } from "@hf-chimera/store";
// Define your entity type
type User = {
id: number;
name: string;
email: string;
age: number;
};
// Create an entity store
const userStore = createChimeraEntityStore<"user", User>({
name: "user",
idGetter: "id", // Can be a string (property name) or function
trustQuery: true,
// Collection fetcher - fetch multiple items
collectionFetcher: async (params, requestParams) => {
const response = await fetch(`/api/users`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ filter: params.filter, order: params.order }),
signal: requestParams.signal,
});
return { data: await response.json() };
},
// Item fetcher - fetch single item by ID
itemFetcher: async (params, requestParams) => {
const response = await fetch(`/api/users/${params.id}`, {
signal: requestParams.signal,
});
return { data: await response.json() };
},
// Item creator - create new item
itemCreator: async (item, requestParams) => {
const response = await fetch(`/api/users`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(item),
signal: requestParams.signal,
});
return { data: await response.json() };
},
// Item updater - update existing item
itemUpdater: async (item, requestParams) => {
const response = await fetch(`/api/users/${item.id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(item),
signal: requestParams.signal,
});
return { data: await response.json() };
},
// Item deleter - delete item by ID
itemDeleter: async (id, requestParams) => {
await fetch(`/api/users/${id}`, {
method: "DELETE",
signal: requestParams.signal,
});
return { result: { id, success: true } };
},
});Working with Data
import {
chimeraCreateConjunction,
chimeraCreateOperator,
chimeraCreateOrderBy,
ChimeraOrderNulls,
} from "@hf-chimera/store";
// Create a new user
const newUserQuery = userStore.createItem({
name: "John Doe",
email: "[email protected]",
age: 30,
});
// Listen for creation events
newUserQuery.on("created", (query) => {
console.log("User created:", query.data);
});
// Get a specific user
const userQuery = userStore.getItem(123);
// Listen for data updates
userQuery.on("ready", (query) => {
console.log("User loaded:", query.data);
});
// Update user data
userQuery.mutable.name = "Jane Doe";
await userQuery.commit();
// Get a collection with filtering and sorting
const activeUsersQuery = userStore.getCollection({
filter: chimeraCreateConjunction("and", [
chimeraCreateOperator("gte", "age", 18),
]),
order: [chimeraCreateOrderBy("name", false, ChimeraOrderNulls.Last)],
});
// Listen for collection updates
activeUsersQuery.on("updated", (query, items, oldItems) => {
console.log("Active users updated:", items);
});
// External updates (e.g., from WebSocket)
userStore.updateOne({
id: 123,
name: "Updated Name",
email: "[email protected]",
age: 31,
});Core Concepts
Entity Store
Each entity type has its own ChimeraEntityStore instance created via createChimeraEntityStore. This provides:
- Isolated caching per entity type
- Type-safe operations for that specific entity
- Event-driven updates for real-time synchronization
- Query management with automatic deduplication
Query Types
- Item Query: Manages a single entity instance
- Collection Query: Manages a filtered/sorted collection of entities
Filtering System
Chimera Store provides a powerful filtering system with support for:
- Operators:
eq,neq,gt,gte,lt,lte,contains,startsWith,endsWith,in,notIn - Conjunctions:
and,or,not - Custom Operators: Extensible operator system for custom logic
- Utility Functions: Use
chimeraCreateOperatorandchimeraCreateConjunctionto build filters
Ordering System
Flexible ordering with support for:
- Multiple Fields: Sort by multiple properties
- Direction: Ascending/descending order
- Null Handling: Configurable null value positioning using
ChimeraOrderNulls.FirstorChimeraOrderNulls.Last - Utility Functions: Use
chimeraCreateOrderByto build order descriptors
API Reference
createChimeraEntityStore
The main function to create an entity store instance.
Signature
function createChimeraEntityStore<
TEntityName extends string,
TItem extends object,
>(
config: ChimeraQueryEntityConfig<TEntityName, TItem, OperatorsMap>,
): ChimeraEntityStore<TEntityName, TItem, OperatorsMap>;Parameters
config: Entity configuration object containing:name: Entity name (string)idGetter: Property name (string) or function to extract ID from entitytrustQuery: Whether to trust query results (boolean)collectionFetcher: Function to fetch multiple itemsitemFetcher: Function to fetch single item by IDitemCreator: Function to create new itemitemUpdater: Function to update existing itemitemDeleter: Function to delete item by ID- Optional:
batchedCreator,batchedUpdater,batchedDeleterfor batch operations - Optional:
updateDebounceTimeoutfor debouncing updates
Returns
ChimeraEntityStore<TEntityName, TItem, OperatorsMap> instance
ChimeraEntityStore
Entity-specific store with query capabilities.
Properties
name: Entity name (readonly)
Methods
createItem(item: DeepPartial<Item>, meta?: any): Create new itemgetItem(id: ChimeraEntityId, meta?: any): Get single itemgetCollection(params: ChimeraCollectionParams): Get filtered/sorted collectionupdateOne(item: Item): Update single item externallyupdateMany(items: Item[]): Update multiple items externallydeleteOne(id: ChimeraEntityId): Delete single item externallydeleteMany(ids: ChimeraEntityId[]): Delete multiple items externallyupdateMixed(toAdd: Item[], toDelete: ChimeraEntityId[]): Mixed update/delete operation
Events
initialized: Store initializeditemAdded: Item added to cacheitemUpdated: Item updatedupdated: Multiple items updateditemDeleted: Item deleteddeleted: Multiple items deleted
ChimeraItemQuery
Represents a single item query with full CRUD operations.
Properties
data: Current item data (throws if not ready)mutable: Mutable reference for updatesstate: Current query stateready: Whether data is availableinProgress: Whether operation is in progressid: Item ID
Methods
refetch(force?: boolean): Refetch dataupdate(item: Item, force?: boolean): Update itemmutate(mutator: (draft: Item) => Item, force?: boolean): Update using mutator functioncommit(force?: boolean): Commit mutable changesdelete(force?: boolean): Delete item
Events
initialized: Query initializedcreated: Item createdready: Data readyupdated: Data updatedselfUpdated: Self-initiated updatedeleted: Item deletedselfDeleted: Self-initiated deletionerror: Error occurred
ChimeraCollectionQuery
Represents a collection query with filtering and sorting.
Properties
length: Number of itemsstate: Current query stateready: Whether data is availableinProgress: Whether operation is in progress
Methods
refetch(force?: boolean): Refetch dataupdate(item: Item): Update single itembatchedUpdate(items: Iterable<Item>): Update multiple itemsdelete(id: ChimeraEntityId): Delete single itembatchedDelete(ids: Iterable<ChimeraEntityId>): Delete multiple itemscreate(item: DeepPartial<Item>): Create new itembatchedCreate(items: Iterable<DeepPartial<Item>>): Create multiple items
Array-like Methods
Collection queries implement the standard Array interface:
at(index: number): Get item at indexfind(predicate): Find item by predicatefilter(predicate): Filter itemsmap(transform): Transform itemsforEach(callback): Iterate over itemsslice(start?, end?): Get subset of items- And many more...
Events
initialized: Query initializedready: Data readyupdated: Data updatedselfUpdated: Self-initiated updateitemAdded: Item addeditemUpdated: Item updatedselfItemUpdated: Self-initiated item updateitemDeleted: Item deletedselfItemDeleted: Self-initiated item deletionerror: Error occurred
Advanced Usage
Event Handling
// Listen to store-level events
userStore.on("itemAdded", ({ instance, item }) => {
console.log("Item added:", item);
});
userStore.on("itemUpdated", ({ instance, item, oldItem }) => {
console.log("User updated:", item, "was:", oldItem);
});
// Listen to query events
const userQuery = userStore.getItem(123);
userQuery.on("updated", (query, item, oldItem) => {
console.log("Query updated:", item);
});Optimistic Updates
// Optimistic update with rollback
const userQuery = userStore.getItem(123);
// Update optimistically
userQuery.mutable.name = "New Name";
try {
await userQuery.commit();
console.log("Update successful");
} catch (error) {
// Rollback on error
await userQuery.refetch();
console.log("Update failed, rolled back");
}Performance Considerations
Memory Management
- Chimera Store uses weak references for automatic memory cleanup
- Queries are automatically cached and deduplicated
- Stale references are cleaned up automatically
Caching Strategy
- Collection queries are cached by filter/order combination
- Item queries are cached by ID
- Cache keys are generated automatically for optimal performance
Update Optimization
- Batch operations for multiple updates
- Optimistic updates for better UX
- Debounced updates to reduce API calls
Browser Support
- Modern Browsers: Full support for ES2021+ features
- Node.js: 14.17.0+ with
--harmony-weak-refsflag, 16.0.0+ stable - TypeScript: 4.5+ recommended
Learn More
License
MIT License — see LICENSE file for details.
