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

@xbyorange/mercury-memory

v1.2.0

Published

Mercury memory origin

Downloads

6

Readme

Build status Coverage Status Quality Gate

NPM dependencies Last commit Last release

NPM downloads License

Mercury Logo Mercury Memory

Overview

This package provides a Mercury origin for handling memory objects.

  • Mercury queries based on object keys.
  • Reactivity to CRUD actions. When a "create", "update" or "delete" method is called over an instance, the cache clean events are dispatched.

Install

npm i @xbyorange/mercury-memory --save

Api

new Memory(defaultValue[, options || uuid][, options])

  • Arguments
    • defaultValue - <Object>. Object to be stored. Default value is assigned too at the same time.
    • uuid - <String>. Unique id to assign to returned Mercury instance. Useful when using mercury sources handler. To be deprecated in next releases. Options object should be passed as second argument instead of uuid.
    • options - <Object> containing properties:
      • queriesDefaultValue - <Boolean> If true, the default value of queried sources will be the value of the "key" in the query. If not defined, the default value of queried sources will be the full defaultValue object.
      • uuid - <String> Unique id to assign to returned Mercury instance. Useful when using mercury sources handler.
      • tags - <String> or <Array of Strings> Tags to assign to the instance. Useful when using mercury sources handler. A "memory" tag will be always added to provided tags by default.

Methods

query

memory.query(key)

  • Arguments
    • key - <String> Key of the memory object to be read, updated, created or deleted.

Cache

All cache will be cleaned when the update, delete or create methods are executed for any specific query.

Default value

The default value of a "queried" instance will be the complete defaultValue object until the "queriesDefaultValue" option is set as true, in which case the default value will be the value of the key corresponding to the query:

import { Memory } from "@xbyorange/mercury-memory";

const fooMemory = new Memory({
  foo: "foo-value",
  var: "var-value"
});

console.log(fooMemory.query("foo").read.value) // {foo: "foo-value", var: "var-value"}

const fooMemory2 = new Memory({
  foo: "foo-value",
  var: "var-value"
}, {
  queriesDefaultValue: true
});

console.log(fooMemory.query("foo").read.value) // "foo"

Examples

Next example will be easier to understand if you are already familiarized with the mercury syntax.

import { Memory } from "@xbyorange/mercury-memory";

const sessionDetails = new Memory({
  userId: null,
  isLogedIn: false
});

const userId = await sessionDetails.read("userId");

sessionDetails.query("isLogedIn").update(true)

Use Mercury Memory objects in combination with Api Origins, and take advantage of the built-in reactivity. Use the memory objects to query the api origins, and, when you update the Memory Object, the API object caches will be cleaned as a consequence:

import { Memory } from "@xbyorange/mercury-memory";
import { Api } from "@xbyorange/mercury-api";

const currentAuthor = new Memory({
  id: null
});
const booksCollection = new Api("http://api.library.com/books");

const currentAuthorBooks = new Selector(
  { 
    source: currentAuthor,
    query: () => "id"
  },
  {
    source: booksCollection,
    query: (query, previousResults) => {
      if(!previousResults[0]) {
        return;
      }
      return {
        queryString: {
          authorId: previousResults[0]
        }
      };
    }
  },
  (currentAuthorId, booksResults) => booksResults
);

// Api request to "http://api.library.com/books". Return all books
await currentAuthorBooks.read();

// Api request is not repeated, as query has no changed.
await currentAuthorBooks.read();

currentAuthor.query("id").update("foo-author-id");

// Api request now is sent to "http://api.library.com/books?authorId=foo-author-id". Return author books
await currentAuthorBooks.read();

Contributing

Contributors are welcome. Please read the contributing guidelines and code of conduct.