// TODO: Look at ts-expect-error once we move to TypeScript 3.9 or above import { ModelInit, PersistentModelConstructor, Predicates, __modelMeta__, } from '../../../src'; import { DataStore, dummyInstance, expectType, LegacyCustomROMETA, LegacyDefaultRO, LegacyCustomRO, LegacyNoMetadata, CustomIdentifierNoRO, } from '../../helpers'; describe('Legacy - backwards compatibility', () => { test(`LegacyNoMetadata`, async () => { expectType>({ // @ts-expect-error // id: '234', name: '', description: '', }); expectType>({ name: '', description: '', // @ts-expect-error // x: 234, }); expectType>({ name: '', description: '', createdAt: '', }); LegacyNoMetadata.copyOf({} as LegacyNoMetadata, d => { d.id; // @ts-expect-error // d.id = ''; d.name = ''; d.description = ''; d.createdAt; d.createdAt = ''; d.updatedAt; d.updatedAt = ''; }); // Query expectType( await DataStore.query(LegacyNoMetadata, 'someid') ); expectType( await DataStore.query(LegacyNoMetadata, { id: 'someid' }) ); expectType(await DataStore.query(LegacyNoMetadata)); expectType( await DataStore.query(LegacyNoMetadata, Predicates.ALL) ); expectType( await DataStore.query(LegacyNoMetadata, c => c.createdAt.ge('2019')) ); // Save expectType( await DataStore.save(dummyInstance()) ); expectType( await DataStore.save(dummyInstance(), c => c.createdAt.ge('2019') ) ); // Delete expectType( await DataStore.delete(LegacyNoMetadata, '') ); expectType( await DataStore.delete(dummyInstance()) ); expectType( await DataStore.delete(dummyInstance(), c => c.description.contains('something') ) ); expectType( await DataStore.delete(LegacyNoMetadata, Predicates.ALL) ); expectType( await DataStore.delete(LegacyNoMetadata, c => c.createdAt.le('2019')) ); // Observe DataStore.observe(LegacyNoMetadata).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(LegacyNoMetadata, c => c.description.beginsWith('something') ).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(dummyInstance()).subscribe( ({ model, element }) => { new model({ name: '', description: '', }); expectType>(model); expectType(element); } ); // Observe query DataStore.observeQuery(LegacyNoMetadata).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery(LegacyNoMetadata, c => c.description.notContains('something') ).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery( LegacyNoMetadata, c => c.description.notContains('something'), { sort: c => c.createdAt('ASCENDING') } ).subscribe(({ items }) => { expectType(items); }); }); test(`LegacyDefaultRO`, async () => { expectType>({ // @ts-expect-error // id: '234', name: '', description: '', }); expectType>({ name: '', description: '', // @ts-expect-error // x: 234, }); LegacyDefaultRO.copyOf({} as LegacyDefaultRO, d => { d.id; // @ts-expect-error // d.id = ''; d.name = ''; d.description = ''; d.createdAt; // @ts-expect-error // d.createdAt = ''; d.updatedAt; // @ts-expect-error // d.updatedAt = ''; }); // Query expectType( (await DataStore.query(LegacyDefaultRO, 'someid'))! ); expectType(await DataStore.query(LegacyDefaultRO)); expectType( await DataStore.query(LegacyDefaultRO, Predicates.ALL) ); expectType( await DataStore.query(LegacyDefaultRO, c => c.createdAt.ge('2019')) ); // Save expectType( await DataStore.save(dummyInstance()) ); expectType( await DataStore.save(dummyInstance(), c => c.createdAt.ge('2019') ) ); // Delete expectType(await DataStore.delete(LegacyDefaultRO, '')); expectType( await DataStore.delete(dummyInstance()) ); expectType( await DataStore.delete(dummyInstance(), c => c.description.contains('something') ) ); expectType( await DataStore.delete(LegacyDefaultRO, Predicates.ALL) ); expectType( await DataStore.delete(LegacyDefaultRO, c => c.createdAt.le('2019')) ); // Observe DataStore.observe(LegacyDefaultRO).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(LegacyDefaultRO, c => c.description.beginsWith('something') ).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(dummyInstance()).subscribe( ({ model, element }) => { expectType>(model); expectType(element); } ); // Observe query DataStore.observeQuery(LegacyDefaultRO).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery(LegacyDefaultRO, c => c.description.notContains('something') ).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery( LegacyDefaultRO, c => c.description.notContains('something'), { sort: c => c.createdAt('ASCENDING') } ).subscribe(({ items }) => { expectType(items); }); }); test(`LegacyCustomRO`, async () => { expectType>({ // @ts-expect-error // id: '234', name: '', description: '', }); expectType>({ name: '', description: '', // @ts-expect-error // createdOn: '', }); expectType>({ name: '', description: '', // @ts-expect-error // createdAt: '', }); LegacyCustomRO.copyOf({} as LegacyCustomRO, d => { d.id; // @ts-expect-error // d.id = ''; d.name = ''; d.description = ''; // @ts-expect-error // d.createdAt; // @ts-expect-error // d.updatedAt; d.createdOn; // @ts-expect-error // d.createdOn = ''; d.updatedOn; // @ts-expect-error // d.updatedOn = ''; }); // Query expectType( (await DataStore.query(LegacyCustomRO, 'someid'))! ); expectType(await DataStore.query(LegacyCustomRO)); expectType( await DataStore.query(LegacyCustomRO, Predicates.ALL) ); expectType( await DataStore.query(LegacyCustomRO, c => c.createdOn.ge('2019')) ); // Save expectType( await DataStore.save(dummyInstance()) ); expectType( await DataStore.save(dummyInstance(), c => c.createdOn.ge('2019') ) ); // Delete expectType(await DataStore.delete(LegacyCustomRO, '')); expectType( await DataStore.delete(dummyInstance()) ); expectType( await DataStore.delete(dummyInstance(), c => c.description.contains('something') ) ); expectType( await DataStore.delete(LegacyCustomRO, Predicates.ALL) ); expectType( await DataStore.delete(LegacyCustomRO, c => c.createdOn.le('2019')) ); // Observe DataStore.observe(LegacyCustomRO).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(LegacyCustomRO, c => c.description.beginsWith('something') ).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(dummyInstance()).subscribe( ({ model, element }) => { expectType>(model); expectType(element); } ); // Observe query DataStore.observeQuery(LegacyCustomRO).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery(LegacyCustomRO, c => c.description.notContains('something') ).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery( LegacyCustomRO, c => c.description.notContains('something'), { sort: c => c.createdOn('ASCENDING') } ).subscribe(({ items }) => { expectType(items); }); }); test(`CustomIdentifierNoRO`, async () => { expectType>({ // @ts-expect-error // id: '234', myId: '23342', name: '', description: '', }); expectType>({ myId: '23342', name: '', description: '', createdAt: '', }); expectType>({ myId: '23342', name: '', description: '', createdAt: '', }); CustomIdentifierNoRO.copyOf({} as CustomIdentifierNoRO, d => { d.myId; // @ts-expect-error // d.myId = ''; d.name = ''; d.description = ''; d.createdAt; d.createdAt = ''; d.updatedAt; d.updatedAt = ''; // @ts-expect-error // d.createdOn; // @ts-expect-error // d.updatedOn; }); // Query expectType( (await DataStore.query(CustomIdentifierNoRO, 'someid'))! ); expectType( (await DataStore.query(CustomIdentifierNoRO, { myId: 'someid' }))! ); expectType( await DataStore.query(CustomIdentifierNoRO) ); expectType( await DataStore.query(CustomIdentifierNoRO, Predicates.ALL) ); expectType( await DataStore.query(CustomIdentifierNoRO, c => c.createdAt.ge('2019')) ); // Save expectType( await DataStore.save(dummyInstance()) ); expectType( await DataStore.save(dummyInstance(), c => c.createdAt.ge('2019') ) ); // Delete expectType( await DataStore.delete(CustomIdentifierNoRO, '') ); expectType( await DataStore.delete(dummyInstance()) ); expectType( await DataStore.delete(dummyInstance(), c => c.description.contains('something') ) ); expectType( await DataStore.delete(CustomIdentifierNoRO, Predicates.ALL) ); expectType( await DataStore.delete(CustomIdentifierNoRO, c => c.createdAt.le('2019')) ); // Observe DataStore.observe(CustomIdentifierNoRO).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(CustomIdentifierNoRO, c => c.description.beginsWith('something') ).subscribe(({ model, element }) => { expectType>(model); expectType(element); }); DataStore.observe(dummyInstance()).subscribe( ({ model, element }) => { expectType>(model); expectType(element); } ); // Observe query DataStore.observeQuery(CustomIdentifierNoRO).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery(CustomIdentifierNoRO, c => c.description.notContains('something') ).subscribe(({ items }) => { expectType(items); }); DataStore.observeQuery( CustomIdentifierNoRO, c => c.description.notContains('something'), { sort: c => c.createdAt('ASCENDING') } ).subscribe(({ items }) => { expectType(items); }); }); });