@aws-amplify/data-schema 1.1.4 → 1.1.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/dist/cjs/CustomOperation.js +46 -0
- package/dist/cjs/CustomOperation.js.map +1 -1
- package/dist/cjs/CustomType.js +32 -0
- package/dist/cjs/CustomType.js.map +1 -1
- package/dist/cjs/Handler.js +62 -0
- package/dist/cjs/Handler.js.map +1 -1
- package/dist/cjs/ModelRelationalField.js +82 -3
- package/dist/cjs/ModelRelationalField.js.map +1 -1
- package/dist/cjs/util/Brand.js +4 -4
- package/dist/cjs/util/Brand.js.map +1 -1
- package/dist/cjs/util/usedMethods.js +4 -0
- package/dist/cjs/util/usedMethods.js.map +1 -0
- package/dist/esm/CustomOperation.d.ts +49 -3
- package/dist/esm/CustomOperation.mjs +46 -0
- package/dist/esm/CustomOperation.mjs.map +1 -1
- package/dist/esm/CustomType.d.ts +35 -3
- package/dist/esm/CustomType.mjs +32 -0
- package/dist/esm/CustomType.mjs.map +1 -1
- package/dist/esm/EnumType.d.ts +7 -9
- package/dist/esm/Handler.d.ts +62 -0
- package/dist/esm/Handler.mjs +62 -0
- package/dist/esm/Handler.mjs.map +1 -1
- package/dist/esm/MappedTypes/CustomOperations.d.ts +3 -3
- package/dist/esm/MappedTypes/ExtractNonModelTypes.d.ts +4 -4
- package/dist/esm/MappedTypes/ResolveFieldProperties.d.ts +2 -2
- package/dist/esm/MappedTypes/ResolveSchema.d.ts +6 -6
- package/dist/esm/ModelField.d.ts +16 -13
- package/dist/esm/ModelRelationalField.d.ts +83 -4
- package/dist/esm/ModelRelationalField.mjs +82 -3
- package/dist/esm/ModelRelationalField.mjs.map +1 -1
- package/dist/esm/ModelSchema.d.ts +4 -4
- package/dist/esm/ModelType.d.ts +16 -12
- package/dist/esm/RefType.d.ts +1 -1
- package/dist/esm/util/Brand.d.ts +1 -2
- package/dist/esm/util/Brand.mjs +1 -1
- package/dist/esm/util/Brand.mjs.map +1 -1
- package/dist/esm/util/usedMethods.d.ts +4 -0
- package/dist/esm/util/usedMethods.mjs +2 -0
- package/dist/esm/util/usedMethods.mjs.map +1 -0
- package/dist/meta/cjs.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/CustomOperation.ts +49 -7
- package/src/CustomType.ts +36 -8
- package/src/EnumType.ts +16 -22
- package/src/Handler.ts +62 -0
- package/src/MappedTypes/CustomOperations.ts +4 -6
- package/src/MappedTypes/ExtractNonModelTypes.ts +39 -40
- package/src/MappedTypes/ResolveFieldProperties.ts +2 -2
- package/src/MappedTypes/ResolveSchema.ts +18 -21
- package/src/ModelField.ts +34 -19
- package/src/ModelRelationalField.ts +83 -4
- package/src/ModelSchema.ts +8 -12
- package/src/ModelType.ts +30 -34
- package/src/RefType.ts +1 -1
- package/src/SchemaProcessor.ts +21 -41
- package/src/util/Brand.ts +1 -1
- package/src/util/usedMethods.ts +5 -0
|
@@ -60,7 +60,7 @@ type ModelRelationalFieldFunctions<
|
|
|
60
60
|
K | 'required'
|
|
61
61
|
>;
|
|
62
62
|
/**
|
|
63
|
-
* Configures field-level authorization rules. Pass in an array of authorizations `(
|
|
63
|
+
* Configures field-level authorization rules. Pass in an array of authorizations `(allow => allow.____)` to mix and match
|
|
64
64
|
* multiple authorization rules for this field.
|
|
65
65
|
*/
|
|
66
66
|
authorization<AuthRuleType extends Authorization<any, any, any>>(
|
|
@@ -184,10 +184,28 @@ export type ModelRelationalTypeArgFactory<
|
|
|
184
184
|
};
|
|
185
185
|
|
|
186
186
|
/**
|
|
187
|
-
* Create
|
|
187
|
+
* Create one-to-one relationship between two models using the `hasOne("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
|
|
188
188
|
* A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field
|
|
189
189
|
* unless overwritten with the `identifier()` method.
|
|
190
|
+
* @example
|
|
191
|
+
* const schema = a.schema({
|
|
192
|
+
* Cart: a.model({
|
|
193
|
+
* items: a.string().required().array(),
|
|
194
|
+
* // 1. Create reference field
|
|
195
|
+
* customerId: a.id(),
|
|
196
|
+
* // 2. Create relationship field with the reference field
|
|
197
|
+
* customer: a.belongsTo('Customer', 'customerId'),
|
|
198
|
+
* }),
|
|
199
|
+
* Customer: a.model({
|
|
200
|
+
* name: a.string(),
|
|
201
|
+
* // 3. Create relationship field with the reference field
|
|
202
|
+
* // from the Cart model
|
|
203
|
+
* activeCart: a.hasOne('Cart', 'customerId')
|
|
204
|
+
* }),
|
|
205
|
+
* });
|
|
206
|
+
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-a-one-to-one-relationship}
|
|
190
207
|
* @param relatedModel the name of the related model
|
|
208
|
+
* @param references the field(s) that should be used to reference the related model
|
|
191
209
|
* @returns a one-to-one relationship definition
|
|
192
210
|
*/
|
|
193
211
|
export function hasOne<RM extends string>(
|
|
@@ -206,8 +224,29 @@ export function hasOne<RM extends string>(
|
|
|
206
224
|
}
|
|
207
225
|
|
|
208
226
|
/**
|
|
209
|
-
* Create a one-directional one-to-many relationship between two models using the `hasMany()` method.
|
|
227
|
+
* Create a one-directional one-to-many relationship between two models using the `hasMany("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
|
|
228
|
+
* @example
|
|
229
|
+
* const schema = a.schema({
|
|
230
|
+
* Member: a.model({
|
|
231
|
+
* name: a.string().required(),
|
|
232
|
+
* // 1. Create a reference field
|
|
233
|
+
* teamId: a.id(),
|
|
234
|
+
* // 2. Create a belongsTo relationship with the reference field
|
|
235
|
+
* team: a.belongsTo('Team', 'teamId'),
|
|
236
|
+
* })
|
|
237
|
+
* .authorization(allow => [allow.publicApiKey()]),
|
|
238
|
+
*
|
|
239
|
+
* Team: a.model({
|
|
240
|
+
* mantra: a.string().required(),
|
|
241
|
+
* // 3. Create a hasMany relationship with the reference field
|
|
242
|
+
* // from the `Member`s model.
|
|
243
|
+
* members: a.hasMany('Member', 'teamId'),
|
|
244
|
+
* })
|
|
245
|
+
* .authorization(allow => [allow.publicApiKey()]),
|
|
246
|
+
* });
|
|
247
|
+
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-one-to-many-relationships}
|
|
210
248
|
* @param relatedModel the name of the related model
|
|
249
|
+
* @param references the field(s) that should be used to reference the related model
|
|
211
250
|
* @returns a one-to-many relationship definition
|
|
212
251
|
*/
|
|
213
252
|
export function hasMany<RM extends string>(
|
|
@@ -226,10 +265,50 @@ export function hasMany<RM extends string>(
|
|
|
226
265
|
}
|
|
227
266
|
|
|
228
267
|
/**
|
|
229
|
-
*
|
|
268
|
+
* Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.
|
|
230
269
|
* The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from
|
|
231
270
|
* parent to the related model.
|
|
271
|
+
*
|
|
272
|
+
* @example
|
|
273
|
+
* // one-to-many relationship
|
|
274
|
+
* const schema = a.schema({
|
|
275
|
+
* Member: a.model({
|
|
276
|
+
* name: a.string().required(),
|
|
277
|
+
* // 1. Create a reference field
|
|
278
|
+
* teamId: a.id(),
|
|
279
|
+
* // 2. Create a belongsTo relationship with the reference field
|
|
280
|
+
* team: a.belongsTo('Team', 'teamId'),
|
|
281
|
+
* })
|
|
282
|
+
* .authorization(allow => [allow.publicApiKey()]),
|
|
283
|
+
*
|
|
284
|
+
* Team: a.model({
|
|
285
|
+
* mantra: a.string().required(),
|
|
286
|
+
* // 3. Create a hasMany relationship with the reference field
|
|
287
|
+
* // from the `Member`s model.
|
|
288
|
+
* members: a.hasMany('Member', 'teamId'),
|
|
289
|
+
* })
|
|
290
|
+
* .authorization(allow => [allow.publicApiKey()]),
|
|
291
|
+
* });
|
|
292
|
+
* @example
|
|
293
|
+
* // one-to-one relationship
|
|
294
|
+
* const schema = a.schema({
|
|
295
|
+
* Cart: a.model({
|
|
296
|
+
* items: a.string().required().array(),
|
|
297
|
+
* // 1. Create reference field
|
|
298
|
+
* customerId: a.id(),
|
|
299
|
+
* // 2. Create relationship field with the reference field
|
|
300
|
+
* customer: a.belongsTo('Customer', 'customerId'),
|
|
301
|
+
* }),
|
|
302
|
+
* Customer: a.model({
|
|
303
|
+
* name: a.string(),
|
|
304
|
+
* // 3. Create relationship field with the reference field
|
|
305
|
+
* // from the Cart model
|
|
306
|
+
* activeCart: a.hasOne('Cart', 'customerId')
|
|
307
|
+
* }),
|
|
308
|
+
* });
|
|
309
|
+
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/}
|
|
232
310
|
* @param relatedModel name of the related `.hasOne()` or `.hasMany()` model
|
|
311
|
+
* @param references the field(s) that should be used to reference the related model
|
|
233
312
|
* @returns a belong-to relationship definition
|
|
234
313
|
*/
|
|
235
314
|
export function belongsTo<RM extends string>(
|
package/src/ModelSchema.ts
CHANGED
|
@@ -7,14 +7,13 @@ import type {
|
|
|
7
7
|
UnionToIntersection,
|
|
8
8
|
} from '@aws-amplify/data-schema-types';
|
|
9
9
|
import {
|
|
10
|
-
type ModelType,
|
|
11
|
-
type ModelTypeParamShape,
|
|
12
10
|
type InternalModel,
|
|
13
11
|
isSchemaModelType,
|
|
14
12
|
SchemaModelType,
|
|
15
13
|
AddRelationshipFieldsToModelTypeFields,
|
|
14
|
+
type BaseModelType,
|
|
16
15
|
} from './ModelType';
|
|
17
|
-
import type { EnumType
|
|
16
|
+
import type { EnumType } from './EnumType';
|
|
18
17
|
import type { CustomType, CustomTypeParamShape } from './CustomType';
|
|
19
18
|
import type {
|
|
20
19
|
CustomOperation,
|
|
@@ -41,9 +40,9 @@ const ddbSchemaBrand = brand(ddbSchemaBrandName);
|
|
|
41
40
|
export type DDBSchemaBrand = Brand<typeof ddbSchemaBrandName>;
|
|
42
41
|
|
|
43
42
|
type SchemaContent =
|
|
44
|
-
|
|
|
43
|
+
| BaseModelType
|
|
45
44
|
| CustomType<CustomTypeParamShape>
|
|
46
|
-
| EnumType
|
|
45
|
+
| EnumType
|
|
47
46
|
| CustomOperation<CustomOperationParamShape, any>;
|
|
48
47
|
|
|
49
48
|
type NonEmpty<T> = keyof T extends never ? never : T;
|
|
@@ -76,10 +75,7 @@ export type BaseSchema<
|
|
|
76
75
|
> = {
|
|
77
76
|
data: T;
|
|
78
77
|
models: {
|
|
79
|
-
[TypeKey in keyof T['types']]: T['types'][TypeKey] extends
|
|
80
|
-
ModelTypeParamShape,
|
|
81
|
-
never | 'identifier'
|
|
82
|
-
>
|
|
78
|
+
[TypeKey in keyof T['types']]: T['types'][TypeKey] extends BaseModelType
|
|
83
79
|
? SchemaModelType<T['types'][TypeKey], TypeKey & string, IsRDS>
|
|
84
80
|
: never;
|
|
85
81
|
};
|
|
@@ -334,7 +330,7 @@ function _rdsSchema<
|
|
|
334
330
|
models[newName] = currentType;
|
|
335
331
|
data.types[newName] = currentType;
|
|
336
332
|
models[newName].data.originalName = curName;
|
|
337
|
-
|
|
333
|
+
|
|
338
334
|
delete models[curName];
|
|
339
335
|
delete data.types[curName];
|
|
340
336
|
});
|
|
@@ -348,7 +344,7 @@ function _rdsSchema<
|
|
|
348
344
|
function _ddbSchema<
|
|
349
345
|
T extends ModelSchemaParamShape,
|
|
350
346
|
DSC extends SchemaConfiguration<any, any>,
|
|
351
|
-
>(types: T['types'], config: DSC) {
|
|
347
|
+
>(types: T['types'], config: DSC): ModelSchema<T> {
|
|
352
348
|
const data: ModelSchemaParamShape = {
|
|
353
349
|
types,
|
|
354
350
|
authorization: [],
|
|
@@ -369,7 +365,7 @@ function _ddbSchema<
|
|
|
369
365
|
},
|
|
370
366
|
models: filterSchemaModelTypes(data.types),
|
|
371
367
|
...ddbSchemaBrand,
|
|
372
|
-
}
|
|
368
|
+
} satisfies ModelSchema<any> as never;
|
|
373
369
|
}
|
|
374
370
|
|
|
375
371
|
type SchemaReturnType<
|
package/src/ModelType.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { SetTypeSubArg } from '@aws-amplify/data-schema-types';
|
|
2
2
|
import type { PrimaryIndexIrShape, SecondaryIndexIrShape } from './runtime';
|
|
3
|
-
import {
|
|
4
|
-
import type {
|
|
3
|
+
import { brand } from './util';
|
|
4
|
+
import type { InternalField, BaseModelField } from './ModelField';
|
|
5
5
|
import type {
|
|
6
6
|
ModelRelationalField,
|
|
7
7
|
InternalRelationalField,
|
|
@@ -9,7 +9,7 @@ import type {
|
|
|
9
9
|
} from './ModelRelationalField';
|
|
10
10
|
import { type AllowModifier, type Authorization, allow } from './Authorization';
|
|
11
11
|
import type { RefType, RefTypeParamShape } from './RefType';
|
|
12
|
-
import type { EnumType
|
|
12
|
+
import type { EnumType } from './EnumType';
|
|
13
13
|
import type { CustomType, CustomTypeParamShape } from './CustomType';
|
|
14
14
|
import {
|
|
15
15
|
type ModelIndexType,
|
|
@@ -20,16 +20,18 @@ import type {
|
|
|
20
20
|
PrimaryIndexFieldsToIR,
|
|
21
21
|
SecondaryIndexToIR,
|
|
22
22
|
} from './MappedTypes/MapIndexes';
|
|
23
|
+
import type { brandSymbol } from './util/Brand.js';
|
|
24
|
+
import type { methodKeyOf } from './util/usedMethods.js';
|
|
23
25
|
|
|
24
26
|
const brandName = 'modelType';
|
|
25
27
|
export type deferredRefResolvingPrefix = 'deferredRefResolving:';
|
|
26
28
|
|
|
27
29
|
type ModelFields = Record<
|
|
28
30
|
string,
|
|
29
|
-
|
|
|
31
|
+
| BaseModelField
|
|
30
32
|
| ModelRelationalField<any, string, any, any>
|
|
31
33
|
| RefType<any, any, any>
|
|
32
|
-
| EnumType
|
|
34
|
+
| EnumType
|
|
33
35
|
| CustomType<CustomTypeParamShape>
|
|
34
36
|
>;
|
|
35
37
|
|
|
@@ -78,10 +80,8 @@ export type ExtractSecondaryIndexIRFields<
|
|
|
78
80
|
T extends ModelTypeParamShape,
|
|
79
81
|
RequiredOnly extends boolean = false,
|
|
80
82
|
> = {
|
|
81
|
-
[FieldProp in keyof T['fields'] as T['fields'][FieldProp] extends
|
|
82
|
-
infer R
|
|
83
|
-
any,
|
|
84
|
-
any
|
|
83
|
+
[FieldProp in keyof T['fields'] as T['fields'][FieldProp] extends BaseModelField<
|
|
84
|
+
infer R
|
|
85
85
|
>
|
|
86
86
|
? NonNullable<R> extends string | number
|
|
87
87
|
? RequiredOnly extends false
|
|
@@ -91,26 +91,22 @@ export type ExtractSecondaryIndexIRFields<
|
|
|
91
91
|
: FieldProp
|
|
92
92
|
: never
|
|
93
93
|
: T['fields'][FieldProp] extends
|
|
94
|
-
| EnumType
|
|
94
|
+
| EnumType
|
|
95
95
|
| RefType<RefTypeParamShape, any, any>
|
|
96
96
|
? FieldProp
|
|
97
|
-
: never]: T['fields'][FieldProp] extends
|
|
97
|
+
: never]: T['fields'][FieldProp] extends BaseModelField<infer R>
|
|
98
98
|
? R
|
|
99
|
-
: T['fields'][FieldProp] extends EnumType<infer
|
|
100
|
-
?
|
|
99
|
+
: T['fields'][FieldProp] extends EnumType<infer values>
|
|
100
|
+
? values[number]
|
|
101
101
|
: T['fields'][FieldProp] extends RefType<infer R, any, any>
|
|
102
102
|
? `${deferredRefResolvingPrefix}${R['link']}`
|
|
103
103
|
: never;
|
|
104
104
|
};
|
|
105
105
|
|
|
106
106
|
type ExtractType<T extends ModelTypeParamShape> = {
|
|
107
|
-
[FieldProp in keyof T['fields'] as T['fields'][FieldProp] extends
|
|
108
|
-
any,
|
|
109
|
-
any,
|
|
110
|
-
any
|
|
111
|
-
>
|
|
107
|
+
[FieldProp in keyof T['fields'] as T['fields'][FieldProp] extends BaseModelField
|
|
112
108
|
? FieldProp
|
|
113
|
-
: never]: T['fields'][FieldProp] extends
|
|
109
|
+
: never]: T['fields'][FieldProp] extends BaseModelField<infer R>
|
|
114
110
|
? R
|
|
115
111
|
: never;
|
|
116
112
|
};
|
|
@@ -197,11 +193,17 @@ export type AddRelationshipFieldsToModelTypeFields<
|
|
|
197
193
|
type _ConflictingAuthRules<T extends ModelTypeParamShape> =
|
|
198
194
|
ConflictingAuthRulesMap<T>[keyof ConflictingAuthRulesMap<T>];
|
|
199
195
|
|
|
196
|
+
export type BaseModelType<T extends ModelTypeParamShape = ModelTypeParamShape> =
|
|
197
|
+
ModelType<T, UsableModelTypeKey>;
|
|
198
|
+
|
|
199
|
+
export type UsableModelTypeKey = methodKeyOf<ModelType>;
|
|
200
|
+
|
|
200
201
|
export type ModelType<
|
|
201
|
-
T extends ModelTypeParamShape,
|
|
202
|
-
|
|
202
|
+
T extends ModelTypeParamShape = ModelTypeParamShape,
|
|
203
|
+
UsedMethod extends UsableModelTypeKey = never,
|
|
203
204
|
> = Omit<
|
|
204
205
|
{
|
|
206
|
+
[brandSymbol]: typeof brandName;
|
|
205
207
|
identifier<
|
|
206
208
|
PrimaryIndexFields = ExtractSecondaryIndexIRFields<T, true>,
|
|
207
209
|
PrimaryIndexPool extends string = keyof PrimaryIndexFields & string,
|
|
@@ -214,7 +216,7 @@ export type ModelType<
|
|
|
214
216
|
identifier: ID,
|
|
215
217
|
): ModelType<
|
|
216
218
|
SetTypeSubArg<T, 'identifier', PrimaryIndexIR>,
|
|
217
|
-
|
|
219
|
+
UsedMethod | 'identifier'
|
|
218
220
|
>;
|
|
219
221
|
secondaryIndexes<
|
|
220
222
|
const SecondaryIndexFields = ExtractSecondaryIndexIRFields<T>,
|
|
@@ -243,7 +245,7 @@ export type ModelType<
|
|
|
243
245
|
) => Indexes,
|
|
244
246
|
): ModelType<
|
|
245
247
|
SetTypeSubArg<T, 'secondaryIndexes', IndexesIR>,
|
|
246
|
-
|
|
248
|
+
UsedMethod | 'secondaryIndexes'
|
|
247
249
|
>;
|
|
248
250
|
authorization<AuthRuleType extends Authorization<any, any, any>>(
|
|
249
251
|
callback: (
|
|
@@ -251,22 +253,18 @@ export type ModelType<
|
|
|
251
253
|
) => AuthRuleType | AuthRuleType[],
|
|
252
254
|
): ModelType<
|
|
253
255
|
SetTypeSubArg<T, 'authorization', AuthRuleType[]>,
|
|
254
|
-
|
|
256
|
+
UsedMethod | 'authorization'
|
|
255
257
|
>;
|
|
256
258
|
},
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
Brand<typeof brandName>;
|
|
259
|
+
UsedMethod
|
|
260
|
+
>;
|
|
260
261
|
|
|
261
262
|
/**
|
|
262
263
|
* External representation of Model Type that exposes the `relationships` modifier.
|
|
263
264
|
* Used on the complete schema object.
|
|
264
265
|
*/
|
|
265
266
|
export type SchemaModelType<
|
|
266
|
-
T extends ModelType<ModelTypeParamShape,
|
|
267
|
-
ModelTypeParamShape,
|
|
268
|
-
never | 'identifier'
|
|
269
|
-
>,
|
|
267
|
+
T extends BaseModelType = ModelType<ModelTypeParamShape, 'identifier'>,
|
|
270
268
|
ModelName extends string = string,
|
|
271
269
|
IsRDS extends boolean = false,
|
|
272
270
|
> = IsRDS extends true
|
|
@@ -279,9 +277,7 @@ export type SchemaModelType<
|
|
|
279
277
|
>(
|
|
280
278
|
relationships: Param,
|
|
281
279
|
): Record<ModelName, Param>;
|
|
282
|
-
fields: T extends ModelType<infer R
|
|
283
|
-
? R['fields']
|
|
284
|
-
: never;
|
|
280
|
+
fields: T extends ModelType<infer R, any> ? R['fields'] : never;
|
|
285
281
|
}
|
|
286
282
|
: T;
|
|
287
283
|
|
package/src/RefType.ts
CHANGED
|
@@ -53,7 +53,7 @@ export type RefType<
|
|
|
53
53
|
Exclude<K, 'required'> | 'array'
|
|
54
54
|
>;
|
|
55
55
|
/**
|
|
56
|
-
* Configures field-level authorization rules. Pass in an array of authorizations `(
|
|
56
|
+
* Configures field-level authorization rules. Pass in an array of authorizations `(allow => allow.____)` to mix and match
|
|
57
57
|
* multiple authorization rules for this field.
|
|
58
58
|
*/
|
|
59
59
|
authorization<AuthRuleType extends Authorization<any, any, any>>(
|
package/src/SchemaProcessor.ts
CHANGED
|
@@ -3,10 +3,10 @@ import {
|
|
|
3
3
|
type ModelField,
|
|
4
4
|
type InternalField,
|
|
5
5
|
string,
|
|
6
|
-
|
|
6
|
+
type BaseModelField,
|
|
7
7
|
} from './ModelField';
|
|
8
8
|
import { type InternalRelationalField } from './ModelRelationalField';
|
|
9
|
-
import type {
|
|
9
|
+
import type { InternalModel } from './ModelType';
|
|
10
10
|
import type { InternalModelIndexType } from './ModelIndex';
|
|
11
11
|
import {
|
|
12
12
|
type Authorization,
|
|
@@ -25,11 +25,7 @@ import {
|
|
|
25
25
|
} from '@aws-amplify/data-schema-types';
|
|
26
26
|
import type { InternalRef, RefType } from './RefType';
|
|
27
27
|
import type { EnumType } from './EnumType';
|
|
28
|
-
import type {
|
|
29
|
-
CustomType,
|
|
30
|
-
CustomTypeAllowedModifiers,
|
|
31
|
-
CustomTypeParamShape,
|
|
32
|
-
} from './CustomType';
|
|
28
|
+
import type { CustomType, CustomTypeParamShape } from './CustomType';
|
|
33
29
|
import { type InternalCustom, CustomOperationNames } from './CustomOperation';
|
|
34
30
|
import { Brand, getBrand } from './util';
|
|
35
31
|
import {
|
|
@@ -57,7 +53,7 @@ type CustomOperationFields = {
|
|
|
57
53
|
subscriptions: string[];
|
|
58
54
|
};
|
|
59
55
|
|
|
60
|
-
function isInternalModel(model:
|
|
56
|
+
function isInternalModel(model: unknown): model is InternalModel {
|
|
61
57
|
if (
|
|
62
58
|
(model as any).data &&
|
|
63
59
|
!isCustomType(model) &&
|
|
@@ -68,9 +64,7 @@ function isInternalModel(model: ModelType<any, any>): model is InternalModel {
|
|
|
68
64
|
return false;
|
|
69
65
|
}
|
|
70
66
|
|
|
71
|
-
function isEnumType(
|
|
72
|
-
data: any,
|
|
73
|
-
): data is EnumType<{ type: 'enum'; values: string[] }> {
|
|
67
|
+
function isEnumType(data: any): data is EnumType {
|
|
74
68
|
if (data?.type === 'enum') {
|
|
75
69
|
return true;
|
|
76
70
|
}
|
|
@@ -120,13 +114,13 @@ function dataSourceIsRef(
|
|
|
120
114
|
}
|
|
121
115
|
|
|
122
116
|
function isScalarField(
|
|
123
|
-
field:
|
|
117
|
+
field: unknown,
|
|
124
118
|
): field is { data: ScalarFieldDef } & Brand<'modelField'> {
|
|
125
119
|
return isScalarFieldDef((field as any)?.data);
|
|
126
120
|
}
|
|
127
121
|
|
|
128
122
|
function isRefField(
|
|
129
|
-
field:
|
|
123
|
+
field: unknown,
|
|
130
124
|
): field is { data: RefFieldDef } & Brand<'modelField'> {
|
|
131
125
|
return isRefFieldDef((field as any)?.data);
|
|
132
126
|
}
|
|
@@ -501,10 +495,7 @@ function escapeGraphQlString(str: string) {
|
|
|
501
495
|
* @param right
|
|
502
496
|
* @returns
|
|
503
497
|
*/
|
|
504
|
-
function areConflicting(
|
|
505
|
-
left: ModelField<any, any>,
|
|
506
|
-
right: ModelField<any, any>,
|
|
507
|
-
): boolean {
|
|
498
|
+
function areConflicting(left: BaseModelField, right: BaseModelField): boolean {
|
|
508
499
|
// These are the only props we care about for this comparison, because the others
|
|
509
500
|
// (required, arrayRequired, etc) are not specified on auth or FK directives.
|
|
510
501
|
const relevantProps = ['array', 'fieldType'] as const;
|
|
@@ -527,8 +518,8 @@ function areConflicting(
|
|
|
527
518
|
* @param additions A field map to merge in
|
|
528
519
|
*/
|
|
529
520
|
function addFields(
|
|
530
|
-
existing: Record<string,
|
|
531
|
-
additions: Record<string,
|
|
521
|
+
existing: Record<string, BaseModelField>,
|
|
522
|
+
additions: Record<string, BaseModelField>,
|
|
532
523
|
): void {
|
|
533
524
|
for (const [k, addition] of Object.entries(additions)) {
|
|
534
525
|
if (!existing[k]) {
|
|
@@ -550,8 +541,8 @@ function addFields(
|
|
|
550
541
|
* @throws An error when an undefined field is used or when a field is used in a way that conflicts with its generated definition
|
|
551
542
|
*/
|
|
552
543
|
function validateStaticFields(
|
|
553
|
-
existing: Record<string,
|
|
554
|
-
implicitFields: Record<string,
|
|
544
|
+
existing: Record<string, BaseModelField>,
|
|
545
|
+
implicitFields: Record<string, BaseModelField> | undefined,
|
|
555
546
|
) {
|
|
556
547
|
if (implicitFields === undefined) {
|
|
557
548
|
return;
|
|
@@ -574,8 +565,8 @@ function validateStaticFields(
|
|
|
574
565
|
* @throws An error when an undefined field is used or when a field is used in a way that conflicts with its generated definition
|
|
575
566
|
*/
|
|
576
567
|
function validateImpliedFields(
|
|
577
|
-
existing: Record<string,
|
|
578
|
-
implicitFields: Record<string,
|
|
568
|
+
existing: Record<string, BaseModelField>,
|
|
569
|
+
implicitFields: Record<string, BaseModelField> | undefined,
|
|
579
570
|
) {
|
|
580
571
|
if (implicitFields === undefined) {
|
|
581
572
|
return;
|
|
@@ -631,7 +622,7 @@ function validateRefUseCases(
|
|
|
631
622
|
* @returns
|
|
632
623
|
*/
|
|
633
624
|
function calculateAuth(authorization: Authorization<any, any, any>[]) {
|
|
634
|
-
const authFields: Record<string,
|
|
625
|
+
const authFields: Record<string, BaseModelField> = {};
|
|
635
626
|
const rules: string[] = [];
|
|
636
627
|
|
|
637
628
|
for (const entry of authorization) {
|
|
@@ -799,8 +790,8 @@ function capitalize<T extends string>(s: T): Capitalize<T> {
|
|
|
799
790
|
}
|
|
800
791
|
|
|
801
792
|
function processFieldLevelAuthRules(
|
|
802
|
-
fields: Record<string,
|
|
803
|
-
authFields: Record<string,
|
|
793
|
+
fields: Record<string, BaseModelField>,
|
|
794
|
+
authFields: Record<string, BaseModelField>,
|
|
804
795
|
) {
|
|
805
796
|
const fieldLevelAuthRules: {
|
|
806
797
|
[k in keyof typeof fields]: string | null;
|
|
@@ -1203,16 +1194,8 @@ const schemaPreprocessor = (
|
|
|
1203
1194
|
|
|
1204
1195
|
const fieldAuthApplicableFields = Object.fromEntries(
|
|
1205
1196
|
Object.entries(fields).filter(
|
|
1206
|
-
(
|
|
1207
|
-
pair
|
|
1208
|
-
): pair is [
|
|
1209
|
-
string,
|
|
1210
|
-
ModelField<
|
|
1211
|
-
ModelFieldTypeParamOuter,
|
|
1212
|
-
CustomTypeAllowedModifiers,
|
|
1213
|
-
any
|
|
1214
|
-
>,
|
|
1215
|
-
] => isModelField(pair[1]),
|
|
1197
|
+
(pair: [string, unknown]): pair is [string, BaseModelField] =>
|
|
1198
|
+
isModelField(pair[1]),
|
|
1216
1199
|
),
|
|
1217
1200
|
);
|
|
1218
1201
|
|
|
@@ -1296,7 +1279,7 @@ const schemaPreprocessor = (
|
|
|
1296
1279
|
} else if (staticSchema) {
|
|
1297
1280
|
const fields = { ...typeDef.data.fields } as Record<
|
|
1298
1281
|
string,
|
|
1299
|
-
|
|
1282
|
+
BaseModelField
|
|
1300
1283
|
>;
|
|
1301
1284
|
|
|
1302
1285
|
validateRefUseCases(typeName, 'model', fields, getRefType);
|
|
@@ -1341,10 +1324,7 @@ const schemaPreprocessor = (
|
|
|
1341
1324
|
const model = `type ${typeName} @model(timestamps: null) ${authString}${refersToString}\n{\n ${joined}\n}`;
|
|
1342
1325
|
gqlModels.push(model);
|
|
1343
1326
|
} else {
|
|
1344
|
-
const fields = typeDef.data.fields as Record<
|
|
1345
|
-
string,
|
|
1346
|
-
ModelField<any, any>
|
|
1347
|
-
>;
|
|
1327
|
+
const fields = typeDef.data.fields as Record<string, BaseModelField>;
|
|
1348
1328
|
|
|
1349
1329
|
validateRefUseCases(typeName, 'model', fields, getRefType);
|
|
1350
1330
|
|
package/src/util/Brand.ts
CHANGED