Skip to content

Commit f0e08f4

Browse files
committed
wip
1 parent c908e02 commit f0e08f4

File tree

3 files changed

+89
-71
lines changed

3 files changed

+89
-71
lines changed

packages/compass-data-modeling/src/services/data-model-storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const EditSchema = z.discriminatedUnion('type', [
3636
type: z.literal('SetModel'),
3737
id: z.string(),
3838
timestamp: z.string(),
39-
model: z.unknown(), // TODO: StaticModelSchema,
39+
model: StaticModelSchema,
4040
}),
4141
z.object({
4242
type: z.literal('AddRelationship'),

packages/compass-data-modeling/src/store/analysis-process.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { Reducer } from 'redux';
22
import { isAction } from './util';
33
import type { DataModelingThunkAction } from './reducer';
4-
import { analyzeDocuments } from 'mongodb-schema';
4+
import { analyzeDocuments, type MongoDBJSONSchema } from 'mongodb-schema';
55
import { getCurrentDiagramFromState } from './diagram';
66
import type { Document } from 'bson';
77
import type { AggregationCursor } from 'mongodb';
8+
import type { Relationship } from '../services/data-model-storage';
89

910
export type AnalysisProcessState = {
1011
currentAnalysisOptions:
@@ -61,9 +62,8 @@ export type AnalysisFinishedAction = {
6162
type: AnalysisProcessActionTypes.ANALYSIS_FINISHED;
6263
name: string;
6364
connectionId: string;
64-
// TODO
65-
schema: Record<string, unknown>;
66-
relations: unknown[];
65+
collections: { ns: string; schema: MongoDBJSONSchema }[];
66+
relations: Relationship[];
6767
};
6868

6969
export type AnalysisFailedAction = {
@@ -157,7 +157,7 @@ export function startAnalysis(
157157
try {
158158
const dataService =
159159
services.connections.getDataServiceForConnection(connectionId);
160-
const schema = await Promise.all(
160+
const collections = await Promise.all(
161161
namespaces.map(async (ns) => {
162162
const sample: AggregationCursor<Document> = dataService.sampleCursor(
163163
ns,
@@ -188,7 +188,7 @@ export function startAnalysis(
188188
type: AnalysisProcessActionTypes.NAMESPACE_SCHEMA_ANALYZED,
189189
namespace: ns,
190190
});
191-
return [ns, schema];
191+
return { ns, schema };
192192
})
193193
);
194194
if (options.automaticallyInferRelations) {
@@ -201,7 +201,7 @@ export function startAnalysis(
201201
type: AnalysisProcessActionTypes.ANALYSIS_FINISHED,
202202
name,
203203
connectionId,
204-
schema: Object.fromEntries(schema),
204+
collections,
205205
relations: [],
206206
});
207207
void services.dataModelStorage.save(

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 81 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { isAction } from './util';
44
import type {
55
Edit,
66
MongoDBDataModelDescription,
7+
StaticModel,
78
} from '../services/data-model-storage';
89
import { AnalysisProcessActionTypes } from './analysis-process';
910
import { memoize } from 'lodash';
@@ -13,9 +14,9 @@ import { showConfirmation, showPrompt } from '@mongodb-js/compass-components';
1314
export type DiagramState =
1415
| (Omit<MongoDBDataModelDescription, 'edits'> & {
1516
edits: {
16-
prev: Edit[];
17+
prev: Edit[][];
1718
current: Edit[];
18-
next: Edit[];
19+
next: Edit[][];
1920
};
2021
})
2122
| null; // null when no diagram is currently open
@@ -47,8 +48,7 @@ export type RenameDiagramAction = {
4748

4849
export type ApplyEditAction = {
4950
type: DiagramActionTypes.APPLY_EDIT;
50-
// TODO
51-
edit: unknown;
51+
edit: Edit;
5252
};
5353

5454
export type UndoEditAction = {
@@ -86,27 +86,35 @@ export const diagramReducer: Reducer<DiagramState> = (
8686
};
8787
}
8888

89-
// if (isAction(action, AnalysisProcessActionTypes.ANALYSIS_FINISHED)) {
90-
// return {
91-
// id: new UUID().toString(),
92-
// name: action.name,
93-
// connectionId: action.connectionId,
94-
// edits: {
95-
// prev: [],
96-
// current: [
97-
// {
98-
// // TODO
99-
// type: 'SetModel',
100-
// model: {
101-
// schema: action.schema,
102-
// relations: action.relations,
103-
// },
104-
// },
105-
// ],
106-
// next: [],
107-
// },
108-
// };
109-
// }
89+
if (isAction(action, AnalysisProcessActionTypes.ANALYSIS_FINISHED)) {
90+
return {
91+
id: new UUID().toString(),
92+
name: action.name,
93+
connectionId: action.connectionId,
94+
edits: {
95+
prev: [],
96+
current: [
97+
{
98+
type: 'SetModel',
99+
id: new UUID().toString(),
100+
timestamp: new Date().toISOString(),
101+
model: {
102+
collections: action.collections.map((collection) => ({
103+
ns: collection.ns,
104+
jsonSchema: collection.schema,
105+
// TODO
106+
indexes: [],
107+
shardKey: undefined,
108+
displayPosition: [0, 0],
109+
})),
110+
relationships: action.relations,
111+
},
112+
},
113+
],
114+
next: [],
115+
},
116+
};
117+
}
110118

111119
// All actions below are only applicable when diagram is open
112120
if (!state) {
@@ -120,6 +128,7 @@ export const diagramReducer: Reducer<DiagramState> = (
120128
};
121129
}
122130
if (isAction(action, DiagramActionTypes.APPLY_EDIT)) {
131+
console.log('Applying edit', action.edit);
123132
return {
124133
...state,
125134
edits: {
@@ -129,34 +138,34 @@ export const diagramReducer: Reducer<DiagramState> = (
129138
},
130139
};
131140
}
132-
// if (isAction(action, DiagramActionTypes.UNDO_EDIT)) {
133-
// const newCurrent = state.edits.prev.pop();
134-
// if (!newCurrent) {
135-
// return state;
136-
// }
137-
// return {
138-
// ...state,
139-
// edits: {
140-
// prev: [...state.edits.prev],
141-
// current: newCurrent,
142-
// next: [...state.edits.next, state.edits.current],
143-
// },
144-
// };
145-
// }
146-
// if (isAction(action, DiagramActionTypes.REDO_EDIT)) {
147-
// const newCurrent = state.edits.next.pop();
148-
// if (!newCurrent) {
149-
// return state;
150-
// }
151-
// return {
152-
// ...state,
153-
// edits: {
154-
// prev: [...state.edits.prev, state.edits.current],
155-
// current: newCurrent,
156-
// next: [...state.edits.next],
157-
// },
158-
// };
159-
// }
141+
if (isAction(action, DiagramActionTypes.UNDO_EDIT)) {
142+
const newCurrent = state.edits.prev.pop();
143+
if (!newCurrent) {
144+
return state;
145+
}
146+
return {
147+
...state,
148+
edits: {
149+
prev: [...state.edits.prev],
150+
current: newCurrent,
151+
next: [...state.edits.next, state.edits.current],
152+
},
153+
};
154+
}
155+
if (isAction(action, DiagramActionTypes.REDO_EDIT)) {
156+
const newCurrent = state.edits.next.pop();
157+
if (!newCurrent) {
158+
return state;
159+
}
160+
return {
161+
...state,
162+
edits: {
163+
prev: [...state.edits.prev, state.edits.current],
164+
current: newCurrent,
165+
next: [...state.edits.next],
166+
},
167+
};
168+
}
160169
return state;
161170
};
162171

@@ -175,7 +184,7 @@ export function redoEdit(): DataModelingThunkAction<void, RedoEditAction> {
175184
}
176185

177186
export function applyEdit(
178-
edit: unknown
187+
edit: Edit
179188
): DataModelingThunkAction<void, ApplyEditAction> {
180189
return (dispatch, getState, { dataModelStorage }) => {
181190
dispatch({ type: DiagramActionTypes.APPLY_EDIT, edit });
@@ -230,20 +239,29 @@ export function renameDiagram(
230239
};
231240
}
232241

233-
// TODO
234-
function _applyEdit(model: any, edit: any) {
235-
if (edit && 'type' in edit) {
236-
if (edit.type === 'SetModel') {
237-
return edit.model;
238-
}
242+
function _applyEdit(edit: Edit, model?: StaticModel): StaticModel {
243+
if (edit.type === 'SetModel') {
244+
return edit.model;
245+
}
246+
if (!model) {
247+
throw new Error('Editing a model that has not been initialized');
248+
}
249+
if (edit.type === 'AddRelationship') {
250+
return {
251+
...model,
252+
relationships: [...model.relationships, edit.relationship],
253+
};
239254
}
240255
return model;
241256
}
242257

243-
export function getCurrentModel(diagram: MongoDBDataModelDescription): unknown {
244-
let model;
258+
export function getCurrentModel(
259+
diagram: MongoDBDataModelDescription
260+
): StaticModel {
261+
let model: StaticModel;
245262
for (const edit of diagram.edits) {
246-
model = _applyEdit(model, edit);
263+
console.log('Applying edit', edit, diagram);
264+
model = _applyEdit(edit, model);
247265
}
248266
return model;
249267
}

0 commit comments

Comments
 (0)