1.0.144 • Published 12 months ago

@brainstack/store v1.0.144

Weekly downloads
-
License
MIT
Repository
-
Last release
12 months ago

@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.

1.0.143

12 months ago

1.0.142

12 months ago

1.0.144

12 months ago

1.0.141

1 year ago

1.0.140

1 year ago

1.0.139

1 year ago

1.0.132

1 year ago

1.0.134

1 year ago

1.0.133

1 year ago

1.0.136

1 year ago

1.0.135

1 year ago

1.0.138

1 year ago

1.0.137

1 year ago

1.0.131

1 year ago

1.0.130

1 year ago

1.0.129

1 year ago

1.0.128

1 year ago

1.0.127

1 year ago

1.0.125

1 year ago

1.0.121

1 year ago

1.0.120

1 year ago

1.0.123

1 year ago

1.0.122

1 year ago

1.0.124

1 year ago

1.0.119

1 year ago

1.0.118

1 year ago

1.0.117

1 year ago

1.0.116

1 year ago

1.0.109

1 year ago

1.0.108

1 year ago

1.0.110

1 year ago

1.0.112

1 year ago

1.0.111

1 year ago

1.0.114

1 year ago

1.0.113

1 year ago

1.0.115

1 year ago

1.0.107

1 year ago

1.0.101

1 year ago

1.0.100

1 year ago

1.0.106

1 year ago

1.0.103

1 year ago

1.0.102

1 year ago

1.0.105

1 year ago

1.0.104

1 year ago

1.0.99

1 year ago

1.0.98

1 year ago

1.0.97

1 year ago

1.0.96

1 year ago

1.0.95

1 year ago

1.0.94

1 year ago

1.0.93

1 year ago

1.0.92

1 year ago

1.0.85

1 year ago

1.0.69

2 years ago

1.0.67

2 years ago

1.0.40

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.55

2 years ago

1.0.54

2 years ago

1.0.53

2 years ago

1.0.52

2 years ago

1.0.59

2 years ago

1.0.58

2 years ago

1.0.57

2 years ago

1.0.56

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.28

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.22

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago