Skip to content

Commit

Permalink
adding unique
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Oct 15, 2024
1 parent 5c12edf commit 50638b1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
12 changes: 11 additions & 1 deletion Example/Sources/Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,22 @@

import Foundation
import SwiftData
import DataThespian

@Model
internal final class Item {
internal final class Item : Unique {
internal private(set) var timestamp: Date

internal init(timestamp: Date) {
self.timestamp = timestamp
}

enum Keys : UniqueKeySet {
typealias Model = Item

static let timestamp : UniqueKey = Self.unique(\.timestamp)



}
}
35 changes: 34 additions & 1 deletion Sources/DataThespian/Databases/Queryable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@
public import SwiftData
public import Foundation

public protocol Unique {
associatedtype Keys : UniqueKeySet<Self>
}

public protocol UniqueKeySet<Model> : Sendable {
associatedtype Model : Unique
}

public struct UniqueKey<Model, T : Sendable & Equatable> : Sendable {
let keyPath : @Sendable () -> KeyPath<Model, T>
}

extension UniqueKeySet {
public static func unique<T : Sendable & Equatable>(keyPath : @escaping @Sendable () -> KeyPath<Model, T>) -> UniqueKey<Model, T> {
.init(keyPath: keyPath)
}

public static func unique<T : Sendable & Equatable>(_ keyPath : @autoclosure @escaping @Sendable () -> KeyPath<Model, T>) -> UniqueKey<Model, T> {
.init(keyPath: keyPath)
}


}

public enum Selector<T : PersistentModel> : Sendable {
public enum Delete : Sendable{
case predicate(Predicate<T>)
Expand All @@ -23,11 +47,20 @@ public enum Selector<T : PersistentModel> : Sendable {
}
}

//extension Selector.Get {
// static func unique<ValueType: Sendable & Equatable>(_ key: UniqueKey<T, ValueType>, equals value: ValueType) -> Self {
//
// #Predicate({ input in
//
// })
// }
//}

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

func get<PersistentModelType, U:Sendable>(
for selector: Selector<PersistentModelType>.Get,
Expand Down

0 comments on commit 50638b1

Please sign in to comment.