-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathschemas.ts
More file actions
155 lines (140 loc) · 5.44 KB
/
schemas.ts
File metadata and controls
155 lines (140 loc) · 5.44 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
/**
* Schemas API Journey — Full lifecycle of schema management
*
* Demonstrates all SchemaClient APIs:
* - Register, get, list, delete schemas
* - Version management (create new versions)
* - Get schema by name (latest version)
* - Get schema by name and version
*
* Run:
* CONDUCTOR_SERVER_URL=http://localhost:8080 npx ts-node examples/api-journeys/schemas.ts
*/
import { OrkesClients } from "../../src/sdk";
async function main() {
const clients = await OrkesClients.from();
const schemas = clients.getSchemaClient();
const schemaName = "journey_order_schema";
try {
// ── 1. Register a schema ────────────────────────────────────────
await schemas.registerSchema([
{
name: schemaName,
version: 1,
type: "JSON",
data: {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
orderId: { type: "string" },
amount: { type: "number" },
items: {
type: "array",
items: { type: "string" },
},
},
required: ["orderId", "amount"],
},
},
]);
console.log("1. Registered schema:", schemaName, "v1");
// ── 2. Get schema by name and version ───────────────────────────
const schemaV1 = await schemas.getSchema(schemaName, 1);
console.log("2. Schema v1:", JSON.stringify({
name: schemaV1.name,
version: schemaV1.version,
type: schemaV1.type,
}));
// ── 3. Get schema by name (latest version) ─────────────────────
const latest = await schemas.getSchemaByName(schemaName);
console.log("3. Latest schema:", JSON.stringify({
name: latest.name,
version: latest.version,
}));
// ── 4. Register a new version ───────────────────────────────────
await schemas.registerSchema(
[
{
name: schemaName,
version: 2,
type: "JSON",
data: {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
orderId: { type: "string" },
amount: { type: "number" },
currency: { type: "string", default: "USD" },
items: {
type: "array",
items: {
type: "object",
properties: {
name: { type: "string" },
quantity: { type: "integer" },
price: { type: "number" },
},
},
},
shippingAddress: {
type: "object",
properties: {
street: { type: "string" },
city: { type: "string" },
zip: { type: "string" },
},
},
},
required: ["orderId", "amount", "currency"],
},
},
],
true // newVersion = true
);
console.log("4. Registered schema v2 (with currency and shippingAddress)");
// ── 5. Get both versions ────────────────────────────────────────
const v1 = await schemas.getSchema(schemaName, 1);
const v2 = await schemas.getSchema(schemaName, 2);
console.log("5. v1 required fields:", (v1.data as Record<string, unknown>)?.required);
console.log(" v2 required fields:", (v2.data as Record<string, unknown>)?.required);
// ── 6. List all schemas ─────────────────────────────────────────
const allSchemas = await schemas.getAllSchemas();
console.log("6. Total schemas:", allSchemas.length);
// ── 7. Register additional schemas ──────────────────────────────
await schemas.registerSchema([
{
name: "journey_user_schema",
version: 1,
type: "JSON",
data: {
type: "object",
properties: {
userId: { type: "string" },
email: { type: "string", format: "email" },
role: { type: "string", enum: ["admin", "user", "viewer"] },
},
required: ["userId", "email"],
},
},
]);
console.log("7. Registered additional schema: journey_user_schema");
// ── 8. Cleanup ──────────────────────────────────────────────────
await schemas.deleteSchema(schemaName, 1);
console.log("8. Deleted schema v1");
await schemas.deleteSchema(schemaName, 2);
console.log("9. Deleted schema v2");
await schemas.deleteSchemaByName("journey_user_schema");
console.log("10. Deleted user schema (all versions)");
} catch (err) {
console.error("Error:", err);
// Best-effort cleanup
try { await schemas.deleteSchemaByName(schemaName); } catch { /* ignore */ }
try { await schemas.deleteSchemaByName("journey_user_schema"); } catch { /* ignore */ }
}
console.log("\nDone.");
process.exit(0);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});