Skip to content

Commit b95a5f4

Browse files
authored
[schema registry json] Initialize schema registry json library (Azure#26673)
1 parent a84e331 commit b95a5f4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+4431
-5
lines changed

common/config/rush/pnpm-lock.yaml

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

rush.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,11 @@
685685
"projectFolder": "sdk/schemaregistry/schema-registry-avro",
686686
"versionPolicyName": "client"
687687
},
688+
{
689+
"packageName": "@azure/schema-registry-json",
690+
"projectFolder": "sdk/schemaregistry/schema-registry-json",
691+
"versionPolicyName": "client"
692+
},
688693
{
689694
"packageName": "@azure/service-bus",
690695
"projectFolder": "sdk/servicebus/service-bus",

sdk/schemaregistry/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ We have two client libraries for Azure Schema Registry
44

55
- `@azure/schema-registry` - The Schema Registry client for storing and retrieving schemas. You can find more info over [here](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/schemaregistry/schema-registry).
66
- `@azure/schema-registry-avro` - The Avro-based serializer that leverages Schema Registry. You can find more info over [here](https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/schemaregistry/schema-registry-avro).
7+
- `@azure/schema-registry-json` - The JSON-based serializer that leverages Schema Registry. You can find more info over [here](https://github.com/Azure/azure-sdk-for-js/tree/schemaregistryjson-init/sdk/schemaregistry/schema-registry-json).
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"include": [
3+
"dist-esm/src/**/*.js"
4+
],
5+
"exclude": [
6+
"**/*.d.ts",
7+
"dist-esm/src/generated/*"
8+
],
9+
"exclude-after-remap": false,
10+
"sourceMap": true,
11+
"produce-source-map": true,
12+
"instrument": true,
13+
"all": true
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Release History
2+
3+
## 1.0.0-beta.1 (2023-08-08)
4+
5+
- Initial preview release
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Microsoft
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# Azure Schema Registry Json Serializer client library for JavaScript
2+
3+
Azure Schema Registry is a schema repository service hosted by Azure Event Hubs,
4+
providing schema storage, versioning, and management. This package provides an
5+
Json serializer capable of serializing and deserializing payloads containing
6+
Json-serialized data.
7+
8+
Key links:
9+
10+
- [Source code](https://github.com/Azure/azure-sdk-for-js/tree/schemaregistryjson-init/sdk/schemaregistry/schema-registry-json)
11+
- [Package (npm)](https://www.npmjs.com/package/@azure/schema-registry-json)
12+
- [API Reference Documentation](https://aka.ms/schemaregistryjson-js-api)
13+
- [Samples](https://github.com/Azure/azure-sdk-for-js/tree/schemaregistryjson-init/sdk/schemaregistry/schema-registry-json/samples)
14+
15+
## Getting started
16+
17+
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
18+
19+
### Prerequisites
20+
21+
- An [Azure subscription][azure_sub]
22+
- An existing [Schema Registry resource](https://aka.ms/schemaregistry)
23+
24+
### Install the `@azure/schema-registry-json` package
25+
26+
Install the Azure Text Analytics client library for JavaScript with `npm`:
27+
28+
```bash
29+
npm install @azure/schema-registry-json
30+
```
31+
32+
## Key concepts
33+
34+
### JsonSerializer
35+
36+
Provides API to serialize to and deserialize from JSON wrapped in a message
37+
with a content type field containing the schema ID. Uses
38+
`SchemaRegistryClient` from the [@azure/schema-registry](https://www.npmjs.com/package/@azure/schema-registry) package
39+
to get schema IDs from schema definition or vice versa. The provided API has internal cache to avoid calling the schema registry service when possible.
40+
41+
### Messages
42+
43+
By default, the serializer will create messages structured as follows:
44+
45+
- `data`: a byte array containing JSON data.
46+
47+
- `contentType`: a string of the following format `application/json+<Schema ID>` where
48+
the `application/json` part signals that this message has a Json-serialized payload
49+
and the `<Schema Id>` part is the Schema ID the Schema Registry service assigned
50+
to the schema used to serialize this payload.
51+
52+
Not all messaging services are supporting the same message structure. To enable
53+
integration with such services, the serializer can act on custom message structures
54+
by setting the `messageAdapter` option in the constructor with a corresponding
55+
message producer and consumer. Azure messaging client libraries export default
56+
adapters for their message types.
57+
58+
## Examples
59+
60+
### Serialize and deserialize an `@azure/event-hubs`'s `EventData`
61+
62+
```javascript
63+
const { DefaultAzureCredential } = require("@azure/identity");
64+
const { createEventDataAdapter } = require("@azure/event-hubs");
65+
const { SchemaRegistryClient } = require("@azure/schema-registry");
66+
const { JsonSerializer } = require("@azure/schema-registry-json");
67+
68+
const client = new SchemaRegistryClient(
69+
"<fully qualified namespace>",
70+
new DefaultAzureCredential()
71+
);
72+
const serializer = new JsonSerializer(client, {
73+
groupName: "<group>",
74+
messageAdapter: createEventDataAdapter(),
75+
});
76+
77+
// Example Json schema
78+
const schema = JSON.stringify({
79+
$schema: "http://json-schema.org/draft-04/schema#",
80+
$id: "person",
81+
title: "Student",
82+
description: "A student in the class",
83+
type: "object",
84+
properties: {
85+
name: {
86+
type: "string",
87+
description: "The name of the student",
88+
},
89+
},
90+
required: ["name"]
91+
});
92+
93+
// Example value that matches the Json schema above
94+
const value = { name: "Bob" };
95+
96+
// Serialize value to a message
97+
const message = await serializer.serialize(value, schema);
98+
99+
// Deserialize a message to value
100+
const deserializedValue = await serializer.deserialize(message);
101+
```
102+
103+
The serializer doesn't check whether the deserialized value matches the schema
104+
but provides an option to implement such validation. The application can pass a
105+
validation callback function as one of the options to the deserialize method where schema validation can be implemented.
106+
To see how the validation might be implemented, please checkout the [`schemaRegistryJsonWithValidation`](https://github.com/Azure/azure-sdk-for-js/tree/schemaregistryjson-init/sdk/schemaregistry/schema-registry-json/samples-dev/schemaRegistryJsonWithValidation.ts)
107+
sample.
108+
109+
## Troubleshooting
110+
111+
The Json serializer communicates with the [Schema Registry][schema_registry] service as needed to register or query schemas and those service calls could throw a [RestError][resterror]. Furthermore, errors of type `Error` will be thrown when serialization or deserialization fails. The `cause` property will contain the underlying error that was thrown from the JSON parser.
112+
113+
### Logging
114+
115+
Enabling logging may help uncover useful information about failures. In order to
116+
see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment
117+
variable to `info`. Alternatively, logging can be enabled at runtime by calling
118+
`setLogLevel` in the `@azure/logger`:
119+
120+
```javascript
121+
const { setLogLevel } = require("@azure/logger");
122+
123+
setLogLevel("info");
124+
```
125+
126+
## Next steps
127+
128+
Please take a look at the
129+
[samples](https://github.com/Azure/azure-sdk-for-js/tree/schemaregistryjson-init/sdk/schemaregistry/schema-registry-json/samples)
130+
directory for detailed examples on how to use this library.
131+
132+
## Contributing
133+
134+
This project welcomes contributions and suggestions. Most contributions require
135+
you to agree to a Contributor License Agreement (CLA) declaring that you have
136+
the right to, and actually do, grant us the rights to use your contribution. For
137+
details, visit https://cla.microsoft.com.
138+
139+
When you submit a pull request, a CLA-bot will automatically determine whether
140+
you need to provide a CLA and decorate the PR appropriately (e.g., label,
141+
comment). Simply follow the instructions provided by the bot. You will only need
142+
to do this once across all repos using our CLA.
143+
144+
This project has adopted the [Microsoft Open Source Code of
145+
Conduct](https://opensource.microsoft.com/codeofconduct/). For more information
146+
see the [Code of Conduct
147+
FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact
148+
[opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional
149+
questions or comments.
150+
151+
If you'd like to contribute to this library, please read the [contributing
152+
guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to
153+
learn more about how to build and test the code.
154+
155+
## Related projects
156+
157+
- [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)
158+
159+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fschemaregistry%2Fschema-registry-json%2FREADME.png)
160+
161+
[azure_cli]: https://docs.microsoft.com/cli/azure
162+
[azure_sub]: https://azure.microsoft.com/free/
163+
[azure_portal]: https://portal.azure.com
164+
[azure_identity]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity
165+
[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential
166+
[resterror]: https://docs.microsoft.com/javascript/api/@azure/core-rest-pipeline/resterror?view=azure-node-latest
167+
[schema_registry]: https://docs.microsoft.com/javascript/api/overview/azure/schema-registry-readme?view=azure-node-latest

0 commit comments

Comments
 (0)