@objectstack/metadata 3.2.9 → 3.3.1
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/index.cjs +2197 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.js +42 -82
- package/dist/index.js.map +1 -1
- package/dist/node.cjs +2201 -0
- package/dist/node.cjs.map +1 -0
- package/dist/node.d.cts +65 -0
- package/dist/node.d.ts +65 -0
- package/dist/{index.mjs → node.js} +3 -1
- package/package.json +18 -13
- package/.turbo/turbo-build.log +0 -22
- package/CHANGELOG.md +0 -496
- package/ROADMAP.md +0 -224
- package/src/index.ts +0 -68
- package/src/loaders/database-loader.test.ts +0 -559
- package/src/loaders/database-loader.ts +0 -352
- package/src/loaders/filesystem-loader.ts +0 -420
- package/src/loaders/loader-interface.ts +0 -89
- package/src/loaders/memory-loader.ts +0 -103
- package/src/loaders/remote-loader.ts +0 -140
- package/src/metadata-manager.ts +0 -1168
- package/src/metadata-service.test.ts +0 -965
- package/src/metadata.test.ts +0 -431
- package/src/migration/executor.ts +0 -54
- package/src/migration/index.ts +0 -3
- package/src/node-metadata-manager.ts +0 -126
- package/src/node.ts +0 -11
- package/src/objects/sys-metadata.object.ts +0 -188
- package/src/plugin.ts +0 -102
- package/src/serializers/json-serializer.ts +0 -73
- package/src/serializers/serializer-interface.ts +0 -65
- package/src/serializers/serializers.test.ts +0 -74
- package/src/serializers/typescript-serializer.ts +0 -127
- package/src/serializers/yaml-serializer.ts +0 -49
- package/tsconfig.json +0 -9
- package/vitest.config.ts +0 -23
- /package/dist/{index.d.mts → index.d.cts} +0 -0
- /package/dist/{index.mjs.map → node.js.map} +0 -0
|
@@ -1,188 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import { ObjectSchema, Field } from '@objectstack/spec/data';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* sys_metadata — System Metadata Object
|
|
7
|
-
*
|
|
8
|
-
* Canonical ObjectStack object definition for the metadata persistence table.
|
|
9
|
-
* Stores all platform-scope and user-scope metadata records (Objects, Views,
|
|
10
|
-
* Flows, etc.) using the MetadataRecordSchema envelope.
|
|
11
|
-
*
|
|
12
|
-
* This is a system object (isSystem: true) — protected from deletion and
|
|
13
|
-
* automatically provisioned by the DatabaseLoader on first use.
|
|
14
|
-
*
|
|
15
|
-
* @see MetadataRecordSchema in metadata-persistence.zod.ts
|
|
16
|
-
*/
|
|
17
|
-
export const SysMetadataObject = ObjectSchema.create({
|
|
18
|
-
namespace: 'sys',
|
|
19
|
-
name: 'metadata',
|
|
20
|
-
label: 'System Metadata',
|
|
21
|
-
pluralLabel: 'System Metadata',
|
|
22
|
-
icon: 'settings',
|
|
23
|
-
isSystem: true,
|
|
24
|
-
description: 'Stores platform and user-scope metadata records (objects, views, flows, etc.)',
|
|
25
|
-
|
|
26
|
-
fields: {
|
|
27
|
-
/** Primary Key (UUID) */
|
|
28
|
-
id: Field.text({
|
|
29
|
-
label: 'ID',
|
|
30
|
-
required: true,
|
|
31
|
-
readonly: true,
|
|
32
|
-
}),
|
|
33
|
-
|
|
34
|
-
/** Machine name — unique identifier used in code references */
|
|
35
|
-
name: Field.text({
|
|
36
|
-
label: 'Name',
|
|
37
|
-
required: true,
|
|
38
|
-
searchable: true,
|
|
39
|
-
maxLength: 255,
|
|
40
|
-
}),
|
|
41
|
-
|
|
42
|
-
/** Metadata type (e.g. "object", "view", "flow") */
|
|
43
|
-
type: Field.text({
|
|
44
|
-
label: 'Metadata Type',
|
|
45
|
-
required: true,
|
|
46
|
-
searchable: true,
|
|
47
|
-
maxLength: 100,
|
|
48
|
-
}),
|
|
49
|
-
|
|
50
|
-
/** Namespace / module grouping (e.g. "crm", "core") */
|
|
51
|
-
namespace: Field.text({
|
|
52
|
-
label: 'Namespace',
|
|
53
|
-
required: false,
|
|
54
|
-
defaultValue: 'default',
|
|
55
|
-
maxLength: 100,
|
|
56
|
-
}),
|
|
57
|
-
|
|
58
|
-
/** Package that owns/delivered this metadata */
|
|
59
|
-
package_id: Field.text({
|
|
60
|
-
label: 'Package ID',
|
|
61
|
-
required: false,
|
|
62
|
-
maxLength: 255,
|
|
63
|
-
}),
|
|
64
|
-
|
|
65
|
-
/** Who manages this record: package, platform, or user */
|
|
66
|
-
managed_by: Field.select(['package', 'platform', 'user'], {
|
|
67
|
-
label: 'Managed By',
|
|
68
|
-
required: false,
|
|
69
|
-
}),
|
|
70
|
-
|
|
71
|
-
/** Scope: system (code), platform (admin DB), user (personal DB) */
|
|
72
|
-
scope: Field.select(['system', 'platform', 'user'], {
|
|
73
|
-
label: 'Scope',
|
|
74
|
-
required: true,
|
|
75
|
-
defaultValue: 'platform',
|
|
76
|
-
}),
|
|
77
|
-
|
|
78
|
-
/** JSON payload — the actual metadata configuration */
|
|
79
|
-
metadata: Field.textarea({
|
|
80
|
-
label: 'Metadata',
|
|
81
|
-
required: true,
|
|
82
|
-
description: 'JSON-serialized metadata payload',
|
|
83
|
-
}),
|
|
84
|
-
|
|
85
|
-
/** Parent metadata name for extension/override */
|
|
86
|
-
extends: Field.text({
|
|
87
|
-
label: 'Extends',
|
|
88
|
-
required: false,
|
|
89
|
-
maxLength: 255,
|
|
90
|
-
}),
|
|
91
|
-
|
|
92
|
-
/** Merge strategy when extending parent metadata */
|
|
93
|
-
strategy: Field.select(['merge', 'replace'], {
|
|
94
|
-
label: 'Strategy',
|
|
95
|
-
required: false,
|
|
96
|
-
defaultValue: 'merge',
|
|
97
|
-
}),
|
|
98
|
-
|
|
99
|
-
/** Owner user ID (for user-scope items) */
|
|
100
|
-
owner: Field.text({
|
|
101
|
-
label: 'Owner',
|
|
102
|
-
required: false,
|
|
103
|
-
maxLength: 255,
|
|
104
|
-
}),
|
|
105
|
-
|
|
106
|
-
/** Lifecycle state */
|
|
107
|
-
state: Field.select(['draft', 'active', 'archived', 'deprecated'], {
|
|
108
|
-
label: 'State',
|
|
109
|
-
required: false,
|
|
110
|
-
defaultValue: 'active',
|
|
111
|
-
}),
|
|
112
|
-
|
|
113
|
-
/** Tenant ID for multi-tenant isolation */
|
|
114
|
-
tenant_id: Field.text({
|
|
115
|
-
label: 'Tenant ID',
|
|
116
|
-
required: false,
|
|
117
|
-
maxLength: 255,
|
|
118
|
-
}),
|
|
119
|
-
|
|
120
|
-
/** Version number for optimistic concurrency */
|
|
121
|
-
version: Field.number({
|
|
122
|
-
label: 'Version',
|
|
123
|
-
required: false,
|
|
124
|
-
defaultValue: 1,
|
|
125
|
-
}),
|
|
126
|
-
|
|
127
|
-
/** Content checksum for change detection */
|
|
128
|
-
checksum: Field.text({
|
|
129
|
-
label: 'Checksum',
|
|
130
|
-
required: false,
|
|
131
|
-
maxLength: 64,
|
|
132
|
-
}),
|
|
133
|
-
|
|
134
|
-
/** Origin of this metadata record */
|
|
135
|
-
source: Field.select(['filesystem', 'database', 'api', 'migration'], {
|
|
136
|
-
label: 'Source',
|
|
137
|
-
required: false,
|
|
138
|
-
}),
|
|
139
|
-
|
|
140
|
-
/** Classification tags (JSON array) */
|
|
141
|
-
tags: Field.textarea({
|
|
142
|
-
label: 'Tags',
|
|
143
|
-
required: false,
|
|
144
|
-
description: 'JSON-serialized array of classification tags',
|
|
145
|
-
}),
|
|
146
|
-
|
|
147
|
-
/** Audit fields */
|
|
148
|
-
created_by: Field.text({
|
|
149
|
-
label: 'Created By',
|
|
150
|
-
required: false,
|
|
151
|
-
readonly: true,
|
|
152
|
-
maxLength: 255,
|
|
153
|
-
}),
|
|
154
|
-
|
|
155
|
-
created_at: Field.datetime({
|
|
156
|
-
label: 'Created At',
|
|
157
|
-
required: false,
|
|
158
|
-
readonly: true,
|
|
159
|
-
}),
|
|
160
|
-
|
|
161
|
-
updated_by: Field.text({
|
|
162
|
-
label: 'Updated By',
|
|
163
|
-
required: false,
|
|
164
|
-
maxLength: 255,
|
|
165
|
-
}),
|
|
166
|
-
|
|
167
|
-
updated_at: Field.datetime({
|
|
168
|
-
label: 'Updated At',
|
|
169
|
-
required: false,
|
|
170
|
-
}),
|
|
171
|
-
},
|
|
172
|
-
|
|
173
|
-
indexes: [
|
|
174
|
-
{ fields: ['type', 'name'], unique: true },
|
|
175
|
-
{ fields: ['type', 'scope'] },
|
|
176
|
-
{ fields: ['tenant_id'] },
|
|
177
|
-
{ fields: ['state'] },
|
|
178
|
-
{ fields: ['namespace'] },
|
|
179
|
-
],
|
|
180
|
-
|
|
181
|
-
enable: {
|
|
182
|
-
trackHistory: true,
|
|
183
|
-
searchable: false,
|
|
184
|
-
apiEnabled: true,
|
|
185
|
-
apiMethods: ['get', 'list', 'create', 'update', 'delete'],
|
|
186
|
-
trash: false,
|
|
187
|
-
},
|
|
188
|
-
});
|
package/src/plugin.ts
DELETED
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
import { Plugin, PluginContext } from '@objectstack/core';
|
|
4
|
-
import { NodeMetadataManager } from './node-metadata-manager.js';
|
|
5
|
-
import { DEFAULT_METADATA_TYPE_REGISTRY } from '@objectstack/spec/kernel';
|
|
6
|
-
import type { MetadataPluginConfig } from '@objectstack/spec/kernel';
|
|
7
|
-
import { SysMetadataObject } from './objects/sys-metadata.object.js';
|
|
8
|
-
|
|
9
|
-
export interface MetadataPluginOptions {
|
|
10
|
-
rootDir?: string;
|
|
11
|
-
watch?: boolean;
|
|
12
|
-
config?: Partial<MetadataPluginConfig>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export class MetadataPlugin implements Plugin {
|
|
16
|
-
name = 'com.objectstack.metadata';
|
|
17
|
-
type = 'standard';
|
|
18
|
-
version = '1.0.0';
|
|
19
|
-
|
|
20
|
-
private manager: NodeMetadataManager;
|
|
21
|
-
private options: MetadataPluginOptions;
|
|
22
|
-
|
|
23
|
-
constructor(options: MetadataPluginOptions = {}) {
|
|
24
|
-
this.options = {
|
|
25
|
-
watch: true,
|
|
26
|
-
...options
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
const rootDir = this.options.rootDir || process.cwd();
|
|
30
|
-
|
|
31
|
-
this.manager = new NodeMetadataManager({
|
|
32
|
-
rootDir,
|
|
33
|
-
watch: this.options.watch ?? true,
|
|
34
|
-
formats: ['yaml', 'json', 'typescript', 'javascript']
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
// Initialize with default type registry
|
|
38
|
-
this.manager.setTypeRegistry(DEFAULT_METADATA_TYPE_REGISTRY);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
init = async (ctx: PluginContext) => {
|
|
42
|
-
ctx.logger.info('Initializing Metadata Manager', {
|
|
43
|
-
root: this.options.rootDir || process.cwd(),
|
|
44
|
-
watch: this.options.watch
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// Register Metadata Manager as primary metadata service provider
|
|
48
|
-
// This takes precedence over ObjectQL's fallback metadata service
|
|
49
|
-
ctx.registerService('metadata', this.manager);
|
|
50
|
-
|
|
51
|
-
// Register metadata system objects so ObjectQLPlugin auto-discovers them
|
|
52
|
-
ctx.registerService('app.com.objectstack.metadata', {
|
|
53
|
-
id: 'com.objectstack.metadata',
|
|
54
|
-
name: 'Metadata',
|
|
55
|
-
version: '1.0.0',
|
|
56
|
-
type: 'plugin',
|
|
57
|
-
namespace: 'sys',
|
|
58
|
-
objects: [SysMetadataObject],
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
ctx.logger.info('MetadataPlugin providing metadata service (primary mode)', {
|
|
62
|
-
mode: 'file-system',
|
|
63
|
-
features: ['watch', 'persistence', 'multi-format', 'query', 'overlay', 'type-registry']
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
start = async (ctx: PluginContext) => {
|
|
68
|
-
ctx.logger.info('Loading metadata from file system...');
|
|
69
|
-
|
|
70
|
-
// Use the type registry to discover metadata types (sorted by loadOrder)
|
|
71
|
-
const sortedTypes = [...DEFAULT_METADATA_TYPE_REGISTRY]
|
|
72
|
-
.sort((a, b) => a.loadOrder - b.loadOrder);
|
|
73
|
-
|
|
74
|
-
let totalLoaded = 0;
|
|
75
|
-
for (const entry of sortedTypes) {
|
|
76
|
-
try {
|
|
77
|
-
const items = await this.manager.loadMany(entry.type, {
|
|
78
|
-
recursive: true
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
if (items.length > 0) {
|
|
82
|
-
// Register loaded items in the in-memory registry
|
|
83
|
-
for (const item of items) {
|
|
84
|
-
const meta = item as any;
|
|
85
|
-
if (meta?.name) {
|
|
86
|
-
await this.manager.register(entry.type, meta.name, item);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
ctx.logger.info(`Loaded ${items.length} ${entry.type} from file system`);
|
|
90
|
-
totalLoaded += items.length;
|
|
91
|
-
}
|
|
92
|
-
} catch (e: any) {
|
|
93
|
-
ctx.logger.debug(`No ${entry.type} metadata found`, { error: e.message });
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
ctx.logger.info('Metadata loading complete', {
|
|
98
|
-
totalItems: totalLoaded,
|
|
99
|
-
registeredTypes: sortedTypes.length,
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* JSON Metadata Serializer
|
|
5
|
-
*
|
|
6
|
-
* Handles JSON format serialization and deserialization
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { z } from 'zod';
|
|
10
|
-
import type { MetadataFormat } from '@objectstack/spec/system';
|
|
11
|
-
import type { MetadataSerializer, SerializeOptions } from './serializer-interface.js';
|
|
12
|
-
|
|
13
|
-
export class JSONSerializer implements MetadataSerializer {
|
|
14
|
-
serialize<T>(item: T, options?: SerializeOptions): string {
|
|
15
|
-
const { prettify = true, indent = 2, sortKeys = false } = options || {};
|
|
16
|
-
|
|
17
|
-
if (sortKeys) {
|
|
18
|
-
// Sort keys recursively
|
|
19
|
-
const sorted = this.sortObjectKeys(item);
|
|
20
|
-
return prettify
|
|
21
|
-
? JSON.stringify(sorted, null, indent)
|
|
22
|
-
: JSON.stringify(sorted);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return prettify
|
|
26
|
-
? JSON.stringify(item, null, indent)
|
|
27
|
-
: JSON.stringify(item);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
deserialize<T>(content: string, schema?: z.ZodSchema): T {
|
|
31
|
-
const parsed = JSON.parse(content);
|
|
32
|
-
|
|
33
|
-
if (schema) {
|
|
34
|
-
return schema.parse(parsed) as T;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return parsed as T;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
getExtension(): string {
|
|
41
|
-
return '.json';
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
canHandle(format: MetadataFormat): boolean {
|
|
45
|
-
return format === 'json';
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
getFormat(): MetadataFormat {
|
|
49
|
-
return 'json';
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Recursively sort object keys
|
|
54
|
-
*/
|
|
55
|
-
private sortObjectKeys(obj: any): any {
|
|
56
|
-
if (obj === null || typeof obj !== 'object') {
|
|
57
|
-
return obj;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (Array.isArray(obj)) {
|
|
61
|
-
return obj.map(item => this.sortObjectKeys(item));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const sorted: Record<string, any> = {};
|
|
65
|
-
const keys = Object.keys(obj).sort();
|
|
66
|
-
|
|
67
|
-
for (const key of keys) {
|
|
68
|
-
sorted[key] = this.sortObjectKeys(obj[key]);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return sorted;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Metadata Serializer Interface
|
|
5
|
-
*
|
|
6
|
-
* Defines the contract for serializing/deserializing metadata
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { z } from 'zod';
|
|
10
|
-
import type { MetadataFormat } from '@objectstack/spec/system';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Serialization options
|
|
14
|
-
*/
|
|
15
|
-
export interface SerializeOptions {
|
|
16
|
-
/** Prettify output (formatted with indentation) */
|
|
17
|
-
prettify?: boolean;
|
|
18
|
-
/** Indentation size (spaces) */
|
|
19
|
-
indent?: number;
|
|
20
|
-
/** Sort object keys alphabetically */
|
|
21
|
-
sortKeys?: boolean;
|
|
22
|
-
/** Include default values in output */
|
|
23
|
-
includeDefaults?: boolean;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Abstract interface for metadata serializers
|
|
28
|
-
* Implementations handle different formats (JSON, YAML, TypeScript, etc.)
|
|
29
|
-
*/
|
|
30
|
-
export interface MetadataSerializer {
|
|
31
|
-
/**
|
|
32
|
-
* Serialize object to string
|
|
33
|
-
* @param item The item to serialize
|
|
34
|
-
* @param options Serialization options
|
|
35
|
-
* @returns Serialized string
|
|
36
|
-
*/
|
|
37
|
-
serialize<T>(item: T, options?: SerializeOptions): string;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Deserialize string to object
|
|
41
|
-
* @param content The content to deserialize
|
|
42
|
-
* @param schema Optional Zod schema for validation
|
|
43
|
-
* @returns Deserialized object
|
|
44
|
-
*/
|
|
45
|
-
deserialize<T>(content: string, schema?: z.ZodSchema): T;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Get file extension for this format
|
|
49
|
-
* @returns File extension (e.g., '.json', '.yaml')
|
|
50
|
-
*/
|
|
51
|
-
getExtension(): string;
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Check if this serializer can handle the format
|
|
55
|
-
* @param format The format to check
|
|
56
|
-
* @returns True if can handle
|
|
57
|
-
*/
|
|
58
|
-
canHandle(format: MetadataFormat): boolean;
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Get the format this serializer handles
|
|
62
|
-
* @returns The metadata format
|
|
63
|
-
*/
|
|
64
|
-
getFormat(): MetadataFormat;
|
|
65
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { JSONSerializer } from '../serializers/json-serializer';
|
|
3
|
-
import { YAMLSerializer } from '../serializers/yaml-serializer';
|
|
4
|
-
import { TypeScriptSerializer } from '../serializers/typescript-serializer';
|
|
5
|
-
|
|
6
|
-
describe('Serializers', () => {
|
|
7
|
-
describe('JSONSerializer', () => {
|
|
8
|
-
const serializer = new JSONSerializer();
|
|
9
|
-
|
|
10
|
-
it('should serialize to JSON', () => {
|
|
11
|
-
const data = { name: 'test', value: 42 };
|
|
12
|
-
const result = serializer.serialize(data);
|
|
13
|
-
expect(result).toContain('"name"');
|
|
14
|
-
expect(result).toContain('"test"');
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
it('should deserialize from JSON', () => {
|
|
18
|
-
const json = '{"name":"test","value":42}';
|
|
19
|
-
const result = serializer.deserialize(json);
|
|
20
|
-
expect(result).toEqual({ name: 'test', value: 42 });
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it('should prettify JSON', () => {
|
|
24
|
-
const data = { name: 'test' };
|
|
25
|
-
const result = serializer.serialize(data, { prettify: true, indent: 2 });
|
|
26
|
-
expect(result).toContain('\n');
|
|
27
|
-
expect(result).toContain(' ');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('should sort keys', () => {
|
|
31
|
-
const data = { zebra: 1, apple: 2, banana: 3 };
|
|
32
|
-
const result = serializer.serialize(data, { sortKeys: true });
|
|
33
|
-
const keys = Object.keys(JSON.parse(result));
|
|
34
|
-
expect(keys).toEqual(['apple', 'banana', 'zebra']);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe('YAMLSerializer', () => {
|
|
39
|
-
const serializer = new YAMLSerializer();
|
|
40
|
-
|
|
41
|
-
it('should serialize to YAML', () => {
|
|
42
|
-
const data = { name: 'test', value: 42 };
|
|
43
|
-
const result = serializer.serialize(data);
|
|
44
|
-
expect(result).toContain('name: test');
|
|
45
|
-
expect(result).toContain('value: 42');
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
it('should deserialize from YAML', () => {
|
|
49
|
-
const yaml = 'name: test\nvalue: 42';
|
|
50
|
-
const result = serializer.deserialize(yaml);
|
|
51
|
-
expect(result).toEqual({ name: 'test', value: 42 });
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
describe('TypeScriptSerializer', () => {
|
|
56
|
-
const serializer = new TypeScriptSerializer('typescript');
|
|
57
|
-
|
|
58
|
-
it('should serialize to TypeScript module', () => {
|
|
59
|
-
const data = { name: 'test', value: 42 };
|
|
60
|
-
const result = serializer.serialize(data);
|
|
61
|
-
expect(result).toContain('import type');
|
|
62
|
-
expect(result).toContain('export const metadata');
|
|
63
|
-
expect(result).toContain('export default metadata');
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it('should get correct extension', () => {
|
|
67
|
-
const ts = new TypeScriptSerializer('typescript');
|
|
68
|
-
expect(ts.getExtension()).toBe('.ts');
|
|
69
|
-
|
|
70
|
-
const js = new TypeScriptSerializer('javascript');
|
|
71
|
-
expect(js.getExtension()).toBe('.js');
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
});
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* TypeScript/JavaScript Metadata Serializer
|
|
5
|
-
*
|
|
6
|
-
* Handles TypeScript/JavaScript module format serialization and deserialization
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { z } from 'zod';
|
|
10
|
-
import type { MetadataFormat } from '@objectstack/spec/system';
|
|
11
|
-
import type { MetadataSerializer, SerializeOptions } from './serializer-interface.js';
|
|
12
|
-
|
|
13
|
-
export class TypeScriptSerializer implements MetadataSerializer {
|
|
14
|
-
constructor(private format: 'typescript' | 'javascript' = 'typescript') {}
|
|
15
|
-
|
|
16
|
-
serialize<T>(item: T, options?: SerializeOptions): string {
|
|
17
|
-
const { prettify = true, indent = 2 } = options || {};
|
|
18
|
-
|
|
19
|
-
const jsonStr = JSON.stringify(item, null, prettify ? indent : 0);
|
|
20
|
-
|
|
21
|
-
if (this.format === 'typescript') {
|
|
22
|
-
return `import type { ServiceObject } from '@objectstack/spec/data';\n\n` +
|
|
23
|
-
`export const metadata: ServiceObject = ${jsonStr};\n\n` +
|
|
24
|
-
`export default metadata;\n`;
|
|
25
|
-
} else {
|
|
26
|
-
return `export const metadata = ${jsonStr};\n\n` +
|
|
27
|
-
`export default metadata;\n`;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
deserialize<T>(content: string, schema?: z.ZodSchema): T {
|
|
32
|
-
// For TypeScript/JavaScript files, we need to extract the exported object
|
|
33
|
-
// Note: This is a simplified parser that works with JSON-like object literals
|
|
34
|
-
// For complex TypeScript with nested objects, consider using a proper TypeScript parser
|
|
35
|
-
|
|
36
|
-
// Try to find the object literal in various export patterns
|
|
37
|
-
// Pattern 1: export const metadata = {...};
|
|
38
|
-
let objectStart = content.indexOf('export const');
|
|
39
|
-
if (objectStart === -1) {
|
|
40
|
-
// Pattern 2: export default {...};
|
|
41
|
-
objectStart = content.indexOf('export default');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
if (objectStart === -1) {
|
|
45
|
-
throw new Error(
|
|
46
|
-
'Could not parse TypeScript/JavaScript module. ' +
|
|
47
|
-
'Expected export pattern: "export const metadata = {...};" or "export default {...};"'
|
|
48
|
-
);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Find the first opening brace after the export statement
|
|
52
|
-
const braceStart = content.indexOf('{', objectStart);
|
|
53
|
-
if (braceStart === -1) {
|
|
54
|
-
throw new Error('Could not find object literal in export statement');
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// Find the matching closing brace by counting braces
|
|
58
|
-
// Handle string literals to avoid counting braces inside strings
|
|
59
|
-
let braceCount = 0;
|
|
60
|
-
let braceEnd = -1;
|
|
61
|
-
let inString = false;
|
|
62
|
-
let stringChar = '';
|
|
63
|
-
|
|
64
|
-
for (let i = braceStart; i < content.length; i++) {
|
|
65
|
-
const char = content[i];
|
|
66
|
-
const prevChar = i > 0 ? content[i - 1] : '';
|
|
67
|
-
|
|
68
|
-
// Track string literals (simple handling of " and ')
|
|
69
|
-
if ((char === '"' || char === "'") && prevChar !== '\\') {
|
|
70
|
-
if (!inString) {
|
|
71
|
-
inString = true;
|
|
72
|
-
stringChar = char;
|
|
73
|
-
} else if (char === stringChar) {
|
|
74
|
-
inString = false;
|
|
75
|
-
stringChar = '';
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// Count braces only when not inside strings
|
|
80
|
-
if (!inString) {
|
|
81
|
-
if (char === '{') braceCount++;
|
|
82
|
-
if (char === '}') {
|
|
83
|
-
braceCount--;
|
|
84
|
-
if (braceCount === 0) {
|
|
85
|
-
braceEnd = i;
|
|
86
|
-
break;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (braceEnd === -1) {
|
|
93
|
-
throw new Error('Could not find matching closing brace for object literal');
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// Extract the object literal
|
|
97
|
-
const objectLiteral = content.substring(braceStart, braceEnd + 1);
|
|
98
|
-
|
|
99
|
-
try {
|
|
100
|
-
// Parse as JSON
|
|
101
|
-
const parsed = JSON.parse(objectLiteral);
|
|
102
|
-
|
|
103
|
-
if (schema) {
|
|
104
|
-
return schema.parse(parsed) as T;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return parsed as T;
|
|
108
|
-
} catch (error) {
|
|
109
|
-
throw new Error(
|
|
110
|
-
`Failed to parse object literal as JSON: ${error instanceof Error ? error.message : String(error)}. ` +
|
|
111
|
-
'Make sure the TypeScript/JavaScript object uses JSON-compatible syntax (no functions, comments, or trailing commas).'
|
|
112
|
-
);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
getExtension(): string {
|
|
117
|
-
return this.format === 'typescript' ? '.ts' : '.js';
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
canHandle(format: MetadataFormat): boolean {
|
|
121
|
-
return format === 'typescript' || format === 'javascript';
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
getFormat(): MetadataFormat {
|
|
125
|
-
return this.format;
|
|
126
|
-
}
|
|
127
|
-
}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* YAML Metadata Serializer
|
|
5
|
-
*
|
|
6
|
-
* Handles YAML format serialization and deserialization
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import * as yaml from 'js-yaml';
|
|
10
|
-
import type { z } from 'zod';
|
|
11
|
-
import type { MetadataFormat } from '@objectstack/spec/system';
|
|
12
|
-
import type { MetadataSerializer, SerializeOptions } from './serializer-interface.js';
|
|
13
|
-
|
|
14
|
-
export class YAMLSerializer implements MetadataSerializer {
|
|
15
|
-
serialize<T>(item: T, options?: SerializeOptions): string {
|
|
16
|
-
const { indent = 2, sortKeys = false } = options || {};
|
|
17
|
-
|
|
18
|
-
return yaml.dump(item, {
|
|
19
|
-
indent,
|
|
20
|
-
sortKeys,
|
|
21
|
-
lineWidth: -1, // Disable line wrapping
|
|
22
|
-
noRefs: true, // Disable YAML references
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
deserialize<T>(content: string, schema?: z.ZodSchema): T {
|
|
27
|
-
// Use JSON_SCHEMA to prevent arbitrary code execution
|
|
28
|
-
// This restricts YAML to JSON-compatible types only
|
|
29
|
-
const parsed = yaml.load(content, { schema: yaml.JSON_SCHEMA });
|
|
30
|
-
|
|
31
|
-
if (schema) {
|
|
32
|
-
return schema.parse(parsed) as T;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return parsed as T;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
getExtension(): string {
|
|
39
|
-
return '.yaml';
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
canHandle(format: MetadataFormat): boolean {
|
|
43
|
-
return format === 'yaml';
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
getFormat(): MetadataFormat {
|
|
47
|
-
return 'yaml';
|
|
48
|
-
}
|
|
49
|
-
}
|