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

Commit

Permalink
Merge pull request #24 from data-provider/v2.0.0
Browse files Browse the repository at this point in the history
V2.0.0
  • Loading branch information
javierbrea authored Mar 14, 2020
2 parents f98eee5 + 952e34e commit 0f3f5b0
Show file tree
Hide file tree
Showing 11 changed files with 1,638 additions and 1,765 deletions.
32 changes: 2 additions & 30 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,2 @@
language: node_js

node_js:
- "10"

cache:
directories:
- "node_modules"
- "$HOME/.sonar/cache"

addons:
sonarcloud:
organization: "data-provider"
branch:
name: "$TRAVIS_CURRENT_BRANCH"

script:
- npm run lint
- npm run test-ci
- npm run build
- npm run coveralls
- 'if [ -n "$SONAR_TOKEN" ]; then sonar-scanner -Dsonar.login=${SONAR_TOKEN}; fi'

deploy:
provider: npm
email: "[email protected]"
api_key: "$NPM_TOKEN"
on:
tags: true
skip_cleanup: true
version: ~> 1.0
import: data-provider/ci-cd-shared-config:.travis.yml@master
32 changes: 28 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,35 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Changed
### Fixed
### Removed
### BREAKING CHANGES

## [TO BE DEPRECATED]
- "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] - 2020-03-14

> DISCLAIMER: This major release still maintains the concept of the previous "data-provider" versions, but a lot of BREAKING CHANGES have been made to the interfaces in order to improve the usage experience, apart of performance improvements and fixes. A chapter "how to migrate from 1.x" will be added to the documentation website to facilitate the migration to this new version, as the maintenance of 1.X versions will be stopped soon. Read Date Provider Changelog and docs in https://www.data-provider.org for further info.
### Changed
- chore(deps): [BREAKING CHANGE] Update @data-provider dependency to v2.0.0
- feat(providers): [BREAKING CHANGE] Remove create method. Update can be used instead.

## [2.0.0.alpha-2] - 2020-02-26

### Changed
- chore(deps): update @data-provider dependency

## [2.0.0.alpha-1] - 2020-02-23

> DISCLAIMER: This major release adapts this origin 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
- chore(umd distribution): [BREAKING CHANGE] - Rename umd dist file name to "index.umd.js"

## [1.4.3] - 2020-01-26
### Changed
- Update dependencies

## [1.4.2] - 2020-01-12
### Changed
Expand Down
158 changes: 53 additions & 105 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,149 +1,97 @@
[![Build status][travisci-image]][travisci-url] [![Coverage Status][coveralls-image]][coveralls-url] [![Quality Gate][quality-gate-image]][quality-gate-url]

[![NPM dependencies][npm-dependencies-image]][npm-dependencies-url] [![Greenkeeper badge](https://badges.greenkeeper.io/data-provider/memory.svg)](https://greenkeeper.io/) [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url]
[![NPM dependencies][npm-dependencies-image]][npm-dependencies-url] [![Renovate](https://img.shields.io/badge/renovate-enabled-brightgreen.svg)](https://renovatebot.com) [![Last commit][last-commit-image]][last-commit-url] [![Last release][release-image]][release-url]

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

## Overview
# Memory origin addon for Data Provider

This package provides a [Data Provider][data-provider-url] for handling memory objects.
It provides CRUD methods for objects saved in memory.

* __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.
## Usage

### Install
Read the [Data Provider][data-provider] docs to learn how to use addons.

```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.
* [Home][data-provider]
* [Get started][get-started]
* [Basic tutorial][basic-tutorial]

## Cache
## Queries

All cache will be cleaned when the `update`, `delete` or `create` methods are executed for any specific query.
When querying providers created with this addon, the query object can have one of the next properties:

## Default value
* `prop` _(String)_: Specific property of the object to be accessed.

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:
### Example

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

const fooMemory = new Memory({
foo: "foo-value",
var: "var-value"
const sessionStatus = new Memory("session-status", {
initialState: {
data: {
loggedIn: false
}
}
});

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
sessionStatus.query({ prop: "loggedIn" }).update(true);
sessionStatus.query({ prop: "loggedIn" }).read().then(result => {
console.log("Is logged in", result);
// true
});

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

## Examples
## Custom methods

Next example will be easier to understand if you are already familiarized with the [Data Provider][data-provider-url] syntax.
Apart of the common Data Provider methods, next ones are available:

```js
import { Memory } from "@data-provider/memory";
### `update(data)`

const sessionDetails = new Memory({
userId: null,
isLogedIn: false
});
Updates an specific property of the stored object when the provider is queried, or the full object when not. When the object is modified, it will __automatically cleans the cache of the provider__ and also the cache of the parent provider when it is queried _(as modifying a property also modifies the full object)_.

// 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);
});
#### Arguments

sessionDetails.query("isLogedIn").update(true);
```
* `data` _(Any)_: New data to be set.

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:
#### Examples

```javascript
// modifies an specific property
sessionStatus.query({ prop: "loggedIn" }).update(true);
```

```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
```javascript
// Overwrites full object
sessionStatus.update({
loggedIn: true
});
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();
### `delete()`

// Api request is not repeated, as query has no changed.
currentAuthorBooks.read();
Removes an specific property of the stored object when the provider is queried, or sets the full object as empty when not. When the object is modified, it will __automatically cleans the cache of the provider__ and also the cache of the parent provider when it is queried _(as deleting a property also modifies the full object)_.

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

```javascript
// removes an specific property
sessionStatus.query({ prop: "loggedIn" }).delete();
```

// Api request now is sent to "http://api.library.com/books?authorId=foo-author-id". Return author books
currentAuthorBooks.read();
```javascript
// Sets the full object as {}
sessionStatus.delete();
```

## Contributing

Contributors are welcome.
Please read the [contributing guidelines](.github/CONTRIBUTING.md) and [code of conduct](.github/CODE_OF_CONDUCT.md).

[data-provider-url]: https://github.com/data-provider/core
[data-provider-instances-docs-url]: https://github.com/data-provider/core/blob/master/docs/instances/api.md
[data-provider]: https://www.data-provider.org
[get-started]: https://www.data-provider.org/docs/getting-started
[basic-tutorial]: https://www.data-provider.org/docs/basics-intro

[coveralls-image]: https://coveralls.io/repos/github/data-provider/memory/badge.svg
[coveralls-url]: https://coveralls.io/github/data-provider/memory
Expand Down
Loading

0 comments on commit 0f3f5b0

Please sign in to comment.