Skip to content

Commit 44b1b34

Browse files
Renamed all datastore accessors to enable better autocomplete options
1 parent 47cacfc commit 44b1b34

File tree

1 file changed

+118
-60
lines changed

1 file changed

+118
-60
lines changed

Sources/CodableDatastore/Datastore/Datastore.swift

Lines changed: 118 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,11 @@ extension Datastore {
469469
}
470470
}
471471

472-
/// Load an instance with a given identifier, or return nil if one is not found.
472+
/// Load an instance with a given identifier, or return `nil` if one is not found.
473+
///
473474
/// - Parameter identifier: The identifier of the instance to load.
474-
/// - Returns: The instance keyed to the identifier, or nil if none are found.
475-
public func load(_ identifier: IdentifierType) async throws -> InstanceType? {
475+
/// - Returns: The instance keyed to the identifier, or `nil` if none are found.
476+
public func load(id identifier: IdentifierType) async throws -> InstanceType? {
476477
try await warmupIfNeeded()
477478

478479
return try await persistence._withTransaction(
@@ -499,6 +500,7 @@ extension Datastore {
499500
}
500501

501502
/// **Internal:** Load a range of instances from a datastore based on the identifier range passed in as an async sequence.
503+
///
502504
/// - Parameters:
503505
/// - identifierRange: The range to load.
504506
/// - order: The order to process instances in.
@@ -538,13 +540,13 @@ extension Datastore {
538540

539541
/// Load a range of instances from a datastore based on the identifier range passed in as an async sequence.
540542
///
541-
/// The sequence should be consumed a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
543+
/// - Important: The sequence should be consumed at most a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
542544
/// - Parameters:
543545
/// - identifierRange: The range to load.
544546
/// - order: The order to process instances in.
545547
/// - Returns: An asynchronous sequence containing the instances matching the range of identifiers.
546548
public nonisolated func load(
547-
_ identifierRange: some IndexRangeExpression<IdentifierType> & Sendable,
549+
range identifierRange: some IndexRangeExpression<IdentifierType> & Sendable,
548550
order: RangeOrder = .ascending
549551
) -> some TypedAsyncSequence<InstanceType> & Sendable where IdentifierType: RangedIndexable {
550552
_load(range: identifierRange, order: order, awaitWarmup: true)
@@ -553,28 +555,29 @@ extension Datastore {
553555

554556
/// Load a range of instances from a datastore based on the identifier range passed in as an async sequence.
555557
///
556-
/// The sequence should be consumed a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
558+
/// - Important: The sequence should be consumed at most a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
557559
/// - Parameters:
558560
/// - identifierRange: The range to load.
559561
/// - order: The order to process instances in.
560562
/// - Returns: An asynchronous sequence containing the instances matching the range of identifiers.
561563
@_disfavoredOverload
564+
@inlinable
562565
public nonisolated func load(
563-
_ identifierRange: IndexRange<IdentifierType>,
566+
range identifierRange: IndexRange<IdentifierType>,
564567
order: RangeOrder = .ascending
565568
) -> some TypedAsyncSequence<InstanceType> & Sendable where IdentifierType: RangedIndexable {
566-
load(identifierRange, order: order)
569+
load(range: identifierRange, order: order)
567570
}
568571

569572
/// Load all instances in a datastore as an async sequence.
570573
///
571-
/// The sequence should be consumed a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
574+
/// - Important: The sequence should be consumed at most a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
572575
/// - Parameters:
573576
/// - unboundedRange: The range to load. Specify `...` to load every instance.
574577
/// - order: The order to process instances in.
575578
/// - Returns: An asynchronous sequence containing all the instances.
576579
public nonisolated func load(
577-
_ unboundedRange: Swift.UnboundedRange,
580+
range unboundedRange: Swift.UnboundedRange,
578581
order: RangeOrder = .ascending
579582
) -> some TypedAsyncSequence<InstanceType> & Sendable {
580583
_load(range: IndexRange.unbounded, order: order, awaitWarmup: true)
@@ -648,36 +651,36 @@ extension Datastore {
648651
///
649652
/// This is conceptually similar to loading all instances and filtering only those who's indexed key path matches the specified value, but is much more efficient as an index is already maintained for that value.
650653
///
651-
/// The sequence should be consumed a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
654+
/// - Important: The sequence should be consumed at most a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
652655
/// - Parameters:
653-
/// - value: The value to match against.
654-
/// - order: The order to process instances in.
655656
/// - index: The index to load from.
657+
/// - value: The value to match against.
658+
/// - order: The order to process instances in.
656659
/// - Returns: An asynchronous sequence containing the instances matching the specified indexed value.
657660
public nonisolated func load<
658661
Value: DiscreteIndexable,
659662
Index: RetrievableIndexRepresentation<InstanceType, Value>
660663
>(
661-
_ value: Value,
662-
order: RangeOrder = .ascending,
663-
from index: KeyPath<Format, Index>
664+
index: KeyPath<Format, Index>,
665+
value: Value,
666+
order: RangeOrder = .ascending
664667
) -> some TypedAsyncSequence<InstanceType> & Sendable {
665668
_load(index: index, range: IndexRange(only: value), order: order)
666669
}
667670

668-
/// Load an instance with the matching indexed value, or return nil if one is not found.
671+
/// Load an instance with the matching indexed value, or return `nil` if one is not found.
669672
///
670673
/// This requires either a ``DatastoreFormat/OneToOneIndex`` or ``DatastoreFormat/ManyToOneIndex`` to be declared as the index, and a guarantee on the caller's part that at most only a single instance will match the specified value. If multiple instancess match, the one with the identifier that sorts first will be returned.
671674
/// - Parameters:
672-
/// - value: The value to match against.
673675
/// - index: The index to load from.
674-
/// - Returns: The instance keyed to the specified indexed value, or nil if none are found.
676+
/// - value: The value to match against.
677+
/// - Returns: The instance keyed to the specified indexed value, or `nil` if none are found.
675678
public nonisolated func load<
676679
Value: DiscreteIndexable,
677680
Index: SingleInstanceIndexRepresentation<InstanceType, Value>
678681
>(
679-
_ value: Value,
680-
from index: KeyPath<Format, Index>
682+
index: KeyPath<Format, Index>,
683+
value: Value
681684
) async throws -> InstanceType? {
682685
try await _load(index: index, range: IndexRange(only: value)).first(where: { _ in true })
683686
}
@@ -686,19 +689,19 @@ extension Datastore {
686689
///
687690
/// This is conceptually similar to loading all instances and filtering only those who's indexed key path matches the specified range, but is much more efficient as an index is already maintained for that range of values.
688691
///
689-
/// The sequence should be consumed a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
692+
/// - Important: The sequence should be consumed at most a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
690693
/// - Parameters:
694+
/// - index: The index to load from.
691695
/// - range: The range to load.
692696
/// - order: The order to process instances in.
693-
/// - index: The index to load from.
694697
/// - Returns: An asynchronous sequence containing the instances matching the range of values in that sequence.
695698
public nonisolated func load<
696699
Value: RangedIndexable,
697700
Index: RetrievableIndexRepresentation<InstanceType, Value>
698701
>(
699-
_ range: some IndexRangeExpression<Value> & Sendable,
700-
order: RangeOrder = .ascending,
701-
from index: KeyPath<Format, Index>
702+
index: KeyPath<Format, Index>,
703+
range: some IndexRangeExpression<Value> & Sendable,
704+
order: RangeOrder = .ascending
702705
) -> some TypedAsyncSequence<InstanceType> & Sendable {
703706
_load(index: index, range: range, order: order)
704707
}
@@ -707,38 +710,38 @@ extension Datastore {
707710
///
708711
/// This is conceptually similar to loading all instances and filtering only those who's indexed key path matches the specified range, but is much more efficient as an index is already maintained for that range of values.
709712
///
710-
/// The sequence should be consumed a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
713+
/// - Important: The sequence should be consumed at most a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
711714
/// - Parameters:
715+
/// - index: The index to load from.
712716
/// - range: The range to load.
713717
/// - order: The order to process instances in.
714-
/// - index: The index to load from.
715718
/// - Returns: An asynchronous sequence containing the instances matching the range of values in that sequence.
716719
@_disfavoredOverload
717720
public nonisolated func load<
718721
Value: RangedIndexable,
719722
Index: RetrievableIndexRepresentation<InstanceType, Value>
720723
>(
721-
_ range: IndexRange<Value>,
722-
order: RangeOrder = .ascending,
723-
from index: KeyPath<Format, Index>
724+
index: KeyPath<Format, Index>,
725+
range: IndexRange<Value>,
726+
order: RangeOrder = .ascending
724727
) -> some TypedAsyncSequence<InstanceType> & Sendable {
725728
_load(index: index, range: range, order: order)
726729
}
727730

728731
/// Load all instances in a datastore in index order as an async sequence.
729732
///
730-
/// The sequence should be consumed a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
733+
/// - Important: The sequence should be consumed at most a single time, ideally within the same transaction it was created in as it holds a reference to that transaction and thus snapshot of the datastore for data consistency.
731734
///
732-
/// - Note: If the index is a Mant-to-Any type of index, a smaller or larger number of results may be returned here, as some instances may not be respresented in the index, while others are other-represented and may show up multiple times.
735+
/// - Note: If the index is a Many-to-Any type of index, a smaller or larger number of results may be returned here, as some instances may not be respresented in the index, while others are over-represented and may show up multiple times.
733736
/// - Parameters:
737+
/// - index: The index to load from.
734738
/// - unboundedRange: The range to load. Specify `...` to load every instance.
735739
/// - order: The order to process instances in.
736-
/// - index: The index to load from.
737740
/// - Returns: An asynchronous sequence containing all the instances, ordered by the specified index.
738741
public nonisolated func load<Index: IndexRepresentation<InstanceType>>(
739-
_ unboundedRange: Swift.UnboundedRange,
740-
order: RangeOrder = .ascending,
741-
from index: KeyPath<Format, Index>
742+
index: KeyPath<Format, Index>,
743+
range unboundedRange: Swift.UnboundedRange,
744+
order: RangeOrder = .ascending
742745
) -> some TypedAsyncSequence<InstanceType> & Sendable {
743746
_load(index: index, range: IndexRange.unbounded, order: order)
744747
}
@@ -747,11 +750,19 @@ extension Datastore {
747750
// MARK: - Observation
748751

749752
extension Datastore {
750-
public func observe(_ identifier: IdentifierType) async throws -> some TypedAsyncSequence<ObservedEvent<IdentifierType, InstanceType>> & Sendable {
751-
try await self.observe()
752-
.filter { $0.id == identifier }
753+
/// Observe changes made to an instance with the given identifier.
754+
///
755+
/// - Parameter identifier: The identifier of the instance to observe.
756+
/// - Returns: An unbounded asynchronous sequence reporting changes to the observed instance.
757+
public func observe(
758+
id identifier: IdentifierType
759+
) async throws -> some TypedAsyncSequence<ObservedEvent<IdentifierType, InstanceType>> & Sendable {
760+
try await observe().filter { $0.id == identifier }
753761
}
754762

763+
/// Observe all changes made to a datastore.
764+
///
765+
/// - Returns: An unbounded asynchronous sequence reporting changes to the datastore.
755766
public func observe() async throws -> some TypedAsyncSequence<ObservedEvent<IdentifierType, InstanceType>> & Sendable {
756767
try await warmupIfNeeded()
757768

@@ -786,7 +797,10 @@ extension Datastore where AccessMode == ReadWrite {
786797
/// - instance: The instance to persist.
787798
/// - identifier: The unique identifier to use to reference the item being persisted.
788799
@discardableResult
789-
public func persist(_ instance: InstanceType, to identifier: IdentifierType) async throws -> InstanceType? {
800+
public func persist(
801+
_ instance: InstanceType,
802+
to identifier: IdentifierType
803+
) async throws -> InstanceType? {
790804
try await warmupIfNeeded()
791805

792806
let updatedDescriptor = try self.generateUpdatedDescriptor()
@@ -964,19 +978,36 @@ extension Datastore where AccessMode == ReadWrite {
964978
/// - instance: The instance to persist.
965979
/// - keypath: The keypath the identifier is located at.
966980
@discardableResult
967-
public func persist(_ instance: InstanceType, id keypath: KeyPath<InstanceType, IdentifierType>) async throws -> InstanceType? {
981+
public func persist(
982+
_ instance: InstanceType,
983+
id keypath: KeyPath<InstanceType, IdentifierType>
984+
) async throws -> InstanceType? {
968985
try await persist(instance, to: instance[keyPath: keypath])
969986
}
970987

988+
/// Delete the instance with the given identifier from the datastore.
989+
///
990+
/// - Throws:Throws ``DatastoreInterfaceError/instanceNotFound`` if the instance does not exist.
991+
/// - Parameter identifier: The identifier of the instance to delete.
992+
/// - Returns: A copy of the instance that was deleted as it existed in the datastore.
993+
@inlinable
971994
@discardableResult
972-
public func delete(_ identifier: IdentifierType) async throws -> InstanceType {
973-
guard let deletedInstance = try await deleteIfPresent(identifier)
995+
public func delete(
996+
id identifier: IdentifierType
997+
) async throws -> InstanceType {
998+
guard let deletedInstance = try await deleteIfPresent(id: identifier)
974999
else { throw DatastoreInterfaceError.instanceNotFound }
9751000
return deletedInstance
9761001
}
9771002

1003+
/// Delete the instance with the given identifier from the datastore whether it exists or not.
1004+
///
1005+
/// - Parameter identifier: The identifier of the instance to delete.
1006+
/// - Returns: A copy of the instance that was deleted as it existed in the datastore, or `nil` if none are found.
9781007
@discardableResult
979-
public func deleteIfPresent(_ identifier: IdentifierType) async throws -> InstanceType? {
1008+
public func deleteIfPresent(
1009+
id identifier: IdentifierType
1010+
) async throws -> InstanceType? {
9801011
try await warmupIfNeeded()
9811012

9821013
return try await persistence._withTransaction(
@@ -1085,32 +1116,59 @@ extension Datastore where InstanceType: Identifiable, IdentifierType == Instance
10851116
///
10861117
/// If an instance does not already exist for the specified identifier, it will be created. If an instance already exists, it will be updated.
10871118
/// - Parameter instance: The instance to persist.
1088-
@_disfavoredOverload
1119+
@inlinable
10891120
@discardableResult
1090-
public func persist(_ instance: InstanceType) async throws -> InstanceType? where AccessMode == ReadWrite {
1091-
try await self.persist(instance, to: instance.id)
1121+
public func persist(
1122+
_ instance: InstanceType
1123+
) async throws -> InstanceType? where AccessMode == ReadWrite {
1124+
try await persist(instance, to: instance.id)
10921125
}
10931126

1094-
@_disfavoredOverload
1127+
/// Delete the instance with the same identifier from the datastore.
1128+
///
1129+
/// - Throws:Throws ``DatastoreInterfaceError/instanceNotFound`` if the instance does not exist.
1130+
/// - Parameter instance: A copy of the instance to delete.
1131+
/// - Returns: A copy of the instance that was deleted as it existed in the datastore.
1132+
@inlinable
10951133
@discardableResult
1096-
public func delete(_ instance: InstanceType) async throws -> InstanceType where AccessMode == ReadWrite {
1097-
try await self.delete(instance.id)
1134+
public func delete(
1135+
instance: InstanceType
1136+
) async throws -> InstanceType where AccessMode == ReadWrite {
1137+
try await delete(id: instance.id)
10981138
}
10991139

1100-
@_disfavoredOverload
1140+
/// Delete the instance with the same identifier from the datastore whether it exists or not.
1141+
///
1142+
/// - Parameter instance: A copy of the instance to delete.
1143+
/// - Returns: A copy of the instance that was deleted as it existed in the datastore, or `nil` if none are found.
1144+
@inlinable
11011145
@discardableResult
1102-
public func deleteIfPresent(_ instance: InstanceType) async throws -> InstanceType? where AccessMode == ReadWrite {
1103-
try await self.deleteIfPresent(instance.id)
1146+
public func deleteIfPresent(
1147+
instance: InstanceType
1148+
) async throws -> InstanceType? where AccessMode == ReadWrite {
1149+
try await deleteIfPresent(id: instance.id)
11041150
}
11051151

1106-
@_disfavoredOverload
1107-
public func load(_ instance: InstanceType) async throws -> InstanceType? {
1108-
try await self.load(instance.id)
1152+
/// Reload an instance with a given identifier and return it, or return `nil` if one is not found.
1153+
///
1154+
/// - Parameter instance: A copy of the instance to load.
1155+
/// - Returns: The instance keyed to the identifier, or `nil` if none are found.
1156+
@inlinable
1157+
public func load(
1158+
instance: InstanceType
1159+
) async throws -> InstanceType? {
1160+
try await load(id: instance.id)
11091161
}
11101162

1111-
@_disfavoredOverload
1112-
public func observe(_ instance: InstanceType) async throws -> some TypedAsyncSequence<ObservedEvent<IdentifierType, InstanceType>> & Sendable {
1113-
try await observe(instance.id)
1163+
/// Observe changes made to an instance with a given identifier.
1164+
///
1165+
/// - Parameter identifier: A copy of the instance to observe.
1166+
/// - Returns: An unbounded asynchronous sequence reporting changes to the observed instance.
1167+
@inlinable
1168+
public func observe(
1169+
instance: InstanceType
1170+
) async throws -> some TypedAsyncSequence<ObservedEvent<IdentifierType, InstanceType>> & Sendable {
1171+
try await observe(id: instance.id)
11141172
}
11151173
}
11161174

0 commit comments

Comments
 (0)