-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement storeSplit helper function
- Loading branch information
Showing
7 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/plugin-firestore/test/internal/storeSplit.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { pokedex } from '@magnetarjs/test-utils' | ||
import { storeSplit } from '@magnetarjs/utils' | ||
import { merge } from 'merge-anything' | ||
import { assert, test } from 'vitest' | ||
import { createMagnetarInstance } from '../helpers/createMagnetarInstance.js' | ||
import { firestoreDeepEqual } from '../helpers/firestoreDeepEqual.js' | ||
|
||
{ | ||
const testName = 'write: merge (document) with storeSplit' | ||
test(testName, async () => { | ||
const { pokedexModule } = await createMagnetarInstance(testName, { | ||
insertDocs: { 'pokedex/1': pokedex(1) }, | ||
}) | ||
|
||
const doc = pokedexModule.doc('1') | ||
assert.deepEqual(doc.data, pokedex(1)) | ||
await firestoreDeepEqual(testName, 'pokedex/1', pokedex(1)) | ||
|
||
try { | ||
await doc.merge( | ||
{ base: { HP: storeSplit({ cache: 9000, remote: '9000' }) } }, | ||
{ syncDebounceMs: 1 }, | ||
) | ||
} catch (error) { | ||
assert.fail(JSON.stringify(error)) | ||
} | ||
|
||
const mergedResult = merge(pokedex(1), { base: { HP: 9000 } }) | ||
|
||
assert.deepEqual(pokedexModule.data.get('1'), mergedResult) | ||
assert.deepEqual(doc.data, mergedResult) | ||
|
||
await firestoreDeepEqual(testName, 'pokedex/1', merge(pokedex(1), { base: { HP: '9000' } })) | ||
|
||
const fetchedDoc = await doc.fetch({ force: true }) | ||
assert.deepEqual(fetchedDoc?.base.HP as any, '9000') | ||
assert.deepEqual(doc.data?.base.HP as any, '9000') | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { isAnyObject } from 'is-what' | ||
|
||
export const storeSplitSymbol = Symbol('storeSplit') | ||
|
||
/** | ||
* A storeSplit function allows you to apply a different payload between your cache store vs your other stores. | ||
* | ||
* It will let TypeScript know that you're trying to apply the type of whatever you pass to the `cache` key, even though your other stores might receive other values. | ||
* | ||
* ### Example Use Case | ||
* ```ts | ||
* import { storeSplit } from '@magnetarjs/utils' | ||
* | ||
* magnetar.collection('user').doc('1').merge({ | ||
* name: 'updated name', | ||
* // ... | ||
* dateUpdated: storeSplit({ | ||
* cache: new Date(), | ||
* remote: serverTimestamp(), | ||
* }) | ||
* }) | ||
* ``` | ||
*/ | ||
export function storeSplit<Payload extends { cache: any; [key: string]: any }>( | ||
payload: Payload, | ||
): Payload['cache'] { | ||
return { | ||
storeSplitSymbol, | ||
storePayloadDic: payload, | ||
} as unknown as Payload['cache'] | ||
} | ||
|
||
export function isStoreSplit(payload: unknown): payload is { | ||
storeSplitSymbol: symbol | ||
storePayloadDic: { cache: any; [key: string]: any } | ||
} { | ||
return isAnyObject(payload) && payload['storeSplitSymbol'] === storeSplitSymbol | ||
} |