Swiftpack.co - sidepelican/SQLKitTyping as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by sidepelican.
sidepelican/SQLKitTyping 0.2.1
Add slightly type safe interface for SQLKit.
⭐️ 0
🕓 27 weeks ago
macOS
.package(url: "https://github.com/sidepelican/SQLKitTyping.git", from: "0.2.1")

SQLKitTyping

Add slightly type safe interface for SQLKit.

Install

.package(url: "https://github.com/sidepelican/SQLKitTyping.git", from: "..."),

Usage

Define table schema

@Schema
enum School: IDSchemaProtocol {
    static var tableName: String { "schools" }

    var id: SchoolID
    var name: String
}

Define entity type with Column Type.

struct SchoolAll: Identifiable, Codable {
    var id: School.Id // macro generates column types
    var name: School.Name
}

Query with type safe column

let id = SchoolID(...)
var rows = try await sql.select()
    .column(School.all)
    .from(School.self)
    .where(School.id, .equal , id) // Only 'SchoolID' type is allowed
    .all(decoding: StudentAll.self)

Eagerload children or slibling entities

@Schema
enum Student: IDSchemaProtocol {
    static var tableName: String { "students" }

    var id: StudentID
    var name: String
    var age: Int?
}

@Schema
enum SchoolStudentRelation: RelationSchemaProtocol {
    static var tableName: String { "schools_students" }

    var schoolID: SchoolID
    var studentID: StudentID

    static var relation: PivotJoinRelation<Self, SchoolID, StudentID> {
        .init(from: schoolID, to: studentID)
    }
}

struct SchoolWithStudents: Decodable, Identifiable {
    var id: School.Id
    var name: School.Name
    var students: [StudentAll] = []

    enum CodingKeys: String, CodingKey {
        case id
        case name
    }
}

var rows = try await sql.select()
    .column(School.all)
    .from(School.self)
    .all(decoding: SchoolWithStudents.self)
try await sql.eagerLoadAllColumns(into: &rows, keyPath: \.students,
                                  targetTable: Student.self,
                                  relationTable: SchoolStudentRelation.self)
XCTAssertEqual(Set(rows.map(\.students.count)), [4, 3, 0])

GitHub

link
Stars: 0
Last commit: 22 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.1.3
22 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics