-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetadata-loader.zod.ts
More file actions
503 lines (423 loc) · 13.4 KB
/
metadata-loader.zod.ts
File metadata and controls
503 lines (423 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* # Metadata Loader Protocol
*
* Defines the standard interface for loading and saving metadata in ObjectStack.
* This protocol enables consistent metadata operations across different storage backends
* (filesystem, HTTP, S3, databases) and serialization formats (JSON, YAML, TypeScript).
*/
/**
* Metadata Format Enum
* Supported serialization formats for metadata
*/
export const MetadataFormatSchema = z.enum(['json', 'yaml', 'typescript', 'javascript']);
/**
* Metadata Statistics
* Information about a metadata item without loading its full content
*/
export const MetadataStatsSchema = z.object({
/**
* Size of the metadata file in bytes
*/
size: z.number().int().min(0).describe('File size in bytes'),
/**
* Last modification timestamp
*/
modifiedAt: z.string().datetime().describe('Last modified date'),
/**
* ETag for cache validation
* Used for conditional requests (If-None-Match header)
*/
etag: z.string().describe('Entity tag for cache validation'),
/**
* Serialization format
*/
format: MetadataFormatSchema.describe('Serialization format'),
/**
* Full file path (if applicable)
*/
path: z.string().optional().describe('File system path'),
/**
* Additional metadata provider-specific properties
*/
metadata: z.record(z.string(), z.unknown()).optional().describe('Provider-specific metadata'),
});
/**
* Metadata Load Options
*/
export const MetadataLoadOptionsSchema = z.object({
/**
* Glob patterns to match files
* Example: ["**\/*.object.ts", "**\/*.object.json"]
*/
patterns: z.array(z.string()).optional().describe('File glob patterns'),
/**
* If-None-Match header for conditional loading
* Only load if ETag doesn't match
*/
ifNoneMatch: z.string().optional().describe('ETag for conditional request'),
/**
* If-Modified-Since header for conditional loading
*/
ifModifiedSince: z.string().datetime().optional().describe('Only load if modified after this date'),
/**
* Whether to validate against Zod schema
*/
validate: z.boolean().default(true).describe('Validate against schema'),
/**
* Whether to use cache if available
*/
useCache: z.boolean().default(true).describe('Enable caching'),
/**
* Filter function (serialized as string)
* Example: "(item) => item.name.startsWith('sys_')"
*/
filter: z.string().optional().describe('Filter predicate as string'),
/**
* Maximum number of items to load
*/
limit: z.number().int().min(1).optional().describe('Maximum items to load'),
/**
* Recursively search subdirectories
*/
recursive: z.boolean().default(true).describe('Search subdirectories'),
});
/**
* Metadata Save Options
*/
export const MetadataSaveOptionsSchema = z.object({
/**
* Serialization format
*/
format: MetadataFormatSchema.default('typescript').describe('Output format'),
/**
* Prettify output (formatted with indentation)
*/
prettify: z.boolean().default(true).describe('Format with indentation'),
/**
* Indentation size (spaces)
*/
indent: z.number().int().min(0).max(8).default(2).describe('Indentation spaces'),
/**
* Sort object keys alphabetically
*/
sortKeys: z.boolean().default(false).describe('Sort object keys'),
/**
* Include default values in output
*/
includeDefaults: z.boolean().default(false).describe('Include default values'),
/**
* Create backup before overwriting
*/
backup: z.boolean().default(false).describe('Create backup file'),
/**
* Overwrite if exists
*/
overwrite: z.boolean().default(true).describe('Overwrite existing file'),
/**
* Atomic write (write to temp file, then rename)
*/
atomic: z.boolean().default(true).describe('Use atomic write operation'),
/**
* Custom file path (overrides default location)
*/
path: z.string().optional().describe('Custom output path'),
});
/**
* Metadata Export Options
*/
export const MetadataExportOptionsSchema = z.object({
/**
* Output file path
*/
output: z.string().describe('Output file path'),
/**
* Export format
*/
format: MetadataFormatSchema.default('json').describe('Export format'),
/**
* Filter predicate as string
*/
filter: z.string().optional().describe('Filter items to export'),
/**
* Include statistics in export
*/
includeStats: z.boolean().default(false).describe('Include metadata statistics'),
/**
* Compress output
*/
compress: z.boolean().default(false).describe('Compress output (gzip)'),
/**
* Pretty print output
*/
prettify: z.boolean().default(true).describe('Pretty print output'),
});
/**
* Metadata Import Options
*/
export const MetadataImportOptionsSchema = z.object({
/**
* Conflict resolution strategy
*/
conflictResolution: z.enum(['skip', 'overwrite', 'merge', 'fail'])
.default('merge')
.describe('How to handle existing items'),
/**
* Validate items against schema
*/
validate: z.boolean().default(true).describe('Validate before import'),
/**
* Dry run (don't actually save)
*/
dryRun: z.boolean().default(false).describe('Simulate import without saving'),
/**
* Continue on errors
*/
continueOnError: z.boolean().default(false).describe('Continue if validation fails'),
/**
* Transform function (as string)
* Example: "(item) => ({ ...item, imported: true })"
*/
transform: z.string().optional().describe('Transform items before import'),
});
/**
* Metadata Loader Result
* Result of a metadata load operation
*/
export const MetadataLoadResultSchema = z.object({
/**
* Loaded data
*/
data: z.unknown().nullable().describe('Loaded metadata'),
/**
* Whether data came from cache (304 Not Modified)
*/
fromCache: z.boolean().default(false).describe('Loaded from cache'),
/**
* Not modified (conditional request matched)
*/
notModified: z.boolean().default(false).describe('Not modified since last request'),
/**
* ETag of loaded data
*/
etag: z.string().optional().describe('Entity tag'),
/**
* Statistics about loaded data
*/
stats: MetadataStatsSchema.optional().describe('Metadata statistics'),
/**
* Load time in milliseconds
*/
loadTime: z.number().min(0).optional().describe('Load duration in ms'),
});
/**
* Metadata Save Result
*/
export const MetadataSaveResultSchema = z.object({
/**
* Whether save was successful
*/
success: z.boolean().describe('Save successful'),
/**
* Path where file was saved
*/
path: z.string().describe('Output path'),
/**
* Generated ETag
*/
etag: z.string().optional().describe('Generated entity tag'),
/**
* File size in bytes
*/
size: z.number().int().min(0).optional().describe('File size'),
/**
* Save time in milliseconds
*/
saveTime: z.number().min(0).optional().describe('Save duration in ms'),
/**
* Backup path (if created)
*/
backupPath: z.string().optional().describe('Backup file path'),
});
/**
* Metadata Watch Event
*/
export const MetadataWatchEventSchema = z.object({
/**
* Event type
*/
type: z.enum(['added', 'changed', 'deleted']).describe('Event type'),
/**
* Metadata type (e.g., 'object', 'view', 'app')
*/
metadataType: z.string().describe('Type of metadata'),
/**
* Item name/identifier
*/
name: z.string().describe('Item identifier'),
/**
* Full file path
*/
path: z.string().describe('File path'),
/**
* Loaded item data (for added/changed events)
*/
data: z.unknown().optional().describe('Item data'),
/**
* Timestamp
*/
timestamp: z.string().datetime().describe('Event timestamp'),
});
/**
* Metadata Collection Info
* Summary of a metadata collection
*/
export const MetadataCollectionInfoSchema = z.object({
/**
* Collection type (e.g., 'object', 'view', 'app')
*/
type: z.string().describe('Collection type'),
/**
* Total items in collection
*/
count: z.number().int().min(0).describe('Number of items'),
/**
* Formats found in collection
*/
formats: z.array(MetadataFormatSchema).describe('Formats in collection'),
/**
* Total size in bytes
*/
totalSize: z.number().int().min(0).optional().describe('Total size in bytes'),
/**
* Last modified timestamp
*/
lastModified: z.string().datetime().optional().describe('Last modification date'),
/**
* Collection location (path or URL)
*/
location: z.string().optional().describe('Collection location'),
});
/**
* Metadata Loader Interface Contract
* Defines the standard methods all metadata loaders must implement
*/
export const MetadataLoaderContractSchema = z.object({
/**
* Loader name/identifier
*/
name: z.string().describe('Loader identifier'),
/**
* Protocol handled by this loader (e.g. 'file:', 'http:', 's3:', 'datasource:')
*/
protocol: z.enum(['file:', 'http:', 's3:', 'datasource:', 'memory:']).describe('Protocol identifier'),
/**
* Detailed capabilities
*/
capabilities: z.object({
read: z.boolean().default(true),
write: z.boolean().default(false),
watch: z.boolean().default(false),
list: z.boolean().default(true),
}).describe('Loader capabilities'),
/**
* Supported formats
*/
supportedFormats: z.array(MetadataFormatSchema).describe('Supported formats'),
/**
* Whether loader supports watching for changes
*/
supportsWatch: z.boolean().default(false).describe('Supports file watching'),
/**
* Whether loader supports saving
*/
supportsWrite: z.boolean().default(true).describe('Supports write operations'),
/**
* Whether loader supports caching
*/
supportsCache: z.boolean().default(true).describe('Supports caching'),
});
/**
* Metadata Fallback Strategy
* Determines behavior when the primary datasource is unavailable.
*/
export const MetadataFallbackStrategySchema = z.enum([
'filesystem', // Fall back to filesystem-based loading
'memory', // Fall back to in-memory storage
'none', // No fallback — fail immediately
]);
/**
* Metadata Manager Configuration
*/
export const MetadataManagerConfigSchema = z.object({
/**
* Datasource Name Reference
* References a DatasourceSchema.name (e.g. 'default').
* At runtime, resolved from kernel service `driver.{name}` to obtain the actual driver.
*/
datasource: z.string().optional().describe('Datasource name reference for database persistence'),
/**
* Metadata Table Name
* The database table used for metadata storage when datasource is configured.
*/
tableName: z.string().default('sys_metadata').describe('Database table name for metadata storage'),
/**
* Fallback Strategy
* Determines behavior when the primary datasource is unavailable.
*/
fallback: MetadataFallbackStrategySchema.default('none').describe('Fallback strategy when datasource is unavailable'),
/**
* Root directory for metadata (for filesystem loaders)
*/
rootDir: z.string().optional().describe('Root directory path'),
/**
* Enabled serialization formats
*/
formats: z.array(MetadataFormatSchema).default(['typescript', 'json', 'yaml']).describe('Enabled formats'),
/**
* Cache configuration
*/
cache: z.object({
enabled: z.boolean().default(true).describe('Enable caching'),
ttl: z.number().int().min(0).default(3600).describe('Cache TTL in seconds'),
maxSize: z.number().int().min(0).optional().describe('Max cache size in bytes'),
}).optional().describe('Cache settings'),
/**
* Watch for file changes
*/
watch: z.boolean().default(false).describe('Enable file watching'),
/**
* Watch options
*/
watchOptions: z.object({
ignored: z.array(z.string()).optional().describe('Patterns to ignore'),
persistent: z.boolean().default(true).describe('Keep process running'),
ignoreInitial: z.boolean().default(true).describe('Ignore initial add events'),
}).optional().describe('File watcher options'),
/**
* Validation settings
*/
validation: z.object({
strict: z.boolean().default(true).describe('Strict validation'),
throwOnError: z.boolean().default(true).describe('Throw on validation error'),
}).optional().describe('Validation settings'),
/**
* Loader-specific options
*/
loaderOptions: z.record(z.string(), z.unknown()).optional().describe('Loader-specific configuration'),
});
// Export types
export type MetadataFormat = z.infer<typeof MetadataFormatSchema>;
export type MetadataStats = z.infer<typeof MetadataStatsSchema>;
export type MetadataLoadOptions = z.input<typeof MetadataLoadOptionsSchema>;
export type MetadataSaveOptions = z.infer<typeof MetadataSaveOptionsSchema>;
export type MetadataExportOptions = z.infer<typeof MetadataExportOptionsSchema>;
export type MetadataImportOptions = z.infer<typeof MetadataImportOptionsSchema>;
export type MetadataLoadResult = z.infer<typeof MetadataLoadResultSchema>;
export type MetadataSaveResult = z.infer<typeof MetadataSaveResultSchema>;
export type MetadataWatchEvent = z.infer<typeof MetadataWatchEventSchema>;
export type MetadataCollectionInfo = z.infer<typeof MetadataCollectionInfoSchema>;
export type MetadataLoaderContract = z.input<typeof MetadataLoaderContractSchema>;
export type MetadataManagerConfig = z.input<typeof MetadataManagerConfigSchema>;
export type MetadataFallbackStrategy = z.infer<typeof MetadataFallbackStrategySchema>;