@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.
Files changed (57) hide show
  1. package/dist/cjs/CustomOperation.js +46 -0
  2. package/dist/cjs/CustomOperation.js.map +1 -1
  3. package/dist/cjs/CustomType.js +32 -0
  4. package/dist/cjs/CustomType.js.map +1 -1
  5. package/dist/cjs/Handler.js +62 -0
  6. package/dist/cjs/Handler.js.map +1 -1
  7. package/dist/cjs/ModelRelationalField.js +82 -3
  8. package/dist/cjs/ModelRelationalField.js.map +1 -1
  9. package/dist/cjs/util/Brand.js +4 -4
  10. package/dist/cjs/util/Brand.js.map +1 -1
  11. package/dist/cjs/util/usedMethods.js +4 -0
  12. package/dist/cjs/util/usedMethods.js.map +1 -0
  13. package/dist/esm/CustomOperation.d.ts +49 -3
  14. package/dist/esm/CustomOperation.mjs +46 -0
  15. package/dist/esm/CustomOperation.mjs.map +1 -1
  16. package/dist/esm/CustomType.d.ts +35 -3
  17. package/dist/esm/CustomType.mjs +32 -0
  18. package/dist/esm/CustomType.mjs.map +1 -1
  19. package/dist/esm/EnumType.d.ts +7 -9
  20. package/dist/esm/Handler.d.ts +62 -0
  21. package/dist/esm/Handler.mjs +62 -0
  22. package/dist/esm/Handler.mjs.map +1 -1
  23. package/dist/esm/MappedTypes/CustomOperations.d.ts +3 -3
  24. package/dist/esm/MappedTypes/ExtractNonModelTypes.d.ts +4 -4
  25. package/dist/esm/MappedTypes/ResolveFieldProperties.d.ts +2 -2
  26. package/dist/esm/MappedTypes/ResolveSchema.d.ts +6 -6
  27. package/dist/esm/ModelField.d.ts +16 -13
  28. package/dist/esm/ModelRelationalField.d.ts +83 -4
  29. package/dist/esm/ModelRelationalField.mjs +82 -3
  30. package/dist/esm/ModelRelationalField.mjs.map +1 -1
  31. package/dist/esm/ModelSchema.d.ts +4 -4
  32. package/dist/esm/ModelType.d.ts +16 -12
  33. package/dist/esm/RefType.d.ts +1 -1
  34. package/dist/esm/util/Brand.d.ts +1 -2
  35. package/dist/esm/util/Brand.mjs +1 -1
  36. package/dist/esm/util/Brand.mjs.map +1 -1
  37. package/dist/esm/util/usedMethods.d.ts +4 -0
  38. package/dist/esm/util/usedMethods.mjs +2 -0
  39. package/dist/esm/util/usedMethods.mjs.map +1 -0
  40. package/dist/meta/cjs.tsbuildinfo +1 -1
  41. package/package.json +1 -1
  42. package/src/CustomOperation.ts +49 -7
  43. package/src/CustomType.ts +36 -8
  44. package/src/EnumType.ts +16 -22
  45. package/src/Handler.ts +62 -0
  46. package/src/MappedTypes/CustomOperations.ts +4 -6
  47. package/src/MappedTypes/ExtractNonModelTypes.ts +39 -40
  48. package/src/MappedTypes/ResolveFieldProperties.ts +2 -2
  49. package/src/MappedTypes/ResolveSchema.ts +18 -21
  50. package/src/ModelField.ts +34 -19
  51. package/src/ModelRelationalField.ts +83 -4
  52. package/src/ModelSchema.ts +8 -12
  53. package/src/ModelType.ts +30 -34
  54. package/src/RefType.ts +1 -1
  55. package/src/SchemaProcessor.ts +21 -41
  56. package/src/util/Brand.ts +1 -1
  57. package/src/util/usedMethods.ts +5 -0
@@ -1,7 +1,7 @@
1
1
  import type { Brand } from './util';
2
- import type { ModelField, InternalField, ModelFieldTypeParamOuter } from './ModelField';
2
+ import type { InternalField, BaseModelField } from './ModelField';
3
3
  import type { RefType } from './RefType';
4
- import type { EnumType, EnumTypeParamShape } from './EnumType';
4
+ import type { EnumType } from './EnumType';
5
5
  /**
6
6
  * Custom Types
7
7
  *
@@ -9,7 +9,7 @@ import type { EnumType, EnumTypeParamShape } from './EnumType';
9
9
  *
10
10
  */
11
11
  export type CustomTypeAllowedModifiers = 'authorization' | 'array' | 'required';
12
- type CustomTypeFields = Record<string, ModelField<ModelFieldTypeParamOuter, CustomTypeAllowedModifiers, any> | RefType<any, any, any> | EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape>>;
12
+ type CustomTypeFields = Record<string, BaseModelField | RefType<any, any, any> | EnumType | CustomType<CustomTypeParamShape>>;
13
13
  type InternalModelFields = Record<string, InternalField>;
14
14
  type CustomTypeData = {
15
15
  fields: CustomTypeFields;
@@ -29,6 +29,38 @@ export type CustomType<T extends CustomTypeParamShape> = T & Brand<'customType'>
29
29
  export type InternalCustomType = CustomType<any> & {
30
30
  data: InternalCustomTypeData;
31
31
  };
32
+ /**
33
+ * Define a custom type. This type represents an inline, typed JSON object.
34
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/add-fields/#specify-a-custom-field-type}
35
+ * @param fields the fields to be added to the custom type
36
+ * @returns a custom type
37
+ * @example
38
+ * a.schema({
39
+ * Post: a.model({
40
+ * location: a.customType({
41
+ * lat: a.float(),
42
+ * long: a.float(),
43
+ * }),
44
+ * content: a.string(),
45
+ * }),
46
+ * });
47
+ * @example
48
+ * a.schema({
49
+ * Location: a.customType({
50
+ * lat: a.float(),
51
+ * long: a.float(),
52
+ * }),
53
+ *
54
+ * Post: a.model({
55
+ * location: a.ref('Location'),
56
+ * content: a.string(),
57
+ * }),
58
+ *
59
+ * User: a.model({
60
+ * lastKnownLocation: a.ref('Location'),
61
+ * }),
62
+ * });
63
+ */
32
64
  export declare function customType<T extends CustomTypeFields>(fields: T): CustomType<{
33
65
  fields: T;
34
66
  }>;
@@ -5,6 +5,38 @@ function _customType(fields) {
5
5
  };
6
6
  return { data };
7
7
  }
8
+ /**
9
+ * Define a custom type. This type represents an inline, typed JSON object.
10
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/add-fields/#specify-a-custom-field-type}
11
+ * @param fields the fields to be added to the custom type
12
+ * @returns a custom type
13
+ * @example
14
+ * a.schema({
15
+ * Post: a.model({
16
+ * location: a.customType({
17
+ * lat: a.float(),
18
+ * long: a.float(),
19
+ * }),
20
+ * content: a.string(),
21
+ * }),
22
+ * });
23
+ * @example
24
+ * a.schema({
25
+ * Location: a.customType({
26
+ * lat: a.float(),
27
+ * long: a.float(),
28
+ * }),
29
+ *
30
+ * Post: a.model({
31
+ * location: a.ref('Location'),
32
+ * content: a.string(),
33
+ * }),
34
+ *
35
+ * User: a.model({
36
+ * lastKnownLocation: a.ref('Location'),
37
+ * }),
38
+ * });
39
+ */
8
40
  function customType(fields) {
9
41
  return _customType(fields);
10
42
  }
@@ -1 +1 @@
1
- {"version":3,"file":"CustomType.mjs","sources":["../../src/CustomType.ts"],"sourcesContent":["function _customType(fields) {\n const data = {\n fields,\n type: 'customType',\n };\n return { data };\n}\nexport function customType(fields) {\n return _customType(fields);\n}\n"],"names":[],"mappings":"AAAA,SAAS,WAAW,CAAC,MAAM,EAAE;AAC7B,IAAI,MAAM,IAAI,GAAG;AACjB,QAAQ,MAAM;AACd,QAAQ,IAAI,EAAE,YAAY;AAC1B,KAAK,CAAC;AACN,IAAI,OAAO,EAAE,IAAI,EAAE,CAAC;AACpB,CAAC;AACM,SAAS,UAAU,CAAC,MAAM,EAAE;AACnC,IAAI,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC;AAC/B;;;;"}
1
+ {"version":3,"file":"CustomType.mjs","sources":["../../src/CustomType.ts"],"sourcesContent":["function _customType(fields) {\n const data = {\n fields,\n type: 'customType',\n };\n return { data };\n}\n/**\n * Define a custom type. This type represents an inline, typed JSON object.\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/add-fields/#specify-a-custom-field-type}\n * @param fields the fields to be added to the custom type\n * @returns a custom type\n * @example\n * a.schema({\n * Post: a.model({\n * location: a.customType({\n * lat: a.float(),\n * long: a.float(),\n * }),\n * content: a.string(),\n * }),\n * });\n * @example\n * a.schema({\n * Location: a.customType({\n * lat: a.float(),\n * long: a.float(),\n * }),\n *\n * Post: a.model({\n * location: a.ref('Location'),\n * content: a.string(),\n * }),\n *\n * User: a.model({\n * lastKnownLocation: a.ref('Location'),\n * }),\n * });\n */\nexport function customType(fields) {\n return _customType(fields);\n}\n"],"names":[],"mappings":"AAAA,SAAS,WAAW,CAAC,MAAM,EAAE;AAC7B,IAAI,MAAM,IAAI,GAAG;AACjB,QAAQ,MAAM;AACd,QAAQ,IAAI,EAAE,YAAY;AAC1B,KAAK,CAAC;AACN,IAAI,OAAO,EAAE,IAAI,EAAE,CAAC;AACpB,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;AACO,SAAS,UAAU,CAAC,MAAM,EAAE;AACnC,IAAI,OAAO,WAAW,CAAC,MAAM,CAAC,CAAC;AAC/B;;;;"}
@@ -1,15 +1,13 @@
1
- import { Brand } from './util';
2
- export type EnumTypeParamShape = {
1
+ import type { brandSymbol } from './util/Brand.js';
2
+ type EnumTypeParamShape<values extends readonly string[] = readonly string[]> = {
3
3
  type: 'enum';
4
- values: readonly string[];
5
- };
6
- export type EnumType<T extends EnumTypeParamShape> = T & Brand<'enum'>;
7
- type EnumTypeArgFactory<Values extends readonly string[]> = {
8
- type: 'enum';
9
- values: Values;
4
+ values: values;
10
5
  };
6
+ export interface EnumType<values extends readonly string[] = readonly string[]> extends EnumTypeParamShape<values> {
7
+ [brandSymbol]: 'enum';
8
+ }
11
9
  /**
12
10
  * this type param pattern allows us to infer literal type values from the array without using the `as const` suffix
13
11
  */
14
- export declare function enumType<Value extends string, T extends readonly Value[]>(values: T): EnumType<EnumTypeArgFactory<T>>;
12
+ export declare function enumType<const values extends readonly string[]>(values: values): EnumType<values>;
15
13
  export {};
@@ -41,12 +41,74 @@ declare const customHandlerBrand = "customHandler";
41
41
  export type CustomHandler = {
42
42
  [dataSymbol]: CustomHandlerData;
43
43
  } & Brand<typeof customHandlerBrand>;
44
+ /**
45
+ * Use a custom JavaScript resolver to handle a query, mutation, or subscription.
46
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/#step-2---configure-custom-business-logic-handler-code}
47
+ * @param customHandler `{ entry: "path-to-javascript-resolver-file.js", dataSource: "Data Source name added via "backend.data.add*DataSoruce(...)"}`
48
+ * @returns A JavaScript resolver attached to the query, mutation, or subscription.
49
+ * @example
50
+ * const schema = a.schema({
51
+ * Post: a.model({
52
+ * content: a.string(),
53
+ * likes: a.integer()
54
+ * .authorization(allow => [allow.authenticated().to(['read'])])
55
+ * }).authorization(allow => [
56
+ * allow.owner(),
57
+ * allow.authenticated().to(['read'])
58
+ * ]),
59
+ *
60
+ * likePost: a
61
+ * .mutation()
62
+ * .arguments({ postId: a.id() })
63
+ * .returns(a.ref('Post'))
64
+ * .authorization(allow => [allow.authenticated()])
65
+ * .handler(a.handler.custom({
66
+ * dataSource: a.ref('Post'),
67
+ * entry: './increment-like.js'
68
+ * }))
69
+ * });
70
+ */
44
71
  declare function custom(customHandler: CustomHandlerInput): CustomHandler;
45
72
  export type FunctionHandlerData = DefineFunction | string;
46
73
  declare const functionHandlerBrand = "functionHandler";
47
74
  export type FunctionHandler = {
48
75
  [dataSymbol]: FunctionHandlerData;
49
76
  } & Brand<typeof functionHandlerBrand>;
77
+ /**
78
+ * Use a function created via `defineFunction` to handle the custom query/mutation/subscription. In your function handler,
79
+ * you can use the `Schema["YOUR_QUERY_OR_MUTATION_NAME"]["functionHandler"]` utility type to type the handler function.
80
+ * @example
81
+ * import {
82
+ * type ClientSchema,
83
+ * a,
84
+ * defineData,
85
+ * defineFunction // 1.Import "defineFunction" to create new functions
86
+ * } from '@aws-amplify/backend';
87
+ *
88
+ * // 2. define a function
89
+ * const echoHandler = defineFunction({
90
+ * entry: './echo-handler/handler.ts'
91
+ * })
92
+ *
93
+ * const schema = a.schema({
94
+ * EchoResponse: a.customType({
95
+ * content: a.string(),
96
+ * executionDuration: a.float()
97
+ * }),
98
+ *
99
+ * echo: a
100
+ * .query()
101
+ * .arguments({ content: a.string() })
102
+ * .returns(a.ref('EchoResponse'))
103
+ * .authorization(allow => [allow.publicApiKey()])
104
+ * // 3. set the function has the handler
105
+ * .handler(a.handler.function(echoHandler))
106
+ * });
107
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/}
108
+ * @param fn A function created via `defineFunction`. Alternatively, you can pass in a "string" of the function name and pass
109
+ * in a corresponding value into the `functionMap` property of defineData.
110
+ * @returns A handler for the query / mutation / subscription
111
+ */
50
112
  declare function fcn(fn: FunctionHandlerData): FunctionHandler;
51
113
  export declare const handler: {
52
114
  inlineSql: typeof inlineSql;
@@ -24,6 +24,33 @@ function sqlReference(sqlFilePath) {
24
24
  };
25
25
  }
26
26
  const customHandlerBrand = 'customHandler';
27
+ /**
28
+ * Use a custom JavaScript resolver to handle a query, mutation, or subscription.
29
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/#step-2---configure-custom-business-logic-handler-code}
30
+ * @param customHandler `{ entry: "path-to-javascript-resolver-file.js", dataSource: "Data Source name added via "backend.data.add*DataSoruce(...)"}`
31
+ * @returns A JavaScript resolver attached to the query, mutation, or subscription.
32
+ * @example
33
+ * const schema = a.schema({
34
+ * Post: a.model({
35
+ * content: a.string(),
36
+ * likes: a.integer()
37
+ * .authorization(allow => [allow.authenticated().to(['read'])])
38
+ * }).authorization(allow => [
39
+ * allow.owner(),
40
+ * allow.authenticated().to(['read'])
41
+ * ]),
42
+ *
43
+ * likePost: a
44
+ * .mutation()
45
+ * .arguments({ postId: a.id() })
46
+ * .returns(a.ref('Post'))
47
+ * .authorization(allow => [allow.authenticated()])
48
+ * .handler(a.handler.custom({
49
+ * dataSource: a.ref('Post'),
50
+ * entry: './increment-like.js'
51
+ * }))
52
+ * });
53
+ */
27
54
  function custom(customHandler) {
28
55
  // used to determine caller directory in order to resolve relative path downstream
29
56
  const stack = new Error().stack;
@@ -33,6 +60,41 @@ function custom(customHandler) {
33
60
  };
34
61
  }
35
62
  const functionHandlerBrand = 'functionHandler';
63
+ /**
64
+ * Use a function created via `defineFunction` to handle the custom query/mutation/subscription. In your function handler,
65
+ * you can use the `Schema["YOUR_QUERY_OR_MUTATION_NAME"]["functionHandler"]` utility type to type the handler function.
66
+ * @example
67
+ * import {
68
+ * type ClientSchema,
69
+ * a,
70
+ * defineData,
71
+ * defineFunction // 1.Import "defineFunction" to create new functions
72
+ * } from '@aws-amplify/backend';
73
+ *
74
+ * // 2. define a function
75
+ * const echoHandler = defineFunction({
76
+ * entry: './echo-handler/handler.ts'
77
+ * })
78
+ *
79
+ * const schema = a.schema({
80
+ * EchoResponse: a.customType({
81
+ * content: a.string(),
82
+ * executionDuration: a.float()
83
+ * }),
84
+ *
85
+ * echo: a
86
+ * .query()
87
+ * .arguments({ content: a.string() })
88
+ * .returns(a.ref('EchoResponse'))
89
+ * .authorization(allow => [allow.publicApiKey()])
90
+ * // 3. set the function has the handler
91
+ * .handler(a.handler.function(echoHandler))
92
+ * });
93
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/}
94
+ * @param fn A function created via `defineFunction`. Alternatively, you can pass in a "string" of the function name and pass
95
+ * in a corresponding value into the `functionMap` property of defineData.
96
+ * @returns A handler for the query / mutation / subscription
97
+ */
36
98
  function fcn(fn) {
37
99
  return { [dataSymbol]: fn, ...buildHandler(functionHandlerBrand) };
38
100
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Handler.mjs","sources":["../../src/Handler.ts"],"sourcesContent":["import { brand } from './util';\nconst dataSymbol = Symbol('Data');\nfunction buildHandler(brandName) {\n return brand(brandName);\n}\nexport function getHandlerData(handler) {\n return handler[dataSymbol];\n}\n//#region handler.inlineSql\nconst inlineSqlBrand = 'inlineSql';\nfunction inlineSql(sql) {\n return { [dataSymbol]: sql, ...buildHandler(inlineSqlBrand) };\n}\n//#endregion\n//#region handler.sqlReference\nconst sqlReferenceBrand = 'sqlReference';\nfunction sqlReference(sqlFilePath) {\n // used to determine caller directory in order to resolve relative path downstream\n const stack = new Error().stack;\n return {\n [dataSymbol]: { stack, entry: sqlFilePath },\n ...buildHandler(sqlReferenceBrand),\n };\n}\nconst customHandlerBrand = 'customHandler';\nfunction custom(customHandler) {\n // used to determine caller directory in order to resolve relative path downstream\n const stack = new Error().stack;\n return {\n [dataSymbol]: { ...customHandler, stack },\n ...buildHandler(customHandlerBrand),\n };\n}\nconst functionHandlerBrand = 'functionHandler';\nfunction fcn(fn) {\n return { [dataSymbol]: fn, ...buildHandler(functionHandlerBrand) };\n}\n//#endregion\nexport const handler = {\n inlineSql,\n sqlReference,\n custom,\n function: fcn,\n};\n"],"names":[],"mappings":";;AACA,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAClC,SAAS,YAAY,CAAC,SAAS,EAAE;AACjC,IAAI,OAAO,KAAK,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AACM,SAAS,cAAc,CAAC,OAAO,EAAE;AACxC,IAAI,OAAO,OAAO,CAAC,UAAU,CAAC,CAAC;AAC/B,CAAC;AACD;AACA,MAAM,cAAc,GAAG,WAAW,CAAC;AACnC,SAAS,SAAS,CAAC,GAAG,EAAE;AACxB,IAAI,OAAO,EAAE,CAAC,UAAU,GAAG,GAAG,EAAE,GAAG,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;AAClE,CAAC;AACD;AACA;AACA,MAAM,iBAAiB,GAAG,cAAc,CAAC;AACzC,SAAS,YAAY,CAAC,WAAW,EAAE;AACnC;AACA,IAAI,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC,KAAK,CAAC;AACpC,IAAI,OAAO;AACX,QAAQ,CAAC,UAAU,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE;AACnD,QAAQ,GAAG,YAAY,CAAC,iBAAiB,CAAC;AAC1C,KAAK,CAAC;AACN,CAAC;AACD,MAAM,kBAAkB,GAAG,eAAe,CAAC;AAC3C,SAAS,MAAM,CAAC,aAAa,EAAE;AAC/B;AACA,IAAI,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC,KAAK,CAAC;AACpC,IAAI,OAAO;AACX,QAAQ,CAAC,UAAU,GAAG,EAAE,GAAG,aAAa,EAAE,KAAK,EAAE;AACjD,QAAQ,GAAG,YAAY,CAAC,kBAAkB,CAAC;AAC3C,KAAK,CAAC;AACN,CAAC;AACD,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;AAC/C,SAAS,GAAG,CAAC,EAAE,EAAE;AACjB,IAAI,OAAO,EAAE,CAAC,UAAU,GAAG,EAAE,EAAE,GAAG,YAAY,CAAC,oBAAoB,CAAC,EAAE,CAAC;AACvE,CAAC;AACD;AACY,MAAC,OAAO,GAAG;AACvB,IAAI,SAAS;AACb,IAAI,YAAY;AAChB,IAAI,MAAM;AACV,IAAI,QAAQ,EAAE,GAAG;AACjB;;;;"}
1
+ {"version":3,"file":"Handler.mjs","sources":["../../src/Handler.ts"],"sourcesContent":["import { brand } from './util';\nconst dataSymbol = Symbol('Data');\nfunction buildHandler(brandName) {\n return brand(brandName);\n}\nexport function getHandlerData(handler) {\n return handler[dataSymbol];\n}\n//#region handler.inlineSql\nconst inlineSqlBrand = 'inlineSql';\nfunction inlineSql(sql) {\n return { [dataSymbol]: sql, ...buildHandler(inlineSqlBrand) };\n}\n//#endregion\n//#region handler.sqlReference\nconst sqlReferenceBrand = 'sqlReference';\nfunction sqlReference(sqlFilePath) {\n // used to determine caller directory in order to resolve relative path downstream\n const stack = new Error().stack;\n return {\n [dataSymbol]: { stack, entry: sqlFilePath },\n ...buildHandler(sqlReferenceBrand),\n };\n}\nconst customHandlerBrand = 'customHandler';\n/**\n * Use a custom JavaScript resolver to handle a query, mutation, or subscription.\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/#step-2---configure-custom-business-logic-handler-code}\n * @param customHandler `{ entry: \"path-to-javascript-resolver-file.js\", dataSource: \"Data Source name added via \"backend.data.add*DataSoruce(...)\"}`\n * @returns A JavaScript resolver attached to the query, mutation, or subscription.\n * @example\n * const schema = a.schema({\n * Post: a.model({\n * content: a.string(),\n * likes: a.integer()\n * .authorization(allow => [allow.authenticated().to(['read'])])\n * }).authorization(allow => [\n * allow.owner(),\n * allow.authenticated().to(['read'])\n * ]),\n *\n * likePost: a\n * .mutation()\n * .arguments({ postId: a.id() })\n * .returns(a.ref('Post'))\n * .authorization(allow => [allow.authenticated()])\n * .handler(a.handler.custom({\n * dataSource: a.ref('Post'),\n * entry: './increment-like.js'\n * }))\n * });\n */\nfunction custom(customHandler) {\n // used to determine caller directory in order to resolve relative path downstream\n const stack = new Error().stack;\n return {\n [dataSymbol]: { ...customHandler, stack },\n ...buildHandler(customHandlerBrand),\n };\n}\nconst functionHandlerBrand = 'functionHandler';\n/**\n * Use a function created via `defineFunction` to handle the custom query/mutation/subscription. In your function handler,\n * you can use the `Schema[\"YOUR_QUERY_OR_MUTATION_NAME\"][\"functionHandler\"]` utility type to type the handler function.\n * @example\n * import {\n * type ClientSchema,\n * a,\n * defineData,\n * defineFunction // 1.Import \"defineFunction\" to create new functions\n * } from '@aws-amplify/backend';\n *\n * // 2. define a function\n * const echoHandler = defineFunction({\n * entry: './echo-handler/handler.ts'\n * })\n *\n * const schema = a.schema({\n * EchoResponse: a.customType({\n * content: a.string(),\n * executionDuration: a.float()\n * }),\n *\n * echo: a\n * .query()\n * .arguments({ content: a.string() })\n * .returns(a.ref('EchoResponse'))\n * .authorization(allow => [allow.publicApiKey()])\n * // 3. set the function has the handler\n * .handler(a.handler.function(echoHandler))\n * });\n * @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/}\n * @param fn A function created via `defineFunction`. Alternatively, you can pass in a \"string\" of the function name and pass\n * in a corresponding value into the `functionMap` property of defineData.\n * @returns A handler for the query / mutation / subscription\n */\nfunction fcn(fn) {\n return { [dataSymbol]: fn, ...buildHandler(functionHandlerBrand) };\n}\n//#endregion\nexport const handler = {\n inlineSql,\n sqlReference,\n custom,\n function: fcn,\n};\n"],"names":[],"mappings":";;AACA,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAClC,SAAS,YAAY,CAAC,SAAS,EAAE;AACjC,IAAI,OAAO,KAAK,CAAC,SAAS,CAAC,CAAC;AAC5B,CAAC;AACM,SAAS,cAAc,CAAC,OAAO,EAAE;AACxC,IAAI,OAAO,OAAO,CAAC,UAAU,CAAC,CAAC;AAC/B,CAAC;AACD;AACA,MAAM,cAAc,GAAG,WAAW,CAAC;AACnC,SAAS,SAAS,CAAC,GAAG,EAAE;AACxB,IAAI,OAAO,EAAE,CAAC,UAAU,GAAG,GAAG,EAAE,GAAG,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;AAClE,CAAC;AACD;AACA;AACA,MAAM,iBAAiB,GAAG,cAAc,CAAC;AACzC,SAAS,YAAY,CAAC,WAAW,EAAE;AACnC;AACA,IAAI,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC,KAAK,CAAC;AACpC,IAAI,OAAO;AACX,QAAQ,CAAC,UAAU,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,WAAW,EAAE;AACnD,QAAQ,GAAG,YAAY,CAAC,iBAAiB,CAAC;AAC1C,KAAK,CAAC;AACN,CAAC;AACD,MAAM,kBAAkB,GAAG,eAAe,CAAC;AAC3C;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,SAAS,MAAM,CAAC,aAAa,EAAE;AAC/B;AACA,IAAI,MAAM,KAAK,GAAG,IAAI,KAAK,EAAE,CAAC,KAAK,CAAC;AACpC,IAAI,OAAO;AACX,QAAQ,CAAC,UAAU,GAAG,EAAE,GAAG,aAAa,EAAE,KAAK,EAAE;AACjD,QAAQ,GAAG,YAAY,CAAC,kBAAkB,CAAC;AAC3C,KAAK,CAAC;AACN,CAAC;AACD,MAAM,oBAAoB,GAAG,iBAAiB,CAAC;AAC/C;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,SAAS,GAAG,CAAC,EAAE,EAAE;AACjB,IAAI,OAAO,EAAE,CAAC,UAAU,GAAG,EAAE,EAAE,GAAG,YAAY,CAAC,oBAAoB,CAAC,EAAE,CAAC;AACvE,CAAC;AACD;AACY,MAAC,OAAO,GAAG;AACvB,IAAI,SAAS;AACb,IAAI,YAAY;AAChB,IAAI,MAAM;AACV,IAAI,QAAQ,EAAE,GAAG;AACjB;;;;"}
@@ -1,7 +1,7 @@
1
1
  import type { GenericModelSchema } from '../ModelSchema';
2
2
  import type { NonModelTypesShape } from './ExtractNonModelTypes';
3
3
  import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
4
- import type { ModelField } from '../ModelField';
4
+ import type { BaseModelField } from '../ModelField';
5
5
  import type { RefType, RefTypeParamShape } from '../RefType';
6
6
  import type { ResolveFieldRequirements, ResolveRefsOfCustomType, ResolveRefValueArrayTraits } from './ResolveFieldProperties';
7
7
  import type { AppSyncResolverHandler } from 'aws-lambda';
@@ -30,14 +30,14 @@ export type CustomOpShapes<Schema extends GenericModelSchema<any>> = {
30
30
  * Digs out custom operation arguments, mapped to the intended graphql types.
31
31
  */
32
32
  export type CustomOpArguments<Shape extends CustomOperationParamShape> = Shape['arguments'] extends null ? never : ResolveFieldRequirements<{
33
- [FieldName in keyof Shape['arguments']]: Shape['arguments'][FieldName] extends ModelField<infer R, any, any> ? R : never;
33
+ [FieldName in keyof Shape['arguments']]: Shape['arguments'][FieldName] extends BaseModelField<infer R> ? R : never;
34
34
  }>;
35
35
  /**
36
36
  * Computes the return type from the `returnType` of a custom operation shape.
37
37
  *
38
38
  * This entails dereferencing refs and inferring graphql types from field-type defs.
39
39
  */
40
- export type CustomOpReturnType<Shape extends CustomOperationParamShape, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape, CustomOperations extends Record<string, CustomOperationParamShape>> = Shape['returnType'] extends RefType<infer RefShape, any, any> ? RefShape['link'] extends keyof CustomOperations ? CustomOpReturnType<CustomOperations[RefShape['link']], FullyResolvedSchema, NonModelTypes, CustomOperations> : ResolveRef<RefShape, FullyResolvedSchema, NonModelTypes, CustomOperations> : Shape['returnType'] extends ModelField<infer R, any, any> ? R : Shape['returnType'] extends CustomType<infer R> ? ResolveFieldRequirements<FieldTypesOfCustomType<{
40
+ export type CustomOpReturnType<Shape extends CustomOperationParamShape, FullyResolvedSchema extends Record<string, unknown>, NonModelTypes extends NonModelTypesShape, CustomOperations extends Record<string, CustomOperationParamShape>> = Shape['returnType'] extends RefType<infer RefShape, any, any> ? RefShape['link'] extends keyof CustomOperations ? CustomOpReturnType<CustomOperations[RefShape['link']], FullyResolvedSchema, NonModelTypes, CustomOperations> : ResolveRef<RefShape, FullyResolvedSchema, NonModelTypes, CustomOperations> : Shape['returnType'] extends BaseModelField<infer R> ? R : Shape['returnType'] extends CustomType<infer R> ? ResolveFieldRequirements<FieldTypesOfCustomType<{
41
41
  thisCustomType: R['fields'];
42
42
  }>['thisCustomType']> | null : never;
43
43
  /**
@@ -1,10 +1,10 @@
1
1
  import type { UnionToIntersection } from '@aws-amplify/data-schema-types';
2
2
  import type { CustomType, CustomTypeParamShape } from '../CustomType';
3
- import type { EnumType, EnumTypeParamShape } from '../EnumType';
3
+ import type { EnumType } from '../EnumType';
4
4
  import type { SchemaTypes, ModelAndCustomTypes, FieldTypesOfCustomType } from './ResolveSchema';
5
5
  import type { ModelType, ModelTypeParamShape } from '../ModelType';
6
6
  export type NonModelTypesShape = {
7
- enums: Record<string, EnumType<any>>;
7
+ enums: Record<string, any>;
8
8
  customTypes: Record<string, any>;
9
9
  };
10
10
  export type ExtractNonModelTypes<Schema> = ResolveNonModelFields<ResolveNonModelTypes<Schema, ExtractImplicitNonModelTypes<Schema>>>;
@@ -40,7 +40,7 @@ export type ExtractNonModelTypes<Schema> = ResolveNonModelFields<ResolveNonModel
40
40
  * ```
41
41
  */
42
42
  export type ExtractAndFlattenImplicitNonModelTypesFromFields<ParentTypeName extends string, Fields> = {
43
- [FieldProp in keyof Fields as Fields[FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? FieldProp : never]: (x: NonNullable<Fields[FieldProp]> extends infer FieldType ? FieldType extends EnumType<EnumTypeParamShape> ? {
43
+ [FieldProp in keyof Fields as Fields[FieldProp] extends EnumType | CustomType<CustomTypeParamShape> ? FieldProp : never]: (x: NonNullable<Fields[FieldProp]> extends infer FieldType ? FieldType extends EnumType ? {
44
44
  [Key in `${ParentTypeName}${Capitalize<FieldProp & string>}`]: Fields[FieldProp];
45
45
  } : FieldType extends CustomType<infer CustomTypeShape extends CustomTypeParamShape> ? // recursively extract to the Nested CustomType, and return the
46
46
  ExtractAndFlattenImplicitNonModelTypesFromFields<`${ParentTypeName}${Capitalize<FieldProp & string>}`, CustomTypeShape['fields']> & {
@@ -60,7 +60,7 @@ export type ExtractImplicitNonModelTypes<Schema, Targets = ModelAndCustomTypes<S
60
60
  }[keyof Targets]>;
61
61
  type ResolveNonModelTypes<Schema, Extracted, ResolvedSchema = SchemaTypes<Schema> & Extracted> = {
62
62
  enums: {
63
- [Model in keyof ResolvedSchema as ResolvedSchema[Model] extends EnumType<EnumTypeParamShape> ? Model : never]: ResolvedSchema[Model] extends EnumType<infer R extends EnumTypeParamShape> ? R['values'][number] : never;
63
+ [Model in keyof ResolvedSchema as ResolvedSchema[Model] extends EnumType ? Model : never]: ResolvedSchema[Model] extends EnumType<infer values> ? values[number] : never;
64
64
  };
65
65
  customTypes: {
66
66
  [Model in keyof ResolvedSchema as ResolvedSchema[Model] extends CustomType<CustomTypeParamShape> ? Model : never]: ResolvedSchema[Model] extends CustomType<infer R extends CustomTypeParamShape> ? R['fields'] : never;
@@ -11,7 +11,7 @@ import type { ModelIdentifier } from './ModelMetadata';
11
11
  import type { RefType, RefTypeParamShape } from '../RefType';
12
12
  import type { NonModelTypesShape } from './ExtractNonModelTypes';
13
13
  import type { CustomType, CustomTypeParamShape } from '../CustomType';
14
- import type { EnumType, EnumTypeParamShape } from '../EnumType';
14
+ import type { EnumType } from '../EnumType';
15
15
  import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
16
16
  export type ResolveFieldProperties<Schema extends GenericModelSchema<any>, NonModelTypes extends NonModelTypesShape, ResolvedSchema = ResolveSchema<Schema>, IdentifierMeta extends Record<string, {
17
17
  identifier: PrimaryIndexIrShape;
@@ -57,7 +57,7 @@ type Intersection<A = Record<never, never>, B = Record<never, never>, C = Record
57
57
  [P in keyof U]: U[P];
58
58
  } : never;
59
59
  export type ModelImpliedAuthFields<Schema extends GenericModelSchema<any>> = {
60
- [ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType<EnumTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomType<CustomTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomOperation<CustomOperationParamShape, any> ? never : ModelKey]: Schema['data']['types'][ModelKey] extends ModelType<infer Model, any> ? AllAuthFieldsForModel<Schema, Model> : object;
60
+ [ModelKey in keyof Schema['data']['types'] as Schema['data']['types'][ModelKey] extends EnumType ? never : Schema['data']['types'][ModelKey] extends CustomType<CustomTypeParamShape> ? never : Schema['data']['types'][ModelKey] extends CustomOperation<CustomOperationParamShape, any> ? never : ModelKey]: Schema['data']['types'][ModelKey] extends ModelType<infer Model, any> ? AllAuthFieldsForModel<Schema, Model> : object;
61
61
  };
62
62
  type AllAuthFieldsForModel<Schema extends GenericModelSchema<any>, Model extends Schema['data']['types'][keyof Schema['data']['types']]> = (Model['authorization'][number] extends never ? Schema['data']['authorization'][number] extends never ? object : ImpliedAuthFields<Schema['data']['authorization'][number]> : ImpliedAuthFields<Model['authorization'][number]>) & ImpliedAuthFieldsFromFields<Model>;
63
63
  type ImpliedAuthFieldsFromFields<T> = UnionToIntersection<T extends ModelTypeParamShape ? T['fields'][keyof T['fields']] extends ModelField<any, any, infer Auth> | ModelRelationalField<any, any, any, infer Auth> | RefType<any, any, infer Auth> ? Auth extends Authorization<any, any, any> ? ImpliedAuthFields<Auth> : object : object : object>;
@@ -1,9 +1,9 @@
1
1
  import type { ModelType } from '../ModelType';
2
2
  import type { GenericModelSchema } from '../ModelSchema';
3
3
  import type { ModelRelationalField, ModelRelationshipTypes, RelationTypeFunctionOmitMapping } from '../ModelRelationalField';
4
- import type { ModelField } from '../ModelField';
4
+ import type { BaseModelField } from '../ModelField';
5
5
  import type { CustomType, CustomTypeParamShape } from '../CustomType';
6
- import type { EnumType, EnumTypeParamShape } from '../EnumType';
6
+ import type { EnumType } from '../EnumType';
7
7
  import type { RefType, RefTypeParamShape } from '../RefType';
8
8
  import type { CustomOperation, CustomOperationParamShape } from '../CustomOperation';
9
9
  export type ResolveSchema<Schema> = FieldTypes<ModelTypes<SchemaTypes<Schema>>>;
@@ -16,14 +16,14 @@ export type SchemaTypes<T> = T extends GenericModelSchema<any> ? T['data']['type
16
16
  * added to ModelMeta in ClientSchema.ts
17
17
  */
18
18
  export type ModelTypes<Schema> = {
19
- [Model in keyof Schema as Schema[Model] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<infer R, any> ? R['fields'] : never;
19
+ [Model in keyof Schema as Schema[Model] extends EnumType | CustomType<CustomTypeParamShape> | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<infer R, any> ? R['fields'] : never;
20
20
  };
21
21
  /**
22
22
  * Gets the collection of all ModelTypes and CustomTypes which are explicitly
23
23
  * defined in the schema.
24
24
  */
25
25
  export type ModelAndCustomTypes<Schema> = {
26
- [Model in keyof Schema as Schema[Model] extends EnumType<EnumTypeParamShape> | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<any, any> ? Schema[Model] : Schema[Model] extends CustomType<CustomTypeParamShape> ? Schema[Model] : never;
26
+ [Model in keyof Schema as Schema[Model] extends EnumType | CustomOperation<CustomOperationParamShape, any> ? never : Model]: Schema[Model] extends ModelType<any, any> | CustomType<CustomTypeParamShape> ? Schema[Model] : never;
27
27
  };
28
28
  /**
29
29
  * Resolves field types
@@ -32,7 +32,7 @@ export type ModelAndCustomTypes<Schema> = {
32
32
  */
33
33
  export type FieldTypes<T> = {
34
34
  [ModelProp in keyof T]: {
35
- [FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends ModelField<infer R, any, any> ? R : T[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['valueRequired'] extends true ? T[ModelProp][FieldProp] : T[ModelProp][FieldProp] | null : T[ModelProp][FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? RefType<{
35
+ [FieldProp in keyof T[ModelProp]]: T[ModelProp][FieldProp] extends BaseModelField<infer R> ? R : T[ModelProp][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['valueRequired'] extends true ? T[ModelProp][FieldProp] : T[ModelProp][FieldProp] | null : T[ModelProp][FieldProp] extends EnumType | CustomType<CustomTypeParamShape> ? RefType<{
36
36
  link: `${Capitalize<ModelProp & string>}${Capitalize<FieldProp & string>}`;
37
37
  type: 'ref';
38
38
  valueRequired: false;
@@ -52,7 +52,7 @@ export type FieldTypes<T> = {
52
52
  */
53
53
  export type FieldTypesOfCustomType<T> = {
54
54
  [CustomTypeName in keyof T]: {
55
- [FieldProp in keyof T[CustomTypeName]]: T[CustomTypeName][FieldProp] extends ModelField<infer R, any, any> ? R : T[CustomTypeName][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['valueRequired'] extends true ? T[CustomTypeName][FieldProp] : T[CustomTypeName][FieldProp] | null : T[CustomTypeName][FieldProp] extends EnumType<EnumTypeParamShape> | CustomType<CustomTypeParamShape> ? RefType<{
55
+ [FieldProp in keyof T[CustomTypeName]]: T[CustomTypeName][FieldProp] extends BaseModelField<infer R> ? R : T[CustomTypeName][FieldProp] extends RefType<infer R extends RefTypeParamShape, any, any> ? R['valueRequired'] extends true ? T[CustomTypeName][FieldProp] : T[CustomTypeName][FieldProp] | null : T[CustomTypeName][FieldProp] extends EnumType | CustomType<CustomTypeParamShape> ? RefType<{
56
56
  link: `${Capitalize<CustomTypeName & string>}${Capitalize<FieldProp & string>}`;
57
57
  type: 'ref';
58
58
  valueRequired: false;
@@ -1,5 +1,6 @@
1
- import { Brand } from './util';
2
1
  import { AllowModifier, Authorization } from './Authorization';
2
+ import type { methodKeyOf, satisfy } from './util/usedMethods.js';
3
+ import type { brandSymbol } from './util/Brand.js';
3
4
  /**
4
5
  * Used to "attach" auth types to ModelField without exposing them on the builder.
5
6
  */
@@ -45,42 +46,44 @@ export type ModelFieldTypeParamOuter = ModelFieldTypeParamInner | Array<ModelFie
45
46
  export type Nullable<T> = T | null;
46
47
  export type Required<T> = Exclude<T, null>;
47
48
  export type ArrayField<T> = [T] extends [ModelFieldTypeParamInner] ? Array<T> | null : never;
49
+ export type BaseModelField<T extends ModelFieldTypeParamOuter = ModelFieldTypeParamOuter> = ModelField<T, UsableModelFieldKey, any>;
50
+ export type UsableModelFieldKey = satisfy<methodKeyOf<ModelField>, 'required' | 'default' | 'authorization'>;
48
51
  /**
49
52
  * Public API for the chainable builder methods exposed by Model Field.
50
53
  * The type is narrowing e.g., after calling .array() it will be omitted from intellisense suggestions
51
54
  *
52
55
  * @typeParam T - holds the JS data type of the field
53
- * @typeParam K - union of strings representing already-invoked method names. Used to improve Intellisense
56
+ * @typeParam UsedMethod - union of strings representing already-invoked method names. Used to improve Intellisense
54
57
  */
55
- export type ModelField<T extends ModelFieldTypeParamOuter, K extends keyof ModelField<T> = never, Auth = undefined> = Omit<{
58
+ export type ModelField<T extends ModelFieldTypeParamOuter = ModelFieldTypeParamOuter, UsedMethod extends UsableModelFieldKey = never, Auth = undefined> = Omit<{
59
+ [__auth]?: Auth;
60
+ [brandSymbol]: typeof brandName;
56
61
  /**
57
62
  * Marks a field as required.
58
63
  */
59
- required(): ModelField<Required<T>, K | 'required'>;
64
+ required(): ModelField<Required<T>, UsedMethod | 'required'>;
60
65
  /**
61
66
  * Converts a field type definition to an array of the field type.
62
67
  */
63
- array(): ModelField<ArrayField<T>, Exclude<K, 'required'> | 'array'>;
68
+ array(): ModelField<ArrayField<T>, Exclude<UsedMethod, 'required'>>;
64
69
  /**
65
70
  * Sets a default value for the scalar type.
66
71
  * @param value the default value
67
72
  */
68
- default(value: ModelFieldTypeParamOuter): ModelField<T, K | 'default'>;
73
+ default(value: ModelFieldTypeParamOuter): ModelField<T, UsedMethod | 'default'>;
69
74
  /**
70
- * Configures field-level authorization rules. Pass in an array of authorizations `(a.allow.____)` to mix and match
75
+ * Configures field-level authorization rules. Pass in an array of authorizations `(allow => allow.____)` to mix and match
71
76
  * multiple authorization rules for this field.
72
77
  */
73
- authorization<AuthRuleType extends Authorization<any, any, any>>(callback: (allow: Omit<AllowModifier, 'resource'>) => AuthRuleType | AuthRuleType[]): ModelField<T, K | 'authorization', AuthRuleType>;
74
- }, K> & {
75
- [__auth]?: Auth;
76
- } & Brand<typeof brandName>;
78
+ authorization<AuthRuleType extends Authorization<any, any, any>>(callback: (allow: Omit<AllowModifier, 'resource'>) => AuthRuleType | AuthRuleType[]): ModelField<T, UsedMethod | 'authorization', AuthRuleType>;
79
+ }, UsedMethod>;
77
80
  /**
78
81
  * Internal representation of Model Field that exposes the `data` property.
79
82
  * Used at buildtime.
80
83
  */
81
- export type InternalField = ModelField<ModelFieldTypeParamOuter, never> & {
84
+ export interface InternalField extends ModelField {
82
85
  data: FieldData;
83
- };
86
+ }
84
87
  /**
85
88
  * A unique identifier scalar type. This scalar is serialized like a String but isn't meant to be human-readable.
86
89
  * If not specified on create operations, a ULID will be auto-generated service-side.
@@ -41,7 +41,7 @@ type ModelRelationalFieldFunctions<T extends ModelRelationalFieldParamShape, RM
41
41
  */
42
42
  required(): ModelRelationalField<SetTypeSubArg<T, 'arrayRequired', true>, K | 'required'>;
43
43
  /**
44
- * Configures field-level authorization rules. Pass in an array of authorizations `(a.allow.____)` to mix and match
44
+ * Configures field-level authorization rules. Pass in an array of authorizations `(allow => allow.____)` to mix and match
45
45
  * multiple authorization rules for this field.
46
46
  */
47
47
  authorization<AuthRuleType extends Authorization<any, any, any>>(callback: (allow: AllowModifier) => AuthRuleType | AuthRuleType[]): ModelRelationalField<T, K | 'authorization', K, AuthRuleType>;
@@ -67,24 +67,103 @@ export type ModelRelationalTypeArgFactory<RM extends string, RT extends Relation
67
67
  references: string[];
68
68
  };
69
69
  /**
70
- * Create a one-directional one-to-one relationship between two models using the `hasOne("MODEL_NAME")` method.
70
+ * Create one-to-one relationship between two models using the `hasOne("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
71
71
  * A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field
72
72
  * unless overwritten with the `identifier()` method.
73
+ * @example
74
+ * const schema = a.schema({
75
+ * Cart: a.model({
76
+ * items: a.string().required().array(),
77
+ * // 1. Create reference field
78
+ * customerId: a.id(),
79
+ * // 2. Create relationship field with the reference field
80
+ * customer: a.belongsTo('Customer', 'customerId'),
81
+ * }),
82
+ * Customer: a.model({
83
+ * name: a.string(),
84
+ * // 3. Create relationship field with the reference field
85
+ * // from the Cart model
86
+ * activeCart: a.hasOne('Cart', 'customerId')
87
+ * }),
88
+ * });
89
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-a-one-to-one-relationship}
73
90
  * @param relatedModel the name of the related model
91
+ * @param references the field(s) that should be used to reference the related model
74
92
  * @returns a one-to-one relationship definition
75
93
  */
76
94
  export declare function hasOne<RM extends string>(relatedModel: RM, references: string | string[]): ModelRelationalField<ModelRelationalTypeArgFactory<RM, ModelRelationshipTypes.hasOne, false>, RM, "valueRequired", undefined>;
77
95
  /**
78
- * Create a one-directional one-to-many relationship between two models using the `hasMany()` method.
96
+ * Create a one-directional one-to-many relationship between two models using the `hasMany("MODEL_NAME", "REFERENCE_FIELD(s)")` method.
97
+ * @example
98
+ * const schema = a.schema({
99
+ * Member: a.model({
100
+ * name: a.string().required(),
101
+ * // 1. Create a reference field
102
+ * teamId: a.id(),
103
+ * // 2. Create a belongsTo relationship with the reference field
104
+ * team: a.belongsTo('Team', 'teamId'),
105
+ * })
106
+ * .authorization(allow => [allow.publicApiKey()]),
107
+ *
108
+ * Team: a.model({
109
+ * mantra: a.string().required(),
110
+ * // 3. Create a hasMany relationship with the reference field
111
+ * // from the `Member`s model.
112
+ * members: a.hasMany('Member', 'teamId'),
113
+ * })
114
+ * .authorization(allow => [allow.publicApiKey()]),
115
+ * });
116
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/#model-one-to-many-relationships}
79
117
  * @param relatedModel the name of the related model
118
+ * @param references the field(s) that should be used to reference the related model
80
119
  * @returns a one-to-many relationship definition
81
120
  */
82
121
  export declare function hasMany<RM extends string>(relatedModel: RM, references: string | string[]): ModelRelationalField<ModelRelationalTypeArgFactory<RM, ModelRelationshipTypes.hasMany, true>, RM, "required", undefined>;
83
122
  /**
84
- * Make a `hasOne()` or `hasMany()` relationship bi-directional using the `belongsTo()` method.
123
+ * Use `belongsTo()` to create a field to query the related `hasOne()` or `hasMany()` relationship.
85
124
  * The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from
86
125
  * parent to the related model.
126
+ *
127
+ * @example
128
+ * // one-to-many relationship
129
+ * const schema = a.schema({
130
+ * Member: a.model({
131
+ * name: a.string().required(),
132
+ * // 1. Create a reference field
133
+ * teamId: a.id(),
134
+ * // 2. Create a belongsTo relationship with the reference field
135
+ * team: a.belongsTo('Team', 'teamId'),
136
+ * })
137
+ * .authorization(allow => [allow.publicApiKey()]),
138
+ *
139
+ * Team: a.model({
140
+ * mantra: a.string().required(),
141
+ * // 3. Create a hasMany relationship with the reference field
142
+ * // from the `Member`s model.
143
+ * members: a.hasMany('Member', 'teamId'),
144
+ * })
145
+ * .authorization(allow => [allow.publicApiKey()]),
146
+ * });
147
+ * @example
148
+ * // one-to-one relationship
149
+ * const schema = a.schema({
150
+ * Cart: a.model({
151
+ * items: a.string().required().array(),
152
+ * // 1. Create reference field
153
+ * customerId: a.id(),
154
+ * // 2. Create relationship field with the reference field
155
+ * customer: a.belongsTo('Customer', 'customerId'),
156
+ * }),
157
+ * Customer: a.model({
158
+ * name: a.string(),
159
+ * // 3. Create relationship field with the reference field
160
+ * // from the Cart model
161
+ * activeCart: a.hasOne('Cart', 'customerId')
162
+ * }),
163
+ * });
164
+ * @see {@link https://docs.amplify.aws/react/build-a-backend/data/data-modeling/relationships/}
87
165
  * @param relatedModel name of the related `.hasOne()` or `.hasMany()` model
166
+ * @param references the field(s) that should be used to reference the related model
88
167
  * @returns a belong-to relationship definition
89
168
  */
90
169
  export declare function belongsTo<RM extends string>(relatedModel: RM, references: string | string[]): ModelRelationalField<ModelRelationalTypeArgFactory<RM, ModelRelationshipTypes.belongsTo, false>, RM, "required" | "valueRequired", undefined>;