Socket
Socket
Sign inDemoInstall

@datx/core

Package Overview
Dependencies
1
Maintainers
7
Versions
33
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @datx/core

A MobX data store


Version published
Weekly downloads
237
decreased by-45.64%
Maintainers
7
Install size
685 kB
Created
Weekly downloads
 

Readme

Source

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 default, it uses the MobX state management library, but this is optional and can be used as a pure JS library.


Basic usage

import { Collection, Model, Attribute } from '@datx/core';
import { computed } from 'mobx';

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

  @Attribute()
  public name!: string; // A normal property without a default value

  @Attribute()
  public surname!: string;

  @Attribute({ toOne: Person })
  public spouse?: Person; // A reference to a Person model

  @computed
  public 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

npm install --save @datx/core

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

License

The MIT License

Credits

datx is maintained and sponsored by Infinum.

FAQs

Last updated on 05 Sep 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc