@tonoid/helpers

0.1.2 • Public • Published

@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

Dependencies (0)

    Dev Dependencies (3)

    Package Sidebar

    Install

    npm i @tonoid/helpers

    Weekly Downloads

    3

    Version

    0.1.2

    License

    MIT

    Unpacked Size

    6.91 kB

    Total Files

    6

    Last publish

    Collaborators

    • melalj