Skip to content

AdonisJS Addon Provider for Mongorito (mongodb package for nodejs)

License

Notifications You must be signed in to change notification settings

allanfreitas/adonis-mongorito

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

adonis-mongorito

AdonisJS Addon Provider for Mongorito (mongodb package for nodejs)

Installation

$ npm install adonis-mongorito --save

The command above will install the package mongorito too.

You need to create a config/mongo.js file with the contents:

'use strict'

const Env = use('Env')

module.exports = {
  host: Env.get('MONGO_HOST', 'localhost'),
  port: Env.get('MONGO_PORT', '27017'),
  user: Env.get('MONGO_USER', ''),
  pass: Env.get('MONGO_PASS', ''),
  db: Env.get('MONGO_DATABASE', 'adonisjs'),
  options: Env.get('MONGO_OPTIONS', '')
}

You are using a connection local without user and pass, leave it blank like on the example.

Add the entry adonis-mongorito/providers/MongoritoProvider.js to the providers array on bootstrap/app.js like this:

const providers = [
  'adonis-framework/providers/ConfigProvider',
  'adonis-framework/providers/EnvProvider',
   //..OTHER DEFAULT PROVIDERS...//
  'adonis-middleware/providers/AppMiddlewareProvider',
  'adonis-auth/providers/AuthManagerProvider',
  'adonis-mongorito/providers/MongoritoProvider' //add this line after install the package
]

The following its to add the Authenticator combination so you can perform Mongo db check in authentication.

  // change here if you are using jwt, otherwise change it in you right authenticator
  jwt: {
    serializer: 'MongoritoSerializer', // this is new
    model: 'App/Model/User',
    scheme: 'MongoritoScheme', // and this too
    uid: 'username',
    password: 'password',
    secret: Config.get('app.appKey')
  }

Add the entry MongoritoModel: 'Adonis/Addons/MongoritoModel' on the aliases object on bootstrap/app.js file like this:

const aliases = {
  Command: 'Adonis/Src/Command',
  Config: 'Adonis/Src/Config',
   //..OTHER DEFAULT PROVIDERS...//
  View: 'Adonis/Src/View',
  MongoritoModel: 'Adonis/Addons/MongoritoModel' //this line
  MongoritoScheme: 'adonis-mongorito/AuthManager/MongoritoScheme' // and this line
}

Usage

Now you can create Mongo MODELS like Lucid Models

app/Model/Post.js

'use strict'

const MongoritoModel = use('MongoritoModel')

class Post extends MongoritoModel {

}

module.exports = Post

And use like this:

app/Http/routes.js

'use strict'

const Route = use('Route')

const Post = use('App/Model/Post')

Route.get('/posts', function * (request, response) {
  //Simple get All Posts
  const posts = yield Post.all()

  response.json(posts)
})

Route.post('/posts', function * (request, response) {

  //create new Post
  let post = new Post({
    title: request.input('title'),
    author: {
      name: request.input('author_name')
    }
  });

  yield post.save();

  response.json(post)
})

As you can see, it's very easy to use.


How to use Mongorito stuff?

Found any Bugs?

Please before open a Issue on this repository, check if it's not a bug on Mongorito package here: https://github.com/vdemedes/mongorito/issues

License

Adonis-Mongorito is released under the MIT License.

About

AdonisJS Addon Provider for Mongorito (mongodb package for nodejs)

Resources

License

Stars

Watchers

Forks

Packages

No packages published