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

Extract EncodableRecord from MutablePersistableRecord #499

Merged
merged 4 commits into from
Mar 10, 2019

Conversation

groue
Copy link
Owner

@groue groue commented Mar 10, 2019

This PR introduces a new record protocol, EncodableRecord, and redefines PersistableRecord on top of it:

// Defines how a record encodes itself into the database
protocol EncodableRecord {
    /// Defines the values persisted in the database
    func encode(to container: inout PersistenceContainer)
}

// Adds persistence methods
protocol MutablePersistableRecord: TableRecord, EncodableRecord {
    /// Optional method that lets your adopting type store its rowID upon
    /// successful insertion. Don't call it directly: it is called for you.
    mutating func didInsert(with rowID: Int64, for column: String?)
}

// Adds immutability
protocol PersistableRecord: MutablePersistableRecord {
    /// Non-mutating version of the optional didInsert(with:for:)
    func didInsert(with rowID: Int64, for column: String?)
}

An EncodableRecord type defines its database encoding, but remains deprived from the persistence methods granted by PersistableRecord. This makes it possible to use all Association APIs on read-only records.

Before this PR, one had to conform to PersistableRecord in order to use an association request(for:) method. #426 has identified this as an annoyance.

Despite the change in the record protocol hierarchy, this is a purely additive PR, which should not break any existing code. No test for public APIs had to be changed.

@groue groue merged commit 4ce6271 into GRDB-4.0 Mar 10, 2019
@groue groue deleted the feature/EndodableRecord branch March 10, 2019 22:01
groue added a commit that referenced this pull request Mar 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant