diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e18cb9..0aa3517 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +## v1.1.2 +### Changed + - _helpers / updateQueryHelper_ - added _sourceDefault_ option (details in _Readme_) + - _utils_ - improved _get_ utility + - _Readme_ - added _sourceDefault_ definition + ## v1.1.1 ### Changed - _Readme.md_ content link fix, fixed explanation comments' mistakes diff --git a/README.md b/README.md index a466ace..719ed05 100644 --- a/README.md +++ b/README.md @@ -3,15 +3,16 @@ 🚀 Apollo 🛠‍ Tool represented as InMemoryCache 🧙 wrapper for 🗄 storing / 🗃 restoring ✅ selected only 🗂️ queries and for updating ⛓ linked / nested without 🆔 IDs ## Content - - [Install](#install) - - [Usage](#usage) - - [Creating Enchanted InMemoryCache Config](#creating-enchanted-inmemorycache-config) - - [Basic usage](#basic-usage) - - [API](#api) - - [SubscribedQuery](#subscribedquery) - - [updateQueryHelper: Updater](#updatequeryhelper-updater) - - [Types](#types) - - [License](#license) + +- [Install](#install) +- [Usage](#usage) + - [Creating Enchanted InMemoryCache Config](#creating-enchanted-inmemorycache-config) + - [Basic usage](#basic-usage) +- [API](#api) + - [SubscribedQuery](#subscribedquery) + - [updateQueryHelper: Updater](#updatequeryhelper-updater) +- [Types](#types) +- [License](#license) ## Install @@ -171,6 +172,8 @@ const inMemoryCache = new InMemoryCache({ // ... }); // ... +const logCacheWrite = true; // for debug/log reasons + const cache = createEnchantedInMemoryCache( inMemoryCache, subscribedQueries, @@ -221,13 +224,14 @@ Array\ #### updateQueryHelper: Updater -| Prop | Type | Default | Note | -| ------------- | ----------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `sourceQuery` | `QueryObject` | _(required)_ | Object Data of source Query tracked for updating target Query | -| `sourcePath` | `ObjectPath` | `[]` | path to Data source Query object field | -| `targetQuery` | `QueryObject` | | Object Data of target Query should be updated | -| `targetPath` | `ObjectPath` | `[]` | path to Data target Query object field | -| `updateType` | `UpdateTypesEnum` | `replace` | `replace` - just replacing target Data at object some field by source Data
`rootMerge` - merge target Data Object at object field by source Data with replacing tested Data
`deepMerge` - merge target Object Data at all fields (`sourcePath` of `sourceQuery`) by source Object Data with same fields (`targetPath` of `targetQuery`); begins at source Object field and goes recursively into the depths | +| Prop | Type | Default | Note | +| --------------- | ----------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `sourceQuery` | `QueryObject` | _(required)_ | Object Data of source Query tracked for updating target Query | +| `sourcePath` | `ObjectPath` | `[]` | path to Data source Query object field | +| `targetQuery` | `QueryObject` | | Object Data of target Query should be updated | +| `targetPath` | `ObjectPath` | `[]` | path to Data target Query object field | +| `updateType` | `UpdateTypesEnum` | `replace` | `replace` - just replacing target Data at object some field by source Data
`rootMerge` - merge target Data Object at object field by source Data with replacing tested Data
`deepMerge` - merge target Object Data at all fields (`sourcePath` of `sourceQuery`) by source Object Data with same fields (`targetPath` of `targetQuery`); begins at source Object field and goes recursively into the depths | +| `sourceDefault` | `any` | `null` | data to be used for updating the target Object if no present in the source Object | ## Types @@ -236,7 +240,7 @@ type ArrayPath = Array; // ['a', 'b', 0, 'c', 1] type ObjectPath = ArrayPath | string; // ['a', 'b', 'c', 0] | 'a.b.c.0' -type QueryObject = { Object, Data }; // Query result data +type QueryObject = { Object; Data }; // Query result data type Updater = ( sourceQuery: QueryObject, @@ -253,7 +257,7 @@ type LinkedQuery = { queryNode: DocumentNode; // Apollo Query definition, returned by gql`...` updateName: string; updater?: Updater; -} +}; type StoredQuery = { name: string; @@ -262,9 +266,9 @@ type StoredQuery = { nest?: ObjectPath; retrieveField?: string; retriever?: Retriever; -} +}; -type SubscribedQuery = LinkedQuery | StoredQuery +type SubscribedQuery = LinkedQuery | StoredQuery; type SubscribedQueries = Array; diff --git a/package.json b/package.json index 296703a..c9c68e5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "apollo-enchanted-cache-inmemory", - "version": "1.1.1", + "version": "1.1.2", "description": "Apollo InMemoryCache wrapper for storing selected only queries and for updating linked/nested without IDs", "main": "src/index.js", "scripts": { diff --git a/src/helpers/index.js b/src/helpers/index.js index 2782aa1..518d590 100644 --- a/src/helpers/index.js +++ b/src/helpers/index.js @@ -15,6 +15,7 @@ export const UpdateTypesEnum = { * updateType?: UpdateTypesEnum * withMerge?: Boolean * withMergeRootOnly?: Boolean + * sourceDefault?: any * }} UpdateInput * withMerge - deep merge or replace; DEFAULT = FALSE * withMergeRootOnly - either root merge with replacing nested @@ -33,8 +34,9 @@ export const updateQueryHelper = updateInput => { withMerge = false, withMergeRootOnly = false, updateType = UpdateTypesEnum.replace, + sourceDefault = null, } = updateInput; - const newData = get(sourceQuery, sourcePath); + const newData = get(sourceQuery, sourcePath, sourceDefault); let setData = null; if (updateType === UpdateTypesEnum.replace) { // just replace diff --git a/src/utils/index.js b/src/utils/index.js index d3e377f..a6d5f5d 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -55,8 +55,7 @@ export function get(obj, path, def) { return fullPath.every(everyFunc) ? obj : def; function everyFunc(step) { - // eslint-disable-next-line - return !(step != null && (obj = obj[step]) === undefined); + return !(obj == null || (step != null && (obj = obj[step]) === undefined)); } }