Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): fix syncExpression types (aws-amplify#7097)
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev authored Nov 2, 2020
1 parent 57f8d5f commit 947197d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
6 changes: 3 additions & 3 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ class DataStore {
private storage: Storage;
private sync: SyncEngine;
private syncPageSize: number;
private syncExpressions: SyncExpression<any>[];
private syncExpressions: SyncExpression[];
private syncPredicates: WeakMap<
SchemaModel,
ModelPredicate<any>
Expand Down Expand Up @@ -1103,8 +1103,8 @@ class DataStore {

const syncPredicates = await Promise.all(
this.syncExpressions.map(
async <T extends PersistentModel>(
syncExpression: SyncExpression<T>
async (
syncExpression: SyncExpression
): Promise<[SchemaModel, ModelPredicate<any>]> => {
const { modelConstructor, conditionProducer } = await syncExpression;
const modelDefinition = getModelDefinition(modelConstructor);
Expand Down
42 changes: 24 additions & 18 deletions packages/datastore/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,21 +448,19 @@ export type DataStoreConfig = {
maxRecordsToSync?: number; // merge
syncPageSize?: number;
fullSyncInterval?: number;
syncExpressions?: SyncExpression<PersistentModel>[];
syncExpressions?: SyncExpression[];
};
conflictHandler?: ConflictHandler; // default : retry until client wins up to x times
errorHandler?: (error: SyncError) => void; // default : logger.warn
maxRecordsToSync?: number; // merge
syncPageSize?: number;
fullSyncInterval?: number;
syncExpressions?: SyncExpression<PersistentModel>[];
syncExpressions?: SyncExpression[];
};

export type SyncExpression<T extends PersistentModel> = Promise<{
modelConstructor: PersistentModelConstructor<T>;
conditionProducer:
| ProducerModelPredicate<T>
| (() => ProducerModelPredicate<T>);
export type SyncExpression = Promise<{
modelConstructor: any;
conditionProducer: (c?: any) => any;
}>;

/*
Expand All @@ -479,20 +477,28 @@ syncExpressions: [
}),
]
*/
export async function syncExpression<T extends PersistentModel, P>(
type Option0 = [];
type Option1<T extends PersistentModel> = [ModelPredicate<T> | undefined];
type Option<T extends PersistentModel> = Option0 | Option1<T>;

type Lookup<T extends PersistentModel> = {
0: ProducerModelPredicate<T> | Promise<ProducerModelPredicate<T>>;
1: ModelPredicate<T> | undefined;
};

type ConditionProducer<T extends PersistentModel, A extends Option<T>> = (
...args: A
) => A['length'] extends keyof Lookup<T> ? Lookup<T>[A['length']] : never;

export async function syncExpression<
T extends PersistentModel,
A extends Option<T>
>(
modelConstructor: PersistentModelConstructor<T>,
conditionProducer: (
condition: P | ModelPredicate<T>
) => P extends ModelPredicate<T>
? ModelPredicate<T>
: ProducerModelPredicate<T>
conditionProducer: ConditionProducer<T, A>
): Promise<{
modelConstructor: PersistentModelConstructor<T>;
conditionProducer: (
condition: P | ModelPredicate<T>
) => P extends ModelPredicate<T>
? ModelPredicate<T>
: ProducerModelPredicate<T>;
conditionProducer: ConditionProducer<T, A>;
}> {
return {
modelConstructor,
Expand Down

0 comments on commit 947197d

Please sign in to comment.