bencebalogh / avro-schema-registry

Confluent Schema Registry implementation in javascript to easily serialize and deserialize kafka messages
MIT License
28 stars 30 forks source link

Is it possible to decode a message with a different schema? #35

Closed lizas2110 closed 4 years ago

lizas2110 commented 4 years ago

Hey, when using decode/decodeMessage function, is it possible to pass a schema and decode the message by the given schema and not only by the schema witch attached to the message?

bencebalogh commented 4 years ago

Hi! Since avsc is a peer dependency you'll have that installed already and you can do that (code mostly copied from the README from there):

const avro = require('avsc');

const type = avro.Type.forSchema({
  type: 'record',
  fields: [
    {name: 'kind', type: {type: 'enum', symbols: ['CAT', 'DOG']}},
    {name: 'name', type: 'string'}
  ]
});

const val = type.fromBuffer(buf);

If your message contains the ID in the buffer you need to cut out the first 5 bytes (1 magic byte + 4 id bytes), you can read about that here

Let me know if you need any help!