Skip to content

Commit 73d1b14

Browse files
authored
[Schema Registry] Add getSchemaByVersion (Azure#23162)
* [Schema Registry] Add getSchemaByVersion * added changelog entry * address feedback * add a test
1 parent 1ed6b15 commit 73d1b14

File tree

22 files changed

+666
-100
lines changed

22 files changed

+666
-100
lines changed

sdk/schemaregistry/schema-registry/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Release History
22

3+
## 1.1.0 (Unreleased)
4+
5+
### Features Added
6+
7+
- `getSchemaByVersion` is added to query schemas by their version.
8+
- `version` property is added to `SchemaProperties`.
9+
310
## 1.1.0 (2022-05-10)
411

512
### Features Added

sdk/schemaregistry/schema-registry/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ if (foundSchema) {
118118
}
119119
```
120120

121+
### Get definition of existing schema by version
122+
123+
```javascript
124+
const { DefaultAzureCredential } = require("@azure/identity");
125+
const { SchemaRegistryClient } = require("@azure/schema-registry");
126+
127+
const client = new SchemaRegistryClient("<fullyQualifiedNamespace>", new DefaultAzureCredential());
128+
const foundSchema = await client.getSchemaByVersion({ name:"<schema name>", groupName: "group name", version });
129+
if (foundSchema) {
130+
console.log(`Got schema definition=${foundSchema.definition}`);
131+
}
132+
```
133+
121134
## Troubleshooting
122135

123136
### Logging

sdk/schemaregistry/schema-registry/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"//metadata": {
4343
"constantPaths": [
4444
{
45-
"path": "src/generated/generatedSchemaRegistryClientContext.ts",
45+
"path": "src/generated/generatedSchemaRegistryClient.ts",
4646
"prefix": "packageDetails"
4747
},
4848
{

sdk/schemaregistry/schema-registry/recordings/browsers/schemaregistryclient/recording_gets_schema_by_version.json

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/schemaregistry/schema-registry/recordings/node/schemaregistryclient/recording_gets_schema_by_version.json

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/schemaregistry/schema-registry/review/schema-registry.api.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import { CommonClientOptions } from '@azure/core-client';
88
import { OperationOptions } from '@azure/core-client';
99
import { TokenCredential } from '@azure/core-auth';
1010

11+
// @public
12+
export interface GetSchemaByVersionOptions extends OperationOptions {
13+
}
14+
1115
// @public
1216
export interface GetSchemaOptions extends OperationOptions {
1317
}
@@ -40,6 +44,7 @@ export interface SchemaProperties {
4044
groupName: string;
4145
id: string;
4246
name: string;
47+
version: number;
4348
}
4449

4550
// @public
@@ -54,6 +59,7 @@ export class SchemaRegistryClient implements SchemaRegistry {
5459
constructor(fullyQualifiedNamespace: string, credential: TokenCredential, options?: SchemaRegistryClientOptions);
5560
readonly fullyQualifiedNamespace: string;
5661
getSchema(schemaId: string, options?: GetSchemaOptions): Promise<Schema>;
62+
getSchemaByVersion(schemaVersion: SchemaVersion, options?: GetSchemaByVersionOptions): Promise<Schema>;
5763
getSchemaProperties(schema: SchemaDescription, options?: GetSchemaPropertiesOptions): Promise<SchemaProperties>;
5864
registerSchema(schema: SchemaDescription, options?: RegisterSchemaOptions): Promise<SchemaProperties>;
5965
}
@@ -63,6 +69,13 @@ export interface SchemaRegistryClientOptions extends CommonClientOptions {
6369
apiVersion?: string;
6470
}
6571

72+
// @public
73+
export interface SchemaVersion {
74+
groupName: string;
75+
name: string;
76+
version: number;
77+
}
78+
6679
// (No @packageDocumentation comment for this package)
6780

6881
```
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Demonstrates the use of getSchemaByVersion.
6+
*/
7+
8+
import { DefaultAzureCredential } from "@azure/identity";
9+
import { SchemaRegistryClient, SchemaDescription } from "@azure/schema-registry";
10+
11+
// Load the .env file if it exists
12+
import * as dotenv from "dotenv";
13+
dotenv.config();
14+
15+
// Set these environment variables or edit the following values
16+
const fullyQualifiedNamespace =
17+
process.env["SCHEMA_REGISTRY_ENDPOINT"] || "<fullyQualifiedNamespace>";
18+
const groupName = process.env["SCHEMA_REGISTRY_GROUP"] || "AzureSdkSampleGroup";
19+
20+
// Sample Avro Schema for user with first and last names
21+
const schemaObject = {
22+
type: "record",
23+
name: "User",
24+
namespace: "com.azure.schemaregistry.samples",
25+
fields: [
26+
{
27+
name: "firstName",
28+
type: "string",
29+
},
30+
{
31+
name: "lastName",
32+
type: "string",
33+
},
34+
],
35+
};
36+
37+
const name = `${schemaObject.namespace}-${schemaObject.name}`;
38+
39+
// Description of the schema for registration
40+
const schemaDescription: SchemaDescription = {
41+
name,
42+
groupName,
43+
format: "Avro",
44+
definition: JSON.stringify(schemaObject),
45+
};
46+
47+
export async function main() {
48+
// Create a new client
49+
const client = new SchemaRegistryClient(fullyQualifiedNamespace, new DefaultAzureCredential());
50+
51+
// Register a schema and get back its ID and version.
52+
const { id, version } = await client.registerSchema(schemaDescription);
53+
console.log(
54+
`Registered schema with the following properties:\n- ID=${id}\n- Version: ${version}`
55+
);
56+
57+
// Get definition of existing schema by its version
58+
const foundSchema = await client.getSchemaByVersion({
59+
groupName,
60+
name,
61+
version,
62+
});
63+
if (foundSchema) {
64+
console.log(`Got schema definition=${foundSchema.definition}`);
65+
}
66+
}
67+
68+
main().catch((err) => {
69+
console.error("The sample encountered an error:", err);
70+
});

0 commit comments

Comments
 (0)