Skip to content

Commit 5d5f569

Browse files
authored
feat(core): full JSONMatch support w/ path helpers (#573)
1 parent c7ed1d0 commit 5d5f569

File tree

6 files changed

+145
-645
lines changed

6 files changed

+145
-645
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"@sanity/comlink": "^3.0.4",
6161
"@sanity/diff-match-patch": "^3.2.0",
6262
"@sanity/diff-patch": "^6.0.0",
63+
"@sanity/json-match": "^1.0.5",
6364
"@sanity/message-protocol": "^0.12.0",
6465
"@sanity/mutate": "^0.12.4",
6566
"@sanity/types": "^3.83.0",

packages/core/src/_exports/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export {
100100
type TransactionAcceptedEvent,
101101
type TransactionRevertedEvent,
102102
} from '../document/events'
103-
export {type JsonMatch, jsonMatch} from '../document/patchOperations'
103+
export {type JsonMatch} from '../document/patchOperations'
104104
export {type DocumentPermissionsResult, type PermissionDeniedReason} from '../document/permissions'
105105
export type {FavoriteStatusResponse} from '../favorites/favorites'
106106
export {getFavoritesState, resolveFavoritesState} from '../favorites/favorites'
@@ -137,4 +137,12 @@ export {getUsersState, loadMoreUsers, resolveUsers} from '../users/usersStore'
137137
export {type FetcherStore, type FetcherStoreState} from '../utils/createFetcherStore'
138138
export {createGroqSearchFilter} from '../utils/createGroqSearchFilter'
139139
export {CORE_SDK_VERSION} from '../version'
140+
export {
141+
getIndexForKey,
142+
getPathDepth,
143+
joinPaths,
144+
jsonMatch,
145+
slicePath,
146+
stringifyPath,
147+
} from '@sanity/json-match'
140148
export type {CurrentUser, Role, SanityDocument} from '@sanity/types'

packages/core/src/document/documentStore.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {type Action} from '@sanity/client'
22
import {getPublishedId} from '@sanity/client/csm'
3+
import {jsonMatch} from '@sanity/json-match'
34
import {type SanityDocument} from 'groq'
45
import {type ExprNode} from 'groq-js'
56
import {
@@ -36,7 +37,7 @@ import {type DocumentAction} from './actions'
3637
import {API_VERSION, INITIAL_OUTGOING_THROTTLE_TIME} from './documentConstants'
3738
import {type DocumentEvent, getDocumentEvents} from './events'
3839
import {listen, OutOfSyncError} from './listen'
39-
import {type JsonMatch, jsonMatch} from './patchOperations'
40+
import {type JsonMatch} from './patchOperations'
4041
import {calculatePermissions, createGrantsLookup, type DatasetAcl, type Grant} from './permissions'
4142
import {ActionError} from './processActions'
4243
import {
@@ -192,8 +193,11 @@ const _getDocumentState = bindActionByDataset(
192193
// wait for draft and published to be loaded before returning a value
193194
if (draft === undefined || published === undefined) return undefined
194195
const document = draft ?? published
195-
if (path) return jsonMatch(document, path).at(0)?.value
196-
return document
196+
if (!path) return document
197+
const result = jsonMatch(document, path).next()
198+
if (result.done) return undefined
199+
const {value} = result.value
200+
return value
197201
},
198202
onSubscribe: (context, options: DocumentOptions<string | undefined>) =>
199203
manageSubscriberIds(context, options.documentId),

0 commit comments

Comments
 (0)