Swiftpack.co - Package - hummingbird-project/hummingbird-fluent

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

hummingbird-project/hummingbird-fluent

Hummingbird integration with Fluent database ORM

Hummingbird Fluent

Hummingbird interface to the Fluent database ORM.

Hummingbird doesn't come with any database drivers or ORM. This library provides a connection to Vapor's database ORM. The Vapor guys have been generous and forward thinking enough to ensure Fluent-kit can be used independent of Vapor. They have a small library that links Vapor to Fluent, this library does pretty much the same thing for Hummingbird.

Usage

The following initializes an SQLite database and adds a single migration CreateTodo.

import FluentSQLiteDriver
import HummingbirdFluent

let app = HBApplication()
// add Fluent
app.addFluent()
// add sqlite database
app.fluent.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite)
// add migrations
app.fluent.migrations.add(CreateTodo())
// migrate
if arguments.migrate {
    try app.fluent.migrate().wait()
}

In general the interface to Fluent follows the same pattern as Vapor, except the db and migrations objects are only accessible from within the fluent object, and you need to call HBApplication.addFluent() at initialization.

Fluent can be used from a route as follows. The database is accessible via HBRequest.db.

app.router
    .endpoint("todos")
    .get(":id") { request in 
        guard let id = request.parameters.get("id", as: UUID.self) else { return request.failure(HBHTTPError(.badRequest)) }
        return Todo.find(id, on: request.db)
    }

Here we are returning a Todo with an id specified in the path.

You can find more documentation on Fluent here.

Github

link
Stars: 1
Last commit: 5 days ago

Releases

v0.1.0 Initial version - 2021-02-01T17:58:51

Hummingbird interface to Fluent database ORM from Vapor