Skip to content

Commit eb26f30

Browse files
author
Dane Pilcher
committed
refactor: change to array of mappings
1 parent bff2de7 commit eb26f30

File tree

4 files changed

+78
-38
lines changed

4 files changed

+78
-38
lines changed

packages/backend-data/src/factory.test.ts

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,14 @@ void describe('Table Import', () => {
970970
`;
971971
const dataFactory = defineData({
972972
schema,
973-
migratedAmplifyGen1DynamoDbTableMap: {
974-
dev: {
975-
ImportedModel: 'ImportedModel-1234-dev',
973+
migratedAmplifyGen1DynamoDbTableMap: [
974+
{
975+
branchName: 'dev',
976+
modelTableNameMap: {
977+
ImportedModel: 'ImportedModel-1234-dev',
978+
},
976979
},
977-
},
980+
],
978981
});
979982
const getInstanceProps = createInstancePropsBySetupCDKApp({
980983
isSandboxMode: false,
@@ -1002,11 +1005,14 @@ void describe('Table Import', () => {
10021005
`;
10031006
const dataFactory = defineData({
10041007
schema,
1005-
migratedAmplifyGen1DynamoDbTableMap: {
1006-
dev: {
1007-
ImportedModel: 'ImportedModel-1234-dev',
1008+
migratedAmplifyGen1DynamoDbTableMap: [
1009+
{
1010+
branchName: 'dev',
1011+
modelTableNameMap: {
1012+
ImportedModel: 'ImportedModel-1234-dev',
1013+
},
10081014
},
1009-
},
1015+
],
10101016
});
10111017
const getInstanceProps = createInstancePropsBySetupCDKApp({
10121018
isSandboxMode: false,
@@ -1032,11 +1038,14 @@ void describe('Table Import', () => {
10321038
`;
10331039
const dataFactory = defineData({
10341040
schema,
1035-
migratedAmplifyGen1DynamoDbTableMap: {
1036-
dev: {
1037-
ImportedModel: 'ImportedModel-1234-dev',
1041+
migratedAmplifyGen1DynamoDbTableMap: [
1042+
{
1043+
branchName: 'dev',
1044+
modelTableNameMap: {
1045+
ImportedModel: 'ImportedModel-1234-dev',
1046+
},
10381047
},
1039-
},
1048+
],
10401049
});
10411050
const getInstanceProps = createInstancePropsBySetupCDKApp({
10421051
isSandboxMode: false,
@@ -1055,14 +1064,20 @@ void describe('Table Import', () => {
10551064
`;
10561065
const dataFactory = defineData({
10571066
schema,
1058-
migratedAmplifyGen1DynamoDbTableMap: {
1059-
dev: {
1060-
ImportedModel: 'ImportedModel-1234-dev',
1067+
migratedAmplifyGen1DynamoDbTableMap: [
1068+
{
1069+
branchName: 'dev',
1070+
modelTableNameMap: {
1071+
ImportedModel: 'ImportedModel-1234-dev',
1072+
},
10611073
},
1062-
prod: {
1063-
ImportedModel: 'ImportedModel-1234-prod',
1074+
{
1075+
branchName: 'prod',
1076+
modelTableNameMap: {
1077+
ImportedModel: 'ImportedModel-1234-prod',
1078+
},
10641079
},
1065-
},
1080+
],
10661081
});
10671082
const getInstanceProps = createInstancePropsBySetupCDKApp({
10681083
isSandboxMode: false,
@@ -1089,14 +1104,20 @@ void describe('Table Import', () => {
10891104
`;
10901105
const dataFactory = defineData({
10911106
schema,
1092-
migratedAmplifyGen1DynamoDbTableMap: {
1093-
dev: {
1094-
ImportedModel: 'ImportedModel-1234-dev',
1107+
migratedAmplifyGen1DynamoDbTableMap: [
1108+
{
1109+
branchName: 'dev',
1110+
modelTableNameMap: {
1111+
ImportedModel: 'ImportedModel-1234-dev',
1112+
},
10951113
},
1096-
sandbox: {
1097-
ImportedModel: 'ImportedModel-1234-sandbox',
1114+
{
1115+
branchName: 'sandbox',
1116+
modelTableNameMap: {
1117+
ImportedModel: 'ImportedModel-1234-sandbox',
1118+
},
10981119
},
1099-
},
1120+
],
11001121
});
11011122
const getInstanceProps = createInstancePropsBySetupCDKApp({
11021123
isSandboxMode: true,
@@ -1122,12 +1143,18 @@ void describe('Table Import', () => {
11221143
`;
11231144
const dataFactory = defineData({
11241145
schema,
1125-
migratedAmplifyGen1DynamoDbTableMap: {
1126-
dev: {
1127-
ImportedModel: 'ImportedModel-1234-dev',
1146+
migratedAmplifyGen1DynamoDbTableMap: [
1147+
{
1148+
branchName: 'dev',
1149+
modelTableNameMap: {
1150+
ImportedModel: 'ImportedModel-1234-dev',
1151+
},
11281152
},
1129-
prod: undefined,
1130-
},
1153+
{
1154+
branchName: 'prop',
1155+
modelTableNameMap: undefined,
1156+
},
1157+
],
11311158
});
11321159
const getInstanceProps = createInstancePropsBySetupCDKApp({
11331160
isSandboxMode: false,

packages/backend-data/src/factory.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,9 @@ class DataGenerator implements ConstructContainerEntryGenerator {
155155
// use the sandbox key when the branch name is not available (e.g. in the sandbox deployment)
156156
const amplifyBranchName =
157157
scope.node.tryGetContext('amplifyEnvironmentName') ?? 'sandbox';
158-
const importedTableMap = (this.props
159-
.migratedAmplifyGen1DynamoDbTableMap ?? {})[amplifyBranchName];
158+
const tableMapForCurrentBranch = (
159+
this.props.migratedAmplifyGen1DynamoDbTableMap ?? []
160+
).find((tableMap) => tableMap.branchName === amplifyBranchName);
160161
const splitSchemas: {
161162
schema: string | DerivedModelSchema;
162163
importedTableName?: string;
@@ -165,7 +166,7 @@ class DataGenerator implements ConstructContainerEntryGenerator {
165166
if (!isDataSchema(schema)) {
166167
const { importedSchemas, nonImportedSchema } = extractImportedModels(
167168
schema,
168-
importedTableMap
169+
tableMapForCurrentBranch?.modelTableNameMap
169170
);
170171
if (importedSchemas.length > 0) {
171172
return [

packages/backend-data/src/types.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,9 @@ export type DataProps = {
152152

153153
/**
154154
* Mapping of model name to existing DynamoDB table that should be used as the data source.
155+
* Each element in the array represents a mapping for a specific branch.
155156
*/
156-
migratedAmplifyGen1DynamoDbTableMap?: Record<
157-
string,
158-
Record<string, string> | undefined
159-
>;
157+
migratedAmplifyGen1DynamoDbTableMap?: AmplifyGen1DynamoDbTableMap[];
160158
};
161159

162160
export type AmplifyDataError =
@@ -245,3 +243,17 @@ export type DataLogLevel = Extract<
245243
LogLevel,
246244
'none' | 'all' | 'info' | 'debug' | 'error'
247245
>;
246+
247+
/**
248+
* Mapping of model name to existing DynamoDB table that should be used as the data source.
249+
* The mapping will only apply to the branch specified.
250+
*/
251+
export type AmplifyGen1DynamoDbTableMap = {
252+
branchName: string;
253+
modelTableNameMap?: ModelTableNameMap;
254+
};
255+
256+
/**
257+
* Mapping of model name to dynamodb table name.
258+
*/
259+
export type ModelTableNameMap = Record<string, string>;

packages/platform-core/API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,12 @@ export const packageJsonSchema: z.ZodObject<{
199199
type: z.ZodOptional<z.ZodUnion<[z.ZodLiteral<"module">, z.ZodLiteral<"commonjs">]>>;
200200
}, "strip", z.ZodTypeAny, {
201201
name?: string | undefined;
202-
type?: "module" | "commonjs" | undefined;
203202
version?: string | undefined;
203+
type?: "module" | "commonjs" | undefined;
204204
}, {
205205
name?: string | undefined;
206-
type?: "module" | "commonjs" | undefined;
207206
version?: string | undefined;
207+
type?: "module" | "commonjs" | undefined;
208208
}>;
209209

210210
// @public

0 commit comments

Comments
 (0)