-
Notifications
You must be signed in to change notification settings - Fork 121
[Woo POS][Local catalog] Add relationship mapping and saving for POS models #16063
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
Conversation
10ff73e to
f416265
Compare
|
|
| ) | ||
| } | ||
|
|
||
| func toPOSProduct(db: GRDBDatabaseConnection) throws -> POSProduct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ what are the use cases for fetching products, then their relationships separately, as this method seems to do? I'd think the products (with some filter) are fetched including the relationships?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think they are – relationships are just foreign keys. If you fetch a record from SQLite, you'll get the id for looking up the relationship, but it won't walk the graph for you. I'll double check that GRDB doesn't do it for us, but I don't think it does.
| let images = try db.read { db in | ||
| return try request(for: PersistedProduct.images).fetchAll(db) | ||
| } | ||
| let attributes = try db.read { db in | ||
| return try request(for: PersistedProduct.attributes).fetchAll(db) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could these be combined into one database transaction, potentially saving some overhead from each db.read:
| let images = try db.read { db in | |
| return try request(for: PersistedProduct.images).fetchAll(db) | |
| } | |
| let attributes = try db.read { db in | |
| return try request(for: PersistedProduct.attributes).fetchAll(db) | |
| } | |
| let (images, attributes) = try db.read { db in | |
| let images = try request(for: PersistedProduct.images).fetchAll(db) | |
| let attributes = try request(for: PersistedProduct.attributes).fetchAll(db) | |
| return (images, attributes) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both done in e3d7b85
| // MARK: - POSProduct Storage Extensions | ||
| extension POSProduct { | ||
| public func save(to db: GRDBDatabaseConnection) throws { | ||
| try db.write { db in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you know if it's recommended to weak self in the db.write closure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know of a recommendation.
The updates closure that we pass to write is non-escaping, so it can't be stored by GRDB, and we don't hold it beyond our function scope either.
I don't see any reason to make self weak here, at least not to avoid retain cycles.
Modules/Sources/Yosemite/Model/Storage/PersistedProductVariation+Conversions.swift
Outdated
Show resolved
Hide resolved
|
|
||
| // Save related attributes | ||
| for attribute in self.attributes { | ||
| var persistedAttribute = PersistedProductVariationAttribute(from: attribute, productVariationID: self.productVariationID) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: could be let?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No – insert is mutating for attributes, because of the autoincremented ID.
…d' into woomob-1210-relationship-based-mapping-and-saving-for-POS-entities
…OS-entities' into woomob-1210-tests-for-saving-pos-models
2159f0d
into
woomob-1210-basic-mapping-from-POS-entities-to-persisted

Part of: WOOMOB-1210
Merge after: #16062
Description
This PR add functions to save whole POSProduct and POSProductVariation models, complete with their sub-models, i.e. attributes and images.
Tests got a bit out of hand, I've put them in a separate PR
Steps to reproduce
None, these are not used in the app yet, just check unit tests.
RELEASE-NOTES.txtif necessary.