# @datx/core

> A MobX data store

Latest version **2.6.2-beta.3** (published 2023-09-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @datx/core
pnpm add @datx/core
yarn add @datx/core
bun add @datx/core
```

## Health

**Score 35/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 2.6.2-beta.3 |
| Published | 2023-09-05 |
| First published | 2021-01-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 586.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 146 |
| Author | Infinum JavaScript Team |
| Maintainers | danipavic, kristian240, infinumcom, darkokukovec, safo6m, fvoska, ivicabatinic |

## Links

- npm: https://www.npmjs.com/package/@datx/core
- Repository: https://github.com/infinum/datx
- Homepage: https://github.com/infinum/datx#readme
- Issues: https://github.com/infinum/datx/issues
- npm.io page: https://npm.io/package/@datx/core

## Dependencies (1)

- [@datx/utils](https://npm.io/package/@datx/utils.md) 2.6.2-beta.2

## Recent versions

- 2.6.2-beta.3 (latest) — 2023-09-05
- 2.6.2-beta.2 (beta) — 2023-08-07
- 3.0.0-alpha.0 (alpha) — 2022-03-29
- 2.6.2-beta.1 — 2023-07-20
- 2.6.2-beta.0 — 2023-07-11
- 3.0.0 — 2023-06-29
- 3.0.0-beta.1 — 2023-06-29
- 2.6.1 — 2023-06-12
- 2.5.1 — 2023-04-20
- 2.5.0 — 2023-04-20
- 2.5.0-beta.11 — 2023-03-23
- 2.4.12 — 2023-01-25
- 2.4.11 — 2023-01-19
- 2.5.0-beta.6 — 2022-12-14
- 2.4.8 — 2022-11-14
- … 18 more at https://npm.io/package/@datx/core/versions

## README

# 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](https://mobx.js.org/) state management library, but this is optional and can be used as a pure JS library.

---

## Basic usage

```typescript
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

```bash
npm install --save @datx/core
```

- [Installation](https://datx.dev/docs/getting-started/installation)
- [Defining models](https://datx.dev/docs/getting-started/defining-models)
- [References](https://datx.dev/docs/getting-started/references)
- [Configuring the collection](https://datx.dev/docs/getting-started/configuring-the-collection)
- [Using the collection](https://datx.dev/docs/getting-started/using-the-collection)
- [Persisting data locally](https://datx.dev/docs/getting-started/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:

- [Symbol.for](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
- [Object.assign](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign)
- [Array.prototype.find](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find)

[How to add the polyfills](https://datx.dev/docs/troubleshooting/known-issues#the-library-doesnt-work-in-internet-explorer-11).

## Concepts

The library contains two main classes - [`Model`](https://datx.dev/docs/api-reference/model) and [`Collection`](https://datx.dev/docs/api-reference/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](https://datx.dev/docs/mixins/with-actions) and [properties](https://datx.dev/docs/mixins/with-meta), but if they're in collision with your data/logic, you can use a [`PureModel`](https://datx.dev/docs/api-reference/pure-model) class.

## Mixins

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

- [`withActions`](https://datx.dev/docs/mixins/with-actions) (model) - Adds some helper methods to the model - already included in the `Model` class, but not in the `PureModel` class
- [`withMeta`](https://datx.dev/docs/mixins/with-meta) (model) - Adds some helpful meta data to the model - already included in the `Model` class, but not in the `PureModel` class
- [`withPatches`](https://datx.dev/docs/mixins/with-patches) (model, collection) - Adds patch support to models and collections
- [`datx-jsonapi`](https://datx.dev/docs/mixins/jsonapi-mixin) (model, collection and view) - Adds the [JSON API](https://jsonapi.org/) features to the model, collection and view

To check out what are the planed future mixins, check out [the issues](https://github.com/infinum/datx/labels/mixins).

Want to make your own mixin? Check out [the guide](https://datx.dev/docs/mixins/building-your-own-mixin).

## API reference

- [Collection](https://datx.dev/docs/api-reference/collection)
- [Model](https://datx.dev/docs/api-reference/model)
- [View](https://datx.dev/docs/api-reference/view)
- [prop](https://datx.dev/docs/api-reference/prop)
- [PureModel](https://datx.dev/docs/api-reference/pure-model)
- [CompatModel](https://datx.dev/docs/migration-guide/compat-model)
- [CompatCollection](https://datx.dev/docs/migration-guide/compat-collection)
- [Model utils](https://datx.dev/docs/api-reference/model-utils)
- [Lib utils](https://datx.dev/docs/api-reference/lib-utils)
- [TypeScript interfaces](https://datx.dev/docs/api-reference/typescript-interfaces)

## Troubleshooting

Having issues with the library? Check out the [troubleshooting](https://datx.dev/docs/troubleshooting/known-issues) page or [open](https://github.com/infinum/datx/issues/new/choose) an issue.

---

[![Build Status](https://travis-ci.org/infinum/datx.svg?branch=master)](https://travis-ci.org/infinum/datx)

## License

The [MIT License](LICENSE)

## Credits

datx is maintained and sponsored by
[Infinum](https://www.infinum.co).

<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>

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