@stonecrop/stonecrop 0.31.0 → 0.33.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/stonecrop.js +3548 -2740
- package/dist/stonecrop.js.map +1 -1
- package/dist/tsdoc-metadata.json +1 -1
- package/package.json +33 -27
- package/dist/src/client-action.d.ts +0 -31
- package/dist/src/client-action.d.ts.map +0 -1
- package/dist/src/client-action.js +0 -169
- package/dist/src/client-handler.d.ts +0 -43
- package/dist/src/client-handler.d.ts.map +0 -1
- package/dist/src/client-handler.js +0 -42
- package/dist/src/composables/lazy-link.d.ts +0 -25
- package/dist/src/composables/lazy-link.d.ts.map +0 -1
- package/dist/src/composables/lazy-link.js +0 -124
- package/dist/src/composables/operation-log.d.ts +0 -136
- package/dist/src/composables/operation-log.d.ts.map +0 -1
- package/dist/src/composables/operation-log.js +0 -224
- package/dist/src/composables/stonecrop.d.ts +0 -37
- package/dist/src/composables/stonecrop.d.ts.map +0 -1
- package/dist/src/composables/stonecrop.js +0 -433
- package/dist/src/doctype.d.ts +0 -243
- package/dist/src/doctype.d.ts.map +0 -1
- package/dist/src/doctype.js +0 -325
- package/dist/src/draft.d.ts +0 -35
- package/dist/src/draft.d.ts.map +0 -1
- package/dist/src/draft.js +0 -36
- package/dist/src/exceptions.d.ts +0 -14
- package/dist/src/exceptions.d.ts.map +0 -1
- package/dist/src/exceptions.js +0 -16
- package/dist/src/field-triggers.d.ts +0 -184
- package/dist/src/field-triggers.d.ts.map +0 -1
- package/dist/src/field-triggers.js +0 -577
- package/dist/src/index.d.ts +0 -25
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/index.js +0 -39
- package/dist/src/plugins/index.d.ts +0 -35
- package/dist/src/plugins/index.d.ts.map +0 -1
- package/dist/src/plugins/index.js +0 -98
- package/dist/src/registry.d.ts +0 -161
- package/dist/src/registry.d.ts.map +0 -1
- package/dist/src/registry.js +0 -384
- package/dist/src/schema-validator.d.ts +0 -78
- package/dist/src/schema-validator.d.ts.map +0 -1
- package/dist/src/schema-validator.js +0 -398
- package/dist/src/stonecrop.d.ts +0 -539
- package/dist/src/stonecrop.d.ts.map +0 -1
- package/dist/src/stonecrop.js +0 -610
- package/dist/src/stores/hst.d.ts +0 -92
- package/dist/src/stores/hst.d.ts.map +0 -1
- package/dist/src/stores/hst.js +0 -487
- package/dist/src/stores/index.d.ts +0 -9
- package/dist/src/stores/index.d.ts.map +0 -1
- package/dist/src/stores/index.js +0 -13
- package/dist/src/stores/operation-log.d.ts +0 -262
- package/dist/src/stores/operation-log.d.ts.map +0 -1
- package/dist/src/stores/operation-log.js +0 -571
- package/dist/src/stores/validation.d.ts +0 -83
- package/dist/src/stores/validation.d.ts.map +0 -1
- package/dist/src/stores/validation.js +0 -104
- package/dist/src/types/client-action.d.ts +0 -121
- package/dist/src/types/client-action.d.ts.map +0 -1
- package/dist/src/types/client-action.js +0 -0
- package/dist/src/types/composable.d.ts +0 -268
- package/dist/src/types/composable.d.ts.map +0 -1
- package/dist/src/types/composable.js +0 -0
- package/dist/src/types/doctype.d.ts +0 -34
- package/dist/src/types/doctype.d.ts.map +0 -1
- package/dist/src/types/doctype.js +0 -0
- package/dist/src/types/field-triggers.d.ts +0 -186
- package/dist/src/types/field-triggers.d.ts.map +0 -1
- package/dist/src/types/field-triggers.js +0 -4
- package/dist/src/types/hst.d.ts +0 -70
- package/dist/src/types/hst.d.ts.map +0 -1
- package/dist/src/types/hst.js +0 -0
- package/dist/src/types/index.d.ts +0 -11
- package/dist/src/types/index.d.ts.map +0 -1
- package/dist/src/types/index.js +0 -11
- package/dist/src/types/operation-log.d.ts +0 -163
- package/dist/src/types/operation-log.d.ts.map +0 -1
- package/dist/src/types/operation-log.js +0 -0
- package/dist/src/types/plugin.d.ts +0 -37
- package/dist/src/types/plugin.d.ts.map +0 -1
- package/dist/src/types/plugin.js +0 -0
- package/dist/src/types/registry.d.ts +0 -11
- package/dist/src/types/registry.d.ts.map +0 -1
- package/dist/src/types/registry.js +0 -0
- package/dist/src/types/schema-validator.d.ts +0 -66
- package/dist/src/types/schema-validator.d.ts.map +0 -1
- package/dist/src/types/schema-validator.js +0 -13
- package/dist/src/types/stonecrop.d.ts +0 -32
- package/dist/src/types/stonecrop.d.ts.map +0 -1
- package/dist/src/types/stonecrop.js +0 -0
- package/dist/stonecrop.tsbuildinfo +0 -1
- package/src/client-action.ts +0 -207
- package/src/client-handler.ts +0 -55
- package/src/composables/lazy-link.ts +0 -145
- package/src/composables/operation-log.ts +0 -257
- package/src/composables/stonecrop.ts +0 -556
- package/src/doctype.ts +0 -360
- package/src/draft.ts +0 -38
- package/src/exceptions.ts +0 -16
- package/src/field-triggers.ts +0 -686
- package/src/index.ts +0 -81
- package/src/plugins/index.ts +0 -113
- package/src/registry.ts +0 -433
- package/src/schema-validator.ts +0 -476
- package/src/stonecrop.ts +0 -721
- package/src/stores/hst.ts +0 -626
- package/src/stores/index.ts +0 -21
- package/src/stores/operation-log.ts +0 -675
- package/src/stores/validation.ts +0 -138
- package/src/types/client-action.ts +0 -121
- package/src/types/composable.ts +0 -288
- package/src/types/doctype.ts +0 -35
- package/src/types/field-triggers.ts +0 -201
- package/src/types/hst.ts +0 -77
- package/src/types/index.ts +0 -11
- package/src/types/operation-log.ts +0 -202
- package/src/types/plugin.ts +0 -38
- package/src/types/registry.ts +0 -10
- package/src/types/schema-validator.ts +0 -69
- package/src/types/stonecrop.ts +0 -33
package/src/stonecrop.ts
DELETED
|
@@ -1,721 +0,0 @@
|
|
|
1
|
-
import type { DataClient, GetRecordOptions, GetRecordsOptions, GetRecordsResult } from '@stonecrop/schema'
|
|
2
|
-
import { reactive } from 'vue'
|
|
3
|
-
|
|
4
|
-
import Doctype from './doctype'
|
|
5
|
-
import { isDraftRecordId } from './draft'
|
|
6
|
-
import Registry from './registry'
|
|
7
|
-
import { createHST, type HSTNode } from './stores/hst'
|
|
8
|
-
import { useOperationLogStore } from './stores/operation-log'
|
|
9
|
-
import type { OperationLogConfig } from './types/operation-log'
|
|
10
|
-
import type { RouteContext } from './types/registry'
|
|
11
|
-
import type { PageInfo, StonecropOptions } from './types/stonecrop'
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Main Stonecrop class with HST integration and built-in Operation Log
|
|
15
|
-
* @public
|
|
16
|
-
*/
|
|
17
|
-
export class Stonecrop {
|
|
18
|
-
/**
|
|
19
|
-
* Singleton instance of Stonecrop. Only one Stonecrop instance can exist
|
|
20
|
-
* per application, ensuring consistent HST state and registry access.
|
|
21
|
-
* Subsequent constructor calls return this instance instead of creating new ones.
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
static _root: Stonecrop
|
|
25
|
-
|
|
26
|
-
/** The HST store instance for reactive state management */
|
|
27
|
-
private hstStore!: HSTNode
|
|
28
|
-
private _operationLogStore?: ReturnType<typeof useOperationLogStore>
|
|
29
|
-
private _operationLogConfig?: Partial<OperationLogConfig>
|
|
30
|
-
private _client?: DataClient
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Per-doctype page info from the last list read, keyed by slug. Reactive so views tracking
|
|
34
|
-
* it update on fetch. Deliberately not in HST — see {@link Stonecrop.getRecords}.
|
|
35
|
-
*/
|
|
36
|
-
private pageInfo: Record<string, PageInfo> = reactive({})
|
|
37
|
-
|
|
38
|
-
/** The registry instance containing all doctype definitions */
|
|
39
|
-
readonly registry!: Registry
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Creates a new Stonecrop instance with HST integration (singleton pattern)
|
|
43
|
-
* @param registry - The Registry instance containing doctype definitions
|
|
44
|
-
* @param operationLogConfig - Optional configuration for the operation log
|
|
45
|
-
* @param options - Options including the data client (can be set later via setClient)
|
|
46
|
-
*/
|
|
47
|
-
constructor(registry: Registry, operationLogConfig?: Partial<OperationLogConfig>, options?: StonecropOptions) {
|
|
48
|
-
if (Stonecrop._root) {
|
|
49
|
-
return Stonecrop._root
|
|
50
|
-
}
|
|
51
|
-
Stonecrop._root = this
|
|
52
|
-
|
|
53
|
-
this.registry = registry
|
|
54
|
-
|
|
55
|
-
// Store config for lazy initialization
|
|
56
|
-
this._operationLogConfig = operationLogConfig
|
|
57
|
-
|
|
58
|
-
// Store data client (can be set later via setClient)
|
|
59
|
-
this._client = options?.client
|
|
60
|
-
|
|
61
|
-
// Initialize HST store with auto-sync to Registry
|
|
62
|
-
this.initializeHSTStore()
|
|
63
|
-
this.setupRegistrySync()
|
|
64
|
-
return this
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Set the data client for fetching doctype metadata and records.
|
|
69
|
-
* Use this for deferred configuration in Nuxt/Vue plugin setups.
|
|
70
|
-
*
|
|
71
|
-
* @param client - DataClient implementation (e.g., StonecropClient from \@stonecrop/graphql-client)
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```ts
|
|
75
|
-
* const { setClient } = useStonecropRegistry()
|
|
76
|
-
* const client = new StonecropClient({ endpoint: '/graphql' })
|
|
77
|
-
* setClient(client)
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
setClient(client: DataClient): void {
|
|
81
|
-
this._client = client
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Get the current data client
|
|
86
|
-
* @returns The DataClient instance or undefined if not set
|
|
87
|
-
*/
|
|
88
|
-
getClient(): DataClient | undefined {
|
|
89
|
-
return this._client
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Get the operation log store (lazy initialization)
|
|
94
|
-
* @internal
|
|
95
|
-
*/
|
|
96
|
-
getOperationLogStore() {
|
|
97
|
-
if (!this._operationLogStore) {
|
|
98
|
-
this._operationLogStore = useOperationLogStore()
|
|
99
|
-
if (this._operationLogConfig) {
|
|
100
|
-
this._operationLogStore.configure(this._operationLogConfig)
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return this._operationLogStore
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Initialize the HST store structure
|
|
108
|
-
*/
|
|
109
|
-
private initializeHSTStore(): void {
|
|
110
|
-
const initialStoreStructure: Record<string, any> = {}
|
|
111
|
-
|
|
112
|
-
// Auto-populate from existing Registry doctypes
|
|
113
|
-
Object.keys(this.registry.registry).forEach(doctypeSlug => {
|
|
114
|
-
initialStoreStructure[doctypeSlug] = {}
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
// Wrap the store in Vue's reactive() for automatic change detection
|
|
118
|
-
// This enables Vue computed properties to track HST store changes
|
|
119
|
-
this.hstStore = createHST(reactive(initialStoreStructure), 'StonecropStore')
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Setup automatic sync with Registry when doctypes are added
|
|
124
|
-
*/
|
|
125
|
-
private setupRegistrySync(): void {
|
|
126
|
-
// Extend Registry.addDoctype to auto-create HST store sections
|
|
127
|
-
const originalAddDoctype = this.registry.addDoctype.bind(this.registry)
|
|
128
|
-
|
|
129
|
-
this.registry.addDoctype = (doctype: Doctype) => {
|
|
130
|
-
// Call original method
|
|
131
|
-
originalAddDoctype(doctype)
|
|
132
|
-
|
|
133
|
-
// Auto-create HST store section for new doctype
|
|
134
|
-
if (!this.hstStore.has(doctype.slug)) {
|
|
135
|
-
this.hstStore.set(doctype.slug, {})
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Get records hash for a doctype
|
|
142
|
-
* @param doctype - The doctype to get records for
|
|
143
|
-
* @returns HST node containing records hash
|
|
144
|
-
*/
|
|
145
|
-
records(doctype: string | Doctype): HSTNode {
|
|
146
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
147
|
-
this.ensureDoctypeExists(slug)
|
|
148
|
-
return this.hstStore.getNode(slug)
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Add a record to the store
|
|
153
|
-
* @param doctype - The doctype
|
|
154
|
-
* @param recordId - The record ID
|
|
155
|
-
* @param recordData - The record data
|
|
156
|
-
*/
|
|
157
|
-
addRecord(doctype: string | Doctype, recordId: string, recordData: any): void {
|
|
158
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
159
|
-
|
|
160
|
-
this.ensureDoctypeExists(slug)
|
|
161
|
-
|
|
162
|
-
// Store raw record data - let HST handle wrapping with proper hierarchy
|
|
163
|
-
this.hstStore.set(`${slug}.${recordId}`, recordData)
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Get a specific record
|
|
168
|
-
* @param doctype - The doctype
|
|
169
|
-
* @param recordId - The record ID
|
|
170
|
-
* @returns HST node for the record or undefined
|
|
171
|
-
*/
|
|
172
|
-
getRecordById(doctype: string | Doctype, recordId: string): HSTNode | undefined {
|
|
173
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
174
|
-
this.ensureDoctypeExists(slug)
|
|
175
|
-
|
|
176
|
-
// First check if the record exists
|
|
177
|
-
const recordExists = this.hstStore.has(`${slug}.${recordId}`)
|
|
178
|
-
if (!recordExists) {
|
|
179
|
-
return undefined
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
// Check if the actual value is undefined (i.e., record was removed)
|
|
183
|
-
const recordValue = this.hstStore.get(`${slug}.${recordId}`)
|
|
184
|
-
if (recordValue === undefined) {
|
|
185
|
-
return undefined
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
// Use getNode to get the properly wrapped HST node with correct ancestor relationships
|
|
189
|
-
return this.hstStore.getNode(`${slug}.${recordId}`)
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
/**
|
|
193
|
-
* Remove a record from the store
|
|
194
|
-
* @param doctype - The doctype
|
|
195
|
-
* @param recordId - The record ID
|
|
196
|
-
*/
|
|
197
|
-
removeRecord(doctype: string | Doctype, recordId: string): void {
|
|
198
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
199
|
-
this.ensureDoctypeExists(slug)
|
|
200
|
-
|
|
201
|
-
// Remove the specific record directly by setting to undefined
|
|
202
|
-
if (this.hstStore.has(`${slug}.${recordId}`)) {
|
|
203
|
-
this.hstStore.set(`${slug}.${recordId}`, undefined)
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
/**
|
|
208
|
-
* Get all record IDs for a doctype
|
|
209
|
-
* @param doctype - The doctype
|
|
210
|
-
* @returns Array of record IDs
|
|
211
|
-
*/
|
|
212
|
-
getRecordIds(doctype: string | Doctype): string[] {
|
|
213
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
214
|
-
this.ensureDoctypeExists(slug)
|
|
215
|
-
|
|
216
|
-
const doctypeNode = this.hstStore.get(slug)
|
|
217
|
-
if (!doctypeNode || typeof doctypeNode !== 'object') {
|
|
218
|
-
return []
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return Object.keys(doctypeNode).filter(key => doctypeNode[key] !== undefined)
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Clear all records for a doctype
|
|
226
|
-
* @param doctype - The doctype
|
|
227
|
-
*/
|
|
228
|
-
clearRecords(doctype: string | Doctype): void {
|
|
229
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
230
|
-
this.ensureDoctypeExists(slug)
|
|
231
|
-
|
|
232
|
-
// Get all record IDs and remove them
|
|
233
|
-
const recordIds = this.getRecordIds(slug)
|
|
234
|
-
recordIds.forEach(recordId => {
|
|
235
|
-
this.hstStore.set(`${slug}.${recordId}`, undefined)
|
|
236
|
-
})
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Setup method for doctype initialization
|
|
241
|
-
* @param doctype - The doctype to setup
|
|
242
|
-
*/
|
|
243
|
-
setup(doctype: Doctype): void {
|
|
244
|
-
// Ensure doctype exists in store
|
|
245
|
-
this.ensureDoctypeExists(doctype.slug)
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Get the effective blockWorkflows value for a link.
|
|
250
|
-
* Returns true if blockWorkflows is explicitly true, or if it's absent and fetch method is 'sync'.
|
|
251
|
-
* @param link - The link declaration
|
|
252
|
-
* @returns Whether workflows should be blocked until this link is loaded
|
|
253
|
-
*/
|
|
254
|
-
private getEffectiveBlockWorkflows(link: { blockWorkflows?: boolean; fetch?: { method?: string } }): boolean {
|
|
255
|
-
if (link.blockWorkflows !== undefined) {
|
|
256
|
-
return link.blockWorkflows
|
|
257
|
-
}
|
|
258
|
-
// TODO: For custom fetch handlers, this returns false (not blocking), but the custom handler
|
|
259
|
-
// may still be invoked by useLazyLink. Future: custom handlers should be able to declare they
|
|
260
|
-
// satisfy blockWorkflows, or validation should reject custom + blockWorkflows: true.
|
|
261
|
-
return link.fetch?.method === 'sync'
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
/**
|
|
265
|
-
* Check if workflow actions are ready to run (all required link data is loaded).
|
|
266
|
-
* A link's data is considered loaded if it exists in HST at `slug.recordId.linkname`.
|
|
267
|
-
* @param doctype - The doctype to check
|
|
268
|
-
* @param recordId - The record ID
|
|
269
|
-
* @returns Object with `ready: true` if all blocked links are loaded, or `ready: false` with `blockedLinks` array
|
|
270
|
-
*/
|
|
271
|
-
isWorkflowReady(doctype: Doctype, recordId: string): { ready: boolean; blockedLinks?: string[] } {
|
|
272
|
-
// New records don't block workflows - they haven't been saved yet
|
|
273
|
-
if (isDraftRecordId(recordId)) {
|
|
274
|
-
return { ready: true }
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const links = this.registry.getDescendantLinks(doctype.slug)
|
|
278
|
-
const blockedLinks: string[] = []
|
|
279
|
-
|
|
280
|
-
for (const link of links) {
|
|
281
|
-
if (this.getEffectiveBlockWorkflows(link)) {
|
|
282
|
-
const linkPath = `${doctype.slug}.${recordId}.${link.fieldname}`
|
|
283
|
-
if (!this.hstStore.has(linkPath)) {
|
|
284
|
-
blockedLinks.push(link.fieldname)
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
if (blockedLinks.length > 0) {
|
|
290
|
-
return { ready: false, blockedLinks }
|
|
291
|
-
}
|
|
292
|
-
return { ready: true }
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
/**
|
|
296
|
-
* Fetch a doctype's records from the server and store them in HST.
|
|
297
|
-
*
|
|
298
|
-
* This is the one read path for a list. Every caller shares its keying rule, so a row is
|
|
299
|
-
* always stored under the identity its Edit link will later ask for.
|
|
300
|
-
*
|
|
301
|
-
* Deliberately unguarded, unlike {@link Stonecrop.getRecord}: a list is a view of data that
|
|
302
|
-
* changes, so revisiting one must re-read it rather than serve whatever HST happens to hold.
|
|
303
|
-
*
|
|
304
|
-
* `options` is forwarded to the client untouched — no row limit is invented here, because
|
|
305
|
-
* nothing on this side of the wire knows what is safe for an arbitrary backend. A caller that
|
|
306
|
-
* passes none gets whatever the server considers a reasonable page.
|
|
307
|
-
*
|
|
308
|
-
* `options.offset === 0` (or omitted) replaces the doctype's HST records; `offset > 0` appends.
|
|
309
|
-
*
|
|
310
|
-
* @param doctype - The doctype
|
|
311
|
-
* @param options - Query options (filters, orderBy, limit, offset), forwarded to the client
|
|
312
|
-
* @throws Error if no data client has been configured
|
|
313
|
-
* @public
|
|
314
|
-
*/
|
|
315
|
-
async getRecords(doctype: Doctype, options?: GetRecordsOptions): Promise<GetRecordsResult> {
|
|
316
|
-
if (!this._client) {
|
|
317
|
-
throw new Error(
|
|
318
|
-
'No data client configured. Call setClient() with a DataClient implementation ' +
|
|
319
|
-
'(e.g., StonecropClient from @stonecrop/graphql-client) before fetching records.'
|
|
320
|
-
)
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
const result = await this._client.getRecords(doctype, options)
|
|
324
|
-
const records = result.data
|
|
325
|
-
const offset = options?.offset ?? 0
|
|
326
|
-
|
|
327
|
-
// Page info is kept beside HST, not in it. `getRecordIds` reads the raw keys under a
|
|
328
|
-
// doctype node, so anything filed there becomes a phantom record — the same hazard that
|
|
329
|
-
// ruled out seeding a draft node.
|
|
330
|
-
const pageEntry: PageInfo = {
|
|
331
|
-
hasMore: result.hasMore,
|
|
332
|
-
offset,
|
|
333
|
-
limit: records.length,
|
|
334
|
-
}
|
|
335
|
-
if (result.count != null) {
|
|
336
|
-
pageEntry.count = result.count
|
|
337
|
-
}
|
|
338
|
-
this.pageInfo[doctype.slug] = pageEntry
|
|
339
|
-
|
|
340
|
-
// offset === 0 replaces the doctype's HST records; offset > 0 appends. A failed fetch
|
|
341
|
-
// must not clear — that happens only after a successful response lands above.
|
|
342
|
-
if (offset === 0) {
|
|
343
|
-
this.clearRecords(doctype)
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
// Key each record by its declared primary key, falling back to `id`. This used to read
|
|
347
|
-
// `record.id` only, which silently dropped every row of a natural-keyed doctype (no `id`
|
|
348
|
-
// column at all) — the records never entered HST, so the list rendered empty and no
|
|
349
|
-
// downstream id-resolution could recover them.
|
|
350
|
-
records.forEach(record => {
|
|
351
|
-
const recordId = doctype.getRecordId(record)
|
|
352
|
-
if (recordId !== undefined) {
|
|
353
|
-
this.addRecord(doctype, recordId, record)
|
|
354
|
-
}
|
|
355
|
-
})
|
|
356
|
-
|
|
357
|
-
return result
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
/**
|
|
361
|
-
* What the last {@link Stonecrop.getRecords} for this doctype returned about the wider set.
|
|
362
|
-
*
|
|
363
|
-
* Answers the question HST cannot: HST holds what was fetched, so counting its keys reports
|
|
364
|
-
* how much has been seen, never how much exists. Reactive, so a view reading it re-renders
|
|
365
|
-
* when a fetch lands.
|
|
366
|
-
*
|
|
367
|
-
* @param doctype - The doctype slug string or Doctype object
|
|
368
|
-
* @returns Page info, or undefined if this doctype's records have not been fetched
|
|
369
|
-
* @public
|
|
370
|
-
*/
|
|
371
|
-
getPageInfo(doctype: string | Doctype): PageInfo | undefined {
|
|
372
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
373
|
-
return this.pageInfo[slug]
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Fetch a single record from the server and store it in HST.
|
|
378
|
-
*
|
|
379
|
-
* This is the one read path for a record, and it owns the whole job: deciding whether a fetch
|
|
380
|
-
* is warranted, performing it, and writing the result back under the doctype's declared
|
|
381
|
-
* identity. Callers ask for a record and get one; they do not re-derive when to ask.
|
|
382
|
-
*
|
|
383
|
-
* Two cases return without touching the network, because in both the answer is already known:
|
|
384
|
-
* a draft does not exist on the server yet, and a record already in HST has been read.
|
|
385
|
-
* Refetching the latter would also discard unsaved edits sitting in the store.
|
|
386
|
-
*
|
|
387
|
-
* @param doctype - The doctype slug string or Doctype object
|
|
388
|
-
* @param recordId - The record ID
|
|
389
|
-
* @param options - Query options (includeNested, maxDepth), forwarded to the client
|
|
390
|
-
* @throws Error if no data client has been configured
|
|
391
|
-
* @throws Error if a slug string is given and no matching doctype is found in the registry
|
|
392
|
-
* @public
|
|
393
|
-
*/
|
|
394
|
-
async getRecord(doctype: string | Doctype, recordId: string, options?: GetRecordOptions): Promise<void> {
|
|
395
|
-
if (!this._client) {
|
|
396
|
-
throw new Error(
|
|
397
|
-
'No data client configured. Call setClient() with a DataClient implementation ' +
|
|
398
|
-
'(e.g., StonecropClient from @stonecrop/graphql-client) before fetching records.'
|
|
399
|
-
)
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
const resolved = typeof doctype === 'string' ? this.registry.getDoctype(doctype) : doctype
|
|
403
|
-
if (!resolved) {
|
|
404
|
-
throw new Error(`Doctype not found: ${typeof doctype === 'string' ? doctype : doctype.slug}`)
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
// An unsaved draft has no server record, so a fetch could only ever miss. This guard used
|
|
408
|
-
// to live in every host, which is how the private draft-id scheme leaked into all of them.
|
|
409
|
-
if (isDraftRecordId(recordId)) {
|
|
410
|
-
return
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
if (this.getRecordById(resolved.slug, recordId)) {
|
|
414
|
-
return
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
const result = await this._client.getRecord(resolved, recordId, options)
|
|
418
|
-
|
|
419
|
-
if (result?.record) {
|
|
420
|
-
this.addRecord(resolved, recordId, result.record)
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* Dispatch an action to the server via the configured data client, and file the record it
|
|
426
|
-
* returns into HST under the identity the *server* settled on.
|
|
427
|
-
*
|
|
428
|
-
* The write is the point. For a created record the settled identity is never the one that was
|
|
429
|
-
* dispatched, so a caller that stores the result under the id it sent files the record under a
|
|
430
|
-
* key nothing can fetch and leaves the next save creating a second one. Every host that
|
|
431
|
-
* hand-rolled this got it wrong the same way, so it stops being the caller's job — this is the
|
|
432
|
-
* write-side twin of {@link Stonecrop.getRecords}, which keys reads by the same rule.
|
|
433
|
-
*
|
|
434
|
-
* Two things are deliberately NOT done here, because both need the id that was dispatched and
|
|
435
|
-
* that lives inside `args` — an opaque array whose shape is a convention between a host's
|
|
436
|
-
* client and its server handlers, not something this layer may parse. Dropping the stale key
|
|
437
|
-
* and moving the route therefore stay with `useClientAction`, which knows both ids.
|
|
438
|
-
*
|
|
439
|
-
* A result that states no identity of its own — a `{ state: 'APPROVED' }` outcome — is left
|
|
440
|
-
* alone rather than guessed at, for the same reason `settledRecordId` is strict: a partial
|
|
441
|
-
* record must not be able to look like a rename.
|
|
442
|
-
*
|
|
443
|
-
* @param doctype - The doctype
|
|
444
|
-
* @param action - Action name to execute (e.g., 'SUBMIT', 'APPROVE', 'save')
|
|
445
|
-
* @param args - Action arguments (typically record ID and/or form data)
|
|
446
|
-
* @returns Action result with success status, response data, and any error
|
|
447
|
-
* @throws Error if no data client has been configured
|
|
448
|
-
*/
|
|
449
|
-
async dispatchAction(
|
|
450
|
-
doctype: Doctype,
|
|
451
|
-
action: string,
|
|
452
|
-
args?: unknown[]
|
|
453
|
-
): Promise<{ success: boolean; data: unknown; error: string | null }> {
|
|
454
|
-
if (!this._client) {
|
|
455
|
-
throw new Error(
|
|
456
|
-
'No data client configured. Call setClient() with a DataClient implementation ' +
|
|
457
|
-
'(e.g., StonecropClient from @stonecrop/graphql-client) before dispatching actions.'
|
|
458
|
-
)
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
const result = await this._client.runAction(doctype, action, args)
|
|
462
|
-
|
|
463
|
-
if (result.success && result.data && typeof result.data === 'object' && !Array.isArray(result.data)) {
|
|
464
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- the guard above confirms a non-null, non-array object; the action result payload is an opaque JSON scalar
|
|
465
|
-
const record = result.data as Record<string, unknown>
|
|
466
|
-
// Require the declared key to be *present* before trusting `getRecordId`, which falls
|
|
467
|
-
// back to `id`. Without that, a handler returning `{ id, total }` for a natural-keyed
|
|
468
|
-
// doctype would relocate the record to a key the adapter cannot look up.
|
|
469
|
-
if (record[doctype.recordIdField] !== undefined) {
|
|
470
|
-
const settledId = doctype.getRecordId(record)
|
|
471
|
-
if (settledId !== undefined) {
|
|
472
|
-
this.addRecord(doctype, settledId, record)
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
return result
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
/**
|
|
481
|
-
* Ensure doctype section exists in HST store
|
|
482
|
-
* @param slug - The doctype slug
|
|
483
|
-
*/
|
|
484
|
-
private ensureDoctypeExists(slug: string): void {
|
|
485
|
-
if (!this.hstStore.has(slug)) {
|
|
486
|
-
this.hstStore.set(slug, {})
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
/**
|
|
491
|
-
* Get doctype metadata from the registry
|
|
492
|
-
* @param context - The route context
|
|
493
|
-
* @returns The doctype metadata
|
|
494
|
-
*/
|
|
495
|
-
async getMeta(context: RouteContext): Promise<any> {
|
|
496
|
-
if (!this.registry.getMeta) {
|
|
497
|
-
throw new Error('No getMeta function provided to Registry')
|
|
498
|
-
}
|
|
499
|
-
return await this.registry.getMeta(context)
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
/**
|
|
503
|
-
* Get the root HST store node for advanced usage
|
|
504
|
-
* @returns Root HST node
|
|
505
|
-
*/
|
|
506
|
-
getStore(): HSTNode {
|
|
507
|
-
return this.hstStore
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
/**
|
|
511
|
-
* Determine the current workflow state for a record.
|
|
512
|
-
*
|
|
513
|
-
* Reads the record's `status` field from the HST store. If the field is absent or
|
|
514
|
-
* empty the doctype's declared `workflow.initial` state is used as the fallback,
|
|
515
|
-
* giving callers a reliable state name without having to duplicate that logic.
|
|
516
|
-
*
|
|
517
|
-
* @param doctype - The doctype slug or Doctype instance
|
|
518
|
-
* @param recordId - The record identifier
|
|
519
|
-
* @returns The current state name, or an empty string if the doctype has no workflow
|
|
520
|
-
*
|
|
521
|
-
* @public
|
|
522
|
-
*/
|
|
523
|
-
getRecordState(doctype: string | Doctype, recordId: string): string {
|
|
524
|
-
const slug = typeof doctype === 'string' ? doctype : doctype.slug
|
|
525
|
-
const meta = this.registry.getDoctype(slug)
|
|
526
|
-
if (!meta?.workflow) return ''
|
|
527
|
-
|
|
528
|
-
const record = this.getRecordById(slug, recordId)
|
|
529
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- HSTNode.get returns any; status is always string in workflow records
|
|
530
|
-
const status = record?.get('status') as string | undefined
|
|
531
|
-
|
|
532
|
-
// Handle both XState format and WorkflowMeta format
|
|
533
|
-
const workflow = meta.workflow
|
|
534
|
-
let initialState: string
|
|
535
|
-
|
|
536
|
-
if (Array.isArray(workflow.states)) {
|
|
537
|
-
// WorkflowMeta format: states is a string array
|
|
538
|
-
initialState = workflow.states[0] ?? ''
|
|
539
|
-
} else {
|
|
540
|
-
// XState format: states is an object, use initial or first key
|
|
541
|
-
// oxlint-disable typescript/no-unsafe-type-assertion -- XState MachineConfig has `initial` but type union doesn't expose it; runtime-guarded by typeof check
|
|
542
|
-
initialState =
|
|
543
|
-
typeof (workflow as { initial?: unknown }).initial === 'string'
|
|
544
|
-
? (workflow as { initial: string }).initial
|
|
545
|
-
: (Object.keys(workflow.states ?? {})[0] ?? '')
|
|
546
|
-
// oxlint-enable typescript/no-unsafe-type-assertion
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
return status || initialState
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
* Collect a record payload with all nested doctype fields from HST
|
|
554
|
-
* @param doctype - The doctype metadata
|
|
555
|
-
* @param recordId - The record ID to collect
|
|
556
|
-
* @returns The complete record payload ready for API submission
|
|
557
|
-
* @public
|
|
558
|
-
*/
|
|
559
|
-
collectRecordPayload(doctype: Doctype, recordId: string): Record<string, any> {
|
|
560
|
-
const recordPath = `${doctype.slug}.${recordId}`
|
|
561
|
-
const recordData = this.hstStore.get(recordPath) || {}
|
|
562
|
-
|
|
563
|
-
const payload: Record<string, any> = { ...recordData }
|
|
564
|
-
|
|
565
|
-
// Collect nested data from links
|
|
566
|
-
if (doctype.links) {
|
|
567
|
-
for (const [fieldname, link] of Object.entries(doctype.links)) {
|
|
568
|
-
const fieldPath = `${recordPath}.${fieldname}`
|
|
569
|
-
const isMany = link.cardinality === 'noneOrMany' || link.cardinality === 'atLeastOne'
|
|
570
|
-
|
|
571
|
-
if (isMany) {
|
|
572
|
-
const arrayData = this.hstStore.get(fieldPath)
|
|
573
|
-
if (Array.isArray(arrayData)) {
|
|
574
|
-
payload[fieldname] = arrayData
|
|
575
|
-
}
|
|
576
|
-
} else {
|
|
577
|
-
const targetDoctype = this.registry.getDoctype(link.target)
|
|
578
|
-
if (targetDoctype?.links) {
|
|
579
|
-
payload[fieldname] = this.collectNestedData(fieldPath, targetDoctype)
|
|
580
|
-
} else {
|
|
581
|
-
payload[fieldname] = this.hstStore.get(fieldPath) || {}
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
// The store's shape, inline links included. Reducing them to what a column takes is the
|
|
588
|
-
// adapter's job at dispatch — storage is behind the API.
|
|
589
|
-
return payload
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
/**
|
|
593
|
-
* Scaffold empty descendant records from defaults for all descendant links.
|
|
594
|
-
*
|
|
595
|
-
* Initializes all scalar and link fields at their HST paths with default values.
|
|
596
|
-
* For new records, call this after setting up the doctype to ensure all paths exist.
|
|
597
|
-
*
|
|
598
|
-
* @param path - HST path (e.g., "customer.new")
|
|
599
|
-
* @param doctype - The doctype to initialize
|
|
600
|
-
* @public
|
|
601
|
-
*/
|
|
602
|
-
initializeNestedData(path: string, doctype: Doctype): void {
|
|
603
|
-
const slug = doctype.slug
|
|
604
|
-
this.ensureDoctypeExists(slug)
|
|
605
|
-
|
|
606
|
-
// Resolve schema and initialize with defaults
|
|
607
|
-
const resolvedSchema = this.registry.resolveSchema(doctype)
|
|
608
|
-
const record = this.registry.initializeRecord(resolvedSchema)
|
|
609
|
-
|
|
610
|
-
// Ensure the ancestor path exists in HST before setting descendant fields
|
|
611
|
-
const existingData = this.hstStore.get(path)
|
|
612
|
-
if (!existingData) {
|
|
613
|
-
this.hstStore.set(path, {}, 'system')
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
// Store each field at its own HST path
|
|
617
|
-
for (const [key, value] of Object.entries(record)) {
|
|
618
|
-
this.hstStore.set(`${path}.${key}`, value, 'system')
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
/**
|
|
623
|
-
* Fetch a record and its nested data from the server.
|
|
624
|
-
*
|
|
625
|
-
* Calls `_client.getRecord()` with nested sub-selections and stores each scalar field at its own HST path
|
|
626
|
-
* (`slug.recordId.fieldname`), descendants at the link-level path (`slug.recordId.linkname`).
|
|
627
|
-
*
|
|
628
|
-
* @param path - HST path (e.g., "recipe.r1")
|
|
629
|
-
* @param doctype - The doctype to fetch
|
|
630
|
-
* @param recordId - Record ID to fetch
|
|
631
|
-
* @param options - Query options (includeNested to control which links are fetched)
|
|
632
|
-
* @throws Error with code `"CLIENT_REQUIRED"` if no data client is configured
|
|
633
|
-
* @throws Error with code `"RECORD_NOT_FOUND"` if the server returns null
|
|
634
|
-
* @public
|
|
635
|
-
*/
|
|
636
|
-
async fetchNestedData(
|
|
637
|
-
path: string,
|
|
638
|
-
doctype: Doctype,
|
|
639
|
-
recordId: string,
|
|
640
|
-
options?: { includeNested?: boolean | string[] }
|
|
641
|
-
): Promise<void> {
|
|
642
|
-
if (!this._client) {
|
|
643
|
-
throw createCodedError(
|
|
644
|
-
'No data client configured. Call setClient() with a DataClient implementation ' +
|
|
645
|
-
'(e.g., StonecropClient from @stonecrop/graphql-client) before fetching records.',
|
|
646
|
-
'CLIENT_REQUIRED'
|
|
647
|
-
)
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
const result = await this._client.getRecord({ name: doctype.doctype }, recordId, {
|
|
651
|
-
includeNested: options?.includeNested ?? true,
|
|
652
|
-
})
|
|
653
|
-
|
|
654
|
-
if (!result?.record) {
|
|
655
|
-
throw createCodedError(`Record not found: ${doctype.doctype} ${recordId}`, 'RECORD_NOT_FOUND')
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
// Store each scalar field at its own HST path, descendants at link-level path
|
|
659
|
-
const slug = doctype.slug
|
|
660
|
-
this.ensureDoctypeExists(slug)
|
|
661
|
-
|
|
662
|
-
// Ensure the ancestor path exists in HST before setting descendant fields
|
|
663
|
-
const existingData = this.hstStore.get(`${slug}.${recordId}`)
|
|
664
|
-
if (!existingData) {
|
|
665
|
-
this.hstStore.set(`${slug}.${recordId}`, {}, 'system')
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
for (const [key, value] of Object.entries(result.record)) {
|
|
669
|
-
this.hstStore.set(`${slug}.${recordId}.${key}`, value, 'system')
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
|
|
673
|
-
/**
|
|
674
|
-
* Recursively collect nested data from HST
|
|
675
|
-
* @param basePath - The base path in HST (e.g., "customer.123.address")
|
|
676
|
-
* @param doctype - The doctype whose links drive the recursive traversal
|
|
677
|
-
* @returns The collected data object
|
|
678
|
-
*/
|
|
679
|
-
private collectNestedData(basePath: string, doctype: Doctype): Record<string, any> {
|
|
680
|
-
const data = this.hstStore.get(basePath) || {}
|
|
681
|
-
const payload: Record<string, any> = { ...data }
|
|
682
|
-
|
|
683
|
-
if (!doctype.links) return payload
|
|
684
|
-
|
|
685
|
-
for (const [fieldname, link] of Object.entries(doctype.links)) {
|
|
686
|
-
const fieldPath = `${basePath}.${fieldname}`
|
|
687
|
-
const isMany = link.cardinality === 'noneOrMany' || link.cardinality === 'atLeastOne'
|
|
688
|
-
|
|
689
|
-
if (isMany) {
|
|
690
|
-
const arrayData = this.hstStore.get(fieldPath)
|
|
691
|
-
if (Array.isArray(arrayData)) {
|
|
692
|
-
payload[fieldname] = arrayData
|
|
693
|
-
}
|
|
694
|
-
} else {
|
|
695
|
-
const targetDoctype = this.registry.getDoctype(link.target)
|
|
696
|
-
if (targetDoctype?.links) {
|
|
697
|
-
payload[fieldname] = this.collectNestedData(fieldPath, targetDoctype)
|
|
698
|
-
} else {
|
|
699
|
-
payload[fieldname] = this.hstStore.get(fieldPath) || {}
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
return payload
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
/**
|
|
709
|
-
* Create an Error with a `code` property for programmatic error handling.
|
|
710
|
-
* @internal
|
|
711
|
-
*/
|
|
712
|
-
interface CodedError extends Error {
|
|
713
|
-
code: string
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
function createCodedError(message: string, code: string): CodedError {
|
|
717
|
-
// oxlint-disable-next-line typescript/no-unsafe-type-assertion -- extending Error with code property; CodedError cast is the standard pattern for typed error objects
|
|
718
|
-
const error = new Error(message) as CodedError
|
|
719
|
-
error.code = code
|
|
720
|
-
return error
|
|
721
|
-
}
|