@aws-amplify/datastore 3.7.2 → 3.7.3-unstable.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -258,108 +258,101 @@ function modelInstanceCreator<T extends PersistentModel = PersistentModel>(
258
258
  return <T>new modelConstructor(init);
259
259
  }
260
260
 
261
- const validateModelFields = (modelDefinition: SchemaModel | SchemaNonModel) => (
262
- k: string,
263
- v: any
264
- ) => {
265
- const fieldDefinition = modelDefinition.fields[k];
261
+ const validateModelFields =
262
+ (modelDefinition: SchemaModel | SchemaNonModel) => (k: string, v: any) => {
263
+ const fieldDefinition = modelDefinition.fields[k];
266
264
 
267
- if (fieldDefinition !== undefined) {
268
- const {
269
- type,
270
- isRequired,
271
- isArrayNullable,
272
- name,
273
- isArray,
274
- } = fieldDefinition;
265
+ if (fieldDefinition !== undefined) {
266
+ const { type, isRequired, isArrayNullable, name, isArray } =
267
+ fieldDefinition;
275
268
 
276
- if (
277
- ((!isArray && isRequired) || (isArray && !isArrayNullable)) &&
278
- (v === null || v === undefined)
279
- ) {
280
- throw new Error(`Field ${name} is required`);
281
- }
269
+ if (
270
+ ((!isArray && isRequired) || (isArray && !isArrayNullable)) &&
271
+ (v === null || v === undefined)
272
+ ) {
273
+ throw new Error(`Field ${name} is required`);
274
+ }
282
275
 
283
- if (isGraphQLScalarType(type)) {
284
- const jsType = GraphQLScalarType.getJSType(type);
285
- const validateScalar = GraphQLScalarType.getValidationFunction(type);
276
+ if (isGraphQLScalarType(type)) {
277
+ const jsType = GraphQLScalarType.getJSType(type);
278
+ const validateScalar = GraphQLScalarType.getValidationFunction(type);
286
279
 
287
- if (type === 'AWSJSON') {
288
- if (typeof v === jsType) {
289
- return;
290
- }
291
- if (typeof v === 'string') {
292
- try {
293
- JSON.parse(v);
280
+ if (type === 'AWSJSON') {
281
+ if (typeof v === jsType) {
294
282
  return;
295
- } catch (error) {
296
- throw new Error(`Field ${name} is an invalid JSON object. ${v}`);
283
+ }
284
+ if (typeof v === 'string') {
285
+ try {
286
+ JSON.parse(v);
287
+ return;
288
+ } catch (error) {
289
+ throw new Error(`Field ${name} is an invalid JSON object. ${v}`);
290
+ }
297
291
  }
298
292
  }
299
- }
300
293
 
301
- if (isArray) {
302
- let errorTypeText: string = jsType;
303
- if (!isRequired) {
304
- errorTypeText = `${jsType} | null | undefined`;
305
- }
294
+ if (isArray) {
295
+ let errorTypeText: string = jsType;
296
+ if (!isRequired) {
297
+ errorTypeText = `${jsType} | null | undefined`;
298
+ }
299
+
300
+ if (!Array.isArray(v) && !isArrayNullable) {
301
+ throw new Error(
302
+ `Field ${name} should be of type [${errorTypeText}], ${typeof v} received. ${v}`
303
+ );
304
+ }
306
305
 
307
- if (!Array.isArray(v) && !isArrayNullable) {
306
+ if (
307
+ !isNullOrUndefined(v) &&
308
+ (<[]>v).some(e =>
309
+ isNullOrUndefined(e) ? isRequired : typeof e !== jsType
310
+ )
311
+ ) {
312
+ const elemTypes = (<[]>v)
313
+ .map(e => (e === null ? 'null' : typeof e))
314
+ .join(',');
315
+
316
+ throw new Error(
317
+ `All elements in the ${name} array should be of type ${errorTypeText}, [${elemTypes}] received. ${v}`
318
+ );
319
+ }
320
+
321
+ if (validateScalar && !isNullOrUndefined(v)) {
322
+ const validationStatus = (<[]>v).map(e => {
323
+ if (!isNullOrUndefined(e)) {
324
+ return validateScalar(e);
325
+ } else if (isNullOrUndefined(e) && !isRequired) {
326
+ return true;
327
+ } else {
328
+ return false;
329
+ }
330
+ });
331
+
332
+ if (!validationStatus.every(s => s)) {
333
+ throw new Error(
334
+ `All elements in the ${name} array should be of type ${type}, validation failed for one or more elements. ${v}`
335
+ );
336
+ }
337
+ }
338
+ } else if (!isRequired && v === undefined) {
339
+ return;
340
+ } else if (typeof v !== jsType && v !== null) {
308
341
  throw new Error(
309
- `Field ${name} should be of type [${errorTypeText}], ${typeof v} received. ${v}`
342
+ `Field ${name} should be of type ${jsType}, ${typeof v} received. ${v}`
310
343
  );
311
- }
312
-
313
- if (
344
+ } else if (
314
345
  !isNullOrUndefined(v) &&
315
- (<[]>v).some(e =>
316
- isNullOrUndefined(e) ? isRequired : typeof e !== jsType
317
- )
346
+ validateScalar &&
347
+ !validateScalar(v)
318
348
  ) {
319
- const elemTypes = (<[]>v)
320
- .map(e => (e === null ? 'null' : typeof e))
321
- .join(',');
322
-
323
349
  throw new Error(
324
- `All elements in the ${name} array should be of type ${errorTypeText}, [${elemTypes}] received. ${v}`
350
+ `Field ${name} should be of type ${type}, validation failed. ${v}`
325
351
  );
326
352
  }
327
-
328
- if (validateScalar && !isNullOrUndefined(v)) {
329
- const validationStatus = (<[]>v).map(e => {
330
- if (!isNullOrUndefined(e)) {
331
- return validateScalar(e);
332
- } else if (isNullOrUndefined(e) && !isRequired) {
333
- return true;
334
- } else {
335
- return false;
336
- }
337
- });
338
-
339
- if (!validationStatus.every(s => s)) {
340
- throw new Error(
341
- `All elements in the ${name} array should be of type ${type}, validation failed for one or more elements. ${v}`
342
- );
343
- }
344
- }
345
- } else if (!isRequired && v === undefined) {
346
- return;
347
- } else if (typeof v !== jsType && v !== null) {
348
- throw new Error(
349
- `Field ${name} should be of type ${jsType}, ${typeof v} received. ${v}`
350
- );
351
- } else if (
352
- !isNullOrUndefined(v) &&
353
- validateScalar &&
354
- !validateScalar(v)
355
- ) {
356
- throw new Error(
357
- `Field ${name} should be of type ${type}, validation failed. ${v}`
358
- );
359
353
  }
360
354
  }
361
- }
362
- };
355
+ };
363
356
 
364
357
  const castInstanceType = (
365
358
  modelDefinition: SchemaModel | SchemaNonModel,
@@ -414,11 +407,10 @@ const createModelClass = <T extends PersistentModel>(
414
407
  (draft: Draft<T & ModelInstanceMetadata>) => {
415
408
  initializeInstance(init, modelDefinition, draft);
416
409
 
417
- const modelInstanceMetadata: ModelInstanceMetadata = instancesMetadata.has(
418
- init
419
- )
420
- ? <ModelInstanceMetadata>(<unknown>init)
421
- : <ModelInstanceMetadata>{};
410
+ const modelInstanceMetadata: ModelInstanceMetadata =
411
+ instancesMetadata.has(init)
412
+ ? <ModelInstanceMetadata>(<unknown>init)
413
+ : <ModelInstanceMetadata>{};
422
414
  const {
423
415
  id: _id,
424
416
  _version,
@@ -614,9 +606,8 @@ async function checkSchemaVersion(
614
606
  storage: Storage,
615
607
  version: string
616
608
  ): Promise<void> {
617
- const Setting = dataStoreClasses.Setting as PersistentModelConstructor<
618
- Setting
619
- >;
609
+ const Setting =
610
+ dataStoreClasses.Setting as PersistentModelConstructor<Setting>;
620
611
 
621
612
  const modelDefinition = schema.namespaces[DATASTORE].models.Setting;
622
613
 
@@ -704,10 +695,8 @@ class DataStore {
704
695
  private sync: SyncEngine;
705
696
  private syncPageSize: number;
706
697
  private syncExpressions: SyncExpression[];
707
- private syncPredicates: WeakMap<
708
- SchemaModel,
709
- ModelPredicate<any>
710
- > = new WeakMap<SchemaModel, ModelPredicate<any>>();
698
+ private syncPredicates: WeakMap<SchemaModel, ModelPredicate<any>> =
699
+ new WeakMap<SchemaModel, ModelPredicate<any>>();
711
700
  private sessionId: string;
712
701
  private storageAdapter: Adapter;
713
702
 
@@ -1206,7 +1195,7 @@ class DataStore {
1206
1195
  itemsChanged.set(element.id, element);
1207
1196
  }
1208
1197
 
1209
- const isSynced = this.sync.getModelSyncedStatus(model);
1198
+ const isSynced = this.sync?.getModelSyncedStatus(model) ?? false;
1210
1199
 
1211
1200
  const limit =
1212
1201
  itemsChanged.size - deletedItemIds.length >= this.syncPageSize;
@@ -1228,7 +1217,7 @@ class DataStore {
1228
1217
 
1229
1218
  // TODO: abstract this function into a util file to be able to write better unit tests
1230
1219
  const generateSnapshot = (): DataStoreSnapshot<T> => {
1231
- const isSynced = this.sync.getModelSyncedStatus(model);
1220
+ const isSynced = this.sync?.getModelSyncedStatus(model) ?? false;
1232
1221
  const itemsArray = [
1233
1222
  ...Array.from(items.values()),
1234
1223
  ...Array.from(itemsChanged.values()),