@tonoid/helpers

Helper library to init modules (logger, mongo, redis, bull, express...) to quickstart your backend

Usage no npm install needed!

<script type="module">
  import tonoidHelpers from 'https://cdn.skypack.dev/@tonoid/helpers';
</script>

README

@tonoid/helpers

npm npm npm GitHub stars

Quickstart your backend project with helpers all abstract boilerplate code.

Available helpers

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