@magnetarjs/plugin-vue3
Advanced tools
Comparing version 0.1.24 to 0.1.26
@@ -100,18 +100,99 @@ 'use strict'; | ||
/** | ||
* Filters a Collection module's data map `Map<string, DocData>` based on provided clauses. | ||
* | ||
* @param {Map<string, Record<string, any>>} collectionDB | ||
* @param {Clauses} clauses | ||
* @returns {Map<string, Record<string, any>>} | ||
*/ | ||
function filterDataPerClauses(collectionDB, clauses) { | ||
const { where = [], orderBy = [], limit } = clauses; | ||
// return the same collectionDB to be sure to keep reactivity | ||
if (!where.length && !orderBy.length && !isWhat.isNumber(limit)) | ||
return collectionDB; | ||
// all other cases we need to create a new Map() with the results | ||
const entries = []; | ||
collectionDB.forEach((docData, docId) => { | ||
const passesWhereFilters = where.every((whereQuery) => { | ||
const [fieldPath, operator, expectedValue] = whereQuery; | ||
const valueAtFieldPath = utils.parseValueForFilters(pathToProp.getProp(docData, fieldPath)); | ||
let passes = false; | ||
switch (operator) { | ||
case '==': | ||
passes = valueAtFieldPath == expectedValue; | ||
break; | ||
case '!=': | ||
passes = valueAtFieldPath != expectedValue; | ||
break; | ||
case '<': | ||
passes = valueAtFieldPath < expectedValue; | ||
break; | ||
case '<=': | ||
passes = valueAtFieldPath <= expectedValue; | ||
break; | ||
case '>': | ||
passes = valueAtFieldPath > expectedValue; | ||
break; | ||
case '>=': | ||
passes = valueAtFieldPath >= expectedValue; | ||
break; | ||
case 'in': | ||
passes = isWhat.isArray(expectedValue) && expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'not-in': | ||
passes = isWhat.isArray(expectedValue) && !expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'array-contains': | ||
passes = isWhat.isArray(valueAtFieldPath) && valueAtFieldPath.includes(expectedValue); | ||
break; | ||
case 'array-contains-any': | ||
passes = | ||
isWhat.isArray(valueAtFieldPath) && | ||
valueAtFieldPath.some((v) => isWhat.isArray(expectedValue) && expectedValue.includes(v)); | ||
break; | ||
default: | ||
throw new Error('invalid operator'); | ||
} | ||
return passes; | ||
}); | ||
if (!passesWhereFilters) | ||
return; | ||
entries.push([docId, docData]); | ||
}); | ||
// orderBy | ||
const by = orderBy.reduce((carry, [path, direction = 'asc']) => { | ||
const sorter = { | ||
[direction]: (entry) => pathToProp.getProp(entry[1], path), | ||
}; | ||
carry.push(sorter); | ||
return carry; | ||
}, []); | ||
const entriesOrdered = orderBy.length ? fastSort.sort(entries).by(by) : entries; | ||
// limit | ||
const entriesLimited = isWhat.isNumber(limit) ? entriesOrdered.slice(0, limit) : entriesOrdered; | ||
// turn back into MAP | ||
const filteredDataMap = new Map(entriesLimited); | ||
return filteredDataMap; | ||
} | ||
function fetchActionFactory(data, Vue3StoreOptions) { | ||
return function ({ payload, collectionPath, docId, actionConfig, pluginModuleConfig, }) { | ||
const optimisticFetch = !payload || !Object.hasOwnProperty.call(payload || {}, 'force') || (payload === null || payload === void 0 ? void 0 : payload.force) === false; | ||
const force = (payload === null || payload === void 0 ? void 0 : payload.force) === true; | ||
const optimisticFetch = !force; | ||
if (optimisticFetch) { | ||
const collectionData = data[collectionPath]; | ||
if (!docId && collectionData.size > 0) { | ||
const localDocs = [...collectionData.entries()].map(([_docId, data]) => ({ | ||
data, | ||
exists: 'unknown', | ||
id: _docId, | ||
})); | ||
const fetchResponse = { docs: localDocs }; | ||
return fetchResponse; | ||
if (!docId) { | ||
const { where, orderBy, limit } = pluginModuleConfig; | ||
const collectionData = filterDataPerClauses(data[collectionPath], { where, orderBy, limit }); | ||
if (collectionData.size > 0) { | ||
const localDocs = [...collectionData.entries()].map(([_docId, data]) => ({ | ||
data, | ||
exists: 'unknown', | ||
id: _docId, | ||
})); | ||
const fetchResponse = { docs: localDocs }; | ||
return fetchResponse; // if size === 0 fall through to returning DoOnFetch down below | ||
} | ||
} | ||
if (docId) { | ||
const localDoc = collectionData.get(docId); | ||
const localDoc = data[collectionPath].get(docId); | ||
// if already fetched | ||
@@ -218,79 +299,2 @@ if (localDoc) { | ||
/** | ||
* Filters a Collection module's data map `Map<string, DocData>` based on provided clauses. | ||
* | ||
* @param {Map<string, Record<string, any>>} collectionDB | ||
* @param {Clauses} clauses | ||
* @returns {Map<string, Record<string, any>>} | ||
*/ | ||
function filterDataPerClauses(collectionDB, clauses) { | ||
const { where = [], orderBy = [], limit } = clauses; | ||
// return the same collectionDB to be sure to keep reactivity | ||
if (!where.length && !orderBy.length && !isWhat.isNumber(limit)) | ||
return collectionDB; | ||
// all other cases we need to create a new Map() with the results | ||
const entries = []; | ||
collectionDB.forEach((docData, docId) => { | ||
const passesWhereFilters = where.every((whereQuery) => { | ||
const [fieldPath, operator, expectedValue] = whereQuery; | ||
const valueAtFieldPath = utils.parseValueForFilters(pathToProp.getProp(docData, fieldPath)); | ||
let passes = false; | ||
switch (operator) { | ||
case '==': | ||
passes = valueAtFieldPath == expectedValue; | ||
break; | ||
case '!=': | ||
passes = valueAtFieldPath != expectedValue; | ||
break; | ||
case '<': | ||
passes = valueAtFieldPath < expectedValue; | ||
break; | ||
case '<=': | ||
passes = valueAtFieldPath <= expectedValue; | ||
break; | ||
case '>': | ||
passes = valueAtFieldPath > expectedValue; | ||
break; | ||
case '>=': | ||
passes = valueAtFieldPath >= expectedValue; | ||
break; | ||
case 'in': | ||
passes = isWhat.isArray(expectedValue) && expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'not-in': | ||
passes = isWhat.isArray(expectedValue) && !expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'array-contains': | ||
passes = isWhat.isArray(valueAtFieldPath) && valueAtFieldPath.includes(expectedValue); | ||
break; | ||
case 'array-contains-any': | ||
passes = | ||
isWhat.isArray(valueAtFieldPath) && | ||
valueAtFieldPath.some((v) => isWhat.isArray(expectedValue) && expectedValue.includes(v)); | ||
break; | ||
default: | ||
throw new Error('invalid operator'); | ||
} | ||
return passes; | ||
}); | ||
if (!passesWhereFilters) | ||
return; | ||
entries.push([docId, docData]); | ||
}); | ||
// orderBy | ||
const by = orderBy.reduce((carry, [path, direction = 'asc']) => { | ||
const sorter = { | ||
[direction]: (entry) => pathToProp.getProp(entry[1], path), | ||
}; | ||
carry.push(sorter); | ||
return carry; | ||
}, []); | ||
const entriesOrdered = orderBy.length ? fastSort.sort(entries).by(by) : entries; | ||
// limit | ||
const entriesLimited = isWhat.isNumber(limit) ? entriesOrdered.slice(0, limit) : entriesOrdered; | ||
// turn back into MAP | ||
const filteredDataMap = new Map(entriesLimited); | ||
return filteredDataMap; | ||
} | ||
/** | ||
* a Magnetar plugin is a single function that returns a `PluginInstance` | ||
@@ -297,0 +301,0 @@ * the plugin implements the logic for all actions that a can be called from a Magnetar module instance |
@@ -96,18 +96,99 @@ import { copy } from 'copy-anything'; | ||
/** | ||
* Filters a Collection module's data map `Map<string, DocData>` based on provided clauses. | ||
* | ||
* @param {Map<string, Record<string, any>>} collectionDB | ||
* @param {Clauses} clauses | ||
* @returns {Map<string, Record<string, any>>} | ||
*/ | ||
function filterDataPerClauses(collectionDB, clauses) { | ||
const { where = [], orderBy = [], limit } = clauses; | ||
// return the same collectionDB to be sure to keep reactivity | ||
if (!where.length && !orderBy.length && !isNumber(limit)) | ||
return collectionDB; | ||
// all other cases we need to create a new Map() with the results | ||
const entries = []; | ||
collectionDB.forEach((docData, docId) => { | ||
const passesWhereFilters = where.every((whereQuery) => { | ||
const [fieldPath, operator, expectedValue] = whereQuery; | ||
const valueAtFieldPath = parseValueForFilters(getProp(docData, fieldPath)); | ||
let passes = false; | ||
switch (operator) { | ||
case '==': | ||
passes = valueAtFieldPath == expectedValue; | ||
break; | ||
case '!=': | ||
passes = valueAtFieldPath != expectedValue; | ||
break; | ||
case '<': | ||
passes = valueAtFieldPath < expectedValue; | ||
break; | ||
case '<=': | ||
passes = valueAtFieldPath <= expectedValue; | ||
break; | ||
case '>': | ||
passes = valueAtFieldPath > expectedValue; | ||
break; | ||
case '>=': | ||
passes = valueAtFieldPath >= expectedValue; | ||
break; | ||
case 'in': | ||
passes = isArray(expectedValue) && expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'not-in': | ||
passes = isArray(expectedValue) && !expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'array-contains': | ||
passes = isArray(valueAtFieldPath) && valueAtFieldPath.includes(expectedValue); | ||
break; | ||
case 'array-contains-any': | ||
passes = | ||
isArray(valueAtFieldPath) && | ||
valueAtFieldPath.some((v) => isArray(expectedValue) && expectedValue.includes(v)); | ||
break; | ||
default: | ||
throw new Error('invalid operator'); | ||
} | ||
return passes; | ||
}); | ||
if (!passesWhereFilters) | ||
return; | ||
entries.push([docId, docData]); | ||
}); | ||
// orderBy | ||
const by = orderBy.reduce((carry, [path, direction = 'asc']) => { | ||
const sorter = { | ||
[direction]: (entry) => getProp(entry[1], path), | ||
}; | ||
carry.push(sorter); | ||
return carry; | ||
}, []); | ||
const entriesOrdered = orderBy.length ? sort(entries).by(by) : entries; | ||
// limit | ||
const entriesLimited = isNumber(limit) ? entriesOrdered.slice(0, limit) : entriesOrdered; | ||
// turn back into MAP | ||
const filteredDataMap = new Map(entriesLimited); | ||
return filteredDataMap; | ||
} | ||
function fetchActionFactory(data, Vue3StoreOptions) { | ||
return function ({ payload, collectionPath, docId, actionConfig, pluginModuleConfig, }) { | ||
const optimisticFetch = !payload || !Object.hasOwnProperty.call(payload || {}, 'force') || (payload === null || payload === void 0 ? void 0 : payload.force) === false; | ||
const force = (payload === null || payload === void 0 ? void 0 : payload.force) === true; | ||
const optimisticFetch = !force; | ||
if (optimisticFetch) { | ||
const collectionData = data[collectionPath]; | ||
if (!docId && collectionData.size > 0) { | ||
const localDocs = [...collectionData.entries()].map(([_docId, data]) => ({ | ||
data, | ||
exists: 'unknown', | ||
id: _docId, | ||
})); | ||
const fetchResponse = { docs: localDocs }; | ||
return fetchResponse; | ||
if (!docId) { | ||
const { where, orderBy, limit } = pluginModuleConfig; | ||
const collectionData = filterDataPerClauses(data[collectionPath], { where, orderBy, limit }); | ||
if (collectionData.size > 0) { | ||
const localDocs = [...collectionData.entries()].map(([_docId, data]) => ({ | ||
data, | ||
exists: 'unknown', | ||
id: _docId, | ||
})); | ||
const fetchResponse = { docs: localDocs }; | ||
return fetchResponse; // if size === 0 fall through to returning DoOnFetch down below | ||
} | ||
} | ||
if (docId) { | ||
const localDoc = collectionData.get(docId); | ||
const localDoc = data[collectionPath].get(docId); | ||
// if already fetched | ||
@@ -214,79 +295,2 @@ if (localDoc) { | ||
/** | ||
* Filters a Collection module's data map `Map<string, DocData>` based on provided clauses. | ||
* | ||
* @param {Map<string, Record<string, any>>} collectionDB | ||
* @param {Clauses} clauses | ||
* @returns {Map<string, Record<string, any>>} | ||
*/ | ||
function filterDataPerClauses(collectionDB, clauses) { | ||
const { where = [], orderBy = [], limit } = clauses; | ||
// return the same collectionDB to be sure to keep reactivity | ||
if (!where.length && !orderBy.length && !isNumber(limit)) | ||
return collectionDB; | ||
// all other cases we need to create a new Map() with the results | ||
const entries = []; | ||
collectionDB.forEach((docData, docId) => { | ||
const passesWhereFilters = where.every((whereQuery) => { | ||
const [fieldPath, operator, expectedValue] = whereQuery; | ||
const valueAtFieldPath = parseValueForFilters(getProp(docData, fieldPath)); | ||
let passes = false; | ||
switch (operator) { | ||
case '==': | ||
passes = valueAtFieldPath == expectedValue; | ||
break; | ||
case '!=': | ||
passes = valueAtFieldPath != expectedValue; | ||
break; | ||
case '<': | ||
passes = valueAtFieldPath < expectedValue; | ||
break; | ||
case '<=': | ||
passes = valueAtFieldPath <= expectedValue; | ||
break; | ||
case '>': | ||
passes = valueAtFieldPath > expectedValue; | ||
break; | ||
case '>=': | ||
passes = valueAtFieldPath >= expectedValue; | ||
break; | ||
case 'in': | ||
passes = isArray(expectedValue) && expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'not-in': | ||
passes = isArray(expectedValue) && !expectedValue.includes(valueAtFieldPath); | ||
break; | ||
case 'array-contains': | ||
passes = isArray(valueAtFieldPath) && valueAtFieldPath.includes(expectedValue); | ||
break; | ||
case 'array-contains-any': | ||
passes = | ||
isArray(valueAtFieldPath) && | ||
valueAtFieldPath.some((v) => isArray(expectedValue) && expectedValue.includes(v)); | ||
break; | ||
default: | ||
throw new Error('invalid operator'); | ||
} | ||
return passes; | ||
}); | ||
if (!passesWhereFilters) | ||
return; | ||
entries.push([docId, docData]); | ||
}); | ||
// orderBy | ||
const by = orderBy.reduce((carry, [path, direction = 'asc']) => { | ||
const sorter = { | ||
[direction]: (entry) => getProp(entry[1], path), | ||
}; | ||
carry.push(sorter); | ||
return carry; | ||
}, []); | ||
const entriesOrdered = orderBy.length ? sort(entries).by(by) : entries; | ||
// limit | ||
const entriesLimited = isNumber(limit) ? entriesOrdered.slice(0, limit) : entriesOrdered; | ||
// turn back into MAP | ||
const filteredDataMap = new Map(entriesLimited); | ||
return filteredDataMap; | ||
} | ||
/** | ||
* a Magnetar plugin is a single function that returns a `PluginInstance` | ||
@@ -293,0 +297,0 @@ * the plugin implements the logic for all actions that a can be called from a Magnetar module instance |
{ | ||
"name": "@magnetarjs/plugin-vue3", | ||
"version": "0.1.24", | ||
"version": "0.1.26", | ||
"sideEffects": false, | ||
@@ -24,4 +24,4 @@ "description": "Magnetar plugin vue3", | ||
"dependencies": { | ||
"@magnetarjs/core": "^0.3.5", | ||
"@magnetarjs/utils": "^0.1.23", | ||
"@magnetarjs/core": "^0.3.7", | ||
"@magnetarjs/utils": "^0.1.25", | ||
"copy-anything": "^2.0.3", | ||
@@ -36,3 +36,3 @@ "fast-sort": "^3.1.1", | ||
"devDependencies": { | ||
"@magnetarjs/test-utils": "^0.1.16", | ||
"@magnetarjs/test-utils": "^0.1.18", | ||
"vue": "^3.2.26" | ||
@@ -83,3 +83,3 @@ }, | ||
}, | ||
"gitHead": "a07b7b0bd09341394ac94c9627650d813052d15a" | ||
"gitHead": "641b0f9aadf1a8ac1394eece5b126ee6dbd29c87" | ||
} |
@@ -8,2 +8,3 @@ import { | ||
} from '@magnetarjs/core' | ||
import { filterDataPerClauses } from '../helpers/dataHelpers' | ||
import { Vue3StoreModuleConfig, Vue3StoreOptions } from '../CreatePlugin' | ||
@@ -23,17 +24,20 @@ import { insertActionFactory } from './insert' | ||
}: PluginFetchActionPayload<Vue3StoreModuleConfig>): FetchResponse | DoOnFetch { | ||
const optimisticFetch = | ||
!payload || !Object.hasOwnProperty.call(payload || {}, 'force') || payload?.force === false | ||
const force = payload?.force === true | ||
const optimisticFetch = !force | ||
if (optimisticFetch) { | ||
const collectionData = data[collectionPath] | ||
if (!docId && collectionData.size > 0) { | ||
const localDocs: DocMetadata[] = [...collectionData.entries()].map(([_docId, data]) => ({ | ||
data, | ||
exists: 'unknown', | ||
id: _docId, | ||
})) | ||
const fetchResponse: FetchResponse = { docs: localDocs } | ||
return fetchResponse | ||
if (!docId) { | ||
const { where, orderBy, limit } = pluginModuleConfig | ||
const collectionData = filterDataPerClauses(data[collectionPath], { where, orderBy, limit }) | ||
if (collectionData.size > 0) { | ||
const localDocs: DocMetadata[] = [...collectionData.entries()].map(([_docId, data]) => ({ | ||
data, | ||
exists: 'unknown', | ||
id: _docId, | ||
})) | ||
const fetchResponse: FetchResponse = { docs: localDocs } | ||
return fetchResponse // if size === 0 fall through to returning DoOnFetch down below | ||
} | ||
} | ||
if (docId) { | ||
const localDoc = collectionData.get(docId) | ||
const localDoc = data[collectionPath].get(docId) | ||
// if already fetched | ||
@@ -40,0 +44,0 @@ if (localDoc) { |
99063
2351
Updated@magnetarjs/core@^0.3.7
Updated@magnetarjs/utils@^0.1.25