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

falcor-local-datasource

v1.2.0

Published

An implementation of the Falcor DataSource interface exposing a local JSONGraph object

Downloads

12

Readme

Build Status

Falcor LocalDataSource

Falcor DataSource interface exposing a local JSON Graph object for frontend storage.

Use cases:

  • developing a frontend-only application that doesn't need to persist data between browser reloads
  • simple mocking of a falcor-router for testing or prototyping

Simply using a falcor model with a cache supports all model.get() and model.set() functionality: const model = new falcor.Model({ cache: myJSONGraphObject });. However, the model cache does not allow function methods to be defined on the JSONGraph object, and hence does not expose model.call() operations to, for example, add or delete nodes to/from the graph.

Usage

To initialize the LocalDataSource, simply define methods on the JSONGraph object and pass to the LocalDataSource class constructor. JSONGraph functions can now be invoked via model.call(callPath:Path, arguments:any[], refPaths?: PathSet[], thisPaths?:PathSet[]). The function signature is (graph:JSONGraph, arguments:any[]).

Similar to the Falcor Router, JSONGraph methods must return a JSONGraphEnvelope or Array of PathValues that describe the changes to the graph. It is recommended that you not mutate the graph directly, but rather describe the changes to the graph via the returned JSONGraphEnvelope or PathValues array.

const falcor = require('falcor');
const LocalDatasource = require('../src/index');

const graph = {
  todos: {
    0: { $type: 'ref', value: ['todosById', 'id_0'] },
    1: { $type: 'ref', value: ['todosById', 'id_1'] },
    length: 2,
    add(graph, args) {
      const newTodoLabel = args[0];
      const todoCount = graph.todos.length;
      // NOTE: this is a pretty naive way to derive new ids.  a more robust approach might generate unique ids using
      // a specific node in the graph, or use a closure, or some other mechanism to yield unique incremental values
      const newId = `id_${todoCount}`;

      // return array of pathValues
      return [
        {
          path: ['todos', todoCount],
          value: { $type: 'ref', value: ['todosById', newId] }
        },
        {
          path: ['todosById', newId, 'label'],
          value: newTodoLabel
        },
        {
          path: ['todosById', newId, 'completed'],
          value: false
        },
        {
          path: ['todos', 'length'],
          value: todoCount + 1
        }
      ];
    }
  },
  todosById: {
    id_0: { label: 'tap dance', completed: false },
    id_1: { label: 'see above', completed: false }
  }
};

const model = new falcor.Model({ source: new LocalDatasource(graph) });

As with the Falcor Router, the values returned from the call to model.call() are managed by the refPaths and thisPaths arguments.

model.call(['todos', 'add'], ['dupstep dance party'])
  .subscribe(res => {
    // never runs
  }, err => {
    console.error('Error adding new Todo', err);
  }, () => {
    console.log('added new todo');
  });
// > added new todo

model.call(['todos', 'add'], ['jumpstyle'], [[['label', 'completed']]], [['length']])
  .subscribe(res => {
    console.log('returned', res.json);
  }, err => {
    console.error('Error adding new Todo', err);
  }, () => {
    console.log('added new todo');
  });
// > { todos: { id_3: { label: 'jumpstyle', completed: false } }, length: 4 }
// > added new todo

Installation

Current builds only support NPM. If you use a different package manager, or no package manager, either download and build locally, or post an issue and I'll expand the build step.

npm install --save falcor-local-datasource

Development

To test:

npm run test

To publish:

npm run validate && npm publish

Kudos to Asa Ayers for the package name!