We also solved the problem using https://www.npmjs.com/package/@ovotech/avro-decimal
Here are the relevant parts of the code:
const {AvroDecimal} = require('@ovotech/avro-decimal');
const {Decimal} = require('decimal.js');
const registry = new SchemaRegistry({host: 'http://localhost:8201'}, {forSchemaOptions: {logicalTypes: {decimal: AvroDecimal}}})
I asume you use @kafkajs/confluent-schema-registry. As you can see the SchemaRegistry constructor has an optional parameter which lets you add custom serializers and deserializers (AvroDecimal will be our custom serializer/deserializer for decimals).
Then when you build your message, using the Decimal constructor as follow
const myObject = {
stringField: 'whatever String'
decimalField: new Decimal('1.0'),
}
Then just send your message as usual
const id = await registry.getLatestSchemaId(REGISTRY_SUBJECT);
const record = await registry.encode(id, myObject);
await producer.connect();
await producer.send({
topic: MY_TOPIC
messages: [{
value: record
}]
}
);