@tonoid/helpers
v0.1.2
Published
Helper library to init modules (logger, mongo, redis, bull, express...) to quickstart your backend
Downloads
7
Maintainers
Readme
@tonoid/helpers
Quickstart your backend project with helpers all abstract boilerplate code.
Available helpers
- Express: @tonoid/express • HTTP server
- Mongo: @tonoid/mongo • MongoDb client
- Redis: @tonoid/redis • Redis client
- Bull: @tonoid/bull • Bull client to run tasks in the background
- Logger: @tonoid/logger • Better way to handle your logs with winston
Full example
A full usage example is available on the folder example
on this repo
Example with express and mongo
Index file
const { init } = require('@tonoid/helpers');
const mongo = require('@tonoid/mongo');
const express = require('@tonoid/express');
const logger = require('@tonoid/logger');
const apiEndpoint = require('./api');
init([
mongo(),
express({
port: 3000,
jsonLog: false,
endpoints: [
{ path: '/api', handler: apiEndpoint },
]
}),
], {
logger,
loggerOptions: { colorize: true, json: false }
});
Api file
const ctx = require('@tonoid/helpers').context;
module.exports = ({ getRouter, asyncHandler }) => {
const router = getRouter();
// GET /api
router.get('/', asyncHandler(async (req, res) => {
const mongoDb = ctx.mongo.db();
const filters = {};
if (req.query.category) filters.category = req.query.category;
const products = await mongoDb.collection('products').find(filters).toArray();
res.send(products);
}));
return router;
};
Credits
This module is maintained by Simo Elalj @tonoid