@magnetarjs/core
Advanced tools
Comparing version 1.1.0 to 1.2.1
import { merge } from 'merge-anything'; | ||
export function defaultsGlobalConfig(config) { | ||
if (!('stores' in config)) { | ||
throw new Error('GlobalConfig must have a stores property with at least one plugin provided for the `cache` key'); | ||
} | ||
const defaults = { | ||
localStoreName: '', | ||
stores: {}, | ||
executionOrder: { | ||
@@ -7,0 +8,0 @@ read: [], |
@@ -9,5 +9,5 @@ import type { DocMetadata, DoOnFetch, OnAddedFn, OnModifiedFn, OnRemovedFn } from '@magnetarjs/types'; | ||
modifyReadResultFns: (OnAddedFn | OnModifiedFn | OnRemovedFn)[]; | ||
localStoreFns: (DoOnFetch | OnAddedFn | OnModifiedFn | OnRemovedFn)[]; | ||
cacheStoreFns: (DoOnFetch | OnAddedFn | OnModifiedFn | OnRemovedFn)[]; | ||
payload: Payload; | ||
docMetaData: DocMetadata; | ||
}): Payload | undefined; |
@@ -5,3 +5,3 @@ /** | ||
export function executeOnFns(params) { | ||
const { modifyReadResultFns, localStoreFns, payload, docMetaData } = params; | ||
const { modifyReadResultFns, cacheStoreFns, payload, docMetaData } = params; | ||
let newPayload = payload; | ||
@@ -13,3 +13,3 @@ for (const fn of modifyReadResultFns) { | ||
} | ||
for (const fn of localStoreFns) { | ||
for (const fn of cacheStoreFns) { | ||
// we only want to execute these always, regardless wether or not there's a payload | ||
@@ -16,0 +16,0 @@ newPayload = fn(newPayload, docMetaData); |
@@ -20,15 +20,15 @@ import type { GlobalConfig, ModuleConfig, PluginModuleConfig } from '@magnetarjs/types'; | ||
export declare function executeSetupModulePerStore(globalConfigStores: GlobalConfig['stores'], [collectionPath, docId]: [string, string | undefined], moduleConfig: ModuleConfig): undefined; | ||
/** Executes the `getModuleData` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleData` function from the store specified as 'cache' */ | ||
export declare function getDataFromDataStore(moduleConfig: ModuleConfig, globalConfig: GlobalConfig, collectionPath: string): Map<string, { | ||
[key: string]: any; | ||
}>; | ||
/** Executes the `getModuleData` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleData` function from the store specified as 'cache' */ | ||
export declare function getDataFromDataStore(moduleConfig: ModuleConfig, globalConfig: GlobalConfig, collectionPath: string, docId: string): { | ||
[key: string]: any; | ||
}; | ||
/** Executes the `getModuleData` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleData` function from the store specified as 'cache' */ | ||
export declare function getExistsFromDataStore(globalConfig: GlobalConfig, collectionPath: string, docId: string): undefined | 'error' | boolean; | ||
/** Executes the `getModuleCount` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleCount` function from the store specified as 'cache' */ | ||
export declare function getCountFromDataStore(moduleConfig: ModuleConfig, globalConfig: GlobalConfig, collectionPath: string): number; | ||
/** Executes the `getModuleAggregate` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleAggregate` function from the store specified as 'cache' */ | ||
export declare function getAggregateFromDataStore(kind: 'sum' | 'average', moduleConfig: ModuleConfig, globalConfig: GlobalConfig, collectionPath: string): { | ||
@@ -35,0 +35,0 @@ [key in string]: number | { |
import { isFunction, isPlainObject } from 'is-what'; | ||
import { throwIfNolocalStoreName } from './throwFns.js'; | ||
/** | ||
@@ -33,18 +32,14 @@ * Extracts the PluginModuleConfig from the ModuleConfig | ||
} | ||
/** Executes the `getModuleData` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleData` function from the store specified as 'cache' */ | ||
export function getDataFromDataStore(moduleConfig, globalConfig, collectionPath, docId) { | ||
const localStoreName = globalConfig.localStoreName; | ||
throwIfNolocalStoreName(localStoreName); | ||
const getModuleData = globalConfig.stores[localStoreName]?.getModuleData; | ||
const getModuleData = globalConfig.stores['cache']?.getModuleData; | ||
if (!getModuleData) { | ||
throw new Error('The data store did not provide a getModuleData function!'); | ||
} | ||
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, localStoreName); | ||
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, 'cache'); | ||
return getModuleData({ collectionPath, docId, pluginModuleConfig }); | ||
} | ||
/** Executes the `getModuleData` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleData` function from the store specified as 'cache' */ | ||
export function getExistsFromDataStore(globalConfig, collectionPath, docId) { | ||
const localStoreName = globalConfig.localStoreName; | ||
throwIfNolocalStoreName(localStoreName); | ||
const getModuleExists = globalConfig.stores[localStoreName]?.getModuleExists; | ||
const getModuleExists = globalConfig.stores['cache']?.getModuleExists; | ||
if (!getModuleExists) { | ||
@@ -55,22 +50,18 @@ throw new Error('The data store did not provide a getModuleExists function!'); | ||
} | ||
/** Executes the `getModuleCount` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleCount` function from the store specified as 'cache' */ | ||
export function getCountFromDataStore(moduleConfig, globalConfig, collectionPath) { | ||
const localStoreName = globalConfig.localStoreName; | ||
throwIfNolocalStoreName(localStoreName); | ||
const getModuleCount = globalConfig.stores[localStoreName]?.getModuleCount; | ||
const getModuleCount = globalConfig.stores['cache']?.getModuleCount; | ||
if (!getModuleCount) { | ||
throw new Error('The data store did not provide a getModuleCount function!'); | ||
} | ||
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, localStoreName); | ||
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, 'cache'); | ||
return getModuleCount({ collectionPath, pluginModuleConfig }); | ||
} | ||
/** Executes the `getModuleAggregate` function from the store specified as 'localStoreName' */ | ||
/** Executes the `getModuleAggregate` function from the store specified as 'cache' */ | ||
export function getAggregateFromDataStore(kind, moduleConfig, globalConfig, collectionPath) { | ||
const localStoreName = globalConfig.localStoreName; | ||
throwIfNolocalStoreName(localStoreName); | ||
const getModuleAggregate = globalConfig.stores[localStoreName]?.getModuleAggregate; | ||
const getModuleAggregate = globalConfig.stores['cache']?.getModuleAggregate; | ||
if (!getModuleAggregate) { | ||
throw new Error('The data store did not provide a getModuleAggregate function!'); | ||
} | ||
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, localStoreName); | ||
const pluginModuleConfig = getPluginModuleConfig(moduleConfig, 'cache'); | ||
return getModuleAggregate(kind, { collectionPath, pluginModuleConfig }); | ||
@@ -77,0 +68,0 @@ } |
@@ -8,3 +8,2 @@ import type { DoOnStreamFns, StreamResponse } from '@magnetarjs/types'; | ||
export declare function throwIfNoFnsToExecute(storesToExecute: string[]): undefined; | ||
export declare function throwIfNolocalStoreName(localStoreName: string): undefined; | ||
export declare function throwIfInvalidModulePath(modulePath: string, moduleType: 'collection' | 'doc'): undefined; |
import { isCollectionModule, isDocModule } from '@magnetarjs/utils'; | ||
import { isFullString } from 'is-what'; | ||
export function logError(errorMessage) { | ||
@@ -28,8 +27,2 @@ console.error('[@magnetarjs error]\n', errorMessage); | ||
} | ||
export function throwIfNolocalStoreName(localStoreName) { | ||
if (isFullString(localStoreName)) | ||
return; | ||
const errorMessage = `No 'localStoreName' provided.`; | ||
logErrorAndThrow(errorMessage); | ||
} | ||
export function throwIfInvalidModulePath(modulePath, moduleType) { | ||
@@ -36,0 +29,0 @@ let errorMessage = ''; |
@@ -69,3 +69,3 @@ import { isBoolean, isPromise } from 'is-what'; | ||
/** | ||
* each each time a store returns a `FetchResponse` then it must first go through all on added fns to potentially modify the retuned payload before writing locally | ||
* each each time a store returns a `FetchResponse` then it must first go through all on added fns to potentially modify the retuned payload before writing to cache | ||
*/ | ||
@@ -140,3 +140,3 @@ const doOnAddedFns = modifyReadResponseMap.added; | ||
modifyReadResultFns: doOnAddedFns, | ||
localStoreFns: doOnFetchFns, | ||
cacheStoreFns: doOnFetchFns, | ||
payload: docMetaData.data, | ||
@@ -146,3 +146,3 @@ docMetaData, | ||
// after doing all `doOnAddedFns` (modifying the read result) | ||
// and all `doOnFetchFns` (adding it to the local store) | ||
// and all `doOnFetchFns` (adding it to the local cache store) | ||
// we still have a record, so must return it when resolving the fetch action | ||
@@ -149,0 +149,0 @@ if (docResult) |
@@ -67,3 +67,3 @@ import { isPromise } from 'is-what'; | ||
modifyReadResultFns: modifyReadResponseFns.added, | ||
localStoreFns: doOnStreamFns.added, | ||
cacheStoreFns: doOnStreamFns.added, | ||
payload: result.payload, | ||
@@ -88,3 +88,3 @@ docMetaData: result.meta, | ||
modifyReadResultFns: modifyReadResponseFns.added, | ||
localStoreFns: doOnStreamFns.added, | ||
cacheStoreFns: doOnStreamFns.added, | ||
payload: result.payload, | ||
@@ -102,3 +102,3 @@ docMetaData: result.meta, | ||
modifyReadResultFns: modifyReadResponseFns.removed, | ||
localStoreFns: doOnStreamFns.removed, | ||
cacheStoreFns: doOnStreamFns.removed, | ||
payload: _payload, | ||
@@ -105,0 +105,0 @@ docMetaData: _meta, |
@@ -0,3 +1,5 @@ | ||
import { isStoreSplit } from '@magnetarjs/utils'; | ||
import { mapGetOrSet } from 'getorset-anything'; | ||
import { isFullArray, isFullString } from 'is-what'; | ||
import { isAnyObject, isFullArray, isFullString } from 'is-what'; | ||
import { mapObject } from 'map-anything'; | ||
import { getEventNameFnsMap } from '../helpers/eventHelpers.js'; | ||
@@ -17,3 +19,3 @@ import { getModifyPayloadFnsMap } from '../helpers/modifyPayload.js'; | ||
}); | ||
// we need to create a promise we'll resolve later to prevent any incoming docs from being written to the local state during this time | ||
// we need to create a promise we'll resolve later to prevent any incoming docs from being written to the cache store during this time | ||
if (writeLock.promise === null) { | ||
@@ -53,5 +55,13 @@ writeLock.promise = new Promise((resolve) => { | ||
throwIfNoFnsToExecute(storesToExecute); | ||
const unwrapStoreSplits = (payloadChunk, storeName) => { | ||
return isStoreSplit(payloadChunk) | ||
? payloadChunk.storePayloadDic[storeName] | ||
: isAnyObject(payloadChunk) | ||
? mapObject(payloadChunk, (value) => unwrapStoreSplits(value, storeName)) | ||
: payloadChunk; | ||
}; | ||
// update the payload | ||
let storePayloadDic = storesToExecute.reduce((dic, storeName) => ({ ...dic, [storeName]: unwrapStoreSplits(payload, storeName) }), {}); | ||
for (const modifyFn of modifyPayloadFnsMap[actionName]) { | ||
payload = modifyFn(payload, docId); | ||
storePayloadDic = mapObject(storePayloadDic, (payloadValue) => modifyFn(payloadValue, docId)); | ||
} | ||
@@ -91,3 +101,3 @@ let stopExecution = false; | ||
pluginAction, | ||
payload, // should always use the payload as passed originally for clarity | ||
payload: storePayloadDic[storeName], // should always use the payload as passed originally for clarity | ||
actionConfig, | ||
@@ -109,3 +119,3 @@ eventNameFnsMap, | ||
await pluginRevertAction({ | ||
payload, | ||
payload: storePayloadDic[storeName], | ||
actionConfig, | ||
@@ -121,3 +131,3 @@ collectionPath, | ||
for (const fn of eventNameFnsMap.revert) { | ||
await fn({ payload, result: resultFromPlugin, actionName, storeName, collectionPath, docId, path: modulePath, pluginModuleConfig }); // prettier-ignore | ||
await fn({ payload: storePayloadDic[storeName], result: resultFromPlugin, actionName, storeName, collectionPath, docId, path: modulePath, pluginModuleConfig }); // prettier-ignore | ||
} | ||
@@ -124,0 +134,0 @@ } |
{ | ||
"name": "@magnetarjs/core", | ||
"version": "1.1.0", | ||
"version": "1.2.1", | ||
"type": "module", | ||
@@ -16,2 +16,7 @@ "sideEffects": false, | ||
], | ||
"scripts": { | ||
"typecheck": "tsc --noEmit", | ||
"build": "del-cli dist && tsc", | ||
"test": "vitest run" | ||
}, | ||
"author": "Luca Ban - Mesqueeb", | ||
@@ -22,7 +27,9 @@ "funding": "https://github.com/sponsors/mesqueeb", | ||
"dependencies": { | ||
"@magnetarjs/types": "*", | ||
"@magnetarjs/utils": "*", | ||
"filter-anything": "^4.0.0", | ||
"getorset-anything": "^0.1.0", | ||
"is-what": "^5.0.0", | ||
"merge-anything": "^5.1.7", | ||
"@magnetarjs/types": "1.1.0", | ||
"@magnetarjs/utils": "1.1.0" | ||
"map-anything": "^3.0.0", | ||
"merge-anything": "^5.1.7" | ||
}, | ||
@@ -47,2 +54,4 @@ "keywords": [ | ||
"data-store", | ||
"data-cache", | ||
"cache-store", | ||
"local-store", | ||
@@ -61,9 +70,3 @@ "remote-store", | ||
"url": "https://github.com/cycraft/magnetar/issues" | ||
}, | ||
"scripts": { | ||
"typecheck": "tsc --noEmit", | ||
"build": "del-cli dist && tsc", | ||
"dev": "pnpm build --watch", | ||
"test": "vitest run" | ||
} | ||
} | ||
} |
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
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 2 instances 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
71045
7
39
1421
4
+ Addedfilter-anything@^4.0.0
+ Addedmap-anything@^3.0.0
+ Added@magnetarjs/types@1.2.7(transitive)
+ Added@magnetarjs/utils@1.2.7(transitive)
+ Addedfilter-anything@4.0.2(transitive)
+ Addedmap-anything@3.0.1(transitive)
+ Addedmerge-anything@6.0.2(transitive)
+ Addedpath-to-prop@3.0.1(transitive)
+ Addedts-toolbelt@9.6.0(transitive)
- Removed@magnetarjs/types@1.1.0(transitive)
- Removed@magnetarjs/utils@1.1.0(transitive)
- Removedpath-to-prop@2.0.4(transitive)
Updated@magnetarjs/types@*
Updated@magnetarjs/utils@*