|
| 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 | + |
| 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