hummingbird-fluent

1.1.0

Hummingbird integration with Fluent database ORM
hummingbird-project/hummingbird-fluent

What's New

v1.1.0

2023-08-14T10:16:15Z

Minor release changes

  • Setup HBFluent and HBFluentPersistDriver so they can be used separate from HBApplication

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. This package collates the fluent features into one. It also provides a driver for the Hummingbird Persist framework.

Usage

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

import FluentSQLiteDriver
import HummingbirdFluent

let logger = Logger(label: "MyApp")
let fluent = Fluent(logger: logger)
// add sqlite database
fluent.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite)
// add migration
await fluent.migrations.add(CreateTodo())
// migrate
if arguments.migrate {
    try fluent.migrate().wait()
}

Fluent can be used from a route as follows.

let router = Router()
router
    .group("todos")
    .get(":id") { request, context in 
        guard let id = context.parameters.get("id", as: UUID.self) else { return request.failure(HTTPError(.badRequest)) }
        return Todo.find(id, on: fluent.db())
    }

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

You can then bring this together by creating an application that uses the router and adding fluent to its list of services

var app = Application(router: router)
// add the fluent service to the application so it can manage shutdown correctly
app.addServices(fluent)
try await app.runService()

Documentation

Reference documentation for HummingbirdFluent can be found here here and you can find more documentation on Fluent here.

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Wed May 29 2024 06:42:25 GMT-0900 (Hawaii-Aleutian Daylight Time)