-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-schema-example.js
More file actions
101 lines (86 loc) · 2.92 KB
/
simple-schema-example.js
File metadata and controls
101 lines (86 loc) · 2.92 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
import { KurrentDBClient } from '@kurrent/kurrentdb-client';
import { v4 as uuid } from 'uuid';
/**
* Simple Schema Registry Example
* Quick start guide for using KurrentDB Schema Registry
*/
async function simpleSchemaExample() {
const client = KurrentDBClient.connectionString`kurrentdb://localhost:2113?tls=false`;
try {
const schemaName = `order-schema-${uuid()}`;
// 1. Create a schema with an initial version
console.log('Creating schema...');
const orderSchema = JSON.stringify({
type: 'object',
properties: {
orderId: { type: 'string' },
customerId: { type: 'string' },
amount: { type: 'number' }
},
required: ['orderId', 'customerId', 'amount']
});
await client.createSchema(
schemaName,
{
dataFormat: 'json',
compatibility: 'backward',
description: 'Order event schema'
},
{
schemaDefinition: orderSchema
}
);
console.log('✓ Schema created!\n');
// 2. Retrieve the schema
const schema = await client.getSchema(schemaName);
console.log('Schema details:');
console.log(` Name: ${schema.schemaName}`);
console.log(` Format: ${schema.details.dataFormat}`);
console.log(` Latest Version: ${schema.latestSchemaVersion}\n`);
// 3. Get the schema version (with definition)
const version = await client.getSchemaVersion(schemaName);
const decoder = new TextDecoder();
const definition = JSON.parse(decoder.decode(version.schemaDefinition));
console.log('Schema definition:');
console.log(JSON.stringify(definition, null, 2), '\n');
// 4. Check if a new schema is compatible
const updatedSchema = JSON.stringify({
type: 'object',
properties: {
orderId: { type: 'string' },
customerId: { type: 'string' },
amount: { type: 'number' },
status: { type: 'string' } // New optional field - compatible!
},
required: ['orderId', 'customerId', 'amount']
});
const compatibility = await client.checkSchemaCompatibility(
updatedSchema,
{
schemaName: schemaName,
dataFormat: 'json'
}
);
console.log(`Compatible: ${compatibility.isCompatible ? 'Yes ✓' : 'No ✗'}\n`);
// 5. Register the new version
if (compatibility.isCompatible) {
const newVersion = await client.registerSchemaVersion(
schemaName,
updatedSchema
);
console.log(`New version registered: v${newVersion.versionNumber}\n`);
}
// 6. List all versions
const versions = await client.listSchemaVersions(schemaName);
console.log(`Total versions: ${versions.length}`);
versions.forEach(v => {
console.log(` - Version ${v.versionNumber} (${v.schemaVersionId})`);
});
console.log();
} catch (error) {
console.error('Error:', error.message);
} finally {
await client.dispose();
}
}
simpleSchemaExample().catch(console.error);