Skip to content

Commit

Permalink
Remove "HB" prefix (#15)
Browse files Browse the repository at this point in the history
* Remove HB prefix

* Add deprecations

* Use hummingbird main
  • Loading branch information
adam-fowler committed Mar 12, 2024
1 parent e9f1789 commit f2022d4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 32 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -13,7 +13,7 @@ import FluentSQLiteDriver
import HummingbirdFluent

let logger = Logger(label: "MyApp")
let fluent = HBFluent(logger: logger)
let fluent = Fluent(logger: logger)
// add sqlite database
fluent.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite)
// add migration
Expand All @@ -27,11 +27,11 @@ if arguments.migrate {
Fluent can be used from a route as follows.

```swift
let router = HBRouter()
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(HBHTTPError(.badRequest)) }
guard let id = context.parameters.get("id", as: UUID.self) else { return request.failure(HTTPError(.badRequest)) }
return Todo.find(id, on: fluent.db())
}
```
Expand All @@ -40,7 +40,7 @@ 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

```swift
var app = HBApplication(router: router)
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()
Expand Down
24 changes: 24 additions & 0 deletions Sources/HummingbirdFluent/Deprecations.swift
@@ -0,0 +1,24 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

// Below is a list of deprecated symbols with the "HB" prefix. These are available
// temporarily to ease transition from the old symbols that included the "HB"
// prefix to the new ones.
//
// This file will be removed before we do a 2.0 release

@_documentation(visibility: internal) @available(*, deprecated, renamed: "Fluent")
public typealias HBFluent = Fluent
@_documentation(visibility: internal) @available(*, deprecated, renamed: "FluentPersistDriver")
public typealias HBFluentPersistDriver = FluentPersistDriver
7 changes: 2 additions & 5 deletions Sources/HummingbirdFluent/Fluent.swift
Expand Up @@ -22,10 +22,7 @@ public struct MainActorBox<Value>: Sendable {
}

/// Manage fluent databases and migrations
///
/// You can either create this separate from `HBApplication` or add it to your application
/// using `HBApplication.addFluent`.
public struct HBFluent: Sendable, Service {
public struct Fluent: Sendable, Service {
/// Event loop group
public let eventLoopGroup: EventLoopGroup
/// Logger
Expand All @@ -39,7 +36,7 @@ public struct HBFluent: Sendable, Service {
private let _databases: UnsafeTransfer<Databases>
private let _migrations: MainActorBox<Migrations>

/// Initialize HBFluent
/// Initialize Fluent
/// - Parameters:
/// - eventLoopGroup: EventLoopGroup used by databases
/// - threadPool: NIOThreadPool used by databases
Expand Down
12 changes: 6 additions & 6 deletions Sources/HummingbirdFluent/Persist+fluent.swift
Expand Up @@ -20,17 +20,17 @@ import NIOCore
import ServiceLifecycle

/// Fluent driver for persist system for storing persistent cross request key/value pairs
public final class HBFluentPersistDriver: HBPersistDriver {
let fluent: HBFluent
public final class FluentPersistDriver: PersistDriver {
let fluent: Fluent
let databaseID: DatabaseID?
let tidyUpFrequency: Duration

/// Initialize HBFluentPersistDriver
/// Initialize FluentPersistDriver
/// - Parameters:
/// - fluent: Fluent setup
/// - databaseID: ID of database to use
/// - tidyUpFrequequency: How frequently cleanup expired database entries should occur
public init(fluent: HBFluent, databaseID: DatabaseID? = nil, tidyUpFrequency: Duration = .seconds(600)) async {
public init(fluent: Fluent, databaseID: DatabaseID? = nil, tidyUpFrequency: Duration = .seconds(600)) async {
self.fluent = fluent
self.databaseID = databaseID
self.tidyUpFrequency = tidyUpFrequency
Expand All @@ -47,7 +47,7 @@ public final class HBFluentPersistDriver: HBPersistDriver {
do {
try await model.save(on: db)
} catch let error as DatabaseError where error.isConstraintFailure {
throw HBPersistError.duplicate
throw PersistError.duplicate
} catch {
self.fluent.logger.debug("Error: \(error)")
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public final class HBFluentPersistDriver: HBPersistDriver {
}

/// Service protocol requirements
extension HBFluentPersistDriver {
extension FluentPersistDriver {
public func run() async throws {
let timerSequence = AsyncTimerSequence(interval: self.tidyUpFrequency, clock: .suspending)
.cancelOnGracefulShutdown()
Expand Down
10 changes: 5 additions & 5 deletions Tests/HummingbirdFluentTests/FluentTests.swift
Expand Up @@ -24,7 +24,7 @@ import XCTest

@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *)
final class FluentTests: XCTestCase {
final class Planet: Model, HBResponseCodable {
final class Planet: Model, ResponseCodable {
// Name of the table or collection.
static let schema = "planets"

Expand Down Expand Up @@ -61,13 +61,13 @@ final class FluentTests: XCTestCase {
}
}

struct CreateResponse: HBResponseCodable {
struct CreateResponse: ResponseCodable {
let id: UUID
}

func testPutGet() async throws {
let logger = Logger(label: "FluentTests")
let fluent = HBFluent(
let fluent = Fluent(
logger: logger
)
// add sqlite database
Expand All @@ -90,7 +90,7 @@ final class FluentTests: XCTestCase {
// run migrations
try await fluent.migrate()

let router = HBRouter()
let router = Router()
router.put("planet") { request, context in
let planet = try await request.decode(as: Planet.self, context: context)
try await planet.create(on: fluent.db())
Expand All @@ -102,7 +102,7 @@ final class FluentTests: XCTestCase {
.filter(\.$id == id)
.first()
}
var app = HBApplication(responder: router.buildResponder())
var app = Application(responder: router.buildResponder())
app.addServices(fluent)
try await app.test(.live) { client in
let planet = Planet(name: "Saturn")
Expand Down
24 changes: 12 additions & 12 deletions Tests/HummingbirdFluentTests/PersistTests.swift
Expand Up @@ -20,10 +20,10 @@ import HummingbirdTesting
import XCTest

final class PersistTests: XCTestCase {
func createApplication(_ updateRouter: (HBRouter<HBBasicRequestContext>, HBPersistDriver) -> Void = { _, _ in }) async throws -> some HBApplicationProtocol {
func createApplication(_ updateRouter: (Router<BasicRequestContext>, PersistDriver) -> Void = { _, _ in }) async throws -> some ApplicationProtocol {
var logger = Logger(label: "FluentTests")
logger.logLevel = .trace
let fluent = HBFluent(logger: logger)
let fluent = Fluent(logger: logger)
// add sqlite database
fluent.databases.use(.sqlite(.memory), as: .sqlite)
/* fluent.databases.use(
Expand All @@ -39,11 +39,11 @@ final class PersistTests: XCTestCase {
),
as: .psql
) */
let persist = await HBFluentPersistDriver(fluent: fluent)
let persist = await FluentPersistDriver(fluent: fluent)
// run migrations
try await fluent.migrate()

let router = HBRouter()
let router = Router()

router.put("/persist/:tag") { request, context -> HTTPResponse.Status in
let buffer = try await request.body.collect(upTo: .max)
Expand All @@ -52,23 +52,23 @@ final class PersistTests: XCTestCase {
return .ok
}
router.put("/persist/:tag/:time") { request, context -> HTTPResponse.Status in
guard let time = context.parameters.get("time", as: Int.self) else { throw HBHTTPError(.badRequest) }
guard let time = context.parameters.get("time", as: Int.self) else { throw HTTPError(.badRequest) }
let buffer = try await request.body.collect(upTo: .max)
let tag = try context.parameters.require("tag")
try await persist.set(key: tag, value: String(buffer: buffer), expires: .seconds(time))
return .ok
}
router.get("/persist/:tag") { _, context -> String? in
guard let tag = context.parameters.get("tag", as: String.self) else { throw HBHTTPError(.badRequest) }
guard let tag = context.parameters.get("tag", as: String.self) else { throw HTTPError(.badRequest) }
return try await persist.get(key: tag, as: String.self)
}
router.delete("/persist/:tag") { _, context -> HTTPResponse.Status in
guard let tag = context.parameters.get("tag", as: String.self) else { throw HBHTTPError(.badRequest) }
guard let tag = context.parameters.get("tag", as: String.self) else { throw HTTPError(.badRequest) }
try await persist.remove(key: tag)
return .noContent
}
updateRouter(router, persist)
var app = HBApplication(responder: router.buildResponder())
var app = Application(responder: router.buildResponder())
app.addServices(fluent, persist)

return app
Expand Down Expand Up @@ -112,8 +112,8 @@ final class PersistTests: XCTestCase {
let tag = try context.parameters.require("tag")
do {
try await persist.create(key: tag, value: String(buffer: buffer))
} catch let error as HBPersistError where error == .duplicate {
throw HBHTTPError(.conflict)
} catch let error as PersistError where error == .duplicate {
throw HTTPError(.conflict)
}
return .ok
}
Expand Down Expand Up @@ -171,13 +171,13 @@ final class PersistTests: XCTestCase {
}
let app = try await self.createApplication { router, persist in
router.put("/codable/:tag") { request, context -> HTTPResponse.Status in
guard let tag = context.parameters.get("tag") else { throw HBHTTPError(.badRequest) }
guard let tag = context.parameters.get("tag") else { throw HTTPError(.badRequest) }
let buffer = try await request.body.collect(upTo: .max)
try await persist.set(key: tag, value: TestCodable(buffer: String(buffer: buffer)))
return .ok
}
router.get("/codable/:tag") { _, context -> String? in
guard let tag = context.parameters.get("tag") else { throw HBHTTPError(.badRequest) }
guard let tag = context.parameters.get("tag") else { throw HTTPError(.badRequest) }
let value = try await persist.get(key: tag, as: TestCodable.self)
return value?.buffer
}
Expand Down

0 comments on commit f2022d4

Please sign in to comment.