@magnetarjs/core
Advanced tools
Comparing version 0.0.15 to 0.0.16
@@ -6,2 +6,3 @@ 'use strict'; | ||
var mergeAnything = require('merge-anything'); | ||
var filterAnything = require('filter-anything'); | ||
var isWhat = require('is-what'); | ||
@@ -586,3 +587,3 @@ | ||
const doc = (docId, _moduleConfig = {}) => { | ||
return docFn(`${path}/${docId}`, _moduleConfig); | ||
return docFn(`${path}/${docId}`, mergeAnything.merge(filterAnything.omit(moduleConfig, ['configPerStore']), _moduleConfig)); | ||
}; | ||
@@ -589,0 +590,0 @@ const insert = handleActionPerStore([collectionPath, docId], moduleConfig, globalConfig, 'insert', actionNameTypeMap.insert, docFn, collectionFn); //prettier-ignore |
@@ -1,2 +0,3 @@ | ||
import { mergeAndConcat, merge } from 'merge-anything'; | ||
import { merge, mergeAndConcat } from 'merge-anything'; | ||
import { omit } from 'filter-anything'; | ||
import { isPlainObject, isFunction, isArray, isFullString, isPromise } from 'is-what'; | ||
@@ -581,3 +582,3 @@ | ||
const doc = (docId, _moduleConfig = {}) => { | ||
return docFn(`${path}/${docId}`, _moduleConfig); | ||
return docFn(`${path}/${docId}`, merge(omit(moduleConfig, ['configPerStore']), _moduleConfig)); | ||
}; | ||
@@ -584,0 +585,0 @@ const insert = handleActionPerStore([collectionPath, docId], moduleConfig, globalConfig, 'insert', actionNameTypeMap.insert, docFn, collectionFn); //prettier-ignore |
{ | ||
"name": "@magnetarjs/core", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"sideEffects": false, | ||
@@ -20,2 +20,3 @@ "description": "Magnetar core library.", | ||
"dependencies": { | ||
"filter-anything": "^2.2.1", | ||
"is-what": "^3.14.1", | ||
@@ -70,3 +71,3 @@ "merge-anything": "^4.0.1", | ||
}, | ||
"gitHead": "74d53eacf54c3892f873b23ba4231fe7b821ea47" | ||
"gitHead": "b749a010af8f47d2cd21837c92d4d10ca34375d9" | ||
} |
import { O } from 'ts-toolbelt' | ||
import { merge, mergeAndConcat } from 'merge-anything' | ||
import { omit } from 'filter-anything' | ||
import { | ||
@@ -19,3 +21,2 @@ MagnetarFetchAction, | ||
import { WhereClause, WhereFilterOp, OrderByClause } from './types/clauses' | ||
import { mergeAndConcat } from 'merge-anything' | ||
@@ -78,3 +79,3 @@ export type CollectionInstance<DocDataType extends Record<string, any> = Record<string, any>> = { | ||
const doc: DocFn<DocDataType> = (docId: string, _moduleConfig: ModuleConfig = {}) => { | ||
return docFn(`${path}/${docId}`, _moduleConfig) | ||
return docFn(`${path}/${docId}`, merge(omit(moduleConfig, ['configPerStore']), _moduleConfig)) | ||
} | ||
@@ -81,0 +82,0 @@ |
@@ -12,2 +12,3 @@ import { O } from 'ts-toolbelt' | ||
export { isDocModule, isCollectionModule } from './helpers/pathHelpers' | ||
export type { GlobalConfig, ModuleConfig } from './types/config' | ||
@@ -14,0 +15,0 @@ function configWithDefaults(config: GlobalConfig): O.Compulsory<GlobalConfig> { |
@@ -1,3 +0,9 @@ | ||
import { Magnetar, MagnetarInstance, CollectionInstance, DocInstance } from '../../src' | ||
import { | ||
Magnetar, | ||
MagnetarInstance, | ||
CollectionInstance, | ||
DocInstance, | ||
GlobalConfig, | ||
} from '../../src' | ||
import { | ||
pokedex, | ||
@@ -36,3 +42,5 @@ PokedexEntry, | ||
export function createMagnetarInstance(): { | ||
export function createMagnetarInstance( | ||
magnetarGlobalConfig: Partial<GlobalConfig> = {} | ||
): { | ||
pokedexModule: CollectionInstance<PokedexModuleData> | ||
@@ -52,2 +60,3 @@ trainerModule: DocInstance<TrainerModuleData> | ||
}, | ||
...magnetarGlobalConfig, | ||
}) | ||
@@ -54,0 +63,0 @@ const pokedexModule = magnetar.collection<PokedexModuleData>('pokedex', { |
@@ -5,25 +5,188 @@ import test from 'ava' | ||
test('fetch: can mutate payload & read response', async (t) => { | ||
test('fetch: can mutate payload & read response (config in global magnetar instance)', async (t) => { | ||
function addSeen(payload: any) { | ||
return { ...payload, seen: true } | ||
} | ||
function addToken(payload: any) { | ||
function addToken(payload: any = {}) { | ||
return { ...payload, auth: 'Bearer 123123' } | ||
} | ||
// get resolves once all stores have given a response with data | ||
const { magnetar } = createMagnetarInstance({ | ||
modifyPayloadOn: { read: addToken }, | ||
modifyReadResponseOn: { added: addSeen }, | ||
}) | ||
try { | ||
let payloadInSuccessEvent: any | ||
const result = await magnetar.collection('pokedex').fetch(undefined, { | ||
on: { | ||
success: ({ payload }) => { | ||
payloadInSuccessEvent = payload | ||
}, | ||
}, | ||
}) | ||
// the remote result SHOULD HAVE the applied defaults | ||
t.deepEqual(payloadInSuccessEvent, { auth: 'Bearer 123123' }) | ||
t.deepEqual(result.data.get('136'), { ...pokedex(136), seen: true }) | ||
} catch (error) { | ||
t.fail(error) | ||
} | ||
// the local store SHOULD HAVE the applied defaults | ||
t.deepEqual(magnetar.collection('pokedex').data.get('136'), { ...pokedex(136), seen: true }) | ||
}) | ||
test('stream: can mutate payload & read response (config in global magnetar instance)', async (t) => { | ||
function addSeen(payload: any) { | ||
return { ...payload, seen: true } | ||
} | ||
function addToken(payload: any = {}) { | ||
return { ...payload, auth: 'Bearer 123123' } | ||
} | ||
const { magnetar } = createMagnetarInstance({ | ||
modifyPayloadOn: { read: addToken }, | ||
modifyReadResponseOn: { added: addSeen }, | ||
}) | ||
let payloadInSuccessEvent: any | ||
magnetar.collection('pokedex').stream( | ||
{}, | ||
{ | ||
on: { | ||
before: ({ payload }) => { | ||
payloadInSuccessEvent = payload | ||
}, | ||
}, | ||
} | ||
) | ||
await waitMs(600) | ||
// the local store SHOULD HAVE the applied defaults | ||
t.deepEqual(payloadInSuccessEvent, { auth: 'Bearer 123123' }) | ||
t.deepEqual(magnetar.collection('pokedex').data.get('2'), { ...pokedex(2), seen: true }) | ||
}) | ||
test('insert: can mutate payload (config in global magnetar instance)', async (t) => { | ||
function addSeen(payload: any) { | ||
if (!('seen' in payload)) return { ...payload, seen: true } | ||
} | ||
// get resolves once all stores have given a response with data | ||
const { magnetar } = createMagnetarInstance({ | ||
modifyPayloadOn: { write: addSeen }, | ||
}) | ||
try { | ||
const resultA = await magnetar.collection('pokedex').insert(pokedex(7)) | ||
const resultB = await magnetar.collection('pokedex').doc('10').insert(pokedex(10)) | ||
const resultC = await magnetar.doc('pokedex/25').insert(pokedex(25)) | ||
// the remote result SHOULD HAVE the applied defaults | ||
t.deepEqual(resultA.data, { ...pokedex(7), seen: true }) | ||
t.deepEqual(resultB.data, { ...pokedex(10), seen: true }) | ||
t.deepEqual(resultC.data, { ...pokedex(25), seen: true }) | ||
} catch (error) { | ||
t.fail(error) | ||
} | ||
// the local store SHOULD HAVE the applied defaults | ||
t.deepEqual(magnetar.collection('pokedex').data.get('7'), { ...pokedex(7), seen: true }) | ||
t.deepEqual(magnetar.collection('pokedex').data.get('10'), { ...pokedex(10), seen: true }) | ||
t.deepEqual(magnetar.collection('pokedex').data.get('25'), { ...pokedex(25), seen: true }) | ||
}) | ||
test('fetch: can mutate payload & read response (config in module)', async (t) => { | ||
function addSeen(payload: any) { | ||
return { ...payload, seen: true } | ||
} | ||
function addToken(payload: any = {}) { | ||
return { ...payload, auth: 'Bearer 123123' } | ||
} | ||
// get resolves once all stores have given a response with data | ||
const { magnetar } = createMagnetarInstance() | ||
const pokedexModule = magnetar.collection('pokedex', { | ||
modifyPayloadOn: { read: addToken }, | ||
modifyReadResponseOn: { added: addSeen }, | ||
}) | ||
try { | ||
let payloadInSuccessEvent: any | ||
const result = await pokedexModule.fetch(undefined, { | ||
on: { | ||
success: ({ payload }) => { | ||
payloadInSuccessEvent = payload | ||
}, | ||
}, | ||
}) | ||
// the remote result SHOULD HAVE the applied defaults | ||
t.deepEqual(payloadInSuccessEvent, { auth: 'Bearer 123123' }) | ||
t.deepEqual(result.data.get('136'), { ...pokedex(136), seen: true }) | ||
} catch (error) { | ||
t.fail(error) | ||
} | ||
// the local store SHOULD HAVE the applied defaults | ||
t.deepEqual(pokedexModule.data.get('136'), { ...pokedex(136), seen: true }) | ||
}) | ||
test('stream: can mutate payload & read response (config in module)', async (t) => { | ||
function addSeen(payload: any) { | ||
return { ...payload, seen: true } | ||
} | ||
function addToken(payload: any = {}) { | ||
return { ...payload, auth: 'Bearer 123123' } | ||
} | ||
const { magnetar } = createMagnetarInstance() | ||
const pokedexModule = magnetar.collection('pokedex', { | ||
modifyPayloadOn: { read: addToken }, | ||
modifyReadResponseOn: { added: addSeen }, | ||
}) | ||
let payloadInSuccessEvent: any | ||
pokedexModule.stream( | ||
{}, | ||
{ | ||
on: { | ||
before: ({ payload }) => { | ||
payloadInSuccessEvent = payload | ||
}, | ||
}, | ||
} | ||
) | ||
await waitMs(600) | ||
// the local store SHOULD HAVE the applied defaults | ||
t.deepEqual(payloadInSuccessEvent, { auth: 'Bearer 123123' }) | ||
t.deepEqual(pokedexModule.data.get('2'), { ...pokedex(2), seen: true }) | ||
}) | ||
test('insert: can mutate payload (config in module)', async (t) => { | ||
function addSeen(payload: any) { | ||
if (!('seen' in payload)) return { ...payload, seen: true } | ||
} | ||
// get resolves once all stores have given a response with data | ||
const { magnetar } = createMagnetarInstance() | ||
const pokedexModule = magnetar.collection('pokedex', { | ||
modifyPayloadOn: { write: addSeen }, | ||
}) | ||
try { | ||
const payload = pokedex(7) | ||
const result = await pokedexModule.insert(payload) | ||
// the remote result SHOULD HAVE the applied defaults | ||
t.deepEqual(result.data, { ...pokedex(7), seen: true }) | ||
} catch (error) { | ||
t.fail(error) | ||
} | ||
// the local store SHOULD HAVE the applied defaults | ||
t.deepEqual(pokedexModule.data.get('7'), { ...pokedex(7), seen: true }) | ||
}) | ||
test('fetch: can mutate payload & read response (config in action)', async (t) => { | ||
function addSeen(payload: any) { | ||
return { ...payload, seen: true } | ||
} | ||
function addToken(payload: any = {}) { | ||
return { ...payload, auth: 'Bearer 123123' } | ||
} | ||
// get resolves once all stores have given a response with data | ||
const { pokedexModule } = createMagnetarInstance() | ||
t.deepEqual(pokedexModule.data.get('1'), pokedex(1)) | ||
try { | ||
let payloadInSuccessEvent: any | ||
const result = await pokedexModule.fetch( | ||
{}, | ||
{ | ||
modifyPayloadOn: { | ||
read: addToken, | ||
}, | ||
modifyReadResponseOn: { | ||
added: addSeen, | ||
}, | ||
modifyPayloadOn: { read: addToken }, | ||
modifyReadResponseOn: { added: addSeen }, | ||
on: { | ||
success: ({ payload }) => { | ||
t.deepEqual(payload, { auth: 'Bearer 123123' }) | ||
payloadInSuccessEvent = payload | ||
}, | ||
@@ -36,2 +199,3 @@ }, | ||
t.deepEqual(result.data.get('136'), { ...pokedex(136), seen: true }) | ||
t.deepEqual(payloadInSuccessEvent, { auth: 'Bearer 123123' }) | ||
} catch (error) { | ||
@@ -45,7 +209,7 @@ t.fail(error) | ||
test('stream: can mutate payload & read response', async (t) => { | ||
test('stream: can mutate payload & read response (config in action)', async (t) => { | ||
function addSeen(payload: any) { | ||
return { ...payload, seen: true } | ||
} | ||
function addToken(payload: any) { | ||
function addToken(payload: any = {}) { | ||
return { ...payload, auth: 'Bearer 123123' } | ||
@@ -55,14 +219,11 @@ } | ||
t.deepEqual(pokedexModule.data.get('1'), pokedex(1)) | ||
let payloadInSuccessEvent: any | ||
pokedexModule.stream( | ||
{}, | ||
{ | ||
modifyPayloadOn: { | ||
read: addToken, | ||
}, | ||
modifyReadResponseOn: { | ||
added: addSeen, | ||
}, | ||
modifyPayloadOn: { read: addToken }, | ||
modifyReadResponseOn: { added: addSeen }, | ||
on: { | ||
before: ({ payload }) => { | ||
t.deepEqual(payload, { auth: 'Bearer 123123' }) | ||
payloadInSuccessEvent = payload | ||
}, | ||
@@ -73,2 +234,3 @@ }, | ||
await waitMs(600) | ||
t.deepEqual(payloadInSuccessEvent, { auth: 'Bearer 123123' }) | ||
// the local store SHOULD HAVE the applied defaults | ||
@@ -79,3 +241,3 @@ t.deepEqual(pokedexModule.data.get('1'), { ...pokedex(1), seen: true }) | ||
test('insert: can mutate payload', async (t) => { | ||
test('insert: can mutate payload (config in action)', async (t) => { | ||
function addSeen(payload: any) { | ||
@@ -90,5 +252,3 @@ if (!('seen' in payload)) return { ...payload, seen: true } | ||
const result = await pokedexModule.insert(payload, { | ||
modifyPayloadOn: { | ||
write: addSeen, | ||
}, | ||
modifyPayloadOn: { write: addSeen }, | ||
}) | ||
@@ -104,1 +264,27 @@ // the remote result SHOULD HAVE the applied defaults | ||
}) | ||
test('insert: can mutate payload (config in module - action from doc)', async (t) => { | ||
function addSeen(payload: any) { | ||
if (!('seen' in payload)) return { ...payload, seen: true } | ||
} | ||
// get resolves once all stores have given a response with data | ||
const { magnetar } = createMagnetarInstance() | ||
const pokedexModule = magnetar.collection('pokedex', { | ||
modifyPayloadOn: { write: addSeen }, | ||
}) | ||
try { | ||
const resultWithoutSeen = await magnetar.collection('pokedex').doc('10').insert(pokedex(10)) | ||
const resultWithSeen = await pokedexModule.doc('7').insert(pokedex(7)) | ||
// the remote result not from pokedexModule SHOULD NOT HAVE the applied defaults | ||
t.deepEqual(resultWithoutSeen.data, { ...pokedex(10) }) | ||
t.falsy('seen' in resultWithoutSeen) | ||
// the remote result SHOULD HAVE the applied defaults | ||
t.deepEqual(resultWithSeen.data, { ...pokedex(7), seen: true }) | ||
} catch (error) { | ||
t.fail(error) | ||
} | ||
// the local store result not from pokedexModule SHOULD NOT HAVE the applied defaults | ||
t.deepEqual(pokedexModule.data.get('7'), { ...pokedex(7), seen: true }) | ||
// the local store SHOULD HAVE the applied defaults | ||
t.deepEqual(pokedexModule.data.get('10'), { ...pokedex(10) }) | ||
}) |
@@ -6,2 +6,3 @@ import { O } from 'ts-toolbelt'; | ||
export { isDocModule, isCollectionModule } from './helpers/pathHelpers'; | ||
export type { GlobalConfig, ModuleConfig } from './types/config'; | ||
/** | ||
@@ -8,0 +9,0 @@ * This is the global Magnetar instance that is returned when instantiating with Magnetar() |
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
250171
5660
4
+ Addedfilter-anything@^2.2.1
+ Addedfilter-anything@2.2.4(transitive)