Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CAT-92] DatabaseClient 모듈 추가 #14

Merged
merged 16 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import DependenciesMacros

@DependencyClient
public struct DatabaseClient {
public var initialize: @Sendable () async throws -> Void
public var initialize: @Sendable (Realm.Configuration) async throws -> Void
public var create: @Sendable (any Persistable) async throws -> Void
public var read: @Sendable (any Persistable.Type, ((Query<Results<Object>.Element>) -> Query<Bool>)?) async throws -> [any Persistable]
public var read: @Sendable (Object.Type, (((Query<Results<Object>.Element>) -> Query<Bool>)?)) async throws -> [Object]
devMinseok marked this conversation as resolved.
Show resolved Hide resolved
public var update: @Sendable (any Persistable) async throws -> Void
public var updateWithValue: @Sendable (Object.Type, Any) async throws -> Void

Expand All @@ -24,10 +24,10 @@ public struct DatabaseClient {
}

public func read<T: Persistable>(_ type: T.Type, where isIncluded: ((Query<Results<T.ManagedObject>.Element>) -> Query<Bool>)?) async throws -> [T] {
let result = try await self.read(type, isIncluded as? ((Query<Results<Object>.Element>) -> Query<Bool>))
return result as! [T]
let result = try await self.read(type.ManagedObject, (isIncluded as! (Query<Results<Object>.Element>) -> Query<Bool>))
return result.compactMap { T(managedObject: $0 as! T.ManagedObject) }
}

public func update<T: Persistable>(object: T) async throws {
try await self.update(object)
}
Expand Down
14 changes: 7 additions & 7 deletions Projects/Core/DatabaseClient/Sources/DatabaseClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ extension DatabaseClient: DependencyKey {

private static func live() -> DatabaseClient {
return .init(
initialize: {
initialize: { configuration in
if realmActor == nil {
realmActor = try await RealmActor()
realmActor = try await RealmActor(configuration: configuration)
} else {
throw(NSError(domain: "Realm already initialized", code: 0))
}
Expand All @@ -34,11 +34,11 @@ extension DatabaseClient: DependencyKey {
},
read: { type, isIncluded in
if let realmActor {
let results = realmActor.read(type.ManagedObject)
let results = await realmActor.read(type)
if let isIncluded {
return results.where(isIncluded).compactMap { type(managedObject: $0) }
return results.where(isIncluded).map { $0 }
} else {
return results.compactMap { type(managedObject: $0) }
return results.map { $0 }
}
} else {
throw(NSError(domain: "Realm is not initialized", code: 0))
Expand Down Expand Up @@ -70,8 +70,8 @@ extension DatabaseClient: DependencyKey {
actor RealmActor {
var realm: Realm!

init() async throws {
realm = try await Realm(actor: self)
init(configuration: Realm.Configuration) async throws {
realm = try await Realm(configuration: configuration, actor: self)
}

func create<T: Object>(_ object: ThreadSafeReference<T>) async throws {
Expand Down