Awesome
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.
Basic usage
import { Collection, Model, Field } from '@datx/core';
class Person extends Model {
public static type = 'person'; // Unique name of the model class
@Field()
public name!: string; // A normal property without a default value
@Field()
public surname!: string;
@Field({ toOne: Person })
public spouse?: Person; // A reference to a Person model
public get fullName() {
// Standard JS getter
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
- Installation
- Defining models
- References
- Configuring the collection
- Using the collection
- Persisting data locally
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:
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 theModel
class, but not in thePureModel
classwithMeta
(model) - Adds some helpful meta data to the model - already included in theModel
class, but not in thePureModel
classwithPatches
(model, collection) - Adds patch support to models and collectionsdatx-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
- Collection
- Model
- View
- prop
- PureModel
- CompatModel
- CompatCollection
- Model utils
- Lib utils
- TypeScript interfaces
Troubleshooting
Having issues with the library? Check out the troubleshooting page or open an issue.
License
The MIT License
Credits
datx is maintained and sponsored by Infinum.
<p align="center"> <a href='https://infinum.com'> <picture> <source srcset="https://assets.infinum.com/brand/logo/static/white.svg" media="(prefers-color-scheme: dark)"> <img src="https://assets.infinum.com/brand/logo/static/default.svg"> </picture> </a> </p>