Ideas for synchronous initialisation
I love Massive but there's one aspect that I find hard to deal with is the fact that initialisation is asynchronous.
pg-promise I'm used to having a file in which I create the database connection then export the connection object. I can then import that file as
db in the rest of my app and I don't have to think about it. With
massive I can't do that because the result of the connection is a promise that must be awaited before I can call any functions on it. Even though the app will run for days or weeks, every call to the database must await a promise which was resolved in the first few milliseconds after startup.
Of course, the connecting to the database is always asynchronous but
node-pg hides this fact by buffering any queries that come is before the connection is established. Massive can't currently do this because it doesn't know its own API until introspection has completed.
Unless we use a proxy object to capture all method calls on the massive object and then either run the query or throw a type error once introspection has finished.
Proxies are supported in Node >6.4
Another possibility would be to compute the result of introspection at build time and store it in a file. This could then be read synchronously on startup.
Or the promise returned on connection could be extended with the results of the introspection to become the Massive object. This would mean any queries made before introspection completes would fail but in practice these are known in advance so can be accounted for by awaiting the promise. I actually tried this approach on a previous project that used Massive and it works. But it does have some edge cases around reconnection when running tests.
Or perhaps I'm missing something which means this isn't even a problem?