How use mongodb module inside a js entrypoint file of a docker container

48 views Asked by At

I want to insert a document (with fields that need to be explicitly encrypted) in the collection when initializing the database inside the js entrypoint file. I am successfully creating collections using:

const rootUsername = process.env.MONGO_INITDB_ROOT_USERNAME
const rootPassword = process.env.MONGO_INITDB_ROOT_PASSWORD
const connection = connect(`mongodb://${rootUsername}.${rootPassword}@localhost:27017/admin`)
const db = connection.getSiblingDB(process.env.MONGO_INITDB_DATABASE)

// Creating collections

But when I try to use the "MongoClient" class of the "mongodb" module:

const encryptedClient = new MongoClient(uri, {autoEncryption: autoEncryptionOptions})

I get the following error, which is quite logical: ReferenceError: MongoClient is not defined

When trying to require the module:

const MongoClient = require('mongodb').MongoClient

Then I get the error: Error: Cannot find module 'mongodb'

I need the "mongodb" and "mongodb-client-encryption" modules to get a encryption client to encrypt the document fields.

I did not find documentation on using the JS entrypoint file.

0

There are 0 answers