@aws-amplify/data-schema 1.9.2 → 1.10.0

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.
Files changed (56) hide show
  1. package/dist/cjs/{ModelRelationalField.js → ModelRelationshipField.js} +8 -5
  2. package/dist/cjs/ModelRelationshipField.js.map +1 -0
  3. package/dist/cjs/SchemaProcessor.js +31 -24
  4. package/dist/cjs/SchemaProcessor.js.map +1 -1
  5. package/dist/cjs/a.js +4 -4
  6. package/dist/cjs/a.js.map +1 -1
  7. package/dist/cjs/runtime/internals/APIClient.js +8 -8
  8. package/dist/cjs/runtime/internals/APIClient.js.map +1 -1
  9. package/dist/esm/Authorization.d.ts +7 -0
  10. package/dist/esm/ClientSchema/Core/ClientModel.d.ts +3 -3
  11. package/dist/esm/ClientSchema/utilities/ResolveField.d.ts +5 -5
  12. package/dist/esm/CustomOperation.d.ts +7 -0
  13. package/dist/esm/CustomType.d.ts +5 -0
  14. package/dist/esm/EnumType.d.ts +5 -0
  15. package/dist/esm/MappedTypes/ModelMetadata.d.ts +5 -5
  16. package/dist/esm/MappedTypes/ResolveFieldProperties.d.ts +9 -9
  17. package/dist/esm/MappedTypes/ResolveSchema.d.ts +3 -3
  18. package/dist/esm/ModelField.d.ts +5 -0
  19. package/dist/esm/{ModelRelationalField.d.ts → ModelRelationshipField.d.ts} +31 -14
  20. package/dist/esm/{ModelRelationalField.mjs → ModelRelationshipField.mjs} +8 -5
  21. package/dist/esm/ModelRelationshipField.mjs.map +1 -0
  22. package/dist/esm/ModelSchema.d.ts +9 -3
  23. package/dist/esm/ModelType.d.ts +18 -5
  24. package/dist/esm/RefType.d.ts +6 -0
  25. package/dist/esm/SchemaProcessor.mjs +23 -16
  26. package/dist/esm/SchemaProcessor.mjs.map +1 -1
  27. package/dist/esm/a.d.ts +1 -1
  28. package/dist/esm/a.mjs +1 -1
  29. package/dist/esm/a.mjs.map +1 -1
  30. package/dist/esm/index.d.ts +9 -0
  31. package/dist/esm/runtime/client/index.d.ts +3 -3
  32. package/dist/esm/runtime/internals/APIClient.mjs +8 -8
  33. package/dist/esm/runtime/internals/APIClient.mjs.map +1 -1
  34. package/dist/meta/cjs.tsbuildinfo +1 -1
  35. package/package.json +1 -1
  36. package/src/Authorization.ts +7 -0
  37. package/src/ClientSchema/Core/ClientModel.ts +3 -3
  38. package/src/ClientSchema/utilities/ResolveField.ts +11 -12
  39. package/src/CustomOperation.ts +7 -0
  40. package/src/CustomType.ts +8 -3
  41. package/src/EnumType.ts +5 -0
  42. package/src/MappedTypes/ModelMetadata.ts +7 -7
  43. package/src/MappedTypes/ResolveFieldProperties.ts +13 -13
  44. package/src/MappedTypes/ResolveSchema.ts +5 -5
  45. package/src/ModelField.ts +5 -3
  46. package/src/{ModelRelationalField.ts → ModelRelationshipField.ts} +55 -35
  47. package/src/ModelSchema.ts +16 -6
  48. package/src/ModelType.ts +21 -8
  49. package/src/RefType.ts +6 -0
  50. package/src/SchemaProcessor.ts +45 -25
  51. package/src/a.ts +1 -1
  52. package/src/index.ts +14 -0
  53. package/src/runtime/client/index.ts +7 -3
  54. package/src/runtime/internals/APIClient.ts +8 -8
  55. package/dist/cjs/ModelRelationalField.js.map +0 -1
  56. package/dist/esm/ModelRelationalField.mjs.map +0 -1
package/src/ModelType.ts CHANGED
@@ -6,10 +6,10 @@ import {
6
6
  } from './util';
7
7
  import type { InternalField, BaseModelField } from './ModelField';
8
8
  import type {
9
- ModelRelationalField,
10
- InternalRelationalField,
11
- ModelRelationalFieldParamShape,
12
- } from './ModelRelationalField';
9
+ ModelRelationshipField,
10
+ InternalRelationshipField,
11
+ ModelRelationshipFieldParamShape,
12
+ } from './ModelRelationshipField';
13
13
  import { type AllowModifier, type Authorization, allow } from './Authorization';
14
14
  import type { RefType, RefTypeParamShape } from './RefType';
15
15
  import type { EnumType } from './EnumType';
@@ -32,7 +32,7 @@ export type deferredRefResolvingPrefix = 'deferredRefResolving:';
32
32
  type ModelFields = Record<
33
33
  string,
34
34
  | BaseModelField
35
- | ModelRelationalField<any, string, any, any>
35
+ | ModelRelationshipField<any, string, any, any>
36
36
  | RefType<any, any, any>
37
37
  | EnumType
38
38
  | CustomType<CustomTypeParamShape>
@@ -40,7 +40,7 @@ type ModelFields = Record<
40
40
 
41
41
  type InternalModelFields = Record<
42
42
  string,
43
- InternalField | InternalRelationalField
43
+ InternalField | InternalRelationshipField
44
44
  >;
45
45
 
46
46
  export type DisableOperationsOptions =
@@ -168,7 +168,7 @@ export type AddRelationshipFieldsToModelTypeFields<
168
168
  Model,
169
169
  RelationshipFields extends Record<
170
170
  string,
171
- ModelRelationalField<ModelRelationalFieldParamShape, string, any, any>
171
+ ModelRelationshipField<ModelRelationshipFieldParamShape, string, any, any>
172
172
  >,
173
173
  > =
174
174
  Model extends ModelType<
@@ -217,6 +217,12 @@ export type BaseModelType<T extends ModelTypeParamShape = ModelTypeParamShape> =
217
217
 
218
218
  export type UsableModelTypeKey = methodKeyOf<ModelType>;
219
219
 
220
+ /**
221
+ * Model type definition interface
222
+ *
223
+ * @param T - The shape of the model type
224
+ * @param UsedMethod - The method keys already defined
225
+ */
220
226
  export type ModelType<
221
227
  T extends ModelTypeParamShape = ModelTypeParamShape,
222
228
  UsedMethod extends UsableModelTypeKey = never,
@@ -299,7 +305,7 @@ export type SchemaModelType<
299
305
  relationships<
300
306
  Param extends Record<
301
307
  string,
302
- ModelRelationalField<any, string, any, any>
308
+ ModelRelationshipField<any, string, any, any>
303
309
  > = Record<never, never>,
304
310
  >(
305
311
  relationships: Param,
@@ -385,6 +391,13 @@ export const isSchemaModelType = (
385
391
  );
386
392
  };
387
393
 
394
+ /**
395
+ * Model default identifier
396
+ *
397
+ * @param pk - primary key
398
+ * @param sk - secondary key
399
+ * @param compositeSk - composite secondary key
400
+ */
388
401
  export type ModelDefaultIdentifier = {
389
402
  pk: { readonly id: string };
390
403
  sk: never;
package/src/RefType.ts CHANGED
@@ -26,6 +26,12 @@ export type RefTypeParamShape = {
26
26
 
27
27
  type MutationOperations = 'create' | 'update' | 'delete';
28
28
 
29
+ /**
30
+ * Reference type definition interface
31
+ *
32
+ * @param T - The shape of the reference type
33
+ * @param K - The keys already defined
34
+ */
29
35
  export type RefType<
30
36
  T extends RefTypeParamShape,
31
37
  K extends keyof RefType<T> = never,
@@ -9,8 +9,8 @@ import {
9
9
  } from './ModelField';
10
10
  import {
11
11
  ModelRelationshipTypes,
12
- type InternalRelationalField,
13
- } from './ModelRelationalField';
12
+ type InternalRelationshipField,
13
+ } from './ModelRelationshipField';
14
14
  import type { InternalModel, DisableOperationsOptions } from './ModelType';
15
15
  import type { InternalModelIndexType } from './ModelIndex';
16
16
  import {
@@ -44,7 +44,7 @@ import {
44
44
  type CustomHandler,
45
45
  type SqlReferenceHandler,
46
46
  FunctionHandler,
47
- AsyncFunctionHandler
47
+ AsyncFunctionHandler,
48
48
  } from './Handler';
49
49
  import * as os from 'os';
50
50
  import * as path from 'path';
@@ -61,7 +61,7 @@ import {
61
61
  type ScalarFieldDef = Exclude<InternalField['data'], { fieldType: 'model' }>;
62
62
 
63
63
  type ModelFieldDef = Extract<
64
- InternalRelationalField['data'],
64
+ InternalRelationshipField['data'],
65
65
  { fieldType: 'model' }
66
66
  >;
67
67
 
@@ -301,10 +301,12 @@ function transformFunctionHandler(
301
301
  } else if (typeof handlerData.handler.getInstance === 'function') {
302
302
  const fnName = `Fn${capitalize(functionFieldName)}${idx === 0 ? '' : `${idx + 1}`}`;
303
303
  lambdaFunctionDefinition[fnName] = handlerData.handler;
304
- const invocationTypeArg = handlerData.invocationType === 'Event' ? ', invocationType: Event)' : ')'
304
+ const invocationTypeArg =
305
+ handlerData.invocationType === 'Event'
306
+ ? ', invocationType: Event)'
307
+ : ')';
305
308
  gqlHandlerContent += `@function(name: "${fnName}"${invocationTypeArg} `;
306
- }
307
- else {
309
+ } else {
308
310
  throw new Error(
309
311
  `Invalid value specified for ${functionFieldName} handler.function(). Expected: defineFunction or string.`,
310
312
  );
@@ -1299,13 +1301,18 @@ const schemaPreprocessor = (
1299
1301
  // This is done here so that:
1300
1302
  // - it only happens once per schema
1301
1303
  // - downstream validation based on `getRefTypeForSchema` finds the EventInvocationResponse type
1302
- const containsAsyncLambdaCustomOperation = Object.entries(schema.data.types).find(([_, typeDef]) => {
1303
- return isCustomOperation(typeDef)
1304
- && finalHandlerIsAsyncFunctionHandler(typeDef.data.handlers);
1304
+ const containsAsyncLambdaCustomOperation = Object.entries(
1305
+ schema.data.types,
1306
+ ).find(([_, typeDef]) => {
1307
+ return (
1308
+ isCustomOperation(typeDef) &&
1309
+ finalHandlerIsAsyncFunctionHandler(typeDef.data.handlers)
1310
+ );
1305
1311
  });
1306
1312
 
1307
1313
  if (containsAsyncLambdaCustomOperation) {
1308
- schema.data.types['EventInvocationResponse'] = eventInvocationResponseCustomType;
1314
+ schema.data.types['EventInvocationResponse'] =
1315
+ eventInvocationResponseCustomType;
1309
1316
  }
1310
1317
 
1311
1318
  const topLevelTypes = sortTopLevelTypes(Object.entries(schema.data.types));
@@ -1442,7 +1449,10 @@ const schemaPreprocessor = (
1442
1449
  }
1443
1450
  } else if (isConversationRoute(typeDef)) {
1444
1451
  // TODO: add inferenceConfiguration values to directive.
1445
- const { field, functionHandler } = createConversationField(typeDef, typeName);
1452
+ const { field, functionHandler } = createConversationField(
1453
+ typeDef,
1454
+ typeName,
1455
+ );
1446
1456
  customMutations.push(field);
1447
1457
  Object.assign(lambdaFunctions, functionHandler);
1448
1458
  shouldAddConversationTypes = true;
@@ -1629,7 +1639,11 @@ function validateCustomOperations(
1629
1639
  ) {
1630
1640
  // TODO: There should be a more elegant and readable way to handle this check.
1631
1641
  // Maybe it's not even necessary anymore since we're the setting returnType in the handler() method.
1632
- if (!handlers || handlers.length === 0 || handlers[handlers.length - 1][brandSymbol] !== 'asyncFunctionHandler') {
1642
+ if (
1643
+ !handlers ||
1644
+ handlers.length === 0 ||
1645
+ handlers[handlers.length - 1][brandSymbol] !== 'asyncFunctionHandler'
1646
+ ) {
1633
1647
  const typeDescription =
1634
1648
  opType === 'Generation' ? 'Generation Route' : `Custom ${opType}`;
1635
1649
  throw new Error(
@@ -1721,14 +1735,20 @@ const isCustomHandler = (
1721
1735
  const isFunctionHandler = (
1722
1736
  handler: HandlerType[] | null,
1723
1737
  ): handler is (FunctionHandler | AsyncFunctionHandler)[] => {
1724
- return Array.isArray(handler) && ['functionHandler', 'asyncFunctionHandler'].includes(getBrand(handler[0]));
1738
+ return (
1739
+ Array.isArray(handler) &&
1740
+ ['functionHandler', 'asyncFunctionHandler'].includes(getBrand(handler[0]))
1741
+ );
1725
1742
  };
1726
1743
 
1727
1744
  const finalHandlerIsAsyncFunctionHandler = (
1728
1745
  handler: HandlerType[] | null,
1729
1746
  ): handler is AsyncFunctionHandler[] => {
1730
- return Array.isArray(handler) && getBrand(handler[handler.length - 1]) === 'asyncFunctionHandler';
1731
- }
1747
+ return (
1748
+ Array.isArray(handler) &&
1749
+ getBrand(handler[handler.length - 1]) === 'asyncFunctionHandler'
1750
+ );
1751
+ };
1732
1752
 
1733
1753
  const normalizeDataSourceName = (
1734
1754
  dataSource: undefined | string | RefType<any, any, any>,
@@ -1820,11 +1840,11 @@ const eventInvocationResponseCustomType = {
1820
1840
  required: true,
1821
1841
  array: false,
1822
1842
  arrayRequired: false,
1823
- }
1824
- }
1843
+ },
1844
+ },
1825
1845
  },
1826
- type: 'customType'
1827
- }
1846
+ type: 'customType',
1847
+ },
1828
1848
  };
1829
1849
 
1830
1850
  function transformCustomOperations(
@@ -1988,8 +2008,8 @@ function validateRelationships(
1988
2008
  );
1989
2009
 
1990
2010
  // Validate that the references defined in the relationship follow the
1991
- // relational definition rules.
1992
- validateRelationalReferences(relationship);
2011
+ // relationship definition rules.
2012
+ validateRelationshipReferences(relationship);
1993
2013
  }
1994
2014
  }
1995
2015
 
@@ -2024,7 +2044,7 @@ function describeConnectFieldRelationship(
2024
2044
  * Validates that the types of child model's reference fields match the types of the parent model's identifier fields.
2025
2045
  * @param relationship The {@link ModelRelationship} to validate.
2026
2046
  */
2027
- function validateRelationalReferences(relationship: ModelRelationship) {
2047
+ function validateRelationshipReferences(relationship: ModelRelationship) {
2028
2048
  const {
2029
2049
  parent,
2030
2050
  parentConnectionField,
@@ -2101,7 +2121,7 @@ type ConnectionField = {
2101
2121
  /**
2102
2122
  * An internal representation of a model relationship used by validation functions.
2103
2123
  * Use {@link getModelRelationship} to create this.
2104
- * See {@link validateRelationalReferences} for validation example.
2124
+ * See {@link validateRelationshipReferences} for validation example.
2105
2125
  */
2106
2126
  type ModelRelationship = {
2107
2127
  /**
@@ -2212,7 +2232,7 @@ function getAssociatedConnectionField(
2212
2232
 
2213
2233
  // In order to find that associated connection field, we need to do some validation that we'll depend on further downstream.
2214
2234
  // 1. Field type matches the source model's type.
2215
- // 2. A valid counterpart relational modifier is defined on the field. See `associatedRelationshipTypes` for more information.
2235
+ // 2. A valid counterpart relationship modifier is defined on the field. See `associatedRelationshipTypes` for more information.
2216
2236
  // 3. The reference arguments provided to the field match (element count + string comparison) references passed to the source connection field.
2217
2237
  return (
2218
2238
  connectionField.data.relatedModel === sourceModelName &&
package/src/a.ts CHANGED
@@ -18,7 +18,7 @@ import {
18
18
  ipAddress,
19
19
  } from './ModelField';
20
20
  import { ref } from './RefType';
21
- import { hasOne, hasMany, belongsTo } from './ModelRelationalField';
21
+ import { hasOne, hasMany, belongsTo } from './ModelRelationshipField';
22
22
  import { customType } from './CustomType';
23
23
  import { enumType } from './EnumType';
24
24
  import { query, mutation, subscription, generation } from './CustomOperation';
package/src/index.ts CHANGED
@@ -4,3 +4,17 @@ import { ClientSchema } from './ClientSchema';
4
4
  export { a };
5
5
 
6
6
  export type { ClientSchema };
7
+
8
+ export type { Authorization } from './Authorization';
9
+ export type { CustomOperation } from './CustomOperation';
10
+ export type { ModelField, Nullable, Json } from './ModelField';
11
+ export type { ModelSchema } from './ModelSchema';
12
+ export type { ModelType, ModelDefaultIdentifier } from './ModelType';
13
+ export type { RefType } from './RefType';
14
+ export type { CustomType } from './CustomType';
15
+ export type {
16
+ ModelRelationshipField,
17
+ ModelRelationshipTypes,
18
+ ModelRelationshipTypeArgFactory,
19
+ } from './ModelRelationshipField';
20
+ export type { EnumType } from './EnumType';
@@ -48,7 +48,7 @@ type Model = Record<string, any>;
48
48
  /**
49
49
  * Currently this omits any object-type fields. Update this when we add custom types/enums.
50
50
  */
51
- type NonRelationalFields<M extends Model> = {
51
+ type NonRelationshipFields<M extends Model> = {
52
52
  [Field in keyof M as UnwrapArray<M[Field]> extends Record<string, unknown>
53
53
  ? never
54
54
  : Field]: M[Field];
@@ -166,7 +166,11 @@ type DeepPickFromPath<
166
166
  : Path extends `${infer Head}.${infer Tail}`
167
167
  ? Head extends keyof FlatModel
168
168
  ? Tail extends '*'
169
- ? { [k in Head]: NonRelationalFields<UnwrapArray<FlatModel[Head]>> }
169
+ ? {
170
+ [k in Head]: NonRelationshipFields<
171
+ UnwrapArray<FlatModel[Head]>
172
+ >;
173
+ }
170
174
  : { [k in Head]: DeepPickFromPath<FlatModel[Head], Tail> }
171
175
  : never
172
176
  : Path extends keyof FlatModel
@@ -275,7 +279,7 @@ type ResolvedModel<
275
279
  Depth extends number = 7,
276
280
  RecursionLoop extends number[] = [-1, 0, 1, 2, 3, 4, 5, 6],
277
281
  > = {
278
- done: NonRelationalFields<Model>;
282
+ done: NonRelationshipFields<Model>;
279
283
  recur: {
280
284
  [Field in keyof Model]: Model[Field] extends (
281
285
  ...args: any
@@ -207,7 +207,7 @@ export function initializeModel(
207
207
  if (record === null || record === undefined) {
208
208
  return record;
209
209
  }
210
- const initializedRelationalFields: Record<string, any> = {};
210
+ const initializedRelationshipFields: Record<string, any> = {};
211
211
  for (const fieldName of modelFields) {
212
212
  const modelField = introModelFields[fieldName];
213
213
  const modelFieldType = modelField?.type as ModelFieldType;
@@ -255,7 +255,7 @@ export function initializeModel(
255
255
  }
256
256
 
257
257
  if (context) {
258
- initializedRelationalFields[fieldName] = (
258
+ initializedRelationshipFields[fieldName] = (
259
259
  contextSpec: AmplifyServer.ContextSpec,
260
260
  options?: LazyLoadOptions,
261
261
  ) => {
@@ -276,7 +276,7 @@ export function initializeModel(
276
276
  return { data: null };
277
277
  };
278
278
  } else {
279
- initializedRelationalFields[fieldName] = (
279
+ initializedRelationshipFields[fieldName] = (
280
280
  options?: LazyLoadOptions,
281
281
  ) => {
282
282
  if (record[targetNames[0]]) {
@@ -347,7 +347,7 @@ export function initializeModel(
347
347
  }
348
348
 
349
349
  if (context) {
350
- initializedRelationalFields[fieldName] = (
350
+ initializedRelationshipFields[fieldName] = (
351
351
  contextSpec: AmplifyServer.ContextSpec,
352
352
  options?: LazyLoadOptions,
353
353
  ) => {
@@ -373,7 +373,7 @@ export function initializeModel(
373
373
  return [];
374
374
  };
375
375
  } else {
376
- initializedRelationalFields[fieldName] = (
376
+ initializedRelationshipFields[fieldName] = (
377
377
  options?: LazyLoadOptions,
378
378
  ) => {
379
379
  if (record[parentPk]) {
@@ -418,7 +418,7 @@ export function initializeModel(
418
418
  }
419
419
 
420
420
  if (context) {
421
- initializedRelationalFields[fieldName] = (
421
+ initializedRelationshipFields[fieldName] = (
422
422
  contextSpec: AmplifyServer.ContextSpec,
423
423
  options?: LazyLoadOptions,
424
424
  ) => {
@@ -444,7 +444,7 @@ export function initializeModel(
444
444
  return [];
445
445
  };
446
446
  } else {
447
- initializedRelationalFields[fieldName] = (
447
+ initializedRelationshipFields[fieldName] = (
448
448
  options?: LazyLoadOptions,
449
449
  ) => {
450
450
  if (record[parentPk]) {
@@ -477,7 +477,7 @@ export function initializeModel(
477
477
  }
478
478
  }
479
479
 
480
- return { ...record, ...initializedRelationalFields };
480
+ return { ...record, ...initializedRelationshipFields };
481
481
  });
482
482
  }
483
483
 
@@ -1 +0,0 @@
1
- {"version":3,"file":"ModelRelationalField.js","sources":["../../src/ModelRelationalField.ts"],"sourcesContent":["\"use strict\";\nObject.defineProperty(exports, \"__esModule\", { value: true });\nexports.belongsTo = exports.hasMany = exports.hasOne = exports.ModelRelationshipTypes = exports.__auth = void 0;\nconst Authorization_1 = require(\"./Authorization\");\n/**\n * Used to \"attach\" auth types to ModelField without exposing them on the builder.\n */\nexports.__auth = Symbol('__auth');\nconst brandName = 'modelRelationalField';\nvar ModelRelationshipTypes;\n(function (ModelRelationshipTypes) {\n ModelRelationshipTypes[\"hasOne\"] = \"hasOne\";\n ModelRelationshipTypes[\"hasMany\"] = \"hasMany\";\n ModelRelationshipTypes[\"belongsTo\"] = \"belongsTo\";\n})(ModelRelationshipTypes || (exports.ModelRelationshipTypes = ModelRelationshipTypes = {}));\nconst relationalModifiers = [\n 'required',\n 'valueRequired',\n 'authorization',\n];\nconst relationModifierMap = {\n belongsTo: ['authorization'],\n hasMany: ['valueRequired', 'authorization'],\n hasOne: ['required', 'authorization'],\n};\nfunction _modelRelationalField(type, relatedModel, references) {\n const data = {\n relatedModel,\n type,\n fieldType: 'model',\n array: false,\n valueRequired: false,\n arrayRequired: false,\n references,\n authorization: [],\n };\n data.array = type === 'hasMany';\n const relationshipBuilderFunctions = {\n required() {\n data.arrayRequired = true;\n return this;\n },\n valueRequired() {\n data.valueRequired = true;\n return this;\n },\n authorization(callback) {\n const rules = callback(Authorization_1.allow);\n data.authorization = Array.isArray(rules) ? rules : [rules];\n return this;\n },\n };\n const builder = Object.fromEntries(relationModifierMap[type].map((key) => [\n key,\n relationshipBuilderFunctions[key],\n ]));\n return {\n ...builder,\n data,\n };\n}\n/**\n * Create one-to-one relationship between two models using the `hasOne(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field\n * unless overwritten with the `identifier()` method.\n * @example\n * const schema = a.schema({\n * Cart: a.model({\n * items: a.string().required().array(),\n * // 1. Create reference field\n * customerId: a.id(),\n * // 2. Create relationship field with the reference field\n * customer: a.belongsTo('Customer', 'customerId'),\n * }),\n * Customer: a.model({\n * name: a.string(),\n * // 3. Create relationship field with the reference field\n * // from the Cart model\n * activeCart: a.hasOne('Cart', 'customerId')\n * }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-a-one-to-one-relationship}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-one relationship definition\n */\nfunction hasOne(relatedModel, references) {\n return _modelRelationalField(ModelRelationshipTypes.hasOne, relatedModel, Array.isArray(references) ? references : [references]);\n}\nexports.hasOne = hasOne;\n/**\n * Create a one-directional one-to-many relationship between two models using the `hasMany(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * @example\n * const schema = a.schema({\n * Member: a.model({\n * name: a.string().required(),\n * // 1. Create a reference field\n * teamId: a.id(),\n * // 2. Create a belongsTo relationship with the reference field\n * team: a.belongsTo('Team', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n *\n * Team: a.model({\n * mantra: a.string().required(),\n * // 3. Create a hasMany relationship with the reference field\n * // from the `Member`s model.\n * members: a.hasMany('Member', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-one-to-many-relationships}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-many relationship definition\n */\nfunction hasMany(relatedModel, references) {\n return _modelRelationalField(ModelRelationshipTypes.hasMany, relatedModel, Array.isArray(references) ? references : [references]);\n}\nexports.hasMany = hasMany;\n/**\n * Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.\n * The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from\n * parent to the related model.\n *\n * @example\n * // one-to-many relationship\n * const schema = a.schema({\n * Member: a.model({\n * name: a.string().required(),\n * // 1. Create a reference field\n * teamId: a.id(),\n * // 2. Create a belongsTo relationship with the reference field\n * team: a.belongsTo('Team', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n *\n * Team: a.model({\n * mantra: a.string().required(),\n * // 3. Create a hasMany relationship with the reference field\n * // from the `Member`s model.\n * members: a.hasMany('Member', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n * });\n * @example\n * // one-to-one relationship\n * const schema = a.schema({\n * Cart: a.model({\n * items: a.string().required().array(),\n * // 1. Create reference field\n * customerId: a.id(),\n * // 2. Create relationship field with the reference field\n * customer: a.belongsTo('Customer', 'customerId'),\n * }),\n * Customer: a.model({\n * name: a.string(),\n * // 3. Create relationship field with the reference field\n * // from the Cart model\n * activeCart: a.hasOne('Cart', 'customerId')\n * }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/}\n * @param relatedModel name of the related `.hasOne()` or `.hasMany()` model\n * @param references the field(s) that should be used to reference the related model\n * @returns a belong-to relationship definition\n */\nfunction belongsTo(relatedModel, references) {\n return _modelRelationalField(ModelRelationshipTypes.belongsTo, relatedModel, Array.isArray(references) ? references : [references]);\n}\nexports.belongsTo = belongsTo;\n"],"names":[],"mappings":";;AACA,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,YAAY,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;AAC9D,OAAO,CAAC,SAAS,GAAG,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,GAAG,OAAO,CAAC,sBAAsB,GAAG,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,CAAC;AAChH,MAAM,eAAe,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC;AACnD;AACA;AACA;AACA,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC;AAElC,IAAI,sBAAsB,CAAC;AAC3B,CAAC,UAAU,sBAAsB,EAAE;AACnC,IAAI,sBAAsB,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;AAChD,IAAI,sBAAsB,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;AAClD,IAAI,sBAAsB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;AACt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A;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAAS,SAAS,CAAC,YAAY,EAAE,UAAU,EAAE;AAC7C,IAAI,OAAO,qBAAqB,CAAC,sBAAsB,CAAC,SAAS,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;AACxI,CAAC;AACD,OAAO,CAAC,SAAS,GAAG,SAAS;;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ModelRelationalField.mjs","sources":["../../src/ModelRelationalField.ts"],"sourcesContent":["import { allow } from './Authorization';\n/**\n * Used to \"attach\" auth types to ModelField without exposing them on the builder.\n */\nexport const __auth = Symbol('__auth');\nconst brandName = 'modelRelationalField';\nexport var ModelRelationshipTypes;\n(function (ModelRelationshipTypes) {\n ModelRelationshipTypes[\"hasOne\"] = \"hasOne\";\n ModelRelationshipTypes[\"hasMany\"] = \"hasMany\";\n ModelRelationshipTypes[\"belongsTo\"] = \"belongsTo\";\n})(ModelRelationshipTypes || (ModelRelationshipTypes = {}));\nconst relationalModifiers = [\n 'required',\n 'valueRequired',\n 'authorization',\n];\nconst relationModifierMap = {\n belongsTo: ['authorization'],\n hasMany: ['valueRequired', 'authorization'],\n hasOne: ['required', 'authorization'],\n};\nfunction _modelRelationalField(type, relatedModel, references) {\n const data = {\n relatedModel,\n type,\n fieldType: 'model',\n array: false,\n valueRequired: false,\n arrayRequired: false,\n references,\n authorization: [],\n };\n data.array = type === 'hasMany';\n const relationshipBuilderFunctions = {\n required() {\n data.arrayRequired = true;\n return this;\n },\n valueRequired() {\n data.valueRequired = true;\n return this;\n },\n authorization(callback) {\n const rules = callback(allow);\n data.authorization = Array.isArray(rules) ? rules : [rules];\n return this;\n },\n };\n const builder = Object.fromEntries(relationModifierMap[type].map((key) => [\n key,\n relationshipBuilderFunctions[key],\n ]));\n return {\n ...builder,\n data,\n };\n}\n/**\n * Create one-to-one relationship between two models using the `hasOne(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field\n * unless overwritten with the `identifier()` method.\n * @example\n * const schema = a.schema({\n * Cart: a.model({\n * items: a.string().required().array(),\n * // 1. Create reference field\n * customerId: a.id(),\n * // 2. Create relationship field with the reference field\n * customer: a.belongsTo('Customer', 'customerId'),\n * }),\n * Customer: a.model({\n * name: a.string(),\n * // 3. Create relationship field with the reference field\n * // from the Cart model\n * activeCart: a.hasOne('Cart', 'customerId')\n * }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-a-one-to-one-relationship}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-one relationship definition\n */\nexport function hasOne(relatedModel, references) {\n return _modelRelationalField(ModelRelationshipTypes.hasOne, relatedModel, Array.isArray(references) ? references : [references]);\n}\n/**\n * Create a one-directional one-to-many relationship between two models using the `hasMany(\"MODEL_NAME\", \"REFERENCE_FIELD(s)\")` method.\n * @example\n * const schema = a.schema({\n * Member: a.model({\n * name: a.string().required(),\n * // 1. Create a reference field\n * teamId: a.id(),\n * // 2. Create a belongsTo relationship with the reference field\n * team: a.belongsTo('Team', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n *\n * Team: a.model({\n * mantra: a.string().required(),\n * // 3. Create a hasMany relationship with the reference field\n * // from the `Member`s model.\n * members: a.hasMany('Member', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-one-to-many-relationships}\n * @param relatedModel the name of the related model\n * @param references the field(s) that should be used to reference the related model\n * @returns a one-to-many relationship definition\n */\nexport function hasMany(relatedModel, references) {\n return _modelRelationalField(ModelRelationshipTypes.hasMany, relatedModel, Array.isArray(references) ? references : [references]);\n}\n/**\n * Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.\n * The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from\n * parent to the related model.\n *\n * @example\n * // one-to-many relationship\n * const schema = a.schema({\n * Member: a.model({\n * name: a.string().required(),\n * // 1. Create a reference field\n * teamId: a.id(),\n * // 2. Create a belongsTo relationship with the reference field\n * team: a.belongsTo('Team', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n *\n * Team: a.model({\n * mantra: a.string().required(),\n * // 3. Create a hasMany relationship with the reference field\n * // from the `Member`s model.\n * members: a.hasMany('Member', 'teamId'),\n * })\n * .authorization(allow => [allow.publicApiKey()]),\n * });\n * @example\n * // one-to-one relationship\n * const schema = a.schema({\n * Cart: a.model({\n * items: a.string().required().array(),\n * // 1. Create reference field\n * customerId: a.id(),\n * // 2. Create relationship field with the reference field\n * customer: a.belongsTo('Customer', 'customerId'),\n * }),\n * Customer: a.model({\n * name: a.string(),\n * // 3. Create relationship field with the reference field\n * // from the Cart model\n * activeCart: a.hasOne('Cart', 'customerId')\n * }),\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/}\n * @param relatedModel name of the related `.hasOne()` or `.hasMany()` model\n * @param references the field(s) that should be used to reference the related model\n * @returns a belong-to relationship definition\n */\nexport function belongsTo(relatedModel, references) {\n return _modelRelationalField(ModelRelationshipTypes.belongsTo, relatedModel, Array.isArray(references) ? references : [references]);\n}\n"],"names":[],"mappings":";;AACA;AACA;AACA;AACY,MAAC,MAAM,GAAG,MAAM,CAAC,QAAQ,EAAE;AAE7B,IAAC,uBAAuB;AAClC,CAAC,UAAU,sBAAsB,EAAE;AACnC,IAAI,sBAAsB,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;AAChD,IAAI,sBAAsB,CAAC,SAAS,CAAC,GAAG,SAAS,CAAC;AAClD,IAAI,sBAAsB,CAAC,WAAW,CAAC,GAAG,WAAW,CAAC;AACtD,CAAC,EAAE,sBAAsB,KAAK,sBAAsB,GAAG,EAAE,CAAC,CAAC,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GAAG,KAAK;AAC9E,QAAQ,GAAG;AACX,QAAQ,4BAA4B,CAAC,GAAG,CAAC;AACzC,KAAK,CAAC,CAAC,CAAC;AACR,IAAI,OAAO;AACX,QAAQ,GAAG,OAAO;AAClB,QAAQ,IAAI;AACZ,KAAK,CAAC;AACN,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,MAAM,CAAC,YAAY,EAAE,UAAU,EAAE;AACjD,IAAI,OAAO,qBAAqB,CAAC,sBAAsB,CAAC,MAAM,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;AACrI,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,OAAO,CAAC,YAAY,EAAE,UAAU,EAAE;AAClD,IAAI,OAAO,qBAAqB,CAAC,sBAAsB,CAAC,OAAO,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;AACtI,CAAC;AACD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,SAAS,CAAC,YAAY,EAAE,UAAU,EAAE;AACpD,IAAI,OAAO,qBAAqB,CAAC,sBAAsB,CAAC,SAAS,EAAE,YAAY,EAAE,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,UAAU,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;AACxI;;;;"}