schema-registry-serdes

1.1.1 • Public • Published

schema-registry-serdes

Serdes for Confluent Schema Registry, compatible with Confluent Cloud.

Features

  • Encode messages using avro schemas
  • Decode messages with same avro schema used in the encode procedure
  • Automatic pushes schema to Confluent schema registry
  • Automatic fetches schema from Confluent schema registry
  • Implements schema cache
  • Authentication with Confluent Cloud

Installing

npm install schema-registry-serdes

Example

const SchemaRegistry = require("schema-registry-serdes");
const { encodeMessage, decodeMessage } = SchemaRegistry("https://localhost.com:8081");
const schema = {type: "string"};
const message = "testing";
const encoded = await encodeMessage("topic", schema, message);
console.log(encoded);   // <Buffer(13) 0 0 0 0 1 14 116 101 115 116 105 110 103>
const decoded = await decodeMessage(encoded);
console.log(decoded);   // testing

API Doc

This library exports a single function that receives two parameters:

  • Schema registry URL
  • Configuration settings
SchemaRegistry(url, config)
SchemaRegistry(
  "https://www.host.com", 
  { 
    key: "provided key", 
    secret: "provided secret", 
    validateMessages: false
  }
);

It returns an object with 4 instance methods:

  • encodeMessage
  • encodeById
  • encodedKey
  • decodeMessage

Config

The config object is used to specify additional behavior, for instance authentication and message validation before even trying to encode the message.

SchemaRegistry(
  "https://www.host.com", 
  { 
    key: "provided key",        // key or username
    secret: "provided secret",  // secret or password  
    validateMessages: true      // Flag for validation before encoding
  }
);
NOTE

Validating messages before encoding is an resource consuming task and is not recommended in production environments

Instance Methods

encodeMessage

Takes 4 parameters:

  • topic: Topic that will be associated with the schema. Schema will have the subject ${topic}-value
  • schema: is an avro schema
  • message: message to be encoded
  • options: parsing options used by avsc, this is an optional parameter
const encoded = await encodeMessage("topic", schema, message);
encodeById

Takes 3 parameters:

  • schemaId: schema id in Confluent Schema Registry
  • message: message to be encoded
  • options: parsing options used by avsc, this is an optional parameter
const encoded = await encodeById(1, message);
encodeKey

Takes 4 parameters:

  • topic: Topic that will be associated with the schema. Schema will have the subject ${topic}-key
  • schema: is an avro schema
  • message: message to be encoded
  • options: parsing options used by avsc, this is an optional parameter
const encoded = await encodeKey("topic", schema, message);
decodeMessage
  • message: encoded message to be decoded (buffer)
  • options: parsing options used by avsc, this is an optional parameter
const decoded = await decodeMessage(encodedMessage);

Package Sidebar

Install

npm i schema-registry-serdes

Weekly Downloads

40

Version

1.1.1

License

MIT

Unpacked Size

37.4 kB

Total Files

19

Last publish

Collaborators

  • lroca88