Skip to content

Commit 91dd0b9

Browse files
authored
chore: kickoff release
2 parents 12511d1 + e886cf0 commit 91dd0b9

File tree

9 files changed

+47
-13
lines changed

9 files changed

+47
-13
lines changed

Amplify/Categories/DataStore/Model/Internal/Schema/ModelField+Association.swift

+11-5
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import Foundation
8888
/// directly by host applications. The behavior of this may change without warning.
8989
public enum ModelAssociation {
9090
case hasMany(associatedFieldName: String?, associatedFieldNames: [String] = [])
91-
case hasOne(associatedFieldName: String?, targetNames: [String])
91+
case hasOne(associatedFieldName: String?, associatedFieldNames: [String] = [], targetNames: [String])
9292
case belongsTo(associatedFieldName: String?, targetNames: [String])
9393

9494
public static let belongsTo: ModelAssociation = .belongsTo(associatedFieldName: nil, targetNames: [])
@@ -108,14 +108,20 @@ public enum ModelAssociation {
108108
)
109109
}
110110

111-
@available(*, deprecated, message: "Use hasOne(associatedWith:targetNames:)")
111+
@available(*, deprecated, message: "Use hasOne(associatedWith:associatedFields:targetNames:)")
112112
public static func hasOne(associatedWith: CodingKey?, targetName: String? = nil) -> ModelAssociation {
113113
let targetNames = targetName.map { [$0] } ?? []
114114
return .hasOne(associatedWith: associatedWith, targetNames: targetNames)
115115
}
116116

117-
public static func hasOne(associatedWith: CodingKey?, targetNames: [String] = []) -> ModelAssociation {
118-
return .hasOne(associatedFieldName: associatedWith?.stringValue, targetNames: targetNames)
117+
public static func hasOne(
118+
associatedWith: CodingKey? = nil,
119+
associatedFields: [CodingKey] = [],
120+
targetNames: [String] = []) -> ModelAssociation {
121+
return .hasOne(
122+
associatedFieldName: associatedWith?.stringValue,
123+
associatedFieldNames: associatedFields.map { $0.stringValue },
124+
targetNames: targetNames)
119125
}
120126

121127
@available(*, deprecated, message: "Use belongsTo(associatedWith:targetNames:)")
@@ -254,7 +260,7 @@ extension ModelField {
254260
let associatedModel = requiredAssociatedModelName
255261
switch association {
256262
case .belongsTo(let associatedKey, _),
257-
.hasOne(let associatedKey, _),
263+
.hasOne(let associatedKey, _, _),
258264
.hasMany(let associatedKey, _):
259265
// TODO handle modelName casing (convert to camelCase)
260266
let key = associatedKey ?? associatedModel

Amplify/Categories/DataStore/Model/Internal/Schema/ModelSchema+Definition.swift

+15
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,21 @@ public enum ModelFieldDefinition {
279279
association: .hasOne(associatedWith: associatedKey, targetNames: targetNames))
280280
}
281281

282+
public static func hasOne(_ key: CodingKey,
283+
is nullability: ModelFieldNullability = .required,
284+
isReadOnly: Bool = false,
285+
ofType type: Model.Type,
286+
associatedFields associatedKeys: [CodingKey],
287+
targetNames: [String] = []) -> ModelFieldDefinition {
288+
return .field(key,
289+
is: nullability,
290+
isReadOnly: isReadOnly,
291+
ofType: .model(type: type),
292+
association: .hasOne(associatedWith: associatedKeys.first,
293+
associatedFields: associatedKeys,
294+
targetNames: targetNames))
295+
}
296+
282297
public static func belongsTo(_ key: CodingKey,
283298
is nullability: ModelFieldNullability = .required,
284299
isReadOnly: Bool = false,

AmplifyPlugins/API/Sources/AWSAPIPlugin/Core/AppSyncListProvider.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public class AppSyncListProvider<Element: Model>: ModelListProvider {
268268
}
269269
let defaultFieldName = modelSchema.name.camelCased() + field.pascalCased() + "Id"
270270
switch modelField.association {
271-
case .belongsTo(_, let targetNames), .hasOne(_, let targetNames):
271+
case .belongsTo(_, let targetNames), .hasOne(_, _, let targetNames):
272272
guard !targetNames.isEmpty else {
273273
return [defaultFieldName]
274274

AmplifyPlugins/API/Tests/APIHostApp/AWSAPIPluginGen2GraphQLTests/Gen2_4/Customer4+Schema.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extension Customer4 {
3232
model.fields(
3333
.field(customer4.id, is: .required, ofType: .string),
3434
.field(customer4.name, is: .optional, ofType: .string),
35-
.hasOne(customer4.activeCart, is: .optional, ofType: Cart4.self, associatedWith: Cart4.keys.customer),
35+
.hasOne(customer4.activeCart, is: .optional, ofType: Cart4.self, associatedFields: [Cart4.keys.customer]),
3636
.field(customer4.createdAt, is: .optional, isReadOnly: true, ofType: .dateTime),
3737
.field(customer4.updatedAt, is: .optional, isReadOnly: true, ofType: .dateTime)
3838
)

AmplifyPlugins/Core/AWSPluginsCore/Model/Support/Model+GraphQL.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ extension Model {
229229
let defaultFieldName = modelName.camelCased() + modelField.name.pascalCased() + "Id"
230230
if case let .belongsTo(_, targetNames) = modelField.association, !targetNames.isEmpty {
231231
return targetNames
232-
} else if case let .hasOne(_, targetNames) = modelField.association,
232+
} else if case let .hasOne(_, _, targetNames) = modelField.association,
233233
!targetNames.isEmpty {
234234
return targetNames
235235
}

AmplifyPlugins/Core/AWSPluginsCore/Model/Support/QueryPredicate+GraphQL.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ extension QueryPredicateOperation: GraphQLFilterConvertible {
131131
}
132132
let targetName = targetNames.first ?? defaultFieldName
133133
return targetName
134-
case .hasOne(_, let targetNames):
134+
case .hasOne(_, _, let targetNames):
135135
guard targetNames.count == 1 else {
136136
preconditionFailure("QueryPredicate not supported on associated field with composite key: \(field)")
137137
}

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/SQLite/ModelSchema+SQLite.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ extension ModelField: SQLColumn {
8282
var sqlName: String {
8383
if case let .belongsTo(_, targetNames) = association {
8484
return foreignKeySqlName(withAssociationTargets: targetNames)
85-
} else if case let .hasOne(_, targetNames) = association {
85+
} else if case let .hasOne(_, _, targetNames) = association {
8686
return foreignKeySqlName(withAssociationTargets: targetNames)
8787
}
8888
return name

AmplifyPlugins/DataStore/Sources/AWSDataStorePlugin/Storage/SQLite/Statement+Model.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ extension Statement: StatementModelConvertible {
203203

204204
private func getTargetNames(field: ModelField) -> [String] {
205205
switch field.association {
206-
case let .some(.hasOne(_, targetNames)):
206+
case let .some(.hasOne(_, _, targetNames)):
207207
return targetNames
208208
case let .some(.belongsTo(_, targetNames)):
209209
return targetNames

AmplifyTests/CategoryTests/DataStore/ModelFieldAssociationTests.swift

+15-2
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,37 @@ class ModelFieldAssociationTests: XCTestCase {
3737

3838
func testHasOneWithCodingKeys() {
3939
let hasOne = ModelAssociation.hasOne(associatedWith: Comment.keys.post, targetNames: [])
40-
guard case .hasOne(let fieldName, let target) = hasOne else {
40+
guard case .hasOne(let fieldName, let fieldNames, let target) = hasOne else {
4141
XCTFail("Should create hasOne association")
4242
return
4343
}
4444
XCTAssertEqual(fieldName, Comment.keys.post.stringValue)
45+
XCTAssertEqual(fieldNames, [])
4546
XCTAssertEqual(target, [])
4647
}
4748

4849
func testHasOneWithCodingKeysWithTargetName() {
4950
let hasOne = ModelAssociation.hasOne(associatedWith: Comment.keys.post, targetNames: ["postID"])
50-
guard case .hasOne(let fieldName, let target) = hasOne else {
51+
guard case .hasOne(let fieldName, let fieldNames, let target) = hasOne else {
5152
XCTFail("Should create hasOne association")
5253
return
5354
}
5455
XCTAssertEqual(fieldName, Comment.keys.post.stringValue)
56+
XCTAssertEqual(fieldNames, [])
5557
XCTAssertEqual(target, ["postID"])
5658
}
5759

60+
func testHasOneWithCodingKeysWithAssociatedFields() {
61+
let hasOne = ModelAssociation.hasOne(associatedFields: [Comment.keys.post])
62+
guard case .hasOne(let fieldName, let fieldNames, let target) = hasOne else {
63+
XCTFail("Should create hasOne association")
64+
return
65+
}
66+
XCTAssertEqual(fieldName, nil)
67+
XCTAssertEqual(fieldNames, ["post"])
68+
XCTAssertEqual(target, [])
69+
}
70+
5871
func testBelongsToWithTargetName() {
5972
let belongsTo = ModelAssociation.belongsTo(targetName: "postID")
6073
guard case .belongsTo(let fieldName, let target) = belongsTo else {

0 commit comments

Comments
 (0)