|
| 1 | +# Indexer |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/@plaindb/index) |
| 4 | +[](https://gitlab.com/frenware/framework/plaindb/index/-/pipelines) |
| 5 | +[](https://gitlab.com/frenware/framework/plaindb/index/-/blob/master/LICENSE) |
| 6 | +[](https://www.npmjs.com/package/@plaindb/index) |
| 7 | + |
| 8 | +[](https://gitlab.com/frenware/framework/plaindb/index) |
| 9 | +[](https://github.com/basedwon/index) |
| 10 | +[](https://twitter.com/basdwon) |
| 11 | +[](https://discordapp.com/users/basedwon) |
| 12 | + |
| 13 | +A comprehensive indexing solution built to cater to a wide variety of indexing requirements. It provides a robust API for adding, removing, updating, and finding data based on multiple types of index fields. Including an inverted-indexing of words within a text field. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- Dynamic field creation for indexing |
| 18 | +- Supports single, multi, and text-based fields |
| 19 | +- Extensible with custom index field types |
| 20 | +- Asynchronous API support |
| 21 | +- Transactional batch operations |
| 22 | +- Formatter and reducer customization |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +Install the package with: |
| 27 | + |
| 28 | +```bash |
| 29 | +npm install @plaindb/index |
| 30 | +``` |
| 31 | + |
| 32 | +## Usage |
| 33 | + |
| 34 | +First, import the `Indexer` library. |
| 35 | + |
| 36 | +```js |
| 37 | +import Indexer from '@plaindb/index' |
| 38 | +``` |
| 39 | +or |
| 40 | +```js |
| 41 | +const Indexer = require('@plaindb/index') |
| 42 | +``` |
| 43 | + |
| 44 | +### Initialize the Index |
| 45 | + |
| 46 | +```js |
| 47 | +const storage = // Your storage instance |
| 48 | +const index = new Index(storage, ['name', 'age|gender', 'description text']) |
| 49 | +``` |
| 50 | + |
| 51 | +### Adding an Entity |
| 52 | + |
| 53 | +```js |
| 54 | +await index.add({ |
| 55 | + id: 1, |
| 56 | + name: 'John', |
| 57 | + age: 30, |
| 58 | + gender: 'male', |
| 59 | + description: 'Software Engineer' |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +### Removing an Entity |
| 64 | + |
| 65 | +```js |
| 66 | +await index.remove({ |
| 67 | + id: 1 |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +### Updating an Entity |
| 72 | + |
| 73 | +```js |
| 74 | +await index.update(oldEntity, newEntity) |
| 75 | +``` |
| 76 | + |
| 77 | +### Finding Entities |
| 78 | + |
| 79 | +You can find entities based on index fields: |
| 80 | + |
| 81 | +```js |
| 82 | +const results = await index.find('age', 30) |
| 83 | +``` |
| 84 | + |
| 85 | +### Custom Formatter and Reducer |
| 86 | + |
| 87 | +You can provide custom formatter and reducer functions through the `opts` parameter: |
| 88 | + |
| 89 | +```js |
| 90 | +const index = new Index(storage, ['name'], { |
| 91 | + formatter: new CustomFormatter(), |
| 92 | + reducer: (entity) => entity.customId |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +## Documentation |
| 97 | + |
| 98 | +- [API Reference](/docs/api.md) |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +## API |
| 105 | + |
| 106 | +### Classes |
| 107 | + |
| 108 | +- `Index`: Manages multiple index fields and dispatches operations. |
| 109 | +- `IndexFactory`: Creates index fields dynamically. |
| 110 | +- `IndexField`: Abstract class for index fields. |
| 111 | +- `SingleIndexField`: Index field for single properties. |
| 112 | +- `MultiIndexField`: Index field for multi-properties. |
| 113 | +- `TextIndexField`: Index field for text-based searching. |
| 114 | + |
| 115 | +### Methods |
| 116 | + |
| 117 | +- `Index.by(slug)`: Dynamically add an index field. |
| 118 | +- `Index.add(entity)`: Add an entity to the index. |
| 119 | +- `Index.remove(entity)`: Remove an entity from the index. |
| 120 | +- `Index.update(oldEntity, newEntity)`: Update an entity in the index. |
| 121 | +- `Index.find(query, ...values)`: Find entities based on a query. |
| 122 | + |
| 123 | +## Extending |
| 124 | + |
| 125 | +To extend the library with a custom index field, create a class extending `IndexField` and register it via `Registry`. |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +## API |
| 132 | + |
| 133 | +- `Index` |
| 134 | + - `constructor(storage, fields[], opts)` |
| 135 | + - `by(slug: string): Index` |
| 136 | + - `add(entity: object): Promise` |
| 137 | + - `remove(entity: object): Promise` |
| 138 | + - `update(oldEntity: object, newEntity: object): Promise` |
| 139 | + - `find(query: string|object, ...values): Promise` |
| 140 | +- `IndexField` |
| 141 | + - `constructor(index, options)` |
| 142 | + - `normalize(value): any` |
| 143 | + - `buildOps(type: string, entity: object, ops: array): array` |
| 144 | + - `add(entity: object, ops: array): array` |
| 145 | + - `update(oldEntity: object, newEntity: object, ops: array): array` |
| 146 | + - `remove(entity: object, ops: array): array` |
| 147 | + - `find(...query): Promise` |
| 148 | +- `SingleIndexField` |
| 149 | + - `find(query: any): Promise` |
| 150 | +- `MultiIndexField` |
| 151 | + - `getKeys(field: string): array` |
| 152 | + - `getPath(entity: object): string` |
| 153 | + - `find(...query): Promise` |
| 154 | +- `TextIndexField` |
| 155 | + - `getKeys(field: string): array` |
| 156 | + - `isMatch(oldEntity: object, newEntity: object): boolean` |
| 157 | + - `buildOps(type: string, entity: object, ops: array): array` |
| 158 | + - `find(...query): Promise` |
| 159 | +- `IndexFactory` |
| 160 | + - `constructor(index)` |
| 161 | + - `getFieldKey(slug: string): string` |
| 162 | + - `getFieldType(slug: string): object` |
| 163 | + - `createField(definition: string): IndexField` |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | + |
| 173 | +## Tests |
| 174 | + |
| 175 | +In order to run the test suite, simply clone the repository and install its dependencies: |
| 176 | + |
| 177 | +```bash |
| 178 | +git clone https://gitlab.com/frenware/framework/plaindb/index.git |
| 179 | +cd indexer |
| 180 | +npm install |
| 181 | +``` |
| 182 | + |
| 183 | +To run the tests: |
| 184 | + |
| 185 | +```bash |
| 186 | +npm test |
| 187 | +``` |
| 188 | + |
| 189 | +## Contributing |
| 190 | + |
| 191 | +Thank you! Please see our [contributing guidelines](/docs/contributing.md) for details. |
| 192 | + |
| 193 | +## Donations |
| 194 | + |
| 195 | +If you find this project useful and want to help support further development, please send us some coin. We greatly appreciate any and all contributions. Thank you! |
| 196 | + |
| 197 | +**Bitcoin (BTC):** |
| 198 | +``` |
| 199 | +1JUb1yNFH6wjGekRUW6Dfgyg4J4h6wKKdF |
| 200 | +``` |
| 201 | + |
| 202 | +**Monero (XMR):** |
| 203 | +``` |
| 204 | +46uV2fMZT3EWkBrGUgszJCcbqFqEvqrB4bZBJwsbx7yA8e2WBakXzJSUK8aqT4GoqERzbg4oKT2SiPeCgjzVH6VpSQ5y7KQ |
| 205 | +``` |
| 206 | + |
| 207 | +## License |
| 208 | + |
| 209 | +@plaindb/index is [MIT licensed](https://gitlab.com/frenware/framework/plaindb/index/-/blob/master/LICENSE). |
0 commit comments