@aws-amplify/data-schema 1.1.6 → 1.2.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.
@@ -25,7 +25,7 @@ import type {
25
25
  } from './CustomOperation';
26
26
  import { processSchema } from './SchemaProcessor';
27
27
  import { AllowModifier, SchemaAuthorization, allow } from './Authorization';
28
- import { Brand, brand } from './util';
28
+ import { Brand, brand, getBrand } from './util';
29
29
  import {
30
30
  ModelRelationalField,
31
31
  ModelRelationalFieldParamShape,
@@ -45,6 +45,10 @@ type SchemaContent =
45
45
  | EnumType
46
46
  | CustomOperation<CustomOperationParamShape, any>;
47
47
 
48
+ // The SQL-only `addToSchema` accepts all top-level entities, excepts models
49
+ type AddToSchemaContent = Exclude<SchemaContent, BaseModelType>;
50
+ type AddToSchemaContents = Record<string, AddToSchemaContent>;
51
+
48
52
  type NonEmpty<T> = keyof T extends never ? never : T;
49
53
 
50
54
  type ModelSchemaContents = Record<string, SchemaContent>;
@@ -103,6 +107,7 @@ export type ModelSchema<
103
107
  DDBSchemaBrand;
104
108
 
105
109
  type RDSModelSchemaFunctions =
110
+ | 'addToSchema'
106
111
  | 'addQueries'
107
112
  | 'addMutations'
108
113
  | 'addSubscriptions'
@@ -124,18 +129,33 @@ export type RDSModelSchema<
124
129
  >,
125
130
  > = Omit<
126
131
  {
132
+ addToSchema: <AddedTypes extends AddToSchemaContents>(
133
+ types: AddedTypes,
134
+ ) => RDSModelSchema<
135
+ SetTypeSubArg<T, 'types', T['types'] & AddedTypes>,
136
+ UsedMethods | 'addToSchema'
137
+ >;
138
+ /**
139
+ * @deprecated use `addToSchema()` to add operations to a SQL schema
140
+ */
127
141
  addQueries: <Queries extends Record<string, QueryCustomOperation>>(
128
142
  types: Queries,
129
143
  ) => RDSModelSchema<
130
144
  SetTypeSubArg<T, 'types', T['types'] & Queries>,
131
145
  UsedMethods | 'addQueries'
132
146
  >;
147
+ /**
148
+ * @deprecated use `addToSchema()` to add operations to a SQL schema
149
+ */
133
150
  addMutations: <Mutations extends Record<string, MutationCustomOperation>>(
134
151
  types: Mutations,
135
152
  ) => RDSModelSchema<
136
153
  SetTypeSubArg<T, 'types', T['types'] & Mutations>,
137
154
  UsedMethods | 'addMutations'
138
155
  >;
156
+ /**
157
+ * @deprecated use `addToSchema()` to add operations to a SQL schema
158
+ */
139
159
  addSubscriptions: <
140
160
  Subscriptions extends Record<string, SubscriptionCustomOperation>,
141
161
  >(
@@ -254,6 +274,22 @@ export const isModelSchema = (
254
274
  return typeof schema === 'object' && schema.data !== undefined;
255
275
  };
256
276
 
277
+ /**
278
+ * Ensures that only supported entities are being added to the SQL schema through `addToSchema`
279
+ * Models are not supported for brownfield SQL
280
+ *
281
+ * @param types - purposely widened to ModelSchemaContents, because we need to validate at runtime that a model is not being passed in here
282
+ */
283
+ function validateAddToSchema(types: ModelSchemaContents): void {
284
+ for (const [name, type] of Object.entries(types)) {
285
+ if (getBrand(type) === 'modelType') {
286
+ throw new Error(
287
+ `Invalid value specified for ${name} in addToSchema(). Models cannot be manually added to a SQL schema.`,
288
+ );
289
+ }
290
+ }
291
+ }
292
+
257
293
  function _rdsSchema<
258
294
  T extends RDSModelSchemaParamShape,
259
295
  DSC extends SchemaConfiguration<any, any>,
@@ -278,6 +314,12 @@ function _rdsSchema<
278
314
  const { authorization: _, ...rest } = this;
279
315
  return rest;
280
316
  },
317
+ addToSchema(types: AddToSchemaContents): any {
318
+ validateAddToSchema(types);
319
+ this.data.types = { ...this.data.types, ...types };
320
+ const { addToSchema: _, ...rest } = this;
321
+ return rest;
322
+ },
281
323
  addQueries(types: Record<string, QueryCustomOperation>): any {
282
324
  this.data.types = { ...this.data.types, ...types };
283
325
  const { addQueries: _, ...rest } = this;
@@ -1159,8 +1159,7 @@ const schemaPreprocessor = (
1159
1159
  ? 'dynamodb'
1160
1160
  : 'sql';
1161
1161
 
1162
- const staticSchema =
1163
- schema.data.configuration.database.engine === 'dynamodb' ? false : true;
1162
+ const staticSchema = databaseType === 'sql';
1164
1163
 
1165
1164
  const topLevelTypes = sortTopLevelTypes(Object.entries(schema.data.types));
1166
1165
 
@@ -1288,11 +1287,7 @@ const schemaPreprocessor = (
1288
1287
  const [partitionKey] = identifier;
1289
1288
 
1290
1289
  const { authString, authFields } = calculateAuth(mostRelevantAuthRules);
1291
- if (authString == '') {
1292
- throw new Error(
1293
- `Model \`${typeName}\` is missing authorization rules. Add global rules to the schema or ensure every model has its own rules.`,
1294
- );
1295
- }
1290
+
1296
1291
  const fieldLevelAuthRules = processFieldLevelAuthRules(
1297
1292
  fields,
1298
1293
  authFields,