Nature's Particle Manager

    @s7x/base-helpers

    1.3.22 • Public • Published

    Fast, unopinionated, minimalist web framework for node.

    NPM Version NPM Downloads Linux Build Windows Build Test Coverage

    const CrudContollerExpress = require('@s7x/base-helpers');

    Installation

    This is a Node.js module available through the npm registry.

    Before installing, download and install Node.js. Node.js 0.10 or higher is required.

    Installation is done using the npm install command:

    $ npm install @s7x/base-helpers

    Follow our installing guide for more information.

    Features

    • A valid OpenAPI (formerly Swagger) spec (2.0 or 3.0 if you choose 😍) is generated from your controllers and models, including:
      • Paths (e.g. GET /users)
      • Definitions based on joi schemas
      • Parameters/model properties marked as required or optional based on joi schemas (e.g. joi.required() string is required in the OpenAPI spec)
      • jsDoc supported for object descriptions (most other metadata can be inferred from joi types)
    • Routes are generated for middleware of choice
      • Express, Hapi, and Koa currently supported, other middleware can be supported using a simple handlebars template
      • Validate request payloads
    • Inter microservice communication
      • NATS.io is used for bidirectional requests.
      • Supports KAFKA for unidirectional message streaming
    • Support for Automatic crud api generation with Mongodb

    Implementation

    Create database model

    const mongoose = require('mongoose');
    const schema = new mongoose.Schema(
        {
            name: { type: String },
            description: { type: String },
            isDeleted: { type: Boolean, default: false }
        },
        { timestamps: true }
    )
    
    module.exports = mongoose.model('Company', schema);

    Create module

    config.js

    const { MongoLookup, BaseConfigMongo } = require("@s7x/base-helpers/lib/types");
    
    module.exports = new BaseConfigMongo({
        findExact: [ 'name', '_id' ],
        searchFields: ['name', 'description'],
        defaultMatch: { isDeleted: false },
        softDeleteKey: 'isDeleted',
        mapping: {},
        uniqueKeys: ['name'],
        lookups: [
            new MongoLookup({
                name: 'branches',
                from: 'branches',
                as: 'branchesData',
                localField: '_id',
                foreignField: 'branchId',
                multi: true
            }),
        ],
        defaultSortDirection: -1,
        defaultSortKey: 'updatedAt',
    });

    index.js

    const { BaseMongo } = require("@s7x/base-helpers/lib/modules");
    const config = require("./config");
    const  Company = require('../../models/mongo/company');
    
    class Module extends BaseMongo {
        constructor() {
            super(Company, config);
        }
    }
    
    module.exports = new Module();

    Add Controller

    const mainModule = require('../../modules/company');
    const CrudContollerExpress = require('@s7x/base-helpers/lib/modules/crud-controller-express');
    
    class Controller extends CrudContollerExpress {
        constructor() {
            super(mainModule);
        }
    }
    
    module.exports = new Controller();

    Add Api definitions

    const controller = require("./controller");
    const CrudApi = require("@s7x/base-helpers/lib/modules/crud-api");
    const joi = require("joi");
    
    let crudApi = new CrudApi(controller)
    let apiPath = 'companies';
    let schema = {
        name: joi.string().required(),
        description: joi.string().allow('').optional(),
    };
    let routes = [
        crudApi.getCreateApi(schema),
        crudApi.getFindAllApi({
            ...validation.paginator
        }),
        crudApi.getFindByIdApi(),
        crudApi.getUpdateByIdApi(schema),
        crudApi.getreplaceByIdApi(schema),
        crudApi.getDeleteByIdApi({}, true)
    ]
    
    module.exports = { basePath: `/${apiPath}`, routes }

    Install

    npm i @s7x/base-helpers

    DownloadsWeekly Downloads

    2

    Version

    1.3.22

    License

    ISC

    Unpacked Size

    55.4 kB

    Total Files

    22

    Last publish

    Collaborators

    • shekhar7x