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

daux

v0.0.2

Published

An immutable model-based state management solution for your JavaScript app

Downloads

33

Readme

Daux

An immutable model-based state management solution for your JavaScript apps.

Design

The library was built by combining some of the concepts of Ember Data and Redux.

Most apps have a model-based state that might look like this:

{
  "user": {
    "user_a": {
      "name": "John Doe",
      "country": "country_a",
      "groups": [
        "group_a"
      ],
      "posts": [
        "post_a"
      ]
    }
  },
  "country": {
    "country_a": {
      "name": "United States of America"
    }
  },
  "group": {
    "group_a": {
      "name": "Hikers Club",
      "members": [
        "user_a"
      ]
    }
  },
  "post": {
    "post_a": {
      "title": "When is the next hike?",
      "author": "user_a"
    }
  }
}

In Daux, these kind of states are managed by the Store. With it, you'll be able to create, read, update, and delete model records.

Installation

Assuming that you're using npm as your package manager:

npm install daux

If you're not using any module bundler, you can use the precompiled production and development UMD builds in the dist folder. For this build, Daux would be available as a window.Daux global variable. You can download the files at unpkg.

Usage

Check out the API reference

Setup your models

Create your models

import { Model, Store } from 'daux';

class User extends Model {
  static get attributes() {
    return ['name'];
  }

  static get relationship() {
    return {
      country: {
        type: 'country',
        kind: 'belongsTo',
        inverse: null,
      },
      groups: {
        type: 'group',
        kind: 'hasMany',
        inverse: 'members',
      },
      posts: {
        type: 'post',
        kind: 'hasMany',
        inverse: 'author',
      },
    };
  }

  /**
   * Optional hook to deserialize a record
   */
  static deserialize(record) {
    const deserializedRecord = {};

    Object.keys(record).forEach((key) => {
      // Use name instead of display_name to match the model attributes
      if (key === 'display_name') {
        deserializedRecord['name'] = record[key];
      }
    });

    return deserializedRecord;
  }
}

Next, we pass-in those models to the Store:

const store = new Store({
  user: User
});

Fetching states

async function fetchUsers() {
  return store.getAll('user', {
    fetch() {
      return fetch('example.com/api/users').then((response) => {
        return response.json();
      });
    }
  });
}

Changing states

async function createUser(newUser) {
  await fetch('example.com/api/users', {
    method: 'POST',
    body: JSON.stringify(newUser)
  });
  store.set('user', newUser);
}

Subscribing to state changes

// Log "Foo" everytime a state changes
const unsubscribe = store.subscribe(() => console.log('Foo'));

// Stop listening to state changes
unsubscribe();

Contributing

Installation

  • git clone <repository-url>
  • cd daux
  • npm install

Running tests

  • npm test

License

This project is licensed under the MIT License.