@aws-amplify/datastore 4.0.0 → 4.0.1-unstable.6

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.
package/src/types.ts CHANGED
@@ -18,11 +18,6 @@ import { Auth } from '@aws-amplify/auth';
18
18
  import { API } from '@aws-amplify/api';
19
19
  import { Cache } from '@aws-amplify/cache';
20
20
  import { Adapter } from './storage/adapter';
21
- import {
22
- ModelPredicateExtender,
23
- PredicateInternalsKey,
24
- ModelPredicate as V5ModelPredicate,
25
- } from './predicates/next';
26
21
 
27
22
  export type Scalar<T> = T extends Array<infer InnerType> ? InnerType : T;
28
23
 
@@ -1096,3 +1091,139 @@ export type AmplifyContext = {
1096
1091
  API: typeof API;
1097
1092
  Cache: typeof Cache;
1098
1093
  };
1094
+
1095
+ // #region V5 predicate types
1096
+
1097
+ export type MatchableTypes =
1098
+ | string
1099
+ | string[]
1100
+ | number
1101
+ | number[]
1102
+ | boolean
1103
+ | boolean[];
1104
+
1105
+ export type AllFieldOperators = keyof AllOperators;
1106
+
1107
+ export type NonNeverKeys<T> = {
1108
+ [K in keyof T]: T[K] extends never ? never : K;
1109
+ }[keyof T];
1110
+
1111
+ export type WithoutNevers<T> = Pick<T, NonNeverKeys<T>>;
1112
+
1113
+ /**
1114
+ * A function that accepts a RecursiveModelPrecicate<T>, which it must use to
1115
+ * return a final condition.
1116
+ *
1117
+ * This is used in `DataStore.query()`, `DataStore.observe()`, and
1118
+ * `DataStore.observeQuery()` as the second argument. E.g.,
1119
+ *
1120
+ * ```
1121
+ * DataStore.query(MyModel, model => model.field.eq('some value'))
1122
+ * ```
1123
+ *
1124
+ * More complex queries should also be supported. E.g.,
1125
+ *
1126
+ * ```
1127
+ * DataStore.query(MyModel, model => model.and(m => [
1128
+ * m.relatedEntity.or(relative => [
1129
+ * relative.relativeField.eq('whatever'),
1130
+ * relative.relativeField.eq('whatever else')
1131
+ * ]),
1132
+ * m.myModelField.ne('something')
1133
+ * ]))
1134
+ * ```
1135
+ */
1136
+ export type RecursiveModelPredicateExtender<RT extends PersistentModel> = (
1137
+ lambda: RecursiveModelPredicate<RT>
1138
+ ) => PredicateInternalsKey;
1139
+
1140
+ export type RecursiveModelPredicateAggregateExtender<
1141
+ RT extends PersistentModel
1142
+ > = (lambda: RecursiveModelPredicate<RT>) => PredicateInternalsKey[];
1143
+
1144
+ export type RecursiveModelPredicateOperator<RT extends PersistentModel> = (
1145
+ predicates: RecursiveModelPredicateAggregateExtender<RT>
1146
+ ) => PredicateInternalsKey;
1147
+
1148
+ export type RecursiveModelPredicateNegation<RT extends PersistentModel> = (
1149
+ predicate: RecursiveModelPredicateExtender<RT>
1150
+ ) => PredicateInternalsKey;
1151
+
1152
+ export type RecursiveModelPredicate<RT extends PersistentModel> = {
1153
+ [K in keyof RT]-?: PredicateFieldType<RT[K]> extends PersistentModel
1154
+ ? RecursiveModelPredicate<PredicateFieldType<RT[K]>>
1155
+ : ValuePredicate<RT, RT[K]>;
1156
+ } & {
1157
+ or: RecursiveModelPredicateOperator<RT>;
1158
+ and: RecursiveModelPredicateOperator<RT>;
1159
+ not: RecursiveModelPredicateNegation<RT>;
1160
+ } & PredicateInternalsKey;
1161
+
1162
+ /**
1163
+ * A function that accepts a ModelPrecicate<T>, which it must use to return a
1164
+ * final condition.
1165
+ *
1166
+ * This is used as predicates in `DataStore.save()`, `DataStore.delete()`, and
1167
+ * DataStore sync expressions.
1168
+ *
1169
+ * ```
1170
+ * DataStore.save(record, model => model.field.eq('some value'))
1171
+ * ```
1172
+ *
1173
+ * Logical operators are supported. But, condtiions are related records are
1174
+ * NOT supported. E.g.,
1175
+ *
1176
+ * ```
1177
+ * DataStore.delete(record, model => model.or(m => [
1178
+ * m.field.eq('whatever'),
1179
+ * m.field.eq('whatever else')
1180
+ * ]))
1181
+ * ```
1182
+ */
1183
+ export type ModelPredicateExtender<RT extends PersistentModel> = (
1184
+ lambda: V5ModelPredicate<RT>
1185
+ ) => PredicateInternalsKey;
1186
+
1187
+ export type ModelPredicateAggregateExtender<RT extends PersistentModel> = (
1188
+ lambda: V5ModelPredicate<RT>
1189
+ ) => PredicateInternalsKey[];
1190
+
1191
+ export type ValuePredicate<
1192
+ RT extends PersistentModel,
1193
+ MT extends MatchableTypes
1194
+ > = {
1195
+ [K in AllFieldOperators]: K extends 'between'
1196
+ ? (
1197
+ inclusiveLowerBound: Scalar<MT>,
1198
+ inclusiveUpperBound: Scalar<MT>
1199
+ ) => PredicateInternalsKey
1200
+ : (operand: Scalar<MT>) => PredicateInternalsKey;
1201
+ };
1202
+
1203
+ export type V5ModelPredicate<RT extends PersistentModel> = WithoutNevers<{
1204
+ [K in keyof RT]-?: PredicateFieldType<RT[K]> extends PersistentModel
1205
+ ? never
1206
+ : ValuePredicate<RT, RT[K]>;
1207
+ }> & {
1208
+ or: ModelPredicateOperator<RT>;
1209
+ and: ModelPredicateOperator<RT>;
1210
+ not: ModelPredicateNegation<RT>;
1211
+ } & PredicateInternalsKey;
1212
+
1213
+ export type ModelPredicateOperator<RT extends PersistentModel> = (
1214
+ predicates: ModelPredicateAggregateExtender<RT>
1215
+ ) => PredicateInternalsKey;
1216
+
1217
+ export type ModelPredicateNegation<RT extends PersistentModel> = (
1218
+ predicate: ModelPredicateExtender<RT>
1219
+ ) => PredicateInternalsKey;
1220
+
1221
+ /**
1222
+ * A pointer used by DataStore internally to lookup predicate details
1223
+ * that should not be exposed on public customer interfaces.
1224
+ */
1225
+ export class PredicateInternalsKey {
1226
+ private __isPredicateInternalsKeySentinel: boolean = true;
1227
+ }
1228
+
1229
+ // #endregion