Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Modules/Sources/Storage/GRDB/Model/PersistedProduct.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import GRDB

// periphery:ignore - TODO: remove ignore when populating database
public struct PersistedProduct: Codable {
public let id: Int64
public let siteID: Int64
Expand Down Expand Up @@ -48,6 +49,7 @@ public struct PersistedProduct: Codable {
}
}

// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProduct: FetchableRecord, PersistableRecord {
public static var databaseTableName: String { "product" }

Expand All @@ -67,8 +69,12 @@ extension PersistedProduct: FetchableRecord, PersistableRecord {
static let stockQuantity = Column(CodingKeys.stockQuantity)
static let stockStatusKey = Column(CodingKeys.stockStatusKey)
}

public static let images = hasMany(PersistedProductImage.self)
public static let attributes = hasMany(PersistedProductAttribute.self)
}

// periphery:ignore - TODO: remove ignore when populating database
private extension PersistedProduct {
enum CodingKeys: String, CodingKey {
case id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import GRDB

// periphery:ignore - TODO: remove ignore when populating database
public struct PersistedProductAttribute: Codable {
public private(set) var id: Int64?
public let productID: Int64
Expand All @@ -27,6 +28,7 @@ public struct PersistedProductAttribute: Codable {
}
}

// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProductAttribute: FetchableRecord, MutablePersistableRecord {
public static var databaseTableName: String { "productAttribute" }

Expand All @@ -46,6 +48,7 @@ extension PersistedProductAttribute: FetchableRecord, MutablePersistableRecord {
}


// periphery:ignore - TODO: remove ignore when populating database
private extension PersistedProductAttribute {
enum CodingKeys: String, CodingKey {
case id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import GRDB

// periphery:ignore - TODO: remove ignore when populating database
public struct PersistedProductImage: Codable {
public let id: Int64
public let productID: Int64
Expand All @@ -27,6 +28,7 @@ public struct PersistedProductImage: Codable {
}
}

// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProductImage: FetchableRecord, PersistableRecord {
public static var databaseTableName: String { "productImage" }

Expand All @@ -42,6 +44,7 @@ extension PersistedProductImage: FetchableRecord, PersistableRecord {
}


// periphery:ignore - TODO: remove ignore when populating database
private extension PersistedProductImage {
enum CodingKeys: String, CodingKey {
case id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import GRDB

// periphery:ignore - TODO: remove ignore when populating database
public struct PersistedProductVariation: Codable {
public let id: Int64
public let siteID: Int64
Expand Down Expand Up @@ -39,6 +40,7 @@ public struct PersistedProductVariation: Codable {
}
}

// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProductVariation: FetchableRecord, PersistableRecord {
public static var databaseTableName: String { "productVariation" }

Expand All @@ -55,9 +57,13 @@ extension PersistedProductVariation: FetchableRecord, PersistableRecord {
static let stockQuantity = Column(CodingKeys.stockQuantity)
static let stockStatusKey = Column(CodingKeys.stockStatusKey)
}

public static let attributes = hasMany(PersistedProductVariationAttribute.self).forKey("attributes")
public static let image = hasOne(PersistedProductVariationImage.self).forKey("image")
}


// periphery:ignore - TODO: remove ignore when populating database
private extension PersistedProductVariation {
enum CodingKeys: String, CodingKey {
case id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import GRDB

// periphery:ignore - TODO: remove ignore when populating database
public struct PersistedProductVariationAttribute: Codable {
public private(set) var id: Int64?
public let productVariationID: Int64
Expand All @@ -18,6 +19,8 @@ public struct PersistedProductVariationAttribute: Codable {
}
}

// periphery:ignore - TODO: remove ignore when populating database
// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProductVariationAttribute: FetchableRecord, MutablePersistableRecord {
public static var databaseTableName: String { "productVariationAttribute" }

Expand All @@ -34,6 +37,7 @@ extension PersistedProductVariationAttribute: FetchableRecord, MutablePersistabl
}


// periphery:ignore - TODO: remove ignore when populating database
private extension PersistedProductVariationAttribute {
enum CodingKeys: String, CodingKey {
case id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import GRDB

// periphery:ignore - TODO: remove ignore when populating database
public struct PersistedProductVariationImage: Codable {
public let id: Int64
public let productVariationID: Int64
Expand All @@ -27,6 +28,7 @@ public struct PersistedProductVariationImage: Codable {
}
}

// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProductVariationImage: FetchableRecord, PersistableRecord {
public static var databaseTableName: String { "productVariationImage" }

Expand All @@ -42,6 +44,7 @@ extension PersistedProductVariationImage: FetchableRecord, PersistableRecord {
}


// periphery:ignore - TODO: remove ignore when populating database
private extension PersistedProductVariationImage {
enum CodingKeys: String, CodingKey {
case id
Expand Down
6 changes: 6 additions & 0 deletions Modules/Sources/Storage/GRDB/Model/PersistedSite.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import Foundation
import GRDB

// periphery:ignore - TODO: remove ignore when populating database
public struct PersistedSite: Codable {
// periphery:ignore - TODO: remove ignore when populating database
public let id: Int64

// periphery:ignore - TODO: remove ignore when populating database
public init(id: Int64) {
self.id = id
}
}

// periphery:ignore - TODO: remove ignore when populating database
extension PersistedSite: FetchableRecord, PersistableRecord {
public static var databaseTableName: String { "site" }

public enum Columns {
// periphery:ignore - TODO: remove ignore when populating database
static let id = Column(CodingKeys.id)
}
}

// periphery:ignore - TODO: remove ignore when populating database
private extension PersistedSite {
enum CodingKeys: String, CodingKey {
case id
Expand Down
1 change: 1 addition & 0 deletions Modules/Sources/Storage/Protocols/Object.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public protocol Object: AnyObject {

/// Returns an instance of ObjectID: expected to identify the current instance, unequivocally.
///
// periphery:ignore - Used in tests, no changes but mysteriously stopped being ignored.
var objectID: ObjectID { get }

/// Returns the receiver's Entity Name.
Expand Down
7 changes: 7 additions & 0 deletions Modules/Sources/Yosemite/Model/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,19 @@ public typealias StorageWooShippingShipment = Storage.WooShippingShipment
public typealias StorageWooShippingOriginAddress = Storage.WooShippingOriginAddress

// MARK: - GRDB Persisted Models
// periphery:ignore - TODO: remove ignore when populating database
public typealias PersistedSite = Storage.PersistedSite
// periphery:ignore - TODO: remove ignore when populating database
public typealias PersistedProduct = Storage.PersistedProduct
// periphery:ignore - TODO: remove ignore when populating database
public typealias PersistedProductAttribute = Storage.PersistedProductAttribute
// periphery:ignore - TODO: remove ignore when populating database
public typealias PersistedProductImage = Storage.PersistedProductImage
// periphery:ignore - TODO: remove ignore when populating database
public typealias PersistedProductVariation = Storage.PersistedProductVariation
// periphery:ignore - TODO: remove ignore when populating database
public typealias PersistedProductVariationAttribute = Storage.PersistedProductVariationAttribute
// periphery:ignore - TODO: remove ignore when populating database
public typealias PersistedProductVariationImage = Storage.PersistedProductVariationImage

// MARK: - Internal ReadOnly Models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import Foundation
import Storage

// MARK: - PersistedProduct Conversions
// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProduct {
public init(from posProduct: POSProduct) {
init(from posProduct: POSProduct) {
self.init(
id: posProduct.productID,
siteID: posProduct.siteID,
Expand All @@ -22,7 +23,7 @@ extension PersistedProduct {
)
}

public func toPOSProduct(images: [ProductImage] = [], attributes: [ProductAttribute] = []) -> POSProduct {
func toPOSProduct(images: [ProductImage] = [], attributes: [ProductAttribute] = []) -> POSProduct {
return POSProduct(
siteID: siteID,
productID: id,
Expand All @@ -42,11 +43,49 @@ extension PersistedProduct {
stockStatusKey: stockStatusKey
)
}

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.

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

return toPOSProduct(
images: images.map { $0.toProductImage() },
attributes: attributes.map { $0.toProductAttribute(siteID: siteID) }
)
}

}

// MARK: - POSProduct Storage Extensions
// periphery:ignore - TODO: remove ignore when populating database
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.

let product = PersistedProduct(from: self)
try product.insert(db)

// Save related images
for image in self.images {
let persistedImage = PersistedProductImage(from: image, productID: self.productID)
try persistedImage.insert(db)
}

// Save related attributes
for attribute in self.attributes {
var persistedAttribute = PersistedProductAttribute(from: attribute, productID: self.productID)
try persistedAttribute.insert(db)
}
}
}
}

// MARK: - PersistedProductAttribute Conversions
// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProductAttribute {
public init(from productAttribute: ProductAttribute, productID: Int64) {
init(from productAttribute: ProductAttribute, productID: Int64) {
self.init(
productID: productID,
name: productAttribute.name,
Expand All @@ -57,7 +96,7 @@ extension PersistedProductAttribute {
)
}

public func toProductAttribute(siteID: Int64) -> ProductAttribute {
func toProductAttribute(siteID: Int64) -> ProductAttribute {
return ProductAttribute(
siteID: siteID,
attributeID: id ?? 0,
Expand All @@ -71,8 +110,9 @@ extension PersistedProductAttribute {
}

// MARK: - PersistedProductImage Conversions
// periphery:ignore - TODO: remove ignore when populating database
extension PersistedProductImage {
public init(from productImage: ProductImage, productID: Int64) {
init(from productImage: ProductImage, productID: Int64) {
self.init(
id: productImage.imageID,
productID: productID,
Expand All @@ -84,7 +124,7 @@ extension PersistedProductImage {
)
}

public func toProductImage() -> ProductImage {
func toProductImage() -> ProductImage {
return ProductImage(
imageID: id,
dateCreated: dateCreated,
Expand Down
Loading