Skip to content

Commit

Permalink
Adding Queryable
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Oct 15, 2024
1 parent e4bed5d commit 5c12edf
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Sources/DataThespian/Databases/Queryable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// Queryable.swift
// DataThespian
//
// Created by Leo Dion on 10/14/24.
//

public import SwiftData
public import Foundation

public enum Selector<T : PersistentModel> : Sendable {
public enum Delete : Sendable{
case predicate(Predicate<T>)
case all
case model(Model<T>)
}
public enum List : Sendable{
case descriptor(FetchDescriptor<T>)
}
public enum Get : Sendable{
case model(Model<T>)
case predicate(Predicate<T>)
}
}

public protocol Queryable {
func insert<PersistentModelType: PersistentModel, U : Sendable>(
_ closuer: @Sendable @escaping () -> PersistentModelType,
with closure: @escaping @Sendable (PersistentModelType) throws -> U
) async -> Model<PersistentModelType>

func get<PersistentModelType, U:Sendable>(
for selector: Selector<PersistentModelType>.Get,
with closure: @escaping @Sendable (PersistentModelType?) throws -> U
) async rethrows -> U

func fetch<PersistentModelType, U:Sendable>(
for selector: Selector<PersistentModelType>.List,
with closure: @escaping @Sendable ([PersistentModelType]) throws -> U
) async rethrows -> U

func delete<PersistentModelType>(_ selector: Selector<PersistentModelType>.Delete) async throws
}

extension Queryable {
func insert<PersistentModelType: PersistentModel>(
_ closuer: @Sendable @escaping () -> PersistentModelType
) async -> Model<PersistentModelType> {
await self.insert(closuer, with: Model.init)
}

func get<PersistentModelType>(
for selector: Selector<PersistentModelType>.Get
) async -> Model<PersistentModelType>? {
await self.get(for: selector) { persistentModel in
persistentModel.flatMap(Model.init)
}
}

func fetch<PersistentModelType>(
for selector: Selector<PersistentModelType>.List
) async -> [Model<PersistentModelType>] {
await self.fetch(for: selector) { persistentModels in
persistentModels.map(Model.init)
}
}
}

0 comments on commit 5c12edf

Please sign in to comment.