Closed Zolmeister closed 6 years ago
Appreciated, but I have to close it since it isn't really actionable.
To clarify, I'm proposing the default massive
promise behave like this proxy object
Sell me on it, then. What's the advantage of lazy init over just taking the introspection hit at startup? What makes it worth the extra complexity?
It's side-effect free Otherwise you have to explicitly mutate a global instance or pay the resolution cost on each usage
// this everywhere
db.then (db) -> ...
// or
// pg.js
module.exports = {db: null}
// setup.js
massive().then(db => pg.db = db) // side-effect
ah. With ES2017 you can db = await massive(...);
which more or less obviates the issue for Node 7+. That leaves 6, and it's a lot less compelling to add and support new features that only matter for the oldest version Massive supports, especially when co exists.
And you can also explicitly create and connect new instance without the need to await it everywhere:
const Database = require('massive').Database
const db = new Database(/* args here */)
await db.reload() // I think...
I've been using the following wrapper that others may find useful. Basically it allows immediate usage of the
massive
promise as if it were resolved, and only when a function is called does it actually connect and resolve the promise (the first time)