castle
castle copied to clipboard
`avro-decimal` does not work with latest version of `decimal.js`
I've been trying to make example code (https://github.com/ovotech/castle/blob/main/packages/avro-decimal/examples/simple.ts) working and it was throwing an error.
After looking into it it seem that avro-decimal
does not work with versions of decimal.js
bigger than 10.2.1
. Seems like they've changed something in exports and the value no longer recognized as instanceof Decimal
.
Downgrading it fixes the issue.
Examples:
- Install
[email protected]
- Run following code:
import avro from "avsc";
import { AvroDecimal } from "@ovotech/avro-decimal";
import decimal from "decimal.js";
const { Decimal } = decimal;
const decimalSchema = {
type: "bytes",
logicalType: "decimal",
precision: 16,
scale: 8,
};
export const DecimalType = avro.Type.forSchema(decimalSchema, {
logicalTypes: { decimal: AvroDecimal },
});
const encoded = DecimalType.toBuffer(new Decimal("100.01"));
const decoded = DecimalType.fromBuffer(encoded);
console.log("decoded", decoded); // output: decoded 100.01
- Install
[email protected]
- Run following code (the only difference is import of
decimal.js
):
import avro from "avsc";
import { AvroDecimal } from "@ovotech/avro-decimal";
import { Decimal } from "decimal.js";
const decimalSchema = {
type: "bytes",
logicalType: "decimal",
precision: 16,
scale: 8,
};
export const DecimalType = avro.Type.forSchema(decimalSchema, {
logicalTypes: { decimal: AvroDecimal },
});
const encoded = DecimalType.toBuffer(new Decimal("100.01"));
const decoded = DecimalType.fromBuffer(encoded);
console.log("decoded", decoded);
That throws this error:
/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/@[email protected][email protected]/node_modules/@ovotech/avro-decimal/dist/index.js:63
throw new Error('expecting Decimal type');
^
Error: expecting Decimal type
at AvroDecimal._toValue (/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/@[email protected][email protected]/node_modules/@ovotech/avro-decimal/dist/index.js:63:20)
at LogicalType._write (/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/[email protected]/node_modules/avsc/lib/types.js:2627:40)
at Type.toBuffer (/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/[email protected]/node_modules/avsc/lib/types.js:658:8)
at file:///Users/tim.shilov/IdeaProjects/affiliate-etl/apps/etl-engine/src/decimal-test.js:16:29
at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
at async loadESM (node:internal/process/esm_loader:28:7)
at async handleMainPromise (node:internal/modules/run_main:113:12)
Node.js v20.12.1