I am trying to connect to a MongoDB database using the new @ryyppy Promises. The earlier solution with a callback, solved earlier by @eWert-Online and @yawaramin doesn’t work when you create a new instance of the MongoClient so you can use it with the new promises.
What needs binding:
const { MongoClient } = require('mongodb')
// or as an es module:
// import { MongoClient } from 'mongodb'
// Connection URL
const url = 'mongodb://localhost:27017'
const client = new MongoClient(url)
// Database Name
const dbName = 'myProject'
async function main() {
// Use connect method to connect to the server
await client.connect()
console.log('Connected successfully to server')
const db = client.db(dbName)
const collection = db.collection('documents')
// the following code examples can be pasted here...
return 'done.'
}
main()
.then(console.log)
.catch(console.error)
.finally(() => client.close())
I started with
type t
@module("mongodb") @new external createMongoClient: string => t = "MongoClient"
let client = createMongoClient("someUri")
That works, but to associate the connect with that client … doesn’t.
@send external connect: t => unit = "connect"
client.connect() // doesn't work
client->connect // doesn't work
What I want to achieve is to be able to connect to a MongoDB database and use connect, db, collection,… commands. If I understand how to bind the connect I can go from there. Help would be greatly appreciated.