Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Latest commit

 

History

History

knack-sr

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

knack-sr

A module for interacting with the Kafka Schema Registry.

Usage

first things first...

$ npm i @optum/knack-sr

options

  • domain: [String] (default: localhost)
  • schema: [String] (default: http)
  • url: [String] overrides all other options

Examples

read schema by id

const KnackSr = require('@optum/knack-sr');

const options = {
    url: 'http://localhost:8081'
};

const schemaId = 123;

const sr = new KnackSr(options);

const schemaResponse = await sr.getSchemaById(schemaId);

console.log(schemaResponse);

read schema by subject

const KnackSr = require('@optum/knack-sr');

const options = {
    url: 'http://localhost:8081'
};

const topic = 'knack-test-topic-v1';

const sr = new KnackSr(options);

const keySubject = `${topic}-${key}`;
const keySchemaResponse = await sr.getSchemaBySubject(keySubject);

const valueSubject = `${topic}-${value}`;
const valueSchemaResponse = await sr.getSchemaBySubject(valueSubject);

console.log(keySchemaResponse);
console.log(valueSchemaResponse);

register schema

const KnackSr = require('@optum/knack-sr');

const topic = 'knack-test-topic-v1';

// can be string or json
const avroSchema = {
    "type": "record",
    "name": "messageInfo",
    "namespace": "io.knack.schemas.avro",
    "fields": [
     {
        "name": "content",
        "type": "string"
    },
    {
        "name": "channel",
        "type": ["null", "string"],
        "default": null
    }
  ]
};

// example of using default options
const sr = new KnackSr();

const valueSubject = `${topic}-${value}`;
const registration = await sr.registerSchema({
    schema: avroSchema,
    subject: valueSubject
});