Skip to content

Conversation

@joshheald
Copy link
Contributor

@joshheald joshheald commented Aug 29, 2025

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.


  • I have considered if this change warrants user-facing release notes and have added them to RELEASE-NOTES.txt if necessary.

@joshheald joshheald added this to the 23.2 milestone Aug 29, 2025
@joshheald joshheald added type: task An internally driven task. status: feature-flagged Behind a feature flag. Milestone is not strongly held. feature: POS labels Aug 29, 2025
@joshheald joshheald force-pushed the woomob-1210-relationship-based-mapping-and-saving-for-POS-entities branch from 10ff73e to f416265 Compare August 29, 2025 17:57
@wpmobilebot
Copy link
Collaborator

wpmobilebot commented Aug 29, 2025

App Icon📲 You can test the changes from this Pull Request in WooCommerce iOS Prototype by scanning the QR code below to install the corresponding build.

App NameWooCommerce iOS Prototype
Build Numberpr16063-d0c1b2d
Version23.1
Bundle IDcom.automattic.alpha.woocommerce
Commitd0c1b2d
Installation URL1oiurjq0cs5co
Automatticians: You can use our internal self-serve MC tool to give yourself access to those builds if needed.

@joshheald joshheald requested a review from jaclync August 29, 2025 18:22
)
}

func toPOSProduct(db: GRDBDatabaseConnection) throws -> POSProduct {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Comment on lines 47 to 52
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)
}
Copy link
Contributor

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:

Suggested change
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)
}

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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.


// Save related attributes
for attribute in self.attributes {
var persistedAttribute = PersistedProductVariationAttribute(from: attribute, productVariationID: self.productVariationID)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: could be let?

Copy link
Contributor Author

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.

@joshheald joshheald marked this pull request as ready for review September 1, 2025 10:00
@joshheald joshheald merged commit 2159f0d into woomob-1210-basic-mapping-from-POS-entities-to-persisted Sep 1, 2025
14 checks passed
@joshheald joshheald deleted the woomob-1210-relationship-based-mapping-and-saving-for-POS-entities branch September 1, 2025 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature: POS status: feature-flagged Behind a feature flag. Milestone is not strongly held. type: task An internally driven task.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants