npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

loki-hooks-immutable

v0.2.1

Published

A hook for loki-hooks that converts a lokijs collection into a collection with immutable documents

Downloads

10

Readme

loki-hooks-immutable

A loki-hooks factory function that turns a loki collection into a collection with immutable documents. Quering the collection will return immutable documents. Insert/update and remove return immutable documents. The insert and update methods accept immer drafts (created with createDraft) as documents. The insert and update methods will then emit insertEvent/updateEvent events with the document and the immer patches as arguments. The insertEvent/updateEvent and deleteEvent can be disable by setting its name to '' in the options.

Usage

import test from 'ava'
import sinon from 'sinon'
import Loki from 'lokijs'
import { createDraft } from 'immer'
import { Hooks } from 'member-hooks'
import { createHooksLoki } from 'loki-hooks'
import { immutable } from '.'


const dbHooks = new Hooks()
const collectionHooks = new Hooks()
collectionHooks.register('immutable', immutable)

const HooksLoki = createHooksLoki(dbHooks, collectionHooks)

test('should make docs immutable, accept immer draft and emit immer patches', t => {
  const db = new HooksLoki('dbname', {
    adapter: new Loki.LokiMemoryAdapter(),
  })
  const collection = db.addCollection('collection', {
    hooks: {
      config: [['immutable', {
        patches: true, // when patches is true, then generate immer patches
        insertEvent: 'inserted', // emit('inserted', doc, immer patches of changes made by app on immer draft)
        updateEvent: 'updated',  // emit('updated', doc, immer patches of changes made by app on immer draft)
        deleteEvent: 'deleted',  // emit('deleted', doc)
        production: false // when set to true, collection documents will not be frozen
      }]]
    }
  })
  const insertedSpy = sinon.spy((doc: any, patches: any) => {
    t.assert(Object.isFrozen(doc))
    t.deepEqual(patches, {
      "patches": [
        {
          "op": "add",
          "path": [
            "id"
          ],
          "value": "id1"
        }
      ],
      "reversePatches": [
        {
          "op": "remove",
          "path": [
            "id"
          ]
        }
      ]
    })
  })
  collection.addListener('inserted', insertedSpy)
  const updatedSpy = sinon.spy((doc: any, patches: any) => {
    t.assert(Object.isFrozen(doc))
    t.deepEqual(patches, {
      "patches": [
        {
          "op": "add",
          "path": [
            "name"
          ],
          "value": "name1"
        }
      ],
      "reversePatches": [
        {
          "op": "remove",
          "path": [
            "name"
          ]
        }
      ]
    })
  })
  collection.addListener('updated', updatedSpy)
  const deletedSpy = sinon.spy((doc: any) => {
    t.assert(Object.isFrozen(doc))
  })
  collection.addListener('deleted', deletedSpy)
  t.assert(Object.isFrozen(collection.insert({ id: 'id1' }))) // emits 'inserted' event
  t.assert(insertedSpy.calledOnce)
  const inserted = collection.get(1)
  t.assert(Object.isFrozen(inserted))
  const draft = createDraft(inserted)
  draft.name = 'name1'
  collection.update(draft) // emits 'updated' event
  t.assert(updatedSpy.calledOnce)
  const updated = collection.get(1)
  t.assert(Object.isFrozen(updated))
  collection.remove(1) // emits 'deleted' event
  t.assert(deletedSpy.calledOnce)
})