# manjar

> Manjar is an MVC Framework like Rails for NodeJs on top of HapiJs and Mongoose.

Latest version **0.5.0** (published 2020-01-16) · ISC license · 0 weekly downloads

## Install

```sh
npm install manjar
pnpm add manjar
yarn add manjar
bun add manjar
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2020-01-16 |
| First published | 2018-10-24 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 34.3 KB |
| Known vulnerabilities | 0 (+10 in 3 direct dependencies) |
| Install scripts | no |
| Maintainers | bysouleater |
| Keywords | Manjar, MVC, Framework, Hapijs, Mongoose, REST, API |

## Links

- npm: https://www.npmjs.com/package/manjar
- Repository: https://gitlab.com/bysouleater/manjar
- Homepage: https://gitlab.com/bysouleater/manjar#readme
- Issues: https://gitlab.com/bysouleater/manjar/issues
- npm.io page: https://npm.io/package/manjar

## Dependencies (5)

- [hapi](https://npm.io/package/hapi.md) ^18.1.0
- [fs-extra](https://npm.io/package/fs-extra.md) ^8.1.0
- [joigoose](https://npm.io/package/joigoose.md) ^6.2.0
- [mongoose](https://npm.io/package/mongoose.md) ^5.8.7
- [@hapi/joi](https://npm.io/package/@hapi/joi.md) ^16.1.8

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 0.5.0 (latest) — 2020-01-16
- 0.4.0 — 2020-01-15
- 0.3.1 — 2020-01-15
- 0.3.0 — 2018-11-28
- 0.2.0 — 2018-11-06
- 0.1.0 — 2018-11-02
- 0.0.1 — 2018-10-24

## README

# Manjar

*[Mongo](https://www.mongodb.com/) and [NodeJs](https://nodejs.org/) API REST*. 

Manjar is an MVC Framework like [Rails](https://rubyonrails.org/) for NodeJs on top of [HapiJs](https://hapijs.com/) **v17.6.0** and [Mongoose](https://mongoosejs.com/) **v5.3.6**.

The main objective is to let you create a full REST API writing as little as possible. In order to do that, you need to follow some coding and file structure conventions but with the ability to be overwritten.

# Installing

To use Manjar you need to set up an empty project yourself (for now) and add Manjar as a dependency.

```
npm i -s manjar mongo-in-memory
```

# Setup
Create an `app.js` file:
```
const MongoInMemory = require('mongo-in-memory');
const Manjar = require('manjar');

const MongoServer = new MongoInMemory();

MongoServer.start((error, config) => {
  Manjar.init();
});
```
And add the following script in `package.json`
```
"scripts": {
  "start": "node app.js",
  ...
}
```

Now you should be able to start the application server by:

```
npm start
```

# Database Connection

Currently, Manjar is only capable of connecting to a Mongo DB Instance on `localhost:27017`. Configurations to come soon [MANJAR-3](https://gitlab.com/bysouleater/manjar/issues/3)

# Model Schemas

Manjar lets you specify your model schemas and automatically create a Mongoose Model for each and let them available for use in [Endpoints](#api-endpoints) or [Services](#api-services). 

In order to create your first Model Schema you have to follow this folder structure:

```
api
  schemas
app.js
package.json
```

Inside `api/schemas` folder you can create as many schema files as you like and can also be grouped into several different folders within the main folder.

For `api/schemas/user.js` example model schema file a `User` model will be created in Mongoose. 

```
module.exports = {
  firstName: { type: String, required: true },
  lastName: { type: String, required: true }
};
```

*Model name will be the filename with the first letter capitalized*

*All [Mongoose Schema Definitions](https://mongoosejs.com/docs/guide.html#definition) are valid on this kind of files*


# API Endpoints

Manjar lets you specify your API Endpoints and automatically route requests to each of them.

In order to create your first API Endpoint you have to follow this folder structure:

```
api
  endpoints
app.js
package.json
```

Inside `api/endpoints` folder you can create as many endpoint files as you like and can also be grouped into several different folders within the main folder.

For `api/endpoints/health.js` example API endpoint file a `GET /health` route will be automatically wired to the Hapi Server.

```
module.exports = {
  handler: async (req, h) => {
    return 'ok';
  }
}
```

*Any handler receives the `req` request param and `h` response handler as any normal [Hapi Handler](https://hapijs.com/api#route.options.handler) would and works exactly the same way*

Also, if you want to use any of your models, you can access them from the injected Manjar parameter in all endpoints. An example `api/endpoints/users/user.getAll.js` endpoint file which creates a `GET /users` route demonstrate how you could use the `User` model defined.

```
module.exports = Manjar => {
  const { User } = Manjar.Models;

  return {
    handler: async (req, h) => {
      return await User.find({});
    }
  };
};
```

## Automatic Routes

Manjar has a default behavior that wires any endpoint file defined in `api/endpoints` folder to a constructed `<METHOD> /<PATH>` route.

The following rules are applied when generating the `<METHOD>` and the `<PATH>` values:

* METHOD value is:
  * **GET** for files that end with `.getAll.js`
  * **GET** for files that end with `.get.js`
  * **POST** for files that end with `.post.js`
  * **PUT** for files that end with `.put.js`
  * **PATCH** for files that end with `.patch.js`
  * **DELETE** for files that end with `.delete.js`
* PATH value is:
  * `/model` for files like `api/endpoints/model/model.getAll.js`
  * `/model` for files like `api/endpoints/model/model.post.js`
  * `/model/{id}` for files like `api/endpoints/model/model.get.js`
  * `/model/{id}` for files like `api/endpoints/model/model.put.js`
  * `/model/{id}` for files like `api/endpoints/model/model.patch.js`
  * `/model/{id}` for files like `api/endpoints/model/model.delete.js`
  * `/file` for files like `api/endpoints/file.js`
  * `/folder/file` for files like `api/endpoints/folder/file.js`
  * `/folder/subfolder/file` for files like `api/endpoints/folder/subfolder/file.js`

*If for some reason you are not satisfied with the automatically generated route, you can always override this values in your endpoint file*

```
module.exports = {
  method: 'POST',
  path: '/something-unexpected',
  handler: async (req, h) => {
    return 'ok';
  }
}
```

---
_Source: https://npm.io/package/manjar · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
