Github-package for asynchronously connecting to your Mongo database
A simple wrapper for instantiating an async connection to Mongo in your express app.
Looking through the MongoDB documentation I felt at odds following the callback style when implementing it in an already async-style app. So here is a way to serve a mongo connection in a small class that reduces reduce boilerplate. It operates in the same way, exposing the db methods - but is now injectable into your killer app 🔥
Ensure your .npmrc
has @seedboot
included as a scoped registry and then...
$ npm i @seedboot/mongo-connector
🤘
This is one severely concatenated and contrived example, but it gives you a flavour...
import { mongoConnect } from '@seedboot/mongo-connect';
import app from './app.ts';
// ... other imports
(async (): Promise<void> => {
// ...
const mongo: MongoConnect = await new MongoConnect({
uri: URI,
databaseName: 'test',
}).connectToDatabase();
const application = await app({ database: mongo });
// ...
})();
// imports etc.
interface App {
database: MongoConnect;
}
export default async ({ database }: App): Promise<express.Application> => {
const app: express.Application = express();
app.use(
routes(
methods(database.db.collection(TYPES.collectionName))
)
);
return app;
}
export default (db: Methods): express.Router => express.Router()
.post(
'/',
async (req, res, next) => {
const { ops: [data] } = await db.createOne();
res.locals.user = data;
next();
},
(req, res) => {
res.send({
user: res.locals.user
})
}
)
Voila! No more callbacks!
- Write tests
- Add injectable error handling/ logger