Home

Awesome

<p align="center"> <a href="https://vapor.codes"> <img src="https://img.shields.io/badge/Vapor-3-brightgreen.svg" alt="Vapor Version"> </a> <a href="license"> <img src="http://img.shields.io/badge/license-MIT-brightgreen.svg" alt="MIT License"> </a> <a href="https://circleci.com/gh/vapor-community/vapor-ext"> <img src="https://circleci.com/gh/vapor-community/vapor-ext.svg?style=shield" alt="Continuous Integration"> </a> <a href="https://codecov.io/gh/vapor-community/vapor-ext"> <img src="https://codecov.io/gh/vapor-community/vapor-ext/branch/master/graph/badge.svg" /> </a> <a href="https://swift.org"> <img src="http://img.shields.io/badge/swift-4.1-brightgreen.svg" alt="Swift 4.1"> </a> </p>

VaporExt

VaporExt is a collection of Swift extensions, with handy methods, syntactic sugar, and performance improvements for wide range of Vapor types and classes.

Requirements:

Installation

You can use <a href="https://swift.org/package-manager">The Swift Package Manager</a> to install <code>VaporExt</code> by adding the proper description to your <code>Package.swift</code> file:

<pre><code class="swift language-swift">import PackageDescription let package = Package( name: "YOUR_PROJECT_NAME", targets: [], dependencies: [ .package(url: "https://github.com/vapor-community/vapor-ext.git", from: "0.1.0"), ] ) </code></pre> <p>Note that the <a href="https://swift.org/package-manager">Swift Package Manager</a> is still in early design and development, for more information checkout its <a href="https://github.com/apple/swift-package-manager">GitHub Page</a></p>

What is included?

The extensions are grouped in 3 modules, AsyncExt, FluentExt and ServiceExt, and the VaporExt which acts as a helper to export the extensions included in the previous packages.

AsyncExt

FluentExt

Query params syntax for filters:

You can set the filter method with this format parameter=method:value and the new filter method will build the filter based on the method, example:

return try User.query(on: req)
            .filter(\User.username, at: "username", on: req)
            .filter(\User.enabled, at: "enabled", on: req)
            .filter(\User.createdAt, at: "created_at", on: req)
            .filter(\User.updatedAt, at: "updated_at", on: req)
            .filter(\User.deletedAt, at: "deleted_at", on: req)

Query params syntax for sorting:

You can set the sorts methods with this format sort=field:direction,field:direction and the new sort method will build the query based on that configuration, for example

return try User.query(on: req)
            .sort(\User.username, as: "username", on: req)
            .sort(\User.createdAt, as: "created_at", default: .ascending, on: req) // if created_at is not present in the url, then the sort is applied using the default direction

Request extensions example:

func index(_ req: Request) throws -> Future<[User]> {
    let repository = try req.make(UserRepository.self)

    let criteria: [FilterOperator<User.Database, User>] = try [
        req.filter(\User.name, at: "name"),
        req.filter(\User.username, at: "username"),
        req.filter(\User.enabled, at: "enabled")
    ].compactMap { $0 }

    var sort: [User.Database.QuerySort] = try [
        req.sort(\User.name, as: "name"),
        req.sort(\User.username, as: "username"),
        req.sort(\User.createdAt, as: "created_at"),
    ].compactMap { $0 }

    if sort.isEmpty {
        let defaultSort = try req.sort(\User.name, as: "name", default: .ascending)
        sort.append(defaultSort)
    }

    return repository.findBy(criteria: criteria, orderBy: sort, on: req)
}

ServiceExt

VaporExt

Get involved:

We want your feedback. Please refer to contributing guidelines before participating.

Discord Channel:

It is always nice to talk with other people using VaporExt and exchange experiences, so come join our Discord channel.

Credits

This package is developed and maintained by Gustavo Perdomo with the collaboration of all vapor community.

License

VaporExt is released under an MIT license. See license for more information.