0

I have a NodeJS project that uses BigInt for its model's ids. I also have a WebSocket where I sent serialized objects to a client. The problem with BigInt is that it is not serializable like other objects (Boolean, Number, etc). Therefore I use MDN recommendation to define a global handler function the BigInt object.

BigInt.prototype.toJSON = function() { return this.toString()  }

I will use this in the entire app scope from now on.

The question is where do I place this code so it is elegant and respects SOLID principles?

I am looking for a good solution for kind of issue.

Currently it is placed in index.js like this:

import 'dotenv/config';
import cors from 'cors';
import express from 'express';
import logger from './middleware/logger';

const main = async () => {
  // where should I modularise BigInt extension
  BigInt.prototype.toJSON = function () {
    return this.toString();
  };

  app.use(cors());

  app.get('/', (req, res) => {
    res.send('Hello World!');
  });

  app.listen(3000, () => console.log(`Example app listening on port 3000!`));
};

main().catch((err) => {
  logger.error(err);
});
motiondev
  • 41
  • 5
  • I think you can find a good solution [here](https://stackoverflow.com/questions/14329210/where-to-change-objects-prototypes-in-node-js) (the ./utils file solution) – Lykos94 Mar 17 '22 at 15:59
  • If you want to respect the **S**ingle responsibility principle, then you don't place that thing in all `BigInt`s. Instead you give all your models a `toJSON` method; or even pass a custom serialiser into `JSON.stringify` in the places where you use it (and also a custom deserialiser into `JSON.parse` respectively, which might be more important even) – Bergi Mar 17 '22 at 16:11
  • @Lykos94 Great reference, I think that would work for me. Thanks! – motiondev Mar 17 '22 at 17:23

1 Answers1

3

The best practice for this kind of extension is to use the solution for this question.

I've refactored the code like this.

utils.ts

(BigInt.prototype as any).toJSON = function () {
  return this.toString();
};

index.ts

import 'dotenv/config';
import cors from 'cors';
import express from 'express';
import logger from './middleware/logger';
import "utils.ts" // <== this code gets executed, therefore BigInt extenstion is in the scope of project from now on

const main = async () => {
  app.use(cors());

  app.get('/', (req, res) => {
    res.send('Hello World!');
  });

  app.listen(3000, () => console.log(`Example app listening on port 3000!`));
};

main().catch((err) => {
  logger.error(err);
});
motiondev
  • 41
  • 5
  • Don't use `(BigInt.prototype as any)`. Instead, declare the `toJSON` method on the `BigInt` interface. – Bergi Mar 21 '22 at 10:28
  • Could you show an example or reference link? – motiondev Mar 21 '22 at 12:54
  • https://www.typescriptlang.org/docs/handbook/declaration-merging.html - see https://stackoverflow.com/questions/58265985/extending-built-in-types-in-typescript – Bergi Mar 21 '22 at 12:59