@aws-amplify/data-schema 0.11.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 (42) hide show
  1. package/LICENSE +175 -0
  2. package/NOTICE +1 -0
  3. package/lib-esm/index.d.ts +4 -0
  4. package/lib-esm/index.js +28 -0
  5. package/lib-esm/src/Authorization.d.ts +262 -0
  6. package/lib-esm/src/Authorization.js +309 -0
  7. package/lib-esm/src/ClientSchema.d.ts +27 -0
  8. package/lib-esm/src/ClientSchema.js +3 -0
  9. package/lib-esm/src/CustomType.d.ts +33 -0
  10. package/lib-esm/src/CustomType.js +14 -0
  11. package/lib-esm/src/EnumType.d.ts +15 -0
  12. package/lib-esm/src/EnumType.js +17 -0
  13. package/lib-esm/src/MappedTypes/ExtractNonModelTypes.d.ts +32 -0
  14. package/lib-esm/src/MappedTypes/ExtractNonModelTypes.js +2 -0
  15. package/lib-esm/src/MappedTypes/ForeignKeys.d.ts +83 -0
  16. package/lib-esm/src/MappedTypes/ForeignKeys.js +2 -0
  17. package/lib-esm/src/MappedTypes/ImplicitFieldInjector.d.ts +37 -0
  18. package/lib-esm/src/MappedTypes/ImplicitFieldInjector.js +2 -0
  19. package/lib-esm/src/MappedTypes/ModelMetadata.d.ts +20 -0
  20. package/lib-esm/src/MappedTypes/ModelMetadata.js +2 -0
  21. package/lib-esm/src/MappedTypes/ResolveFieldProperties.d.ts +58 -0
  22. package/lib-esm/src/MappedTypes/ResolveFieldProperties.js +2 -0
  23. package/lib-esm/src/MappedTypes/ResolveSchema.d.ts +35 -0
  24. package/lib-esm/src/MappedTypes/ResolveSchema.js +2 -0
  25. package/lib-esm/src/ModelField.d.ts +158 -0
  26. package/lib-esm/src/ModelField.js +206 -0
  27. package/lib-esm/src/ModelRelationalField.d.ts +106 -0
  28. package/lib-esm/src/ModelRelationalField.js +113 -0
  29. package/lib-esm/src/ModelSchema.d.ts +39 -0
  30. package/lib-esm/src/ModelSchema.js +34 -0
  31. package/lib-esm/src/ModelType.d.ts +107 -0
  32. package/lib-esm/src/ModelType.js +32 -0
  33. package/lib-esm/src/RefType.d.ts +45 -0
  34. package/lib-esm/src/RefType.js +29 -0
  35. package/lib-esm/src/SchemaProcessor.d.ts +10 -0
  36. package/lib-esm/src/SchemaProcessor.js +408 -0
  37. package/lib-esm/src/index.d.ts +9 -0
  38. package/lib-esm/src/index.js +35 -0
  39. package/lib-esm/src/types.d.ts +18 -0
  40. package/lib-esm/src/types.js +8 -0
  41. package/lib-esm/tsconfig.tsbuildinfo +1 -0
  42. package/package.json +39 -0
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.manyToMany = exports.belongsTo = exports.hasMany = exports.hasOne = exports.ModelRelationshipTypes = exports.__auth = void 0;
4
+ /**
5
+ * Used to "attach" auth types to ModelField without exposing them on the builder.
6
+ */
7
+ exports.__auth = Symbol('__auth');
8
+ var ModelRelationshipTypes;
9
+ (function (ModelRelationshipTypes) {
10
+ ModelRelationshipTypes["hasOne"] = "hasOne";
11
+ ModelRelationshipTypes["hasMany"] = "hasMany";
12
+ ModelRelationshipTypes["belongsTo"] = "belongsTo";
13
+ ModelRelationshipTypes["manyToMany"] = "manyToMany";
14
+ })(ModelRelationshipTypes || (exports.ModelRelationshipTypes = ModelRelationshipTypes = {}));
15
+ const arrayTypeRelationships = ['hasMany', 'manyToMany'];
16
+ const relationalModifiers = [
17
+ 'required',
18
+ 'arrayRequired',
19
+ 'valueRequired',
20
+ 'authorization',
21
+ ];
22
+ const relationModifierMap = {
23
+ belongsTo: ['authorization'],
24
+ hasMany: ['arrayRequired', 'valueRequired', 'authorization'],
25
+ hasOne: ['required', 'authorization'],
26
+ manyToMany: ['arrayRequired', 'valueRequired', 'authorization'],
27
+ };
28
+ function _modelRelationalField(type, relatedModel, relationName) {
29
+ const data = {
30
+ relatedModel,
31
+ type,
32
+ fieldType: 'model',
33
+ array: false,
34
+ valueRequired: false,
35
+ arrayRequired: false,
36
+ relationName,
37
+ authorization: [],
38
+ };
39
+ if (arrayTypeRelationships.includes(type)) {
40
+ data.array = true;
41
+ }
42
+ const relationshipBuilderFunctions = {
43
+ required() {
44
+ data.arrayRequired = true;
45
+ return this;
46
+ },
47
+ arrayRequired() {
48
+ data.arrayRequired = true;
49
+ return this;
50
+ },
51
+ valueRequired() {
52
+ data.valueRequired = true;
53
+ return this;
54
+ },
55
+ authorization(rules) {
56
+ data.authorization = rules;
57
+ return this;
58
+ },
59
+ };
60
+ const builder = Object.fromEntries(relationModifierMap[type].map((key) => [
61
+ key,
62
+ relationshipBuilderFunctions[key],
63
+ ]));
64
+ return {
65
+ ...builder,
66
+ data,
67
+ };
68
+ }
69
+ /**
70
+ * Create a one-directional one-to-one relationship between two models using the `hasOne("MODEL_NAME")` method.
71
+ * A hasOne relationship always uses a reference to the related model's identifier. Typically this is the `id` field
72
+ * unless overwritten with the `identifier()` method.
73
+ * @param relatedModel the name of the related model
74
+ * @returns a one-to-one relationship definition
75
+ */
76
+ function hasOne(relatedModel) {
77
+ return _modelRelationalField(ModelRelationshipTypes.hasOne, relatedModel);
78
+ }
79
+ exports.hasOne = hasOne;
80
+ /**
81
+ * Create a one-directional one-to-many relationship between two models using the `hasMany()` method.
82
+ * @param relatedModel the name of the related model
83
+ * @returns a one-to-many relationship definition
84
+ */
85
+ function hasMany(relatedModel) {
86
+ return _modelRelationalField(ModelRelationshipTypes.hasMany, relatedModel);
87
+ }
88
+ exports.hasMany = hasMany;
89
+ /**
90
+ * Make a `hasOne()` or `hasMany()` relationship bi-directional using the `belongsTo()` method.
91
+ * The belongsTo() method requires that a hasOne() or hasMany() relationship already exists from
92
+ * parent to the related model.
93
+ * @param relatedModel name of the related `.hasOne()` or `.hasMany()` model
94
+ * @returns a belong-to relationship definition
95
+ */
96
+ function belongsTo(relatedModel) {
97
+ return _modelRelationalField(ModelRelationshipTypes.belongsTo, relatedModel);
98
+ }
99
+ exports.belongsTo = belongsTo;
100
+ /**
101
+ * Create a many-to-many relationship between two models with the manyToMany() method.
102
+ * Provide a common relationName on both models to join them into a many-to-many relationship.
103
+ * Under the hood a many-to-many relationship is modeled with a "join table" with corresponding
104
+ * `hasMany()` relationships between the two related models. You must set the same `manyToMany()`
105
+ * field on both models of the relationship.
106
+ * @param relatedModel name of the related model
107
+ * @param opts pass in the `relationName` that will serve as the join table name for this many-to-many relationship
108
+ * @returns a many-to-many relationship definition
109
+ */
110
+ function manyToMany(relatedModel, opts) {
111
+ return _modelRelationalField(ModelRelationshipTypes.manyToMany, relatedModel, opts.relationName);
112
+ }
113
+ exports.manyToMany = manyToMany;
@@ -0,0 +1,39 @@
1
+ import type { DerivedApiDefinition } from '@aws-amplify/data-schema-types';
2
+ import type { ModelType, ModelTypeParamShape, InternalModel } from './ModelType';
3
+ import type { EnumType } from './EnumType';
4
+ import type { CustomType } from './CustomType';
5
+ export { __auth } from './ModelField';
6
+ type ModelSchemaModels = Record<string, ModelType<ModelTypeParamShape, any>>;
7
+ type InternalSchemaModels = Record<string, InternalModel | EnumType<any> | CustomType<any>>;
8
+ export type ModelSchemaParamShape = {
9
+ types: ModelSchemaModels;
10
+ };
11
+ export type InternalSchema = {
12
+ data: {
13
+ types: InternalSchemaModels;
14
+ };
15
+ };
16
+ export type ModelSchema<T extends ModelSchemaParamShape> = {
17
+ data: T;
18
+ transform: () => DerivedApiDefinition;
19
+ };
20
+ /**
21
+ * Amplify API Next Model Schema shape
22
+ */
23
+ export type ModelSchemaType = ModelSchema<ModelSchemaParamShape>;
24
+ /**
25
+ * Model Schema type guard
26
+ * @param schema - api-next ModelSchema or string
27
+ * @returns true if the given value is a ModelSchema
28
+ */
29
+ export declare const isModelSchema: (schema: string | ModelSchemaType) => schema is ModelSchemaType;
30
+ /**
31
+ * The API and data model definition for Amplify Data. Pass in `{ <NAME>: a.model(...) }` to create a database table
32
+ * and exposes CRUDL operations via an API.
33
+ * @param types The API and data model definition
34
+ * @returns An API and data model definition to be deployed with Amplify (Gen 2) experience (`processSchema(...)`)
35
+ * or with the Amplify Data CDK construct (`@aws-amplify/data-construct`)
36
+ */
37
+ export declare function schema<Types extends ModelSchemaModels>(types: Types): ModelSchema<{
38
+ types: Types;
39
+ }>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.schema = exports.isModelSchema = exports.__auth = void 0;
4
+ var ModelField_1 = require("./ModelField");
5
+ Object.defineProperty(exports, "__auth", { enumerable: true, get: function () { return ModelField_1.__auth; } });
6
+ const SchemaProcessor_1 = require("./SchemaProcessor");
7
+ /**
8
+ * Model Schema type guard
9
+ * @param schema - api-next ModelSchema or string
10
+ * @returns true if the given value is a ModelSchema
11
+ */
12
+ const isModelSchema = (schema) => {
13
+ return typeof schema === 'object' && schema.data !== undefined;
14
+ };
15
+ exports.isModelSchema = isModelSchema;
16
+ function _schema(types) {
17
+ const data = { types };
18
+ const transform = () => {
19
+ const internalSchema = { data };
20
+ return (0, SchemaProcessor_1.processSchema)({ schema: internalSchema });
21
+ };
22
+ return { data, transform };
23
+ }
24
+ /**
25
+ * The API and data model definition for Amplify Data. Pass in `{ <NAME>: a.model(...) }` to create a database table
26
+ * and exposes CRUDL operations via an API.
27
+ * @param types The API and data model definition
28
+ * @returns An API and data model definition to be deployed with Amplify (Gen 2) experience (`processSchema(...)`)
29
+ * or with the Amplify Data CDK construct (`@aws-amplify/data-construct`)
30
+ */
31
+ function schema(types) {
32
+ return _schema(types);
33
+ }
34
+ exports.schema = schema;
@@ -0,0 +1,107 @@
1
+ import type { SetTypeSubArg } from '@aws-amplify/data-schema-types';
2
+ import { ModelField, InternalField } from './ModelField';
3
+ import type { ModelRelationalField, InternalRelationalField } from './ModelRelationalField';
4
+ import { Authorization } from './Authorization';
5
+ import { RefType } from './RefType';
6
+ type ModelFields = Record<string, ModelField<any, any> | ModelRelationalField<any, string, any> | RefType<any, any>>;
7
+ type InternalModelFields = Record<string, InternalField | InternalRelationalField>;
8
+ type ModelData = {
9
+ fields: ModelFields;
10
+ identifier: string[];
11
+ authorization: Authorization<any, any>[];
12
+ };
13
+ type InternalModelData = ModelData & {
14
+ fields: InternalModelFields;
15
+ identifier: string[];
16
+ authorization: Authorization<any, any>[];
17
+ };
18
+ export type ModelTypeParamShape = {
19
+ fields: ModelFields;
20
+ identifier: string[];
21
+ authorization: Authorization<any, any>[];
22
+ };
23
+ type ExtractType<T extends ModelTypeParamShape> = {
24
+ [FieldProp in keyof T['fields']]: T['fields'][FieldProp] extends ModelField<infer R, any> ? R : never;
25
+ };
26
+ type GetRequiredFields<T> = {
27
+ [FieldProp in keyof T as T[FieldProp] extends NonNullable<T[FieldProp]> ? FieldProp : never]: T[FieldProp];
28
+ };
29
+ type IdentifierMap<T extends ModelTypeParamShape> = GetRequiredFields<ExtractType<T>>;
30
+ type IdentifierFields<T extends ModelTypeParamShape> = keyof IdentifierMap<T> & string;
31
+ type IdentifierType<T extends ModelTypeParamShape, Fields extends string = IdentifierFields<T>> = Array<Fields>;
32
+ /**
33
+ * For a given ModelTypeParamShape, produces a map of Authorization rules
34
+ * that would *conflict* with the given type.
35
+ *
36
+ * E.g.,
37
+ *
38
+ * ```
39
+ * const test = {
40
+ * fields: {
41
+ * title: fields.string(),
42
+ * otherfield: fields.string().array(),
43
+ * numfield: fields.integer(),
44
+ * },
45
+ * identifier: [],
46
+ * authorization: [],
47
+ * };
48
+ *
49
+ * ConflictingAuthRulesMap<typeof test> === {
50
+ * title: Authorization<"title", true>;
51
+ * otherfield: Authorization<"otherfield", false>;
52
+ * numfield: Authorization<"numfield", true> | Authorization<"numfield", false>;
53
+ * }
54
+ * ```
55
+ */
56
+ type ConflictingAuthRulesMap<T extends ModelTypeParamShape> = {
57
+ [K in keyof ExtractType<T>]: K extends string ? string extends ExtractType<T>[K] ? Authorization<K, true> : string[] extends ExtractType<T>[K] ? Authorization<K, false> : Authorization<K, true> | Authorization<K, false> : never;
58
+ };
59
+ /**
60
+ * For a given ModelTypeParamShape, produces a union of Authorization rules
61
+ * that would *conflict* with the given type.
62
+ *
63
+ * E.g.,
64
+ *
65
+ * ```
66
+ * const test = {
67
+ * fields: {
68
+ * title: fields.string(),
69
+ * otherfield: fields.string().array(),
70
+ * numfield: fields.integer(),
71
+ * },
72
+ * identifier: [],
73
+ * authorization: [],
74
+ * };
75
+ *
76
+ * ConflictingAuthRules<typeof test> ===
77
+ * Authorization<"title", true>
78
+ * | Authorization<"otherfield", false>
79
+ * | Authorization<"numfield", true> | Authorization<"numfield", false>
80
+ * ;
81
+ * ```
82
+ */
83
+ type ConflictingAuthRules<T extends ModelTypeParamShape> = ConflictingAuthRulesMap<T>[keyof ConflictingAuthRulesMap<T>];
84
+ export type ModelType<T extends ModelTypeParamShape, K extends keyof ModelType<T> = never> = Omit<{
85
+ identifier<ID extends IdentifierType<T> = []>(identifier: ID): ModelType<SetTypeSubArg<T, 'identifier', ID>, K | 'identifier'>;
86
+ authorization<AuthRuleType extends Authorization<any, any>>(rules: Exclude<AuthRuleType, ConflictingAuthRules<T>>[]): ModelType<SetTypeSubArg<T, 'authorization', AuthRuleType[]>, K | 'authorization'>;
87
+ }, K>;
88
+ /**
89
+ * Internal representation of Model Type that exposes the `data` property.
90
+ * Used at buildtime.
91
+ */
92
+ export type InternalModel = ModelType<any> & {
93
+ data: InternalModelData;
94
+ };
95
+ /**
96
+ * A data model that creates a matching Amazon DynamoDB table and provides create, read (list and get), update,
97
+ * delete, and subscription APIs.
98
+ *
99
+ * @param fields database table fields. Supports scalar types and relationship types.
100
+ * @returns a data model definition
101
+ */
102
+ export declare function model<T extends ModelFields>(fields: T): ModelType<{
103
+ fields: T;
104
+ identifier: Array<'id'>;
105
+ authorization: [];
106
+ }>;
107
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.model = void 0;
4
+ function _model(fields) {
5
+ const data = {
6
+ fields,
7
+ identifier: ['id'],
8
+ authorization: [],
9
+ };
10
+ const builder = {
11
+ identifier(identifier) {
12
+ data.identifier = identifier;
13
+ return this;
14
+ },
15
+ authorization(rules) {
16
+ data.authorization = rules;
17
+ return this;
18
+ },
19
+ };
20
+ return { ...builder, data };
21
+ }
22
+ /**
23
+ * A data model that creates a matching Amazon DynamoDB table and provides create, read (list and get), update,
24
+ * delete, and subscription APIs.
25
+ *
26
+ * @param fields database table fields. Supports scalar types and relationship types.
27
+ * @returns a data model definition
28
+ */
29
+ function model(fields) {
30
+ return _model(fields);
31
+ }
32
+ exports.model = model;
@@ -0,0 +1,45 @@
1
+ import { SetTypeSubArg } from '@aws-amplify/data-schema-types';
2
+ import { Authorization } from './Authorization';
3
+ import { __auth } from './ModelField';
4
+ export declare const __ref: unique symbol;
5
+ export type RefTypeParamShape = {
6
+ type: 'ref';
7
+ link: string;
8
+ required: boolean;
9
+ authorization: Authorization<any, any>[];
10
+ };
11
+ export type RefType<T extends RefTypeParamShape, K extends keyof RefType<T> = never, Auth = undefined> = Omit<{
12
+ /**
13
+ * Marks a field as required.
14
+ */
15
+ required(): RefType<SetTypeSubArg<T, 'required', true>, K | 'required'>;
16
+ /**
17
+ * Configures field-level authorization rules. Pass in an array of authorizations `(a.allow.____)` to mix and match
18
+ * multiple authorization rules for this field.
19
+ */
20
+ authorization<AuthRuleType extends Authorization<any, any>>(rules: AuthRuleType[]): RefType<T, K | 'authorization', AuthRuleType>;
21
+ [__ref]: typeof __ref;
22
+ }, K> & {
23
+ [__auth]?: Auth;
24
+ };
25
+ type RefTypeData = {
26
+ type: 'ref';
27
+ link: string;
28
+ required: boolean;
29
+ authorization: Authorization<any, any>[];
30
+ };
31
+ /**
32
+ * Internal representation of Ref that exposes the `data` property.
33
+ * Used at buildtime.
34
+ */
35
+ export type InternalRef = RefType<RefTypeParamShape> & {
36
+ data: RefTypeData;
37
+ };
38
+ type RefTypeArgFactory<Link extends string> = {
39
+ type: 'ref';
40
+ link: Link;
41
+ required: false;
42
+ authorization: [];
43
+ };
44
+ export declare function ref<Value extends string, T extends Value>(link: T): RefType<RefTypeArgFactory<T>, never, undefined>;
45
+ export {};
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ref = exports.__ref = void 0;
4
+ const ModelField_1 = require("./ModelField");
5
+ exports.__ref = Symbol('__ref');
6
+ function _ref(link) {
7
+ const data = {
8
+ type: 'ref',
9
+ link,
10
+ required: false,
11
+ authorization: [],
12
+ };
13
+ const builder = {
14
+ required() {
15
+ data.required = true;
16
+ return this;
17
+ },
18
+ authorization(rules) {
19
+ data.authorization = rules;
20
+ return this;
21
+ },
22
+ [exports.__ref]: exports.__ref,
23
+ };
24
+ return { ...builder, data };
25
+ }
26
+ function ref(link) {
27
+ return _ref(link);
28
+ }
29
+ exports.ref = ref;
@@ -0,0 +1,10 @@
1
+ import type { InternalSchema } from './ModelSchema';
2
+ import { DerivedApiDefinition } from './types';
3
+ /**
4
+ * Returns API definition from ModelSchema or string schema
5
+ * @param arg - { schema }
6
+ * @returns DerivedApiDefinition that conforms to IAmplifyGraphqlDefinition
7
+ */
8
+ export declare function processSchema(arg: {
9
+ schema: InternalSchema;
10
+ }): DerivedApiDefinition;