Swiftpack.co - Package - twof/VaporCRUDRouter

CrudRouter

CrudRouter makes it as simple as possible to set up CRUD (Create, Read, Update, Delete) routes for any Model.

Installation

Within your Package.swift

dependencies: [
    .package(url: "https://github.com/twof/VaporCRUDRouter.git", from: "1.0.0")
]

and

targets: [
    .target(name: "App", dependencies: ["CrudRouter"]),
]

Usage

Within your router setup (routes.swift in the default Vapor API template)

router.crud(register: Todo.self)

That's it!

That one line gets you the following routes.

GET /todo
GET /todo/:id
POST /todo
PUT /todo/:id
DELETE /todo/:id

Generated paths default to using lower snake case so for example, if you were to do

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

Path Configuration

If you'd like to supply your own path rather than using the name of the supplied model, you can also do that

router.crud("account", register: User.self)

results in

GET /account
GET /account/:id
POST /account
PUT /account/:id
DELETE /account/:id

Nested Relations

Say you had a model User, which was the parent of another model Todo. If you'd like routes to expose all Todos that belong to a specific User, you can do something like this.

try router.crud(register: User.self) { controller in
    try controller.crud(children: \.todos)
}

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

within the supplied closure, you can also expose routes for related Parents and Siblings

try controller.crud(children: \.todos)
try controller.crud(parent: \.todos)
try controller.crud(siblings: \.todos)

Future features

  • query parameter support
  • PATCH support
  • more fine grained response statuses
  • recursive relation configuration
  • automatically expose relations (blocked by lack of Swift reflection support)
  • documentation for all public functions
  • generate models and rest routes via console command
  • enum based route selection

Github

link
Stars: 5
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

1.4.0 - Oct 4, 2018

Shortened all public APIs from crudRegister() to

try router.crud(register: User.self) { controller in
    try controller.crud(children: \.todos)
}

1.3.0 - Oct 2, 2018

Siblings can now be exposed alongside Children and Parents

try router.crudRegister(for: Todo.self) { controller in
    try controller.crudRegister(forSiblings: \.tags)
}
try router.crudRegister(for: Tag.self) { controller in
    try controller.crudRegister(forSiblings: \.todos)
}

exposes

GET /todo/:id
GET /todo
POST /todo
PUT /todo/:id
DELETE /todo/:id
GET /todo/:id/tag/:id
GET /todo/:id/tag
POST /todo/:id/tag
PUT /todo/:id/tag/:id
DELETE /todo/:id/tag/:id
GET /tag/:id
GET /tag
POST/tag
PUT /tag/:id
DELETE /tag/:id
GET /tag/:id/todo/:id
GET /tag/:id/todo
POST /tag/:id/todo
PUT /tag/:id/todo/:id
DELETE /tag/:id/todo/:id

1.2.0 - Oct 1, 2018

Siblings to come. Just working out some kinks

try router.crudRegister(for: Todo.self) { controller in
    try controller.crudRegister(at: "owner", forParent:  \.owner)
}
try router.crudRegister(for: User.self) { controller in
    try controller.crudRegister(forChildren: \.todos)
}
try router.crudRegister(for: Tag.self)

1.1.0 - Sep 30, 2018

1.0.0 - Sep 29, 2018