Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): Fix query and delete types (#5032)
Browse files Browse the repository at this point in the history
Fixes #4827
  • Loading branch information
manueliglesias committed Mar 4, 2020
1 parent 69d9fd4 commit fdca554
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
30 changes: 22 additions & 8 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { ConsoleLogger as Logger } from '@aws-amplify/core';
import { Draft, immerable, produce, setAutoFreeze } from 'immer';
import { v1 as uuid1, v4 as uuid4 } from 'uuid';
import Observable from 'zen-observable-ts';
import { isPredicatesAll, ModelPredicateCreator } from '../predicates';
import {
isPredicatesAll,
ModelPredicateCreator,
PredicateAll,
} from '../predicates';
import Storage from '../storage/storage';
import { SyncEngine } from '../sync';
import {
Expand Down Expand Up @@ -368,11 +372,11 @@ const remove: {
): Promise<T>;
<T extends PersistentModel>(
modelConstructor: PersistentModelConstructor<T>,
condition: ProducerModelPredicate<T>
condition: ProducerModelPredicate<T> | typeof PredicateAll
): Promise<T[]>;
} = async <T extends PersistentModel>(
modelOrConstructor: T | PersistentModelConstructor<T>,
idOrCriteria?: string | ProducerModelPredicate<T>
idOrCriteria?: string | ProducerModelPredicate<T> | typeof PredicateAll
) => {
await start();
let condition: ModelPredicate<T>;
Expand Down Expand Up @@ -403,7 +407,11 @@ const remove: {
} else {
condition = ModelPredicateCreator.createFromExisting(
getModelDefinition(modelConstructor),
idOrCriteria
/**
* idOrCriteria is always a ProducerModelPredicate<T>, never a symbol.
* The symbol is used only for typing purposes. e.g. see Predicates.ALL
*/
idOrCriteria as ProducerModelPredicate<T>
);

if (!condition || !ModelPredicateCreator.isValidPredicate(condition)) {
Expand Down Expand Up @@ -530,12 +538,12 @@ const query: {
): Promise<T | undefined>;
<T extends PersistentModel>(
modelConstructor: PersistentModelConstructor<T>,
criteria?: ProducerModelPredicate<T>,
criteria?: ProducerModelPredicate<T> | typeof PredicateAll,
pagination?: PaginationInput
): Promise<T[]>;
} = async <T extends PersistentModel>(
modelConstructor: PersistentModelConstructor<T>,
idOrCriteria?: string | ProducerModelPredicate<T>,
idOrCriteria?: string | ProducerModelPredicate<T> | typeof PredicateAll,
pagination?: PaginationInput
) => {
await start();
Expand Down Expand Up @@ -564,11 +572,17 @@ const query: {
return undefined;
}

/**
* idOrCriteria is always a ProducerModelPredicate<T>, never a symbol.
* The symbol is used only for typing purposes. e.g. see Predicates.ALL
*/
const criteria = idOrCriteria as ProducerModelPredicate<T>;

// Predicates.ALL means "all records", so no predicate (undefined)
const predicate = !isPredicatesAll(idOrCriteria)
const predicate = !isPredicatesAll(criteria)
? ModelPredicateCreator.createFromExisting(
getModelDefinition(modelConstructor),
idOrCriteria
criteria
)
: undefined;

Expand Down
9 changes: 6 additions & 3 deletions packages/datastore/src/predicates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,20 @@ import { exhaustiveCheck } from '../util';

const predicatesAllSet = new WeakSet<ProducerModelPredicate<any>>();

export function isPredicatesAll(predicate: ProducerModelPredicate<any>) {
export function isPredicatesAll(predicate: any) {
return predicatesAllSet.has(predicate);
}

// This symbol is not used at runtime, only its type (unique symbol)
export const PredicateAll = Symbol('A predicate that matches all records');

export class Predicates {
public static get ALL() {
public static get ALL(): typeof PredicateAll {
const predicate = <ProducerModelPredicate<any>>(c => c);

predicatesAllSet.add(predicate);

return predicate;
return <typeof PredicateAll>(<unknown>predicate);
}
}

Expand Down

0 comments on commit fdca554

Please sign in to comment.