Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

Commit

Permalink
feat(Memory): [BREAKING CHANGE] Update data-provider to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
javierbrea committed Feb 22, 2020
1 parent ad84ef0 commit ca9646b
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 465 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- "queriesDefaultValue" option has to be removed.
- Options should be accepted only as second argument. "uuid" should be defined only using the "uuid" option, not as second argument.

## [2.0.0.beta-1]

> DISCLAIMER: This major release adapts this plugin to the @data-provider v2.x interface. Read @data-provider docs for further info.
### Changed
- feat(Memory): [BREAKING CHANGE] - Use data-provider v2 standard arguments (id, options)
- feat(Memory): [BREAKING CHANGE] - Queries now has to be defined as an object: { prop: "foo-prop" }
- feat(Memory): [BREAKING CHANGE] - Remove defaultValue argument, now "initialState" option has to be used instead.
- feat(Memory): [BREAKING CHANGE] - Remove uuid option. Now id is required as first argument.
- feat(Memory): [BREAKING CHANGE] - Remove queriesDefaultValue option. Now this is the default behavior

## [1.4.3] - 2020-01-26
### Changed
- Update dependencies
Expand Down
133 changes: 0 additions & 133 deletions README.md
Expand Up @@ -4,139 +4,6 @@

[![NPM downloads][npm-downloads-image]][npm-downloads-url] [![License][license-image]][license-url]

## Overview

This package provides a [Data Provider][data-provider-url] for handling memory objects.

* __Data Provider queries__ based on object keys.
* __Reactivity__ to CRUD actions. When a "create", "update" or "delete" method is called over an instance, the cache clean events are dispatched.

### Install

```bash
npm i @data-provider/memory --save
```

## Api

`new Memory(defaultValue[, options || uuid][, options])`
* Arguments
* defaultValue - _`<Object>`_. Object to be stored. Default value is assigned too at the same time.
* options - `<Object>` containing properties:
* queriesDefaultValue - _`<Boolean>`_ If `true`, the default value of queried instances will be the value of the "key" in the query. If not defined, the default value of queried instances will be the full `defaultValue` object.
* uuid - _`<String>`_ Unique id to assign to returned Data Provider instance. Useful when using [Data Provider `instances` handler][data-provider-instances-docs-url].
* tags - _`<String> or <Array of Strings>`_ Tags to assign to the instance. Useful when using [Data Provider `instances` handler][data-provider-instances-docs-url]. A "memory" tag will be always added to provided tags by default.

## Methods

### query

`memory.query(key)`
* Arguments
* key - `<String>` Key of the memory object to be read, updated, created or deleted.

## Cache

All cache will be cleaned when the `update`, `delete` or `create` methods are executed for any specific query.

## Default value

The default value of a "queried" instance will be the complete `defaultValue` object until the "queriesDefaultValue" option is set as `true`, in which case the default value will be the value of the key corresponding to the query:

```js
import { Memory } from "@data-provider/memory";

const fooMemory = new Memory({
foo: "foo-value",
var: "var-value"
});

console.log(fooMemory.query("foo").read.value); // {foo: "foo-value", var: "var-value"}

const fooMemory2 = new Memory({
foo: "foo-value",
var: "var-value"
}, {
queriesDefaultValue: true
});

console.log(fooMemory2.query("foo").read.value); // "foo"
```

## Examples

Next example will be easier to understand if you are already familiarized with the [Data Provider][data-provider-url] syntax.

```js
import { Memory } from "@data-provider/memory";

const sessionDetails = new Memory({
userId: null,
isLogedIn: false
});

// Pass key to read method
sessionDetails.read("userId").then(userId => {
console.log(userId);
});

// Pass key as a query
sessionDetails
.query("userId")
.read()
.then(userId => {
console.log(userId);
});

sessionDetails.query("isLogedIn").update(true);
```

Use Data Provider Memory objects in combination with Axios Data Provider, and take advantage of the built-in reactivity. Use the memory objects to query the API providers, and, when you update the Memory Object, the API object caches will be cleaned as a consequence:


```js
import { Selector } from "@data-provider/core";
import { Memory } from "@data-provider/memory";
import { Api } from "@data-provider/axios";

const currentAuthor = new Memory({
id: null
});
const booksCollection = new Api("http://api.library.com/books");

const currentAuthorBooks = new Selector(
{
provider: currentAuthor,
query: () => "id"
},
{
provider: booksCollection,
query: (query, previousResults) => {
if (!previousResults[0]) {
return;
}
return {
queryString: {
authorId: previousResults[0]
}
};
}
},
(currentAuthorId, booksResults) => booksResults
);

// Api request to "http://api.library.com/books". Return all books
currentAuthorBooks.read();

// Api request is not repeated, as query has no changed.
currentAuthorBooks.read();

currentAuthor.query("id").update("foo-author-id");

// Api request now is sent to "http://api.library.com/books?authorId=foo-author-id". Return author books
currentAuthorBooks.read();
```

## Contributing

Contributors are welcome.
Expand Down
171 changes: 78 additions & 93 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ca9646b

Please sign in to comment.