skip to Main Content

I’ve already checked the other answers for this scenario on SO,but unfortunately non of them seem to be working for me.

I’m using the following dependencies:
Mongoose: ^5.11.97
Typescript: ^4.7.4

I have a transactions collection which holds the trxValue property whose values are in decimal format. I came across the new Decimal128 type introduced in Mongoose and I tried implementing the same in the following way:


// ITransaction.ts interface file
import { Types } from 'mongoose';

export default interface ITransaction {
  trxNo: string;
  trxType: 'Credit' | 'Debit';
  trxDate: Date;
  trxDesc: string;
  trxValue: string;
  cutomerId: Types.ObjectId;
  accountId: Types.ObjectId;
}
// Transactions.ts model file
import { model, Schema } from 'mongoose';
import ITransaction from '../interfaces/ITransaction';

const trxSchema = new Schema<ITransaction>({
  trxNo: { type: String, required: true },
  trxType: { type: String, required: true },
  trxDate: { type: Date, default: Date.now },
  trxDesc: { type: String, required: true },
  trxValue: {
    type: Schema.Types.Decimal128,
    required: true,
    get: (v: Schema.Types.Decimal128): string => (+v.toString()).toFixed(4),
  },
  cutomerId: { type: Schema.Types.ObjectId, required: true },
  accountId: { type: Schema.Types.ObjectId, required: true },
});

const Transaction = model<ITransaction>('Transaction', trxSchema);
export default Transaction;

The problem is the data types Decimal128 don’t seem to match with typescript primitive data types. I keep getting the following warning at compile time. Can somebody please give me an detailed example on how to store and fetch decimal values in MongoDB using Mongoose + Typescript with precision of 4 digits after the decimal?

Type '{ type: typeof Schema.Types.Decimal128; required: true; get: (v: Schema.Types.Decimal128) => string; }' is not assignable to type 'SchemaDefinitionProperty<string> | undefined'.
  Types of property 'type' are incompatible.
    Type 'typeof Decimal128' is not assignable to type 'typeof Mixed | StringSchemaDefinition | undefined'.
      Type 'typeof Decimal128' is not assignable to type 'typeof Mixed'.
        Types of property 'schemaName' are incompatible.
          Type '"Decimal128"' is not assignable to type '"Mixed"'.ts(2322)

2

Answers


  1. Chosen as BEST ANSWER

    The configuration that seems to be working for me is as follows:

    import { Types, model, Schema } from 'mongoose';
    
    interface ITransaction {
      trxNo: string;
      trxType: 'Credit' | 'Debit';
      trxDate: Date;
      trxDesc: string;
      trxValue: Types.Decimal128;
      customerId: Types.ObjectId;
      accountId: Types.ObjectId;
    }
    
    const trxSchema = new Schema<ITransaction>({
      trxNo: { type: String, required: true },
      trxType: { type: String, required: true },
      trxDate: { type: Date, default: Date.now },
      trxDesc: { type: String, required: true },
      trxValue: {
        default: 0,
        required: true,
        type: Schema.Types.Decimal128,
      },
      customerId: { type: Schema.Types.ObjectId },
      accountId: { type: Schema.Types.ObjectId },
    });
    
    const Transaction = model<ITransaction>('Transaction', trxSchema);
    
    const trxOne = new Transaction({
      customerId: new Types.ObjectId(),
      accountId: new Types.ObjectId(),
      trxDate: new Date(),
      trxDesc: 'Description here',
      trxNo: 'avaksiw8877411',
      trxValue: '100.12258',
      trxType: 'Credit',
    });
    
    console.info(trxOne.toJSON().customerId);
    console.info(trxOne.toJSON().accountId);
    console.info(trxOne.toJSON().trxDate);
    console.info(trxOne.toJSON().trxDesc);
    console.info(trxOne.toJSON().trxNo);
    console.info(trxOne.toJSON().trxValue.toString());
    console.info(trxOne.toJSON().trxType);
    

    Demo


  2. Workaround

    import { Schema } from 'mongoose';
    
    value: new Schema.Types.Decimal128("0.1233"),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search