react-rock 3.2.1 β 3.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Store.d.ts +31 -0
- package/Store.js +266 -0
- package/Store.js.map +1 -0
- package/Store.mjs +264 -0
- package/Store.mjs.map +1 -0
- package/index.d.ts +4 -59
- package/index.js +10 -203
- package/index.js.map +1 -1
- package/index.mjs +8 -203
- package/index.mjs.map +1 -1
- package/package.json +5 -10
- package/readme.md +230 -230
- package/types.d.ts +29 -0
- package/Finder.js +0 -86
- package/Finder.js.map +0 -1
- package/Finder.mjs +0 -86
- package/Finder.mjs.map +0 -1
package/readme.md
CHANGED
|
@@ -1,231 +1,231 @@
|
|
|
1
|
-
<p align="center">
|
|
2
|
-
<img width="120" src="https://raw.githubusercontent.com/devnax/react-rock/main/logo.png" alt="React Rock logo">
|
|
3
|
-
</p>
|
|
4
|
-
|
|
5
|
-
<h1 align="center">React Rock</h1>
|
|
6
|
-
|
|
7
|
-
React-Rock is a lightweight package for managing global state in React applications. It simplifies handling data by providing a store with rows and metadata, while offering methods to perform CRUD operations and more. It enables easy integration with React components, making it an ideal solution for managing complex state in large applications.
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
## Installation
|
|
11
|
-
|
|
12
|
-
To install the React-Rock package, run the following command in your project:
|
|
13
|
-
|
|
14
|
-
```bash
|
|
15
|
-
npm install react-rock
|
|
16
|
-
```
|
|
17
|
-
|
|
18
|
-
## Features
|
|
19
|
-
|
|
20
|
-
- **Global Store Management**: Manage rows and meta data in a global store.
|
|
21
|
-
- **CRUD Operations**: Perform create, read, update, and delete operations on rows.
|
|
22
|
-
- **Meta Management**: Set, get, and delete meta data.
|
|
23
|
-
- **Optimized Re-renders**: Control component re-renders with the `freeze` option.
|
|
24
|
-
- **Class Component Support**: Use the `StoreComponent` for integrating store data into class components.
|
|
25
|
-
|
|
26
|
-
## Basic Example: Creating a Store and Adding Records
|
|
27
|
-
|
|
28
|
-
To create a new store and add a record, use the `createStore` function. Here's an example:
|
|
29
|
-
|
|
30
|
-
```typescript
|
|
31
|
-
import { createStore } from 'react-rock';
|
|
32
|
-
|
|
33
|
-
// Define RowType and MetaType
|
|
34
|
-
type RowType = { name: string, age: number };
|
|
35
|
-
type MetaType = { anykey: any };
|
|
36
|
-
|
|
37
|
-
const default_rows = [{ name: '', age: 0 }]
|
|
38
|
-
// Create a store
|
|
39
|
-
const users = createStore<RowType, MetaType>(default_rows, { anykey: '' });
|
|
40
|
-
|
|
41
|
-
// Add a new row to the store
|
|
42
|
-
users.create({ name: 'John Doe', age: 30 });
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
### RowType Explained
|
|
46
|
-
|
|
47
|
-
When a row is created, it will have the following properties:
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
type RowType<Row> = Row & {
|
|
51
|
-
_id: string; // Unique identifier for the row
|
|
52
|
-
_index: number; // Index of the row in the store
|
|
53
|
-
_observe: number; // Internal property to track changes
|
|
54
|
-
}
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
Each row will include the original data (`Row`) and some additional properties like `_id`, `_index`, and `_observe`.
|
|
58
|
-
|
|
59
|
-
## Methods
|
|
60
|
-
|
|
61
|
-
Hereβs a table with all available methods and their descriptions:
|
|
62
|
-
|
|
63
|
-
| Method | Description |
|
|
64
|
-
| ------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
65
|
-
| `create(row, freeze?)` | Adds a new record to the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
66
|
-
| `createMany(rows, freeze?)` | Adds multiple records to the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
67
|
-
| `update(row, where, freeze?)` | Updates records based on the condition specified in `where`. |
|
|
68
|
-
| `updateAll(row, freeze?)` | Updates all records in the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
69
|
-
| `delete(where, freeze?)` | Deletes records based on the condition specified in `where`. |
|
|
70
|
-
| `move(oldIdx, newIdx, freeze?)` | Moves a record from one index to another. |
|
|
71
|
-
| `clearAll(freeze?)` | Clears all records from the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
72
|
-
| `getAll(args?)` | Retrieves all rows from the store. |
|
|
73
|
-
| `find(where, args?)` | Finds rows based on a condition specified in `where`. |
|
|
74
|
-
| `findFirst(where, freeze?)` | Finds the first row that matches the condition in `where`. |
|
|
75
|
-
| `findById(_id, freeze?)` | Finds a row by its `_id`. |
|
|
76
|
-
| `setMeta(key, value, freeze?)` | Sets a value for a specific meta key. |
|
|
77
|
-
| `getMeta(key, freeze?)` | Retrieves the value of a specific meta key. |
|
|
78
|
-
| `getAllMeta(freeze?)` | Retrieves all meta data from the store. |
|
|
79
|
-
| `deleteMeta(key, freeze?)` | Deletes a specific meta key. |
|
|
80
|
-
| `clearMeta(freeze?)` | Clears all meta data from the store. |
|
|
81
|
-
|
|
82
|
-
### Example of the `find` Method
|
|
83
|
-
|
|
84
|
-
The `find` method allows you to search for rows in the store based on specific conditions:
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
const foundUsers = users.find({ name: 'John Doe' } );
|
|
88
|
-
console.log(foundUsers);
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
### Re-rendering in React Components
|
|
93
|
-
|
|
94
|
-
React-Rock optimizes re-renders by offering a freeze mechanism. When a store update occurs and the `freeze` option is enabled, React components that access the store using methods like `find` or `findFirst` will not automatically re-render. This gives you control over when your components should re-render, improving performance in large applications.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
## WhereType
|
|
98
|
-
|
|
99
|
-
The `WhereType` is used to specify conditions when querying rows. It defines a query structure for filtering rows.
|
|
100
|
-
|
|
101
|
-
### QueryValueType
|
|
102
|
-
|
|
103
|
-
The `QueryValueType` is used within `WhereType` to define possible conditions for querying:
|
|
104
|
-
|
|
105
|
-
| Property | Description |
|
|
106
|
-
| -------------- | ----------------------------------------------------------------- |
|
|
107
|
-
| `contain` | Finds values containing the specified string, number, or boolean. |
|
|
108
|
-
| `startWith` | Finds values that start with the specified string or number. |
|
|
109
|
-
| `endWith` | Finds values that end with the specified string or number. |
|
|
110
|
-
| `equalWith` | Finds values that are exactly equal to the specified value. |
|
|
111
|
-
| `notEqualWith` | Finds values that are not equal to the specified value. |
|
|
112
|
-
| `gt` | Finds values greater than the specified number. |
|
|
113
|
-
| `lt` | Finds values less than the specified number. |
|
|
114
|
-
| `gte` | Finds values greater than or equal to the specified number. |
|
|
115
|
-
| `lte` | Finds values less than or equal to the specified number. |
|
|
116
|
-
|
|
117
|
-
### Example of WhereType
|
|
118
|
-
|
|
119
|
-
```typescript
|
|
120
|
-
const usersOver30 = users.find({ age: { gt: 30 } });
|
|
121
|
-
console.log(usersOver30);
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
## ArgsType
|
|
125
|
-
|
|
126
|
-
The `ArgsType` defines options for customizing query behavior, such as selecting specific rows or skipping rows.
|
|
127
|
-
|
|
128
|
-
| Property | Description |
|
|
129
|
-
| -------- | --------------------------------------------------------- |
|
|
130
|
-
| `getRow` | Custom function to process rows before returning them. |
|
|
131
|
-
| `skip` | Number of rows to skip. |
|
|
132
|
-
| `take` | Number of rows to return. |
|
|
133
|
-
| `freeze` | If `true`, prevents re-rendering when accessing the data. |
|
|
134
|
-
|
|
135
|
-
## Example with Class Component
|
|
136
|
-
|
|
137
|
-
To use the store in a class component, extend the `StoreComponent` class:
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
import { StoreComponent } from 'react-rock';
|
|
141
|
-
|
|
142
|
-
class UserList extends StoreComponent {
|
|
143
|
-
render() {
|
|
144
|
-
const allUsers = users.getAll();
|
|
145
|
-
return (
|
|
146
|
-
<div>
|
|
147
|
-
{allUsers.map(user => <div key={user._id}>{user.name}</div>)}
|
|
148
|
-
</div>
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
## CRUD Example
|
|
155
|
-
|
|
156
|
-
```typescript
|
|
157
|
-
// Create a new user
|
|
158
|
-
users.create({ name: 'Alice', age: 25 });
|
|
159
|
-
|
|
160
|
-
// Update a user
|
|
161
|
-
users.update({ age: 26 }, { name: 'Alice' } );
|
|
162
|
-
|
|
163
|
-
// Delete a user
|
|
164
|
-
users.delete({ name: 'Alice' } );
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
## Examples with `find` and Query
|
|
168
|
-
|
|
169
|
-
```typescript
|
|
170
|
-
// Find users over the age of 25
|
|
171
|
-
const usersOver25 = users.find({ age: { gt: 25 } });
|
|
172
|
-
console.log(usersOver25);
|
|
173
|
-
|
|
174
|
-
// Find the first user with the name 'Alice'
|
|
175
|
-
const alice = users.findFirst({ name: 'Alice' } );
|
|
176
|
-
console.log(alice);
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
## Example of Using the Store in Multiple Components
|
|
180
|
-
|
|
181
|
-
React-Rock allows you to share the same store across multiple components, ensuring a consistent state throughout the app:
|
|
182
|
-
|
|
183
|
-
```typescript
|
|
184
|
-
import { StoreComponent } from 'react-rock';
|
|
185
|
-
|
|
186
|
-
class UserList extends StoreComponent {
|
|
187
|
-
render() {
|
|
188
|
-
const users = users.getAll();
|
|
189
|
-
return (
|
|
190
|
-
<div>
|
|
191
|
-
{users.map(user => <div key={user._id}>{user.name}</div>)}
|
|
192
|
-
</div>
|
|
193
|
-
);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
class UserProfile extends StoreComponent {
|
|
198
|
-
render() {
|
|
199
|
-
const user = users.findFirst({ name: 'John Doe' });
|
|
200
|
-
return <div>{user ? user.name : 'User not found'}</div>;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
## Explanation of Types
|
|
208
|
-
|
|
209
|
-
- **RowType**: Represents a record with an `_id`, `_index`, and `_observe` along with user-defined data fields.
|
|
210
|
-
- **ArgsType**: Defines the options for querying rows with flexibility like skipping, taking, and custom row processing.
|
|
211
|
-
- **WhereType**: Represents the conditions for querying records, using fields like `contain`, `equalWith`, and range queries like `gt`, `lt`, etc.
|
|
212
|
-
- **QueryValueType**: Specifies the allowed condition types for filtering rows based on field values.
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
## License
|
|
216
|
-
|
|
217
|
-
This package is licensed under the MIT License.
|
|
218
|
-
|
|
219
|
-
---
|
|
220
|
-
|
|
221
|
-
This documentation should provide a concise overview of how to use the `react-rock` package effectively.
|
|
222
|
-
|
|
223
|
-
## π€ Contributing
|
|
224
|
-
|
|
225
|
-
Contributions are welcome! Please check out the [contribution guidelines](https://github.com/devnax/react-rock).
|
|
226
|
-
|
|
227
|
-
---
|
|
228
|
-
|
|
229
|
-
## π License
|
|
230
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img width="120" src="https://raw.githubusercontent.com/devnax/react-rock/main/logo.png" alt="React Rock logo">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<h1 align="center">React Rock</h1>
|
|
6
|
+
|
|
7
|
+
React-Rock is a lightweight package for managing global state in React applications. It simplifies handling data by providing a store with rows and metadata, while offering methods to perform CRUD operations and more. It enables easy integration with React components, making it an ideal solution for managing complex state in large applications.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
11
|
+
|
|
12
|
+
To install the React-Rock package, run the following command in your project:
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
npm install react-rock
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- **Global Store Management**: Manage rows and meta data in a global store.
|
|
21
|
+
- **CRUD Operations**: Perform create, read, update, and delete operations on rows.
|
|
22
|
+
- **Meta Management**: Set, get, and delete meta data.
|
|
23
|
+
- **Optimized Re-renders**: Control component re-renders with the `freeze` option.
|
|
24
|
+
- **Class Component Support**: Use the `StoreComponent` for integrating store data into class components.
|
|
25
|
+
|
|
26
|
+
## Basic Example: Creating a Store and Adding Records
|
|
27
|
+
|
|
28
|
+
To create a new store and add a record, use the `createStore` function. Here's an example:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { createStore } from 'react-rock';
|
|
32
|
+
|
|
33
|
+
// Define RowType and MetaType
|
|
34
|
+
type RowType = { name: string, age: number };
|
|
35
|
+
type MetaType = { anykey: any };
|
|
36
|
+
|
|
37
|
+
const default_rows = [{ name: '', age: 0 }]
|
|
38
|
+
// Create a store
|
|
39
|
+
const users = createStore<RowType, MetaType>(default_rows, { anykey: '' });
|
|
40
|
+
|
|
41
|
+
// Add a new row to the store
|
|
42
|
+
users.create({ name: 'John Doe', age: 30 });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### RowType Explained
|
|
46
|
+
|
|
47
|
+
When a row is created, it will have the following properties:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
type RowType<Row> = Row & {
|
|
51
|
+
_id: string; // Unique identifier for the row
|
|
52
|
+
_index: number; // Index of the row in the store
|
|
53
|
+
_observe: number; // Internal property to track changes
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Each row will include the original data (`Row`) and some additional properties like `_id`, `_index`, and `_observe`.
|
|
58
|
+
|
|
59
|
+
## Methods
|
|
60
|
+
|
|
61
|
+
Hereβs a table with all available methods and their descriptions:
|
|
62
|
+
|
|
63
|
+
| Method | Description |
|
|
64
|
+
| ------------------------------- | -------------------------------------------------------------------------------------------- |
|
|
65
|
+
| `create(row, freeze?)` | Adds a new record to the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
66
|
+
| `createMany(rows, freeze?)` | Adds multiple records to the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
67
|
+
| `update(row, where, freeze?)` | Updates records based on the condition specified in `where`. |
|
|
68
|
+
| `updateAll(row, freeze?)` | Updates all records in the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
69
|
+
| `delete(where, freeze?)` | Deletes records based on the condition specified in `where`. |
|
|
70
|
+
| `move(oldIdx, newIdx, freeze?)` | Moves a record from one index to another. |
|
|
71
|
+
| `clearAll(freeze?)` | Clears all records from the store. Optionally, prevents re-rendering if `freeze` is `true`. |
|
|
72
|
+
| `getAll(args?)` | Retrieves all rows from the store. |
|
|
73
|
+
| `find(where, args?)` | Finds rows based on a condition specified in `where`. |
|
|
74
|
+
| `findFirst(where, freeze?)` | Finds the first row that matches the condition in `where`. |
|
|
75
|
+
| `findById(_id, freeze?)` | Finds a row by its `_id`. |
|
|
76
|
+
| `setMeta(key, value, freeze?)` | Sets a value for a specific meta key. |
|
|
77
|
+
| `getMeta(key, freeze?)` | Retrieves the value of a specific meta key. |
|
|
78
|
+
| `getAllMeta(freeze?)` | Retrieves all meta data from the store. |
|
|
79
|
+
| `deleteMeta(key, freeze?)` | Deletes a specific meta key. |
|
|
80
|
+
| `clearMeta(freeze?)` | Clears all meta data from the store. |
|
|
81
|
+
|
|
82
|
+
### Example of the `find` Method
|
|
83
|
+
|
|
84
|
+
The `find` method allows you to search for rows in the store based on specific conditions:
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
const foundUsers = users.find({ name: 'John Doe' } );
|
|
88
|
+
console.log(foundUsers);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
### Re-rendering in React Components
|
|
93
|
+
|
|
94
|
+
React-Rock optimizes re-renders by offering a freeze mechanism. When a store update occurs and the `freeze` option is enabled, React components that access the store using methods like `find` or `findFirst` will not automatically re-render. This gives you control over when your components should re-render, improving performance in large applications.
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
## WhereType
|
|
98
|
+
|
|
99
|
+
The `WhereType` is used to specify conditions when querying rows. It defines a query structure for filtering rows.
|
|
100
|
+
|
|
101
|
+
### QueryValueType
|
|
102
|
+
|
|
103
|
+
The `QueryValueType` is used within `WhereType` to define possible conditions for querying:
|
|
104
|
+
|
|
105
|
+
| Property | Description |
|
|
106
|
+
| -------------- | ----------------------------------------------------------------- |
|
|
107
|
+
| `contain` | Finds values containing the specified string, number, or boolean. |
|
|
108
|
+
| `startWith` | Finds values that start with the specified string or number. |
|
|
109
|
+
| `endWith` | Finds values that end with the specified string or number. |
|
|
110
|
+
| `equalWith` | Finds values that are exactly equal to the specified value. |
|
|
111
|
+
| `notEqualWith` | Finds values that are not equal to the specified value. |
|
|
112
|
+
| `gt` | Finds values greater than the specified number. |
|
|
113
|
+
| `lt` | Finds values less than the specified number. |
|
|
114
|
+
| `gte` | Finds values greater than or equal to the specified number. |
|
|
115
|
+
| `lte` | Finds values less than or equal to the specified number. |
|
|
116
|
+
|
|
117
|
+
### Example of WhereType
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
const usersOver30 = users.find({ age: { gt: 30 } });
|
|
121
|
+
console.log(usersOver30);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## ArgsType
|
|
125
|
+
|
|
126
|
+
The `ArgsType` defines options for customizing query behavior, such as selecting specific rows or skipping rows.
|
|
127
|
+
|
|
128
|
+
| Property | Description |
|
|
129
|
+
| -------- | --------------------------------------------------------- |
|
|
130
|
+
| `getRow` | Custom function to process rows before returning them. |
|
|
131
|
+
| `skip` | Number of rows to skip. |
|
|
132
|
+
| `take` | Number of rows to return. |
|
|
133
|
+
| `freeze` | If `true`, prevents re-rendering when accessing the data. |
|
|
134
|
+
|
|
135
|
+
## Example with Class Component
|
|
136
|
+
|
|
137
|
+
To use the store in a class component, extend the `StoreComponent` class:
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { StoreComponent } from 'react-rock';
|
|
141
|
+
|
|
142
|
+
class UserList extends StoreComponent {
|
|
143
|
+
render() {
|
|
144
|
+
const allUsers = users.getAll();
|
|
145
|
+
return (
|
|
146
|
+
<div>
|
|
147
|
+
{allUsers.map(user => <div key={user._id}>{user.name}</div>)}
|
|
148
|
+
</div>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## CRUD Example
|
|
155
|
+
|
|
156
|
+
```typescript
|
|
157
|
+
// Create a new user
|
|
158
|
+
users.create({ name: 'Alice', age: 25 });
|
|
159
|
+
|
|
160
|
+
// Update a user
|
|
161
|
+
users.update({ age: 26 }, { name: 'Alice' } );
|
|
162
|
+
|
|
163
|
+
// Delete a user
|
|
164
|
+
users.delete({ name: 'Alice' } );
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
## Examples with `find` and Query
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
// Find users over the age of 25
|
|
171
|
+
const usersOver25 = users.find({ age: { gt: 25 } });
|
|
172
|
+
console.log(usersOver25);
|
|
173
|
+
|
|
174
|
+
// Find the first user with the name 'Alice'
|
|
175
|
+
const alice = users.findFirst({ name: 'Alice' } );
|
|
176
|
+
console.log(alice);
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## Example of Using the Store in Multiple Components
|
|
180
|
+
|
|
181
|
+
React-Rock allows you to share the same store across multiple components, ensuring a consistent state throughout the app:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
import { StoreComponent } from 'react-rock';
|
|
185
|
+
|
|
186
|
+
class UserList extends StoreComponent {
|
|
187
|
+
render() {
|
|
188
|
+
const users = users.getAll();
|
|
189
|
+
return (
|
|
190
|
+
<div>
|
|
191
|
+
{users.map(user => <div key={user._id}>{user.name}</div>)}
|
|
192
|
+
</div>
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
class UserProfile extends StoreComponent {
|
|
198
|
+
render() {
|
|
199
|
+
const user = users.findFirst({ name: 'John Doe' });
|
|
200
|
+
return <div>{user ? user.name : 'User not found'}</div>;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
## Explanation of Types
|
|
208
|
+
|
|
209
|
+
- **RowType**: Represents a record with an `_id`, `_index`, and `_observe` along with user-defined data fields.
|
|
210
|
+
- **ArgsType**: Defines the options for querying rows with flexibility like skipping, taking, and custom row processing.
|
|
211
|
+
- **WhereType**: Represents the conditions for querying records, using fields like `contain`, `equalWith`, and range queries like `gt`, `lt`, etc.
|
|
212
|
+
- **QueryValueType**: Specifies the allowed condition types for filtering rows based on field values.
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
This package is licensed under the MIT License.
|
|
218
|
+
|
|
219
|
+
---
|
|
220
|
+
|
|
221
|
+
This documentation should provide a concise overview of how to use the `react-rock` package effectively.
|
|
222
|
+
|
|
223
|
+
## π€ Contributing
|
|
224
|
+
|
|
225
|
+
Contributions are welcome! Please check out the [contribution guidelines](https://github.com/devnax/react-rock).
|
|
226
|
+
|
|
227
|
+
---
|
|
228
|
+
|
|
229
|
+
## π License
|
|
230
|
+
|
|
231
231
|
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { XVInstanceType, Infer } from 'xanv';
|
|
2
|
+
|
|
3
|
+
type RowSchema = {
|
|
4
|
+
[key: string]: XVInstanceType;
|
|
5
|
+
};
|
|
6
|
+
type MetaSchema = {
|
|
7
|
+
[key: string]: XVInstanceType;
|
|
8
|
+
};
|
|
9
|
+
type MakeRowType<RS> = Infer<RS> & {
|
|
10
|
+
id: number;
|
|
11
|
+
observe: number;
|
|
12
|
+
};
|
|
13
|
+
type RowValueType = string | number | boolean | null | undefined;
|
|
14
|
+
type QueryValueType = {
|
|
15
|
+
contain?: RowValueType;
|
|
16
|
+
startWith?: string | number;
|
|
17
|
+
endWith?: string | number;
|
|
18
|
+
equalWith?: RowValueType;
|
|
19
|
+
notEqualWith?: RowValueType;
|
|
20
|
+
gt?: number;
|
|
21
|
+
lt?: number;
|
|
22
|
+
gte?: number;
|
|
23
|
+
lte?: number;
|
|
24
|
+
};
|
|
25
|
+
type WhereType<RS> = {
|
|
26
|
+
[key in keyof Infer<RS>]?: RowValueType | QueryValueType;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type { MakeRowType, MetaSchema, QueryValueType, RowSchema, RowValueType, WhereType };
|
package/Finder.js
DELETED
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
'use strict';Object.defineProperty(exports,'__esModule',{value:true});const isOb = (ob) => typeof ob === "object" && !Array.isArray(ob) && ob !== null;
|
|
2
|
-
const isNum = (n) => typeof n === 'number';
|
|
3
|
-
const excuteQuery = {
|
|
4
|
-
contain: (v, qv) => typeof v === 'string' && v.search(qv) !== -1,
|
|
5
|
-
startWith: (v, qv) => typeof v === 'string' && v.startsWith(qv),
|
|
6
|
-
endWith: (v, qv) => typeof v === 'string' && v.endsWith(qv),
|
|
7
|
-
equalWith: (v, qv) => v === qv,
|
|
8
|
-
notEqualWith: (v, qv) => v !== qv,
|
|
9
|
-
lt: (v, qv) => isNum(v) && v < qv,
|
|
10
|
-
gt: (v, qv) => isNum(v) && v > qv,
|
|
11
|
-
lte: (v, qv) => isNum(v) && v <= qv,
|
|
12
|
-
gte: (v, qv) => isNum(v) && v >= qv,
|
|
13
|
-
};
|
|
14
|
-
const isInQuery = (rowVal, queryObject) => {
|
|
15
|
-
let match = true;
|
|
16
|
-
for (let queryKey in queryObject) {
|
|
17
|
-
let qVal = queryObject[queryKey];
|
|
18
|
-
const qcb = excuteQuery[queryKey];
|
|
19
|
-
if (qcb && !qcb(rowVal, qVal)) {
|
|
20
|
-
match = false;
|
|
21
|
-
break;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return match;
|
|
25
|
-
};
|
|
26
|
-
const Finder = (rows, query, args) => {
|
|
27
|
-
let result = [];
|
|
28
|
-
let indexes = [];
|
|
29
|
-
let ids = [];
|
|
30
|
-
for (let i = 0; i < rows.length; i++) {
|
|
31
|
-
let row = rows[i];
|
|
32
|
-
let found = false;
|
|
33
|
-
if (isOb(query)) {
|
|
34
|
-
for (let rowKey in query) {
|
|
35
|
-
let queryVal = query[rowKey];
|
|
36
|
-
if (!(rowKey in row))
|
|
37
|
-
break;
|
|
38
|
-
if (isOb(queryVal)) {
|
|
39
|
-
if (isInQuery(row[rowKey], queryVal)) {
|
|
40
|
-
found = true;
|
|
41
|
-
}
|
|
42
|
-
else {
|
|
43
|
-
found = false;
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
else if (row[rowKey] === queryVal) {
|
|
48
|
-
found = true;
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
found = false;
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
found = true;
|
|
58
|
-
}
|
|
59
|
-
if (found) {
|
|
60
|
-
if (args === null || args === void 0 ? void 0 : args.getRow) {
|
|
61
|
-
let r = args.getRow(Object.assign({}, row), i);
|
|
62
|
-
if (r) {
|
|
63
|
-
row = r;
|
|
64
|
-
}
|
|
65
|
-
else {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
result.push(Object.assign(Object.assign({}, row), { _index: i }));
|
|
70
|
-
indexes.push(i);
|
|
71
|
-
ids.push(row._id);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const take = (args === null || args === void 0 ? void 0 : args.take) || result.length;
|
|
75
|
-
const skip = (args === null || args === void 0 ? void 0 : args.skip) || 0;
|
|
76
|
-
if ((args === null || args === void 0 ? void 0 : args.take) && (args === null || args === void 0 ? void 0 : args.skip)) {
|
|
77
|
-
result = result.splice(skip, take);
|
|
78
|
-
}
|
|
79
|
-
else if (!(args === null || args === void 0 ? void 0 : args.skip) && (args === null || args === void 0 ? void 0 : args.take)) {
|
|
80
|
-
result.splice(args.take);
|
|
81
|
-
}
|
|
82
|
-
else if (!(args === null || args === void 0 ? void 0 : args.take) && (args === null || args === void 0 ? void 0 : args.skip)) {
|
|
83
|
-
result = result.splice(args.skip);
|
|
84
|
-
}
|
|
85
|
-
return { rows: result, indexes, ids };
|
|
86
|
-
};exports.default=Finder;exports.isOb=isOb;//# sourceMappingURL=Finder.js.map
|
package/Finder.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"Finder.js","sources":["../src/Finder.ts"],"sourcesContent":["import { FinderArgsType, QueryType, QueryValueType } from \"./types\"\n\nexport const isOb = (ob: any) => typeof ob === \"object\" && !Array.isArray(ob) && ob !== null\nconst isNum = (n: any) => typeof n === 'number'\nconst excuteQuery: any = {\n contain: (v: any, qv: any) => typeof v === 'string' && v.search(qv) !== -1,\n startWith: (v: any, qv: any) => typeof v === 'string' && v.startsWith(qv),\n endWith: (v: any, qv: any) => typeof v === 'string' && v.endsWith(qv),\n equalWith: (v: any, qv: any) => v === qv,\n notEqualWith: (v: any, qv: any) => v !== qv,\n lt: (v: any, qv: any) => isNum(v) && v < qv,\n gt: (v: any, qv: any) => isNum(v) && v > qv,\n lte: (v: any, qv: any) => isNum(v) && v <= qv,\n gte: (v: any, qv: any) => isNum(v) && v >= qv,\n}\n\nconst isInQuery = <Row>(rowVal: Row, queryObject: QueryValueType) => {\n let match = true\n for (let queryKey in queryObject) {\n let qVal = (queryObject as any)[queryKey]\n const qcb = excuteQuery[queryKey]\n if (qcb && !qcb(rowVal, qVal)) {\n match = false;\n break;\n }\n }\n return match\n}\n\nconst Finder = <Row extends object>(rows: Row[], query: null | QueryType<Row>, args?: FinderArgsType<Row>) => {\n let result: Row[] = []\n let indexes: number[] = []\n let ids: string[] = []\n\n for (let i = 0; i < rows.length; i++) {\n let row = rows[i]\n let found = false;\n if (isOb(query)) {\n for (let rowKey in query) {\n let queryVal = query[rowKey]\n if (!(rowKey in row)) break;\n\n if (isOb(queryVal)) {\n if (isInQuery(row[rowKey], queryVal as any)) {\n found = true\n } else {\n found = false\n break;\n }\n } else if (row[rowKey] === queryVal) {\n found = true;\n } else {\n found = false\n break;\n }\n }\n } else {\n found = true\n }\n\n if (found) {\n if (args?.getRow) {\n let r = args.getRow({ ...row }, i)\n if (r) {\n row = r\n } else {\n continue;\n }\n }\n result.push({ ...row, _index: i })\n indexes.push(i)\n ids.push((row as any)._id)\n }\n }\n\n const take = args?.take || result.length\n const skip = args?.skip || 0\n\n if (args?.take && args?.skip) {\n result = result.splice(skip, take)\n } else if (!args?.skip && args?.take) {\n result.splice(args.take)\n } else if (!args?.take && args?.skip) {\n result = result.splice(args.skip)\n }\n\n return { rows: result, indexes, ids }\n}\n\nexport default Finder"],"names":[],"mappings":"sEAEa,MAAA,IAAI,GAAG,CAAC,EAAO,KAAK,OAAO,EAAE,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,EAAE,CAAC,IAAI,EAAE,KAAK;AACxF,MAAM,KAAK,GAAG,CAAC,CAAM,KAAK,OAAO,CAAC,KAAK,QAAQ;AAC/C,MAAM,WAAW,GAAQ;IACrB,OAAO,EAAE,CAAC,CAAM,EAAE,EAAO,KAAK,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE;AAC1E,IAAA,SAAS,EAAE,CAAC,CAAM,EAAE,EAAO,KAAK,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,UAAU,CAAC,EAAE,CAAC;AACzE,IAAA,OAAO,EAAE,CAAC,CAAM,EAAE,EAAO,KAAK,OAAO,CAAC,KAAK,QAAQ,IAAI,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;IACrE,SAAS,EAAE,CAAC,CAAM,EAAE,EAAO,KAAK,CAAC,KAAK,EAAE;IACxC,YAAY,EAAE,CAAC,CAAM,EAAE,EAAO,KAAK,CAAC,KAAK,EAAE;AAC3C,IAAA,EAAE,EAAE,CAAC,CAAM,EAAE,EAAO,KAAK,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE;AAC3C,IAAA,EAAE,EA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