https://github.com/greguz/mutent-array
Simple array adapter for mutent
https://github.com/greguz/mutent-array
Last synced: 5 months ago
JSON representation
Simple array adapter for mutent
- Host: GitHub
- URL: https://github.com/greguz/mutent-array
- Owner: greguz
- Created: 2020-11-30T09:17:39.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-08T15:17:41.000Z (almost 2 years ago)
- Last Synced: 2025-08-09T11:31:50.922Z (10 months ago)
- Language: JavaScript
- Size: 53.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# mutent-array
[](https://www.npmjs.com/package/mutent-array)
[](https://standardjs.com)
[](https://github.com/greguz/mutent-array/actions/workflows/ci.yaml)
[](https://coveralls.io/github/greguz/mutent-array?branch=master)
Simple in memory (array) Adapter for [Mutent](https://github.com/greguz/mutent).
## Features
- MongoDB-like queries (see [mql-match](https://github.com/greguz/mql-match) for more info)
- Raw predicate function support (for maximum freedom)
- Lost-update detection and recovery
- Lost-delete detection
- Source array access with `raw` property
- Native TypeScript support
## API
### `new ArrayAdapter([options])`
Class constructor.
- `[options]: `
- `[items]: ` Initialize the internal array. Accepts any **sync** iterable. If It's an array, It will be used directly (adapter will change the array).
- `[onLostUpdate]: ` See [`onLostUpdate`](#onlostupdate) chapter.
- `[onLostDelete]: ` See [`onLostUpdate`](#onlostdelete) chapter.
### `ArrayAdapter::raw`
Returns (property) the raw array used internally by the Adapter.
## Unwrap options
### `onLostUpdate`
Configures the action to do when a lost-update is detected.
A lost-update occours when someone is referencing a particular item inside the array to update, but that item is not present during the actual write.
Can be `"ERROR"`, `"IGNORE"`, or `"CREATE"`:
- `"ERROR"`: Throws an error.
- `"IGNORE"`: Do nothing.
- `"CREATE"`: Create the item instead.
This option can be specified both from the `ArrayAdapter` constructor, or as unwrap option (takes the precedence).
Defaults to `"ERROR"`.
### `onLostDelete`
Configures the action to do when a lost-delete is detected.
A lost-delete with the same condition of a lost-update, but within a delete request.
Can be `"ERROR"` or `"IGNORE"`.
- `"ERROR"`: Throws an error.
- `"IGNORE"`: Do nothing.
This option can be specified both from the `ArrayAdapter` constructor, or as unwrap option (takes the precedence).
Defaults to `"IGNORE"`.
## Example
```javascript
import { Store } from 'mutent'
import ArrayAdapter from 'mutent-array'
const db = new Store({
adapter: new ArrayAdapter({
items: [
{ id: 0, name: 'Piccolo', power: 329 },
{ id: 1, name: 'Krillin', power: 206 },
{ id: 2, name: 'Turtle', power: 0.001 },
{ id: 3, name: 'Bubbles', power: 1000 },
{ id: 4, name: 'Vegeta', power: 18000 }
],
onLostUpdate: 'ERROR',
onLostDelete: 'IGNORE'
})
})
// 5
console.log(db.raw.length)
const strong = await db.filter({ power: { $gte: 1000 } })
.unwrap()
// Bubbles and Vegeta
console.log(strong)
await db.find({ name: 'Krillin' })
.delete()
.consume()
// 4
console.log(db.raw.length)
```