Using big.js for Decimal128

I’m exploring using Decimal128 with Mongo and I saw that it is supported with the mongo-decimal package which is great. The package relies on decimal.js which is kind of large and will be shipped in the client bundle. I’d like to swap decimal.js for big.js. Does anyone know if this is possible?

I’ve tried a number of things including creating a local mongo-decimal package and forking big.js to use a Decimal constructor instead of Big (it looks like the mongo-driver specifically looks for an instanceof Decimal) but didn’t have success. When I started up I got an error like Decimal is not found in mongo-driver.js

@zebralucky I think you might have the most knowledge here :slight_smile:. Appreciate any insights you might have and thanks for making the mongo-decimal package!

1 Like

Just like any other large library, you can load it async from a CDN, on the fly, when you need it.

example code:

const loadScript = (scriptUrl, id, integrity) => {
  const script = document.createElement('script')
  script.src = scriptUrl
  script.type = 'text/javascript'
  // script.crossOrigin = 'anonymous'
  if (id) { script.id = id }
  // script.integrity = integritySet[integrity]
  document.body.appendChild(script)

  return new Promise(resolve => {
    script.onload = resolve
    script.onerror = () => console.log('rejected')
  })
}

1 Like

Thanks for this snippet. Looks pretty handy. Unfortunately, I can’t use it for this scenario but will take advantage of it elsewhere.

Update: I was able to get things working by creating a local mongo-decimal package. Not sure why it didn’t work on the first attempt. Maybe I missed exporting Decimal so that it was available to the mongo-driver.