datx
datx
npm i datx
datx

datx

DatX is an opinionated JS/TS data store. It features support for simple property definition, references to other models and first-class TypeScript support.

by infinum

1.1.4 (see all)License:MITTypeScript:Built-In
npm i datx
Readme

datx

Greenkeeper badge

DatX is an opinionated data store for use with the MobX state management library. It features support for simple observable property definition, references to other models and first-class TypeScript support.


Basic usage

import { Collection, Model, prop } from 'datx';
import { computed } from 'mobx';

class Person extends Model {
  public static type = 'person'; // Unique name of the model class

  @prop name: string; // A normal observable property without a default value
  @prop surname: string;
  @prop.toOne(Person) spouse?: Person; // A reference to a Person model

  @computed get fullName() { // Standard MobX computed props
    return `${this.name} ${this.surname}`;
  }
}

class AppData extends Collection {
  public static types = [Person]; // A list of models available in the collection
}

const store = new AppData();
const john = store.add(new Person({name: 'John', surname: 'Smith'})); // Add a model instance to the store
const jane = store.add({name: 'Jane', surname: 'Smith', spouse: john}, Person); // Add a model to the store

Getting started

Note: datx has a peer dependency to mobx@^4.2.0 or mobx@^5.5.0, so don't forget to install the latest MobX version:

npm install --save datx mobx

Polyfilling

The lib makes use of the following features that are not yet available everywhere. Based on your browser support, you might want to polyfill them:

How to add the polyfills.

Concepts

The library contains two main classes - Model and Collection.

A collection contains models of any kind (they should however be listed in the types property), while a model can be in a single collection (but doesn't need to be in any).

Models also include some useful methods and properties, but if they're in collision with your data/logic, you can use a PureModel class.

Mixins

Mixins are additional plugins that can enhance the regular models and collections. Available mixins:

  • withActions (model) - Adds some helper methods to the model - already included in the Model class, but not in the PureModel class
  • withMeta (model) - Adds some helpful meta data to the model - already included in the Model class, but not in the PureModel class
  • withPatches (model, collection) - Adds patch support to models and collections
  • datx-jsonapi (model, collection and view) - Adds the JSON API features to the model, collection and view

To check out what are the planed future mixins, check out the issues.

Want to make your own mixin? Check out the guide.

API reference

Troubleshooting

Having issues with the library? Check out the troubleshooting page or open an issue.


Build Status

Packagenpm versiondependency statusdev dependency status
datxnpm versionDependency StatusdevDependency Status
datx-jsonapinpm versionDependency StatusdevDependency Status
datx-utilsnpm versionDependency StatusdevDependency Status

License

The MIT License

Credits

datx is maintained and sponsored by Infinum.

Downloads/wk

205

GitHub Stars

125

LAST COMMIT

8mos ago

MAINTAINERS

8

CONTRIBUTORS

18

OPEN ISSUES

10

OPEN PRs

10
VersionTagPublished
1.1.4
latest
7mos ago
2.0.0-beta.12
beta
2yrs ago
0.0.1
alpha
5yrs ago
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

100
No reviews found
Be the first to rate