Skip to content

mohammadanang/core-api

Repository files navigation

API INTI

API core for Node.js using express framework and mongodb NoSQL database.

Using for:

  • Nodejs
  • Expressjs
  • MongoDB
  • Mongoose

List of Api parent class:

  • this.list(q, population)
  • this.show(q, population)
  • this.create(input)
  • this.update(q, input, opts)
  • this.delete(q, opts)
  • this.softDelete(q, opts)
  • this.paginate(q, population, opts)

List of Controller parent class:

  • this.success({code, msg, data, meta})
  • this.error({code, msg})

How to use:

Import in base project file to connect with database. Example:

  • root file
...
require("api-inti").connection(process.env.MONGODB_HOST)
...

Import as parent class or function. For example:

  • actions file
const { Api } = require("api-inti")
const TestModel = require("models/test.model.js")

class Test extends Api {
    constructor() {
        super(TestModel)
    }

    async exec(params, populate) {
        try {
            return await this.list(params, populate)
        } catch(err) {
            throw err
        }
    }
}

module.exports = new Test()
  • controllers file
const { Controller } = require("api-inti")
const TestAction = require("actions/test.action.js")

class TestController extends Controller {
    constructor() {
        super()
    }

    async index(req, res) {
        try {
            let params = {}
            let data = await TestAction.exec(params)

            return res.send(
                this.success({
                    message: "Get test list",
                    data
                })
            )
        } catch(err) {
            return res.send(
                this.error({
                    error: err.message
                })
            )
        }
    }

    ...
}

module.exports = new TestController()

About

API core for class function using express and mongodb

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published