Skip to content

Commit bae94a8

Browse files
committed
📝 Public API documentation (#6)
1 parent 612749d commit bae94a8

12 files changed

+111
-31
lines changed

.github/workflows/deploy_docc.yml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Deploy DocC
2+
3+
on:
4+
push:
5+
branches:
6+
- "main"
7+
8+
9+
permissions:
10+
contents: write
11+
12+
# This allows a subsequently queued workflow run to interrupt previous runs
13+
concurrency:
14+
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
15+
cancel-in-progress: true
16+
17+
jobs:
18+
build_docs:
19+
runs-on: macos-latest
20+
steps:
21+
- uses: swift-actions/setup-swift@v1
22+
with:
23+
swift-version: "5.9"
24+
25+
- name: Checkout 🛎️
26+
uses: actions/checkout@v3
27+
28+
- name: Build DocC
29+
run: |
30+
swift package resolve;
31+
swift package --allow-writing-to-directory docs \
32+
generate-documentation --target FluentData \
33+
--disable-indexing \
34+
--transform-for-static-hosting \
35+
--hosting-base-path FluentData \
36+
--output-path docs
37+
38+
- name: Deploy to GitHub Pages
39+
uses: peaceiris/actions-gh-pages@v3
40+
with:
41+
github_token: ${{ secrets.GITHUB_TOKEN }}
42+
publish_dir: ./docs
43+
destination_dir: docs

Sources/FluentData/Documentation.docc/Documentation.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
# FluentData
1+
# Getting started with FluentData
22

33
Add managed persistence to your SwiftUI app using the Fluent ORM.
44

55
## Overview
66

7-
Combining Fluent's proven ORM technology and Swift's modern concurrency features, FluentData enables you to add persistence to your app quickly, with minimal code. Inspired by SwiftData, FluentData gives additional control and features over data persistence in your SwiftUI app.
7+
Combining Fluent's proven ORM technology and Swift's modern concurrency features, FluentData enables you to add persistence to your app quickly, with minimal
8+
code. Inspired by SwiftData, FluentData gives additional control and features over data persistence in your SwiftUI app.
9+
10+
Browsing the [Fluent](https://docs.vapor.codes/fluent/overview/) documentation will prove to be useful as well, since FluentData relies on this library.
811

912
## Topics
1013

11-
### Base API
14+
### Core API
1215

1316
- ``FluentDataContextKey``
1417
- ``FluentDataContext``
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"meta": {
3+
"title": "FluentData - Documentation"
4+
},
5+
"theme": {}
6+
}

Sources/FluentData/FluentDataContext.swift

-4
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ fileprivate protocol DatabaseStateTracker: AnyObject {
196196
func onUpdate(_ model: AnyModel, on db: Database) async
197197
}
198198

199-
public enum ReadOnlyDatabaseError: Error {
200-
case invalidOperation
201-
}
202-
203199
fileprivate struct ReadOnlyMiddleware: AnyModelMiddleware {
204200
func handle(_ event: FluentKit.ModelEvent, _ model: FluentKit.AnyModel, on db: FluentKit.Database, chainingTo next: FluentKit.AnyModelResponder) -> NIOCore.EventLoopFuture<Void> {
205201
return db.eventLoop.makeFailedFuture(ReadOnlyDatabaseError.invalidOperation)

Sources/FluentData/FluentDataContextKey.swift

+3-20
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,13 @@
11
import Foundation
22

3-
/// Supported storage mediums for a context
4-
///
5-
/// ``memory`` won't persist data across two launches of your app. This can be quite useful during the development process.
6-
///
7-
/// ``file(_:)`` on the other hand, will persist data on disk using the SQLite format.
8-
///
9-
/// ``bundle(_:name:)`` provide a read-only database using the SQLite format from a bundle file.
10-
/// The file will be located in the "Application Support" folder of the currently running application.
11-
public enum FluentDataPersistence {
12-
case memory
13-
case file(_ name: String)
14-
case bundle(_ bundle: Bundle, name: String)
15-
}
16-
17-
public enum MigrationFailurePolicy {
18-
case startFresh
19-
case abort
20-
case backupAndStartFresh(backupHandler: (URL) -> Void)
21-
}
22-
233
/// An unique identifier for a database context
244
public protocol FluentDataContextKey {
5+
/// If set, SQL queries will be logged. Defaults to `true` in DEBUG configuration, false otherwise.
256
static var logQueries: Bool { get }
267

8+
/// Specify FluentData's behaviour if one of the migrations fail. Defaults to ``MigrationFailurePolicy/abort``.
279
static var migrationFailurePolicy: MigrationFailurePolicy { get }
10+
2811
/// The list of migrations to apply
2912
///
3013
/// Migrations allows your data model to evolve with your app. Migrations are automatically applied in order when the database context is created.

Sources/FluentData/FluentDataContexts.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import FluentKit
22
import Foundation
33

4-
/// A registry of `FluentDataContext`
4+
/// A registry for ``FluentDataContext`` instances
55
public enum FluentDataContexts {
66
private static var contexts: [ObjectIdentifier: FluentDataContext] = [:]
77
private static var defaultId: ObjectIdentifier?
@@ -20,6 +20,7 @@ public enum FluentDataContexts {
2020
}
2121
}
2222

23+
/// Returns the default context, if any. Default context can be specified in ``FluentDataContext``'s initializer.
2324
public static var `default`: FluentDataContext? {
2425
guard let defaultId else { return nil }
2526
return contexts[defaultId]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Foundation
2+
3+
/// Specify the storage mediums of a context
4+
public enum FluentDataPersistence {
5+
/// ``memory`` won't persist data across two launches of your app. This can be quite useful during the development process.
6+
case memory
7+
8+
/// ``file(_:)`` on the other hand, will persist data on disk using the SQLite format.
9+
/// The file will be located in the "Application Support" folder of the currently running application.
10+
case file(_ name: String)
11+
12+
/// ``bundle(_:name:)`` provide a read-only database using the SQLite format from a bundle file.
13+
case bundle(_ bundle: Bundle, name: String)
14+
}

Sources/FluentData/FluentQuery.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class FluentQuery<Model: FluentKit.Model> {
88
internal let queryBuilder: (QueryBuilder<Model>) -> QueryBuilder<Model>
99
internal let queryId: UUID
1010
internal let subject = CurrentValueSubject<[Model], Error>([])
11+
12+
/// Any subscriber to this publisher will receive new updates everytime FluentData determines the result has to be updated.
1113
public var publisher: AnyPublisher<[Model], Error> {
1214
subject
1315
.receive(on: DispatchQueue.main)
@@ -19,6 +21,10 @@ public class FluentQuery<Model: FluentKit.Model> {
1921
.eraseToAnyPublisher()
2022
}
2123

24+
/// Create a query object to fetch entries from the specified database context
25+
/// - Parameters:
26+
/// - context: the context object
27+
/// - queryBuilder: optional, can be specified to customize the query, such as filters or the sort order
2228
public init(
2329
context: FluentDataContext,
2430
queryBuilder: @escaping (QueryBuilder<Model>) -> QueryBuilder<Model> = { $0 }
@@ -29,10 +35,10 @@ public class FluentQuery<Model: FluentKit.Model> {
2935
self.context.register(self)
3036
}
3137

32-
/// Create a query object to fetch entries from the specified database context
38+
/// Create a query object to fetch entries from the specified database context key
3339
/// - Parameters:
3440
/// - contextKey: the key which uniquely identify this context
35-
/// - queryBuilder: optional, can be specified to customize the query, such as the sort order
41+
/// - queryBuilder: optional, can be specified to customize the query, such as filters or the sort order
3642
public convenience init<TContextKey: FluentDataContextKey>(
3743
contextKey: TContextKey.Type,
3844
queryBuilder: @escaping (QueryBuilder<Model>) -> QueryBuilder<Model> = { $0 }
@@ -42,7 +48,7 @@ public class FluentQuery<Model: FluentKit.Model> {
4248

4349
/// Create a query object to fetch entries from the default database context
4450
/// - Parameters:
45-
/// - queryBuilder: optional, can be specified to customize the query, such as the sort order
51+
/// - queryBuilder: optional, can be specified to customize the query, such as filters or the sort order
4652
public convenience init(
4753
queryBuilder: @escaping (QueryBuilder<Model>) -> QueryBuilder<Model> = { $0 }
4854
) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Foundation
2+
3+
/// Recovery policy FluentData should adopt when a migration fails to execute
4+
///
5+
/// When using ``startFresh`` or ``backupAndStartFresh(backupHandler:)``, the process will crash if a new database cannot be created (similar to ``abort``)
6+
public enum MigrationFailurePolicy {
7+
/// ``startFresh`` will wipe the database content and recreate a new database. This is not recommended in production as it brings data losses
8+
case startFresh
9+
10+
/// ``abort`` the process will crash voluntarily. Reasons why the migration failed to execute will be available in the logs.
11+
case abort
12+
13+
/// ``backupAndStartFresh(backupHandler:)`` will call the speccified closure with the URL to a copy of the database file which failed to migrate.
14+
/// After the backup handler returns, the behaviour will be equivalent to ``startFresh``.
15+
case backupAndStartFresh(backupHandler: (URL) -> Void)
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// Errors occuring while working with read-only databases
2+
public enum ReadOnlyDatabaseError: Error {
3+
/// Thrown when trying to alter a read-only database
4+
case invalidOperation
5+
}

Sources/FluentData/Utils/FieldKey+Extensions.swift

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
public extension FieldKey {
2+
/// Easily generate the complete field key for a group field
3+
/// - Parameter field: Field key of inner field
4+
/// - Returns: A new field key matching Fluent's expectations
25
func group(field: FieldKey) -> FieldKey {
36
return FieldKey(stringLiteral: "\(description)_\(field.description)")
47
}

Sources/FluentData/WithFluentContext.swift

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
/// Injects a FluentContext instance
12
@propertyWrapper public struct WithFluentContext {
3+
/// Injects the default context, if any, otherwise crashes the process
24
public init() {
35
guard let context = FluentDataContexts.default else {
46
fatalError("FluentData has no default context")
57
}
68
self.context = context
79
}
810

11+
/// Injects the context matching the given key. Context needs to have been created beforehand.
12+
/// - Parameter contextKey: the key which uniquely identify this context
913
public init<K: FluentDataContextKey>(contextKey: K.Type) {
1014
self.context = FluentDataContexts[contextKey]
1115
}

0 commit comments

Comments
 (0)