1.3.22 • Published 4 years ago

@s7x/base-helpers v1.3.22

Weekly downloads
5
License
ISC
Repository
github
Last release
4 years ago

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 }
1.3.19

4 years ago

1.3.20

4 years ago

1.3.21

4 years ago

1.3.22

4 years ago

1.3.18

4 years ago

1.3.17

4 years ago

1.3.16

4 years ago

1.3.14

4 years ago

1.3.15

4 years ago

1.3.10

4 years ago

1.3.13

4 years ago

1.3.11

4 years ago

1.3.12

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.25

4 years ago

1.2.24

4 years ago

1.2.23

4 years ago

1.2.22

4 years ago

1.2.20

4 years ago

1.2.21

4 years ago

1.2.18

4 years ago

1.2.19

4 years ago

1.2.17

4 years ago

1.2.16

4 years ago

1.2.15

4 years ago

1.2.14

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.1

4 years ago

1.2.9

4 years ago

1.2.12

4 years ago

1.2.13

4 years ago

1.2.10

4 years ago

1.2.11

4 years ago

1.1.23

4 years ago

1.1.24

4 years ago

1.1.16

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.21

4 years ago

1.1.11

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.10

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.1.2

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago