Skip to content

Commit 63e2aa6

Browse files
authored
v1.35.9 fix PATCH_DOC (#321)
v1.35.9 fix PATCH_DOC
2 parents 0e1cee1 + 34d9f7b commit 63e2aa6

File tree

6 files changed

+205
-76
lines changed

6 files changed

+205
-76
lines changed

dist/index.cjs.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ var vuexEasyAccess = require('vuex-easy-access');
1111
var isWhat = require('is-what');
1212
var copy = _interopDefault(require('copy-anything'));
1313
var mergeAnything = require('merge-anything');
14-
var flatten = _interopDefault(require('flatten-anything'));
14+
var flatten = require('flatten-anything');
15+
var flatten__default = _interopDefault(flatten);
16+
var pathToProp = _interopDefault(require('path-to-prop'));
1517
var compareAnything = require('compare-anything');
1618
var findAndReplaceAnything = require('find-and-replace-anything');
1719
var filter = _interopDefault(require('filter-anything'));
@@ -329,6 +331,37 @@ function isIncrementHelper(payload) {
329331
payload.isIncrementHelper === true);
330332
}
331333

334+
function convertHelpers(originVal, newVal) {
335+
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
336+
newVal = newVal.executeOn(originVal);
337+
}
338+
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
339+
newVal = newVal.executeOn(originVal);
340+
}
341+
return newVal; // always return newVal as fallback!!
342+
}
343+
/**
344+
* Creates the params needed to $set a target based on a nested.path
345+
*
346+
* @param {object} target
347+
* @param {string} path
348+
* @param {*} value
349+
* @returns {[object, string, any]}
350+
*/
351+
function getSetParams(target, path, value) {
352+
var _a;
353+
var pathParts = path.split('.');
354+
var prop = pathParts.pop();
355+
var pathParent = pathParts.join('.');
356+
var targetForNestedProp = pathToProp(target, pathParent);
357+
if (targetForNestedProp === undefined) {
358+
// the target doesn't have an object ready at this level to set the value to
359+
// so we need to step down a level and try again
360+
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
361+
}
362+
var valueToSet = value;
363+
return [targetForNestedProp, prop, valueToSet];
364+
}
332365
/**
333366
* a function returning the mutations object
334367
*
@@ -402,29 +435,27 @@ function pluginMutations (userState) {
402435
}
403436
},
404437
PATCH_DOC: function (state, patches) {
405-
var _this = this;
438+
var _a;
406439
// Get the state prop ref
407440
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
408441
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
409442
ref = ref[patches.id];
410443
}
411444
if (!ref)
412445
return error('patch-no-ref');
413-
return Object.keys(patches).forEach(function (key) {
414-
var newVal = patches[key];
415-
// Merge if exists
416-
function helpers(originVal, newVal) {
417-
if (isWhat.isArray(originVal) && isArrayHelper(newVal)) {
418-
newVal = newVal.executeOn(originVal);
419-
}
420-
if (isWhat.isNumber(originVal) && isIncrementHelper(newVal)) {
421-
newVal = newVal.executeOn(originVal);
422-
}
423-
return newVal; // always return newVal as fallback!!
424-
}
425-
newVal = mergeAnything.merge({ extensions: [helpers] }, ref[key], patches[key]);
426-
_this._vm.$set(ref, key, newVal);
427-
});
446+
var patchesFlat = flatten.flattenObject(patches);
447+
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
448+
var _c = _b[_i], path = _c[0], value = _c[1];
449+
var targetVal = pathToProp(ref, path);
450+
var newVal = convertHelpers(targetVal, value);
451+
// do not update anything if the values are the same
452+
// this is technically not required, because vue takes care of this as well:
453+
if (targetVal === newVal)
454+
continue;
455+
// update just the nested value
456+
var setParams = getSetParams(ref, path, newVal);
457+
(_a = this._vm).$set.apply(_a, setParams);
458+
}
428459
},
429460
DELETE_DOC: function (state, id) {
430461
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')
@@ -1286,9 +1317,9 @@ function pluginActions (Firebase) {
12861317
var getters = _a.getters, commit = _a.commit;
12871318
var defaultValues = getters.defaultValues;
12881319
var searchTarget = getters.collectionMode ? getters.storeRef[doc.id] : getters.storeRef;
1289-
var compareInfo = compareAnything.compareObjectProps(flatten(doc), // presentIn 0
1290-
flatten(defaultValues), // presentIn 1
1291-
flatten(searchTarget) // presentIn 2
1320+
var compareInfo = compareAnything.compareObjectProps(flatten__default(doc), // presentIn 0
1321+
flatten__default(defaultValues), // presentIn 1
1322+
flatten__default(searchTarget) // presentIn 2
12921323
);
12931324
Object.keys(compareInfo.presentIn).forEach(function (prop) {
12941325
// don't worry about props not in fillables
@@ -1918,7 +1949,7 @@ function pluginGetters (Firebase) {
19181949
patchData.updated_by = state._sync.userId;
19191950
// clean up item
19201951
var cleanedPatchData = filter(patchData, getters.fillables, getters.guard);
1921-
var itemToUpdate = flatten(cleanedPatchData);
1952+
var itemToUpdate = flatten__default(cleanedPatchData);
19221953
// add id (required to get ref later at apiHelpers.ts)
19231954
// @ts-ignore
19241955
itemToUpdate.id = id;

dist/index.esm.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { getDeepRef, getKeysFromPath } from 'vuex-easy-access';
55
import { isAnyObject, isPlainObject, isArray, isFunction, isNumber, isString, isDate } from 'is-what';
66
import copy from 'copy-anything';
77
import { merge } from 'merge-anything';
8-
import flatten from 'flatten-anything';
8+
import flatten, { flattenObject } from 'flatten-anything';
9+
import pathToProp from 'path-to-prop';
910
import { compareObjectProps } from 'compare-anything';
1011
import { findAndReplace, findAndReplaceIf } from 'find-and-replace-anything';
1112
import filter from 'filter-anything';
@@ -323,6 +324,37 @@ function isIncrementHelper(payload) {
323324
payload.isIncrementHelper === true);
324325
}
325326

327+
function convertHelpers(originVal, newVal) {
328+
if (isArray(originVal) && isArrayHelper(newVal)) {
329+
newVal = newVal.executeOn(originVal);
330+
}
331+
if (isNumber(originVal) && isIncrementHelper(newVal)) {
332+
newVal = newVal.executeOn(originVal);
333+
}
334+
return newVal; // always return newVal as fallback!!
335+
}
336+
/**
337+
* Creates the params needed to $set a target based on a nested.path
338+
*
339+
* @param {object} target
340+
* @param {string} path
341+
* @param {*} value
342+
* @returns {[object, string, any]}
343+
*/
344+
function getSetParams(target, path, value) {
345+
var _a;
346+
var pathParts = path.split('.');
347+
var prop = pathParts.pop();
348+
var pathParent = pathParts.join('.');
349+
var targetForNestedProp = pathToProp(target, pathParent);
350+
if (targetForNestedProp === undefined) {
351+
// the target doesn't have an object ready at this level to set the value to
352+
// so we need to step down a level and try again
353+
return getSetParams(target, pathParent, (_a = {}, _a[prop] = value, _a));
354+
}
355+
var valueToSet = value;
356+
return [targetForNestedProp, prop, valueToSet];
357+
}
326358
/**
327359
* a function returning the mutations object
328360
*
@@ -396,29 +428,27 @@ function pluginMutations (userState) {
396428
}
397429
},
398430
PATCH_DOC: function (state, patches) {
399-
var _this = this;
431+
var _a;
400432
// Get the state prop ref
401433
var ref = state._conf.statePropName ? state[state._conf.statePropName] : state;
402434
if (state._conf.firestoreRefType.toLowerCase() === 'collection') {
403435
ref = ref[patches.id];
404436
}
405437
if (!ref)
406438
return error('patch-no-ref');
407-
return Object.keys(patches).forEach(function (key) {
408-
var newVal = patches[key];
409-
// Merge if exists
410-
function helpers(originVal, newVal) {
411-
if (isArray(originVal) && isArrayHelper(newVal)) {
412-
newVal = newVal.executeOn(originVal);
413-
}
414-
if (isNumber(originVal) && isIncrementHelper(newVal)) {
415-
newVal = newVal.executeOn(originVal);
416-
}
417-
return newVal; // always return newVal as fallback!!
418-
}
419-
newVal = merge({ extensions: [helpers] }, ref[key], patches[key]);
420-
_this._vm.$set(ref, key, newVal);
421-
});
439+
var patchesFlat = flattenObject(patches);
440+
for (var _i = 0, _b = Object.entries(patchesFlat); _i < _b.length; _i++) {
441+
var _c = _b[_i], path = _c[0], value = _c[1];
442+
var targetVal = pathToProp(ref, path);
443+
var newVal = convertHelpers(targetVal, value);
444+
// do not update anything if the values are the same
445+
// this is technically not required, because vue takes care of this as well:
446+
if (targetVal === newVal)
447+
continue;
448+
// update just the nested value
449+
var setParams = getSetParams(ref, path, newVal);
450+
(_a = this._vm).$set.apply(_a, setParams);
451+
}
422452
},
423453
DELETE_DOC: function (state, id) {
424454
if (state._conf.firestoreRefType.toLowerCase() !== 'collection')

package-lock.json

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuex-easy-firestore",
3-
"version": "1.35.8",
3+
"version": "1.35.9",
44
"description": "Easy coupling of firestore and a vuex module. 2-way sync with 0 boilerplate!",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",
@@ -45,6 +45,7 @@
4545
"flatten-anything": "^1.4.1",
4646
"is-what": "^3.8.0",
4747
"merge-anything": "^2.4.4",
48+
"path-to-prop": "0.0.3",
4849
"vuex-easy-access": "^3.1.8"
4950
},
5051
"devDependencies": {

src/module/mutations.ts

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { isArray, isFunction, isNumber } from 'is-what'
22
import { getDeepRef } from 'vuex-easy-access'
3+
import { flattenObject } from 'flatten-anything'
4+
import pathToProp from 'path-to-prop'
35
import logError from './errors'
46
import copy from 'copy-anything'
57
import { merge } from 'merge-anything'
@@ -8,6 +10,38 @@ import { isArrayHelper } from '../utils/arrayHelpers'
810
import { isIncrementHelper } from '../utils/incrementHelper'
911
import getStateWithSync from './state'
1012

13+
function convertHelpers (originVal, newVal) {
14+
if (isArray(originVal) && isArrayHelper(newVal)) {
15+
newVal = newVal.executeOn(originVal)
16+
}
17+
if (isNumber(originVal) && isIncrementHelper(newVal)) {
18+
newVal = newVal.executeOn(originVal)
19+
}
20+
return newVal // always return newVal as fallback!!
21+
}
22+
23+
/**
24+
* Creates the params needed to $set a target based on a nested.path
25+
*
26+
* @param {object} target
27+
* @param {string} path
28+
* @param {*} value
29+
* @returns {[object, string, any]}
30+
*/
31+
function getSetParams (target: object, path: string, value: any): [object, string, any] {
32+
const pathParts = path.split('.')
33+
const prop = pathParts.pop()
34+
const pathParent = pathParts.join('.')
35+
const targetForNestedProp = pathToProp(target, pathParent)
36+
if (targetForNestedProp === undefined) {
37+
// the target doesn't have an object ready at this level to set the value to
38+
// so we need to step down a level and try again
39+
return getSetParams(target, pathParent, { [prop]: value })
40+
}
41+
const valueToSet = value
42+
return [targetForNestedProp, prop, valueToSet]
43+
}
44+
1145
/**
1246
* a function returning the mutations object
1347
*
@@ -79,21 +113,18 @@ export default function (userState: object): AnyObject {
79113
ref = ref[patches.id]
80114
}
81115
if (!ref) return logError('patch-no-ref')
82-
return Object.keys(patches).forEach(key => {
83-
let newVal = patches[key]
84-
// Merge if exists
85-
function helpers (originVal, newVal) {
86-
if (isArray(originVal) && isArrayHelper(newVal)) {
87-
newVal = newVal.executeOn(originVal)
88-
}
89-
if (isNumber(originVal) && isIncrementHelper(newVal)) {
90-
newVal = newVal.executeOn(originVal)
91-
}
92-
return newVal // always return newVal as fallback!!
93-
}
94-
newVal = merge({ extensions: [helpers] }, ref[key], patches[key])
95-
this._vm.$set(ref, key, newVal)
96-
})
116+
117+
const patchesFlat = flattenObject(patches)
118+
for (const [path, value] of Object.entries(patchesFlat)) {
119+
const targetVal = pathToProp(ref, path)
120+
const newVal = convertHelpers(targetVal, value)
121+
// do not update anything if the values are the same
122+
// this is technically not required, because vue takes care of this as well:
123+
if (targetVal === newVal) continue
124+
// update just the nested value
125+
const setParams = getSetParams(ref, path, newVal)
126+
this._vm.$set(...setParams)
127+
}
97128
},
98129
DELETE_DOC (state, id) {
99130
if (state._conf.firestoreRefType.toLowerCase() !== 'collection') return

0 commit comments

Comments
 (0)