@brainstack/store
TypeScript icon, indicating that this package has built-in type declarations

1.0.149 • Public • Published

@brainstack/store

A package that combines state management with event handling, providing a convenient solution for managing application state and responding to state changes using event-driven programming.

Installation

To use @brainstack/store, install it using npm or yarn:

npm install @brainstack/store

or

yarn add @brainstack/store

Usage

@brainstack/store offers a way to create a microstore that integrates @brainstack/state and @brainstack/hub libraries to handle state management and event handling.

Here's how you can use @brainstack/store:

import { createStore } from '@brainstack/store';

// Create a microstore instance
const microstore = createStore();

// Subscribe to state changes
const unsubscribe = microstore.subscribe((currentState) => {
  console.log('State changed:', currentState);
});

// Update the state using the mutate method
microstore.mutate((currentState) => {
  return { ...currentState, key: 'new value' };
});

// Unsubscribe when done
unsubscribe();

API

createStore(options?: TCreateStoreOptions)

Creates a store instance with integrated state management and event handling.

  • options: An optional object containing the following properties:
    • initializer: A function that initializes the initial state.
    • eventHubOptions: Options for configuring the event hub.

Store Instance Methods

  • mutate(mutator: (_state: ReturnType<typeof state.getState>) => any): Updates the microstore state with a new value and emits a "state.changed" event.

  • subscribe(callback: (_state: ReturnType<typeof state.getState>) => void): Subscribes to changes in the microstore state and invokes the provided callback when the state changes.

createCRUDObject(domain: keyof typeof state)

Creates a set of CRUD operations for a specific domain in the state which is expected to be an object.

CRUD Object Methods

  • create(item: any): Adds a new item to the domain.
  • read(item: any): Reads an item from the domain by its ID.
  • update(item: any): Updates an item in the domain by its ID.
  • delete(item: { id: string }): Deletes an item from the domain by its ID.
  • list(): Lists all items in the domain.
  • search(keyword: string): Searches items in the domain by a keyword.

createCRUDArray(domain: keyof typeof state)

Creates a set of CRUD operations for a specific domain in the state which is expected to be an array.

CRUD Array Methods

  • create(item: any): Adds a new item to the domain.
  • read(item: { id: string }): Reads an item from the domain by its ID.
  • update(updatedItem: any): Updates an item in the domain by its ID.
  • delete(item: { id: string }): Deletes an item from the domain by its ID.
  • list(): Lists all items in the domain.
  • search(keyword: string): Searches items in the domain by a keyword.

Comprehensive Usage Example

In this example, we'll demonstrate how to use @brainstack/store along with the createCRUDObject and createCRUDArray utility functions. We'll create a store with two domains: profiles (using an object structure) and tasks (using an array structure).

1. Setting Up

First, import the necessary functions and set up your initial state:

import { createStore } from '@brainstack/store';
// ... (import other necessary utilities such as createCRUDObject and createCRUDArray)

const initialState = {
  profiles: {},
  tasks: []
};

const store = createStore({ initializer: initialState });

2. Create CRUD for Each Domain

Now, we'll generate CRUD operations for both profiles and tasks:

const profilesCRUD = createCRUDObject('profiles');
const tasksCRUD = createCRUDArray('tasks');

3. Using the CRUD Operations

Profiles (Object structure)

// Create
const newProfile = { name: 'Alice Brown', age: 25 };
profilesCRUD.create(newProfile);

// Read
const profile = profilesCRUD.read({ id: '123' });
console.log(profile);

// Update
const updatedProfile = { id: '123', name: 'Alice B.', age: 26 };
profilesCRUD.update(updatedProfile);

// Delete
profilesCRUD.delete({ id: '124' });

// List
const allProfiles = profilesCRUD.list();
console.log(allProfiles);

// Search
const foundProfiles = profilesCRUD.search('Alice');
console.log(foundProfiles);

Tasks (Array structure)

// Create
const newTask = { title: 'Go for a walk', completed: false };
const taskId = tasksCRUD.create(newTask);
console.log(taskId);

// Read
const task = tasksCRUD.read({ id: 't1' });
console.log(task);

// Update
const updatedTask = { id: 't1', title: 'Read two books', completed: true };
tasksCRUD.update(updatedTask);

// Delete
tasksCRUD.delete({ id: 't2' });

// List
const allTasks = tasksCRUD.list();
console.log(allTasks);

// Search
const searchTasks = tasksCRUD.search('walk');
console.log(searchTasks);

Contributing

Contributions are welcome! If you would like to contribute to this module, please follow these guidelines:

  • Fork the repository
  • Create a new branch for your changes
  • Make your changes and commit them with descriptive commit messages
  • Push your changes to your fork
  • Submit a pull request

License

This module is released under the MIT License.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.14922latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.14922
1.0.1482
1.0.1472
1.0.1442
1.0.1431
1.0.1421
1.0.1411
1.0.1401
1.0.1391
1.0.1381
1.0.1371
1.0.1361
1.0.1351
1.0.1341
1.0.1331
1.0.1321
1.0.1311
1.0.1301
1.0.1291
1.0.1281
1.0.1271
1.0.1251
1.0.1241
1.0.1232
1.0.1221
1.0.1212
1.0.1201
1.0.1191
1.0.1183
1.0.1171
1.0.1161
1.0.1151
1.0.1141
1.0.1131
1.0.1122
1.0.1112
1.0.1102
1.0.1091
1.0.1081
1.0.1071
1.0.1061
1.0.1051
1.0.1041
1.0.1031
1.0.1021
1.0.1011
1.0.1001
1.0.991
1.0.981
1.0.971
1.0.961
1.0.951
1.0.941
1.0.932
1.0.921
1.0.851
1.0.691
1.0.671
1.0.591
1.0.581
1.0.571
1.0.561
1.0.552
1.0.541
1.0.532
1.0.521
1.0.512
1.0.501
1.0.492
1.0.481
1.0.471
1.0.461
1.0.451
1.0.441
1.0.431
1.0.421
1.0.411
1.0.401
1.0.393
1.0.383
1.0.371
1.0.362
1.0.351
1.0.331
1.0.321
1.0.312
1.0.301
1.0.282
1.0.241
1.0.232
1.0.221
1.0.211
1.0.201
1.0.192
1.0.182
1.0.171
1.0.161
1.0.151
1.0.141
1.0.131
1.0.121
1.0.111
1.0.101
1.0.91
1.0.82
1.0.71
1.0.61
1.0.51
1.0.41
1.0.31
1.0.21
1.0.11
1.0.01

Package Sidebar

Install

npm i @brainstack/store

Weekly Downloads

42

Version

1.0.149

License

MIT

Unpacked Size

36.4 kB

Total Files

24

Last publish

Collaborators

  • infinisoft-world
  • amitbasunia
  • bincharkey
  • hgharbi