1.0.3 • Published 6 years ago

route-model-binding-adonisjs v1.0.3

Weekly downloads
17
License
-
Repository
-
Last release
6 years ago

Adonisjs: route model binding

Route Model Binding. When you inject a model ID into a route, you will often look to retrieve the model that corresponds to that ID. This package provides a convenient solution for Adonis 4.x.

AdonisJs makes it possible to extend routes by adding your own methods to it known as macros. See this example in the official documentation extending routes

You can use this package with Lucid-mongo without problems

Example Usage

Step One:

npm install --save route-model-binding-adonisjs
# or
yarn add route-model-binding-adonisjs

Step Two: Register the provider to your start/app.js file

const providers = [
  ...
  'route-model-binding-adonisjs/providers/RMBProvider',
]

The bind method can be used as follows.

Post refers to the model name.

Make sure that the parameter name is the same as the lowercase model :post

Route.delete('route/:post', 'PostController.destroy').bind('Post');

Finally, you will have access to the post object in the destroy method

class PostController {
  
  async destroy({ post }) {
    
    await post.delete()
    
    return { status: 'post deleted succ...' }

  }
  
}