Skip to content

Commit e8447d5

Browse files
committed
ensureCollectionExists
1 parent 5e72896 commit e8447d5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

packages/compass-data-modeling/src/store/apply-edit.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ function renameFieldInRelationshipSide(
3232
};
3333
}
3434

35+
function ensureCollectionExists(
36+
collections: DataModelCollection[],
37+
ns: string
38+
): void {
39+
const collection = collections.find((c) => c.ns === ns);
40+
if (!collection) {
41+
throw new Error(`Collection '${ns}' not found`);
42+
}
43+
}
44+
3545
export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
3646
if (edit.type === 'SetModel') {
3747
return edit.model;
@@ -81,6 +91,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
8191
};
8292
}
8393
case 'MoveCollection': {
94+
ensureCollectionExists(model.collections, edit.ns);
8495
return {
8596
...model,
8697
collections: model.collections.map((collection) => {
@@ -95,6 +106,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
95106
};
96107
}
97108
case 'RemoveCollection': {
109+
ensureCollectionExists(model.collections, edit.ns);
98110
return {
99111
...model,
100112
// Remove any relationships involving the collection being removed.
@@ -109,6 +121,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
109121
};
110122
}
111123
case 'RenameCollection': {
124+
ensureCollectionExists(model.collections, edit.fromNS);
112125
return {
113126
...model,
114127
// Update relationships to point to the renamed namespace.
@@ -137,6 +150,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
137150
};
138151
}
139152
case 'UpdateCollectionNote': {
153+
ensureCollectionExists(model.collections, edit.ns);
140154
return {
141155
...model,
142156
collections: model.collections.map((collection) => {
@@ -151,6 +165,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
151165
};
152166
}
153167
case 'AddField': {
168+
ensureCollectionExists(model.collections, edit.ns);
154169
return {
155170
...model,
156171
collections: model.collections.map((collection) => {
@@ -169,6 +184,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
169184
};
170185
}
171186
case 'RemoveField': {
187+
ensureCollectionExists(model.collections, edit.ns);
172188
return {
173189
...model,
174190
// Remove any relationships involving the field being removed.
@@ -193,6 +209,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
193209
};
194210
}
195211
case 'RenameField': {
212+
ensureCollectionExists(model.collections, edit.ns);
196213
return {
197214
...model,
198215
// Update any relationships involving the field being renamed.
@@ -227,6 +244,7 @@ export function applyEdit(edit: Edit, model?: StaticModel): StaticModel {
227244
};
228245
}
229246
case 'ChangeFieldType': {
247+
ensureCollectionExists(model.collections, edit.ns);
230248
return {
231249
...model,
232250
collections: model.collections.map((collection) => {

0 commit comments

Comments
 (0)