# daux

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

Latest version **0.0.2** (published 2018-10-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install daux
pnpm add daux
yarn add daux
bun add daux
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2018-10-29 |
| First published | 2018-10-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 446.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Mikko Paderes |
| Maintainers | rmmmp |
| Keywords | daux, state, immutable |

## Links

- npm: https://www.npmjs.com/package/daux
- Repository: https://github.com/dauxjs/daux
- Homepage: https://github.com/dauxjs/daux#readme
- Issues: https://github.com/dauxjs/daux/issues
- npm.io page: https://npm.io/package/daux

## Dependencies (1)

- [@babel/polyfill](https://npm.io/package/@babel/polyfill.md) ^7.0.0

## Alternatives

- [@reckona/mreact-store](https://npm.io/package/@reckona/mreact-store.md) — 976 weekly downloads
- [regular-state](https://npm.io/package/regular-state.md) — 410 weekly downloads
- [@pacote/flux-actions](https://npm.io/package/@pacote/flux-actions.md) — 65 weekly downloads
- [@pilotlab/lux-debug](https://npm.io/package/@pilotlab/lux-debug.md) — 39 weekly downloads
- [vue-persist-state](https://npm.io/package/vue-persist-state.md) — 19 weekly downloads

## Recent versions

- 0.0.2 (latest) — 2018-10-29

## 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](https://github.com/emberjs/data) and [Redux](https://github.com/reduxjs/redux).

Most apps have a model-based state that might look like this:

```json
{
  "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](https://www.npmjs.com/) as your package manager:

```
npm install daux
```

If you're not using any module bundler, you can use the precompiled production and development [UMD](https://github.com/umdjs/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](https://unpkg.com/daux/).

## Usage

Check out the [API reference](API.md)

### Setup your models

Create your models

```javascript
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`:

```javascript
const store = new Store({
  user: User
});
```

### Fetching states

```javascript
async function fetchUsers() {
  return store.getAll('user', {
    fetch() {
      return fetch('example.com/api/users').then((response) => {
        return response.json();
      });
    }
  });
}
```

### Changing states

```javascript
async function createUser(newUser) {
  await fetch('example.com/api/users', {
    method: 'POST',
    body: JSON.stringify(newUser)
  });
  store.set('user', newUser);
}
```

### Subscribing to state changes

```javascript
// 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](LICENSE.md).

---
_Source: https://npm.io/package/daux · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
