Skip to content

Commit

Permalink
Format route results and add .only/.except
Browse files Browse the repository at this point in the history
  • Loading branch information
twof authored Oct 26, 2018
1 parent 4df9f7a commit 5e243fe
Showing 1 changed file with 47 additions and 27 deletions.
74 changes: 47 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,6 +19,7 @@ targets: [
```

## Usage

Within your router setup (`routes.swift` in the default Vapor API template)
```swift
router.crud(register: Todo.self)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5e243fe

Please sign in to comment.