@kitql/client
Advanced tools
Comparing version 0.1.9 to 0.2.0
60
index.js
@@ -72,3 +72,21 @@ 'use strict'; | ||
function objUpdate(found, obj, newData, xPath, id) { | ||
// '_$id(2)' or '[]$add(2)' or '[]$add' | ||
function extractKeyValue(str) { | ||
if (str.startsWith('_$')) { | ||
const [key, value] = str.substring(2).split('('); | ||
return { | ||
key, | ||
value: value.substring(0, value.length - 1) | ||
}; | ||
} | ||
else if (str.includes('[]$add')) { | ||
const [key, value] = str.split('[]$add'); | ||
return { | ||
key, | ||
value: value ? extractKeyValue('_$PROPS' + value).value : -1 | ||
}; | ||
} | ||
return null; | ||
} | ||
function objUpdate(found, obj, newData, xPath) { | ||
// replace directly obj with newData | ||
@@ -89,8 +107,20 @@ if (xPath === null) { | ||
if (segments.length === 1) { | ||
if (segment.includes('=$id')) { | ||
const [key] = segment.split('=$id'); | ||
if (obj[key] !== undefined && obj[key] === id) { | ||
toReturn = { found: true, obj: newData }; | ||
return; | ||
if (segment.includes('[]$add')) { | ||
const kvp = extractKeyValue(segment); | ||
let pos = kvp.value === -1 ? obj[kvp.key].length : kvp.value; | ||
let newArray = obj[kvp.key]; | ||
newArray.splice(pos, 0, newData); | ||
toReturn = { found: true, obj: { ...obj, [kvp.key]: newArray } }; | ||
} | ||
else if (segment.includes('_$')) { | ||
const kvp = extractKeyValue(segment); | ||
if (kvp) { | ||
if (obj[kvp.key] !== undefined && obj[kvp.key].toString() === kvp.value) { | ||
toReturn = { found: true, obj: newData }; | ||
return; | ||
} | ||
} | ||
else { | ||
throw new Error('objUpdate - Invalid segment: ' + segment); | ||
} | ||
} | ||
@@ -110,3 +140,3 @@ else { | ||
obj[propertyName].forEach((arrayItem, i) => { | ||
const result = objUpdate(toReturn.found, obj[propertyName][i], newData, xPathNext, id); | ||
const result = objUpdate(toReturn.found, obj[propertyName][i], newData, xPathNext); | ||
obj[propertyName][i] = result.obj; | ||
@@ -123,3 +153,3 @@ toReturn.found = result.found; | ||
let xPathNext = segments.slice(1).join('.'); | ||
const result = objUpdate(toReturn.found, obj[segment], newData, xPathNext, id); | ||
const result = objUpdate(toReturn.found, obj[segment], newData, xPathNext); | ||
obj[segment] = result.obj; | ||
@@ -284,16 +314,16 @@ toReturn.found = result.found; | ||
} | ||
return nbDeleted; | ||
} | ||
// typing for xPath | ||
storeUpdate(operationKey, store, newData, // To be fragments only? | ||
xPath = null, id = null) { | ||
patch(operationKey, store, newData, // To be fragments only? | ||
xPath = null) { | ||
// remove all from the cache, we will update only the current store | ||
// Can be improved later ;) => Updating all cached data (with option? Perf?) | ||
this.cacheData.remove(operationKey, null, true); | ||
let storeDataUpdated = objUpdate(false, store.data, newData, xPath, id); | ||
let storeDataUpdated = objUpdate(false, store.data, newData, xPath); | ||
const browserAndWantLog = this.logType.includes('client'); | ||
if (!storeDataUpdated.found) { | ||
if (browserAndWantLog) { | ||
this.log.info(`${helper.logCyan('StoreUpdate:')} xPath ${helper.logGreen(xPath)}(${helper.logGreen(id + '')}) ` + | ||
this.log.info(`${helper.logCyan('StoreUpdate:')} xPath ${helper.logGreen(xPath)} ` + | ||
`${helper.logYellow('not found')}, ` + | ||
`${helper.logCyan('Store:')} ${helper.logGreen(operationKey + 'Store')}`); | ||
`${helper.logCyan('Store:')} ${helper.logGreen(operationKey)}`); | ||
} | ||
@@ -305,3 +335,3 @@ } | ||
this.log.info(`${helper.logCyan('StoreUpdate:')} ${helper.logGreen('1')}, ` + | ||
`${helper.logCyan('Store:')} ${helper.logGreen(operationKey + 'Store')}`); | ||
`${helper.logCyan('Store:')} ${helper.logGreen(operationKey)}`); | ||
} | ||
@@ -308,0 +338,0 @@ } |
@@ -110,5 +110,5 @@ export declare type ClientSettings = { | ||
allOperationKey?: boolean | null; | ||
} | null): void; | ||
storeUpdate<D, V>(operationKey: string, store: RequestResult<D, V>, newData: Object, // To be fragments only? | ||
xPath?: string | null, id?: string | number | null): RequestResult<D, V>; | ||
} | null): number; | ||
patch<D, V>(operationKey: string, store: RequestResult<D, V>, newData: Object, // To be fragments only? | ||
xPath?: string | null): RequestResult<D, V>; | ||
} |
@@ -5,2 +5,2 @@ export declare type TUpdate = { | ||
}; | ||
export declare function objUpdate(found: boolean, obj: Object, newData: Object, xPath: string | null, id: string | number | null): TUpdate; | ||
export declare function objUpdate(found: boolean, obj: Object, newData: Object, xPath: string | null): TUpdate; |
{ | ||
"name": "@kitql/client", | ||
"version": "0.1.9", | ||
"version": "0.2.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "@kitql/helper": "0.1.5", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34837
834