Skip to content

Commit fd12602

Browse files
authored
feat(datastore): Add ModelField ReadOnly support (#599)
* feat(datastore): Add ModelField ReadOnly support ModelFields can be readOnly to support non modifiable field types.
1 parent 49529a0 commit fd12602

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2133
-342
lines changed

packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/types/model/FlutterModelField.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ data class FlutterModelField(val map: Map<String, Any>) {
3939
// True if the field is an instance of model.
4040
private val isModel: Boolean = type.isModel();
4141

42+
private val isReadOnly: Boolean = map["isReadOnly"] as Boolean
43+
4244
// An array of rules for owner based authorization
4345
private val authRules: List<FlutterAuthRule>? =
4446
(map["authRules"] as List<Map<String, Any>>?)?.map { serializedAuthRule ->
@@ -64,6 +66,7 @@ data class FlutterModelField(val map: Map<String, Any>) {
6466
.isArray(isArray)
6567
.isEnum(isEnum)
6668
.isModel(isModel)
69+
.isReadOnly(isReadOnly)
6770

6871
if (!authRules.isNullOrEmpty()) {
6972
builder = builder.authRules(authRules.map { authRule ->

packages/amplify_datastore/android/src/test/kotlin/com/amazonaws/amplify/amplify_datastore/AmplifyDataStorePluginTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class AmplifyDataStorePluginTest {
8585
"targetType" to "Blog",
8686
"isRequired" to false,
8787
"isArray" to false,
88+
"isReadOnly" to false,
8889
"type" to mapOf(
8990
"fieldType" to "string"
9091
)

packages/amplify_datastore/example/ios/unit_tests/resources/modelSchema/model_schema_maps.json

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@
99
"fieldType": "string"
1010
},
1111
"isRequired": true,
12-
"isArray": false
12+
"isArray": false,
13+
"isReadOnly": false
1314
},
1415
"name": {
1516
"name": "name",
1617
"type": {
1718
"fieldType": "string"
1819
},
1920
"isRequired": true,
20-
"isArray": false
21+
"isArray": false,
22+
"isReadOnly": false
2123
},
2224
"posts": {
2325
"name": "posts",
@@ -27,6 +29,7 @@
2729
},
2830
"isRequired": false,
2931
"isArray": true,
32+
"isReadOnly": false,
3033
"association": {
3134
"associationType": "HasMany",
3235
"associatedName": "blog",
@@ -46,7 +49,8 @@
4649
"fieldType": "string"
4750
},
4851
"isRequired": true,
49-
"isArray": false
52+
"isArray": false,
53+
"isReadOnly": false
5054
},
5155
"post": {
5256
"name": "post",
@@ -56,6 +60,7 @@
5660
},
5761
"isRequired": false,
5862
"isArray": false,
63+
"isReadOnly": false,
5964
"association": {
6065
"associationType": "BelongsTo",
6166
"targetName": "postID"
@@ -67,7 +72,8 @@
6772
"fieldType": "string"
6873
},
6974
"isRequired": true,
70-
"isArray": false
75+
"isArray": false,
76+
"isReadOnly": false
7177
}
7278
}
7379
},
@@ -82,31 +88,35 @@
8288
"fieldType": "string"
8389
},
8490
"isRequired": true,
85-
"isArray": false
91+
"isArray": false,
92+
"isReadOnly": false
8693
},
8794
"title": {
8895
"name": "title",
8996
"type": {
9097
"fieldType": "string"
9198
},
9299
"isRequired": true,
93-
"isArray": false
100+
"isArray": false,
101+
"isReadOnly": false
94102
},
95103
"created": {
96104
"name": "created",
97105
"type": {
98106
"fieldType": "dateTime"
99107
},
100108
"isRequired": false,
101-
"isArray": false
109+
"isArray": false,
110+
"isReadOnly": false
102111
},
103112
"rating": {
104113
"name": "rating",
105114
"type": {
106115
"fieldType": "int"
107116
},
108117
"isRequired": false,
109-
"isArray": false
118+
"isArray": false,
119+
"isReadOnly": false
110120
},
111121
"blog": {
112122
"name": "blog",
@@ -116,6 +126,7 @@
116126
},
117127
"isRequired": false,
118128
"isArray": false,
129+
"isReadOnly": false,
119130
"association": {
120131
"associationType": "BelongsTo",
121132
"targetName": "blogID"
@@ -129,6 +140,7 @@
129140
},
130141
"isRequired": false,
131142
"isArray": true,
143+
"isReadOnly": false,
132144
"association": {
133145
"associationType": "HasMany",
134146
"associatedName": "post",
@@ -143,6 +155,7 @@
143155
},
144156
"isRequired": true,
145157
"isArray": false,
158+
"isReadOnly": false,
146159
"association": {
147160
"associationType": "BelongsTo",
148161
"targetName": "authorId"
@@ -169,23 +182,26 @@
169182
"fieldType": "string"
170183
},
171184
"isRequired": true,
172-
"isArray": false
185+
"isArray": false,
186+
"isReadOnly": false
173187
},
174188
"title": {
175189
"name": "title",
176190
"type": {
177191
"fieldType": "string"
178192
},
179193
"isRequired": true,
180-
"isArray": false
194+
"isArray": false,
195+
"isReadOnly": false
181196
},
182197
"owner": {
183198
"name": "owner",
184199
"type": {
185200
"fieldType": "string"
186201
},
187202
"isRequired": false,
188-
"isArray": false
203+
"isArray": false,
204+
"isReadOnly": false
189205
}
190206
}
191207
},
@@ -200,79 +216,89 @@
200216
"fieldType": "string"
201217
},
202218
"isRequired": true,
203-
"isArray": false
219+
"isArray": false,
220+
"isReadOnly": false
204221
},
205222
"stringType": {
206223
"name": "stringType",
207224
"type": {
208225
"fieldType": "string"
209226
},
210227
"isRequired": true,
211-
"isArray": false
228+
"isArray": false,
229+
"isReadOnly": false
212230
},
213231
"intType": {
214232
"name": "intType",
215233
"type": {
216234
"fieldType": "int"
217235
},
218236
"isRequired": true,
219-
"isArray": false
237+
"isArray": false,
238+
"isReadOnly": false
220239
},
221240
"floatType": {
222241
"name": "floatType",
223242
"type": {
224243
"fieldType": "double"
225244
},
226245
"isRequired": true,
227-
"isArray": false
246+
"isArray": false,
247+
"isReadOnly": false
228248
},
229249
"boolType": {
230250
"name": "boolType",
231251
"type": {
232252
"fieldType": "bool"
233253
},
234254
"isRequired": true,
235-
"isArray": false
255+
"isArray": false,
256+
"isReadOnly": false
236257
},
237258
"dateType": {
238259
"name": "dateType",
239260
"type": {
240261
"fieldType": "date"
241262
},
242263
"isRequired": true,
243-
"isArray": false
264+
"isArray": false,
265+
"isReadOnly": false
244266
},
245267
"dateTimeType": {
246268
"name": "dateTimeType",
247269
"type": {
248270
"fieldType": "dateTime"
249271
},
250272
"isRequired": true,
251-
"isArray": false
273+
"isArray": false,
274+
"isReadOnly": false
252275
},
253276
"timeType": {
254277
"name": "timeType",
255278
"type": {
256279
"fieldType": "time"
257280
},
258281
"isRequired": true,
259-
"isArray": false
282+
"isArray": false,
283+
"isReadOnly": false
260284
},
261285
"timestampType": {
262286
"name": "timestampType",
263287
"type": {
264288
"fieldType": "timestamp"
265289
},
266290
"isRequired": true,
267-
"isArray": false
291+
"isArray": false,
292+
"isReadOnly": false
268293
},
269294
"enumType": {
270295
"name": "enumType",
271296
"type": {
272297
"fieldType": "string"
273298
},
274299
"isRequired": true,
275-
"isArray": false
300+
"isArray": false,
301+
"isReadOnly": false
276302
}
277303
}
278304
}

packages/amplify_datastore/example/lib/models/BelongsToModel.dart

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16+
import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';
17+
import 'package:flutter/foundation.dart';
18+
1619
// ignore_for_file: public_member_api_docs
1720

1821
import 'ModelProvider.dart';
19-
import 'package:amplify_datastore_plugin_interface/amplify_datastore_plugin_interface.dart';
20-
import 'package:flutter/foundation.dart';
2122

2223
/** This is an auto generated class representing the BelongsToModel type in your schema. */
2324
@immutable
@@ -26,6 +27,8 @@ class BelongsToModel extends Model {
2627
final String id;
2728
final String? _name;
2829
final ChildModel? _child;
30+
final TemporalDateTime? _createdAt;
31+
final TemporalDateTime? _updatedAt;
2932

3033
@override
3134
getInstanceType() => classType;
@@ -61,10 +64,20 @@ class BelongsToModel extends Model {
6164
}
6265
}
6366

67+
TemporalDateTime? get createdAt {
68+
return _createdAt;
69+
}
70+
71+
TemporalDateTime? get updatedAt {
72+
return _updatedAt;
73+
}
74+
6475
const BelongsToModel._internal(
65-
{required this.id, required name, required child})
76+
{required this.id, required name, required child, createdAt, updatedAt})
6677
: _name = name,
67-
_child = child;
78+
_child = child,
79+
_createdAt = createdAt,
80+
_updatedAt = updatedAt;
6881

6982
factory BelongsToModel(
7083
{String? id, required String name, required ChildModel child}) {
@@ -95,14 +108,20 @@ class BelongsToModel extends Model {
95108
buffer.write("BelongsToModel {");
96109
buffer.write("id=" + "$id" + ", ");
97110
buffer.write("name=" + "$_name" + ", ");
98-
buffer.write("child=" + (_child != null ? _child!.toString() : "null"));
111+
buffer.write(
112+
"child=" + (_child != null ? _child!.toString() : "null") + ", ");
113+
buffer.write("createdAt=" +
114+
(_createdAt != null ? _createdAt!.format() : "null") +
115+
", ");
116+
buffer.write(
117+
"updatedAt=" + (_updatedAt != null ? _updatedAt!.format() : "null"));
99118
buffer.write("}");
100119

101120
return buffer.toString();
102121
}
103122

104123
BelongsToModel copyWith({String? id, String? name, ChildModel? child}) {
105-
return BelongsToModel(
124+
return BelongsToModel._internal(
106125
id: id ?? this.id, name: name ?? this.name, child: child ?? this.child);
107126
}
108127

@@ -112,10 +131,21 @@ class BelongsToModel extends Model {
112131
_child = json['child']?['serializedData'] != null
113132
? ChildModel.fromJson(
114133
new Map<String, dynamic>.from(json['child']['serializedData']))
134+
: null,
135+
_createdAt = json['createdAt'] != null
136+
? TemporalDateTime.fromString(json['createdAt'])
137+
: null,
138+
_updatedAt = json['updatedAt'] != null
139+
? TemporalDateTime.fromString(json['updatedAt'])
115140
: null;
116141

117-
Map<String, dynamic> toJson() =>
118-
{'id': id, 'name': _name, 'child': _child?.toJson()};
142+
Map<String, dynamic> toJson() => {
143+
'id': id,
144+
'name': _name,
145+
'child': _child?.toJson(),
146+
'createdAt': _createdAt?.format(),
147+
'updatedAt': _updatedAt?.format()
148+
};
119149

120150
static final QueryField ID = QueryField(fieldName: "belongsToModel.id");
121151
static final QueryField NAME = QueryField(fieldName: "name");
@@ -140,6 +170,18 @@ class BelongsToModel extends Model {
140170
isRequired: true,
141171
targetName: "belongsToModelChildId",
142172
ofModelName: (ChildModel).toString()));
173+
174+
modelSchemaDefinition.addField(ModelFieldDefinition.nonQueryField(
175+
fieldName: "createdAt",
176+
isRequired: false,
177+
isReadOnly: true,
178+
ofType: ModelFieldType(ModelFieldTypeEnum.dateTime)));
179+
180+
modelSchemaDefinition.addField(ModelFieldDefinition.nonQueryField(
181+
fieldName: "updatedAt",
182+
isRequired: false,
183+
isReadOnly: true,
184+
ofType: ModelFieldType(ModelFieldTypeEnum.dateTime)));
143185
});
144186
}
145187

0 commit comments

Comments
 (0)