11import { z } from '@mongodb-js/compass-user-data' ;
2+ import type { MongoDBJSONSchema } from 'mongodb-schema' ;
23import type { ZodError } from 'zod' ;
34
45export const RelationshipSideSchema = z . object ( {
@@ -21,7 +22,7 @@ export const StaticModelSchema = z.object({
2122 collections : z . array (
2223 z . object ( {
2324 ns : z . string ( ) ,
24- jsonSchema : z . unknown ( ) , // MongoDBJSONSchema is not directly representable in zod
25+ jsonSchema : z . unknown ( ) , // skipped for simplicity
2526 indexes : z . array ( z . record ( z . unknown ( ) ) ) ,
2627 shardKey : z . record ( z . unknown ( ) ) . optional ( ) ,
2728 displayPosition : z . tuple ( [ z . number ( ) , z . number ( ) ] ) ,
@@ -30,7 +31,19 @@ export const StaticModelSchema = z.object({
3031 relationships : z . array ( RelationshipSchema ) ,
3132} ) ;
3233
33- export type StaticModel = z . output < typeof StaticModelSchema > ;
34+ export type StaticModel = Omit <
35+ z . output < typeof StaticModelSchema > ,
36+ 'collections'
37+ > & {
38+ collections : Array <
39+ Omit <
40+ z . output < typeof StaticModelSchema > [ 'collections' ] [ number ] ,
41+ 'jsonSchema'
42+ > & {
43+ jsonSchema : MongoDBJSONSchema ;
44+ }
45+ > ;
46+ } ;
3447
3548export const EditSchema = z . discriminatedUnion ( 'type' , [
3649 z . object ( {
@@ -53,6 +66,15 @@ export const EditSchema = z.discriminatedUnion('type', [
5366 } ) ,
5467] ) ;
5568
69+ type BaseEdit = z . output < typeof EditSchema > ;
70+ type SetModelEdit = Omit < Extract < BaseEdit , { type : 'SetModel' } > , 'model' > & {
71+ model : StaticModel ;
72+ } ;
73+
74+ export type Edit =
75+ | SetModelEdit
76+ | Extract < BaseEdit , { type : 'AddRelationship' | 'RemoveRelationship' } > ;
77+
5678export const validateEdit = (
5779 edit : unknown
5880) : { result : true ; errors ?: never } | { result : false ; errors : string [ ] } => {
@@ -71,8 +93,6 @@ export const validateEdit = (
7193 }
7294} ;
7395
74- export type Edit = z . output < typeof EditSchema > ;
75-
7696export const MongoDBDataModelDescriptionSchema = z . object ( {
7797 id : z . string ( ) ,
7898 name : z . string ( ) ,
@@ -87,9 +107,12 @@ export const MongoDBDataModelDescriptionSchema = z.object({
87107 edits : z . array ( EditSchema ) . default ( [ ] ) ,
88108} ) ;
89109
90- export type MongoDBDataModelDescription = z . output <
91- typeof MongoDBDataModelDescriptionSchema
92- > ;
110+ export type MongoDBDataModelDescription = Omit <
111+ z . output < typeof MongoDBDataModelDescriptionSchema > ,
112+ 'edits'
113+ > & {
114+ edits : Array < Edit > ;
115+ } ;
93116
94117export interface DataModelStorage {
95118 save ( description : MongoDBDataModelDescription ) : Promise < boolean > ;
0 commit comments