Skip to content

KazaiMazai/vapor-rest-kit

Repository files navigation

Sublime's custom image

This package is intended to speed up backend development using server side swift framework Vapor

Features

  • CRUDs with Resource and Nested Resource Controllers
  • Parent-Child and Siblings relations for Nested Resource Controllers
  • Nested Resource Controllers for Authenticatable Resource
  • Filter query
  • Sorting query
  • Eager loading query
  • Fluent Model convenience extensions
  • Cursor Pagination

Installation

Add this package to your Package.swift as dependency and to your target.

dependencies: [
    .package(url: "https://github.com/KazaiMazai/vapor-rest-kit",  from: "1.0.0-beta.1.6")
],
targets: [
    .target(name: "App", dependencies: [
        .product(name: "VaporRestKit", package: "vapor-rest-kit")
    ])
]

Import in your code

import VaporRestKit

  1. Define Input, Output structs for your Model, conforming to ResourceUpdateModel, ResourcePatchModel, ResourceOutputModel protocols:
protocol ResourceUpdateModel: Content, Validatable {
    associatedtype Model: Fields

    func update(_: Model) -> Model
}

protocol ResourcePatchModel: Content, Validatable {
    associatedtype Model: Fields

    func patch(_: Model) -> Model
}

protocol ResourceOutputModel: Content {
    associatedtype Model: Fields

    init(_: Model)
}
  1. Define StarsEagerLoadQueryKeys, StarsSortQueryKeys, StarsFilterQueryKeys if needed

  2. Implement controller with RestKit ResourceControllers API:

struct StarForGalaxyNestedController {
     
    func create(req: Request) throws -> EventLoopFuture<Star.Output> {
        try RelatedResourceController<Star.Output>().create(
            req: req,
            using: Star.Input.self,
            relationKeyPath: \Galaxy.$stars)
    }

    func read(req: Request) throws -> EventLoopFuture<Star.Output> {
        try RelatedResourceController<Star.Output>().read(
            req: req,
            queryModifier: .eagerLoading(StarEagerLoadingKeys.self),
            relationKeyPath: \Galaxy.$stars)
    }

    func update(req: Request) throws -> EventLoopFuture<Star.Output> {
        try RelatedResourceController<Star.Output>().update(
            req: req,
            using: Star.Input.self,
            relationKeyPath: \Galaxy.$stars)
    }

    func delete(req: Request) throws -> EventLoopFuture<Star.Output> {
        try RelatedResourceController<Star.Output>().delete(
            req: req,
            relationKeyPath: \Galaxy.$stars)
    }

    func patch(req: Request) throws -> EventLoopFuture<Star.Output> {
        try RelatedResourceController<Star.Output>().patch(
            req: req,
            using: Star.PatchInput.self,
            relationKeyPath: \Galaxy.$stars)
    }

    func index(req: Request) throws -> EventLoopFuture<CursorPage<Star.Output>> {
        try RelatedResourceController<Star.Output>().getCursorPage(
            req: req,
            queryModifier:
                .eagerLoading(StarEagerLoadingKeys.self) &
                .sort(using: StarsSortingKeys.self) &
                .filter(StarsFilterKeys.self),
            relationKeyPath: \Galaxy.$stars)
    }
}
  1. Routes setup:
app.group("galaxies", Galaxy.idPath, "stars") {
    let controller = StarForGalaxyNestedController()

    $0.on(.POST, use: controller.create)
    $0.on(.GET, Star.idPath, use: controller.read)
    $0.on(.PUT, Star.idPath, use: controller.update)
    $0.on(.PATCH, Star.idPath, use: controller.patch)
    $0.on(.DELETE, Star.idPath, use: controller.delete)
    $0.on(.GET, use: controller.index)
}
  1. Get result
HTTP Method Route Result
POST /galaxies/:GalaxyId/stars Create new
GET /galaxies/:GalaxyId/stars/:starId Show existing
PUT /galaxies/:GalaxyId/stars/:starId Replace existing
PATCH /galaxies/:GalaxyId/stars/:starId Patch exsiting
DELETE /galaxies/:GalaxyId/stars/:starId Delete
GET /galaxies/:GalaxyId/stars Show list with cursor pagination

Check out the Docs for more details:

Basics

Fluent Model Extensions

CRUD for Resource Models

CRUD for Related Resource Models

CRUD for Relations

Controller Middlewares

Filtering

Sorting

Eager Loading

QueryModifier

Pagination

VaporRestKit Migration Guides

Migration from v1.x to v2.0

Licensing

Vapor RestKit is licensed under MIT license.