/mutent-array

Simple array adapter for mutent

Primary LanguageJavaScript

mutent-array

npm JavaScript Style Guide ci Coverage Status

Simple in memory (array) Adapter for Mutent.

Features

  • MongoDB-like queries (see 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]: <Object>
    • [items]: <Iterable> Initialize the internal array. Accepts any sync iterable. If It's an array, It will be used directly (adapter will change the array).
    • [onLostUpdate]: <String> See onLostUpdate chapter.
    • [onLostDelete]: <String> See onLostUpdate 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

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)