diff --git a/README.md b/README.md index dee8efb..11499b1 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # CrudRouter -CrudRouter makes it as simple as possible to set up CRUD (Create, Read, Update, Delete) routes for any `Model`. +CrudRouter is a Rails-inspired extension to Vapor's routing system that makes it as simple as possible to set up CRUD (Create, Read, Update, Delete) routes for any `Model`. CrudRouter provides an API very similar to Rails' `resources` but with a few extra features including automatic responder generation and type safety. ## Installation Within your Package.swift @@ -19,6 +19,7 @@ targets: [ ``` ## Usage + Within your router setup (`routes.swift` in the default Vapor API template) ```swift router.crud(register: Todo.self) @@ -28,11 +29,11 @@ That's it! That one line gets you the following routes. ``` -GET /todo -GET /todo/:id -POST /todo -PUT /todo/:id -DELETE /todo/:id +GET /todo // returns all Todos +GET /todo/:id // returns the Todo with :id +POST /todo // create new Todo with provided body +PUT /todo/:id // update Todo with :id +DELETE /todo/:id // delete Todo with :id ``` Generated paths default to using lower snake case so for example, if you were to do @@ -43,11 +44,11 @@ router.crud(register: SchoolTeacher.self) you'd get routes like ``` -GET /school_teacher -GET /school_teacher/:id -POST /school_teacher -PUT /school_teacher/:id -DELETE /school_teacher/:id +GET /school_teacher +GET /school_teacher/:id +POST /school_teacher +PUT /school_teacher/:id +DELETE /school_teacher/:id ``` #### Path Configuration @@ -59,11 +60,11 @@ router.crud("account", register: User.self) results in ``` -GET /account -GET /account/:id -POST /account -PUT /account/:id -DELETE /account/:id +GET /account +GET /account/:id +POST /account +PUT /account/:id +DELETE /account/:id ``` #### Nested Relations @@ -78,17 +79,17 @@ try router.crud(register: User.self) { controller in results in ``` -GET /user -GET /user/:id -POST /user -PUT /user/:id -DELETE /user/:id - -GET/user/:id/todo -GET /user/:id/todo/:id -POST/user/:id/todo -PUT/user/:id/todo/:id -DELETE/user/:id/todo/:id +GET /user +GET /user/:id +POST /user +PUT /user/:id +DELETE /user/:id + +GET /user/:id/todo +GET /user/:id/todo/:id +POST /user/:id/todo +PUT /user/:id/todo/:id +DELETE /user/:id/todo/:id ``` within the supplied closure, you can also expose routes for related `Parent`s and `Sibling`s @@ -99,6 +100,25 @@ try controller.crud(parent: \.todos) try controller.crud(siblings: \.todos) ``` +#### Including or Excluding Specific Routes +If you'd like to register a `Model`, but you don't want every route to be available, you can specify the ones you want, or exclude the ones you don't. + +```swift +try router.crud(register: Todo.self, .except([.create, .delete])) { controller in + try controller.crud(parent: \.owner, .only([.read])) +} +``` + +results in + +``` +PUT /todo/int +GET /todo/int +GET /todo + +GET /todo/int/tag/int +``` + ### Future features - query parameter support - PATCH support