An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# mutent-array

[![npm](https://img.shields.io/npm/v/mutent-array)](https://www.npmjs.com/package/mutent-array)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
[![ci](https://github.com/greguz/mutent-array/actions/workflows/ci.yaml/badge.svg?branch=master)](https://github.com/greguz/mutent-array/actions/workflows/ci.yaml)
[![Coverage Status](https://coveralls.io/repos/github/greguz/mutent-array/badge.svg?branch=master)](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)
```