Skip to content

Commit 65a94c8

Browse files
Added a more complete DatastoreFormat example
1 parent bb1feb7 commit 65a94c8

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Please check the [releases](https://github.com/mochidev/CodableDatastore/release
2929
dependencies: [
3030
.package(
3131
url: "https://github.com/mochidev/CodableDatastore.git",
32-
.upToNextMinor(from: "0.2.0")
32+
.upToNextMinor(from: "0.2.1")
3333
),
3434
],
3535
...

Sources/CodableDatastore/Datastore/DatastoreFormat.swift

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ import Foundation
1414
///
1515
/// This type also exists so implementers can conform a `struct` to it that declares a number of key paths as stored properties.
1616
///
17-
/// Conformers can create subtypes for their versioned models either in the body of their struct or in legacy extentions. Additionally, you are encouraged to make **static** properties available for things like the current version, or a configured ``Datastore`` — this allows easy access to them without mucking around declaring them in far-away places in your code base.
17+
/// Conformers can create subtypes for their versioned models either in the body of their struct or in legacy extensions. Additionally, you are encouraged to make **static** properties available for things like the current version, or a configured ``Datastore`` — this allows easy access to them without mucking around declaring them in far-away places in your code base.
1818
///
1919
/// ```swift
2020
/// struct BooksFormat {
2121
/// static let defaultKey: DatastoreKey = "BooksStore"
22-
/// static let currentVersion: Version = .one
22+
/// static let currentVersion: Version = .current
2323
///
2424
/// enum Version: String {
25-
/// case zero = "2024-04-01"
26-
/// case one = "2024-04-09"
25+
/// case v1 = "2024-04-01"
26+
/// case current = "2024-04-09"
2727
/// }
2828
///
2929
/// typealias Instance = Book
@@ -41,9 +41,36 @@ import Foundation
4141
/// var isbn: ISBN
4242
/// }
4343
///
44+
/// static func datastore(for persistence: DiskPersistence<ReadWrite>) -> Datastore {
45+
/// .JSONStore(
46+
/// persistence: persistence,
47+
/// migrations: [
48+
/// .v1: { data, decoder in
49+
/// Book(try decoder.decode(BookV1.self, from: data))
50+
/// },
51+
/// .current: { data, decoder in
52+
/// try decoder.decode(Book.self, from: data)
53+
/// }
54+
/// ]
55+
/// )
56+
/// }
57+
///
4458
/// let title = Index(\.title)
45-
/// let author = ManyToMany(\.author)
46-
/// let isbn = OneToOne(\.isbn)
59+
/// let author = ManyToManyIndex(\.authors)
60+
/// let isbn = OneToOneIndex(\.isbn)
61+
/// }
62+
///
63+
/// typealias Book = BooksFormat.Book
64+
///
65+
/// extension Book {
66+
/// init(_ bookV1: BooksFormat.BookV1) {
67+
/// self.init(
68+
/// id: id,
69+
/// title: SortableTitle(title),
70+
/// authors: [AuthorID(authors)],
71+
/// isbn: ISBN.generate()
72+
/// )
73+
/// }
4774
/// }
4875
/// ```
4976
///

0 commit comments

Comments
 (0)