-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodelsdk.go
More file actions
306 lines (269 loc) · 7.57 KB
/
modelsdk.go
File metadata and controls
306 lines (269 loc) · 7.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// SPDX-License-Identifier: Apache-2.0
// Package modelsdk provides a Go library for reading and modifying Mendix projects.
//
// This library is a Go alternative to the Mendix Model SDK and Mendix Platform SDK,
// allowing direct manipulation of Mendix project files (.mpr) on disk.
//
// # Overview
//
// Mendix projects are stored in .mpr files which are SQLite databases containing
// BSON-encoded model elements. This library provides:
//
// - Reading and parsing MPR files (both v1 and v2 formats)
// - Type-safe access to all Mendix model elements
// - Creating, updating, and deleting model elements
// - Exporting models to JSON format
//
// # Quick Start
//
// package main
//
// import (
// "fmt"
// "github.com/mendixlabs/mxcli"
// "github.com/mendixlabs/mxcli/sdk/mpr"
// )
//
// func main() {
// // Open a Mendix project
// reader, err := mpr.Open("/path/to/MyApp.mpr")
// if err != nil {
// panic(err)
// }
// defer reader.Close()
//
// // List all modules
// modules, err := reader.ListModules()
// if err != nil {
// panic(err)
// }
//
// for _, m := range modules {
// fmt.Printf("Module: %s\n", m.Name)
// }
// }
//
// # MPR File Formats
//
// The library supports both MPR v1 (single file) and MPR v2 (with mprcontents folder)
// formats. MPR v2 was introduced in Mendix Studio Pro 10.18.
//
// # Model Structure
//
// The Mendix model is organized hierarchically:
//
// - Project
// - Modules
// - Domain Models (Entities, Attributes, Associations)
// - Microflows and Nanoflows
// - Pages, Layouts, and Snippets
// - Enumerations and Constants
// - Scheduled Events
//
// Each element has a unique ID and belongs to a container element.
//
// # Reading Models
//
// Use the mpr.Reader to read model elements:
//
// reader, _ := mpr.Open("MyApp.mpr")
//
// // Get all domain models
// domainModels, _ := reader.ListDomainModels()
//
// // Get a specific module
// module, _ := reader.GetModuleByName("MyModule")
//
// // Get the domain model for a module
// dm, _ := reader.GetDomainModel(module.ID)
//
// # Modifying Models
//
// Use the mpr.Writer to modify model elements:
//
// writer, _ := mpr.NewWriter("MyApp.mpr")
// defer writer.Close()
//
// // Create a new entity
// entity := &domainmodel.Entity{
// Name: "Customer",
// Persistable: true,
// }
// writer.CreateEntity(domainModelID, entity)
//
// // Add an attribute
// attr := &domainmodel.Attribute{
// Name: "CustomerName",
// Type: &domainmodel.StringAttributeType{Length: 200},
// }
// writer.AddAttribute(domainModelID, entity.ID, attr)
//
// # Thread Safety
//
// The Reader is safe for concurrent read access. The Writer should only be used
// from a single goroutine. For concurrent modifications, use transactions.
//
// # Error Handling
//
// All functions that can fail return an error. Errors include:
//
// - File not found
// - Invalid MPR format
// - Element not found
// - BSON parsing errors
// - SQLite errors
package modelsdk
import (
"github.com/mendixlabs/mxcli/model"
"github.com/mendixlabs/mxcli/sdk/domainmodel"
"github.com/mendixlabs/mxcli/sdk/microflows"
"github.com/mendixlabs/mxcli/sdk/mpr"
"github.com/mendixlabs/mxcli/sdk/pages"
)
// Version is the library version.
const Version = "0.1.0"
// Re-export commonly used types for convenience.
type (
// ID is a unique identifier for model elements.
ID = model.ID
// Module represents a Mendix module.
Module = model.Module
// Project represents a Mendix project.
Project = model.Project
// Enumeration represents an enumeration type.
Enumeration = model.Enumeration
// Constant represents a constant value.
Constant = model.Constant
// ConstantDataType represents the data type of a constant.
ConstantDataType = model.ConstantDataType
// ScheduledEvent represents a scheduled event.
ScheduledEvent = model.ScheduledEvent
// DomainModel represents a module's domain model.
DomainModel = domainmodel.DomainModel
// Entity represents an entity in a domain model.
Entity = domainmodel.Entity
// Attribute represents an attribute of an entity.
Attribute = domainmodel.Attribute
// Association represents an association between entities.
Association = domainmodel.Association
// Microflow represents a microflow.
Microflow = microflows.Microflow
// Nanoflow represents a nanoflow.
Nanoflow = microflows.Nanoflow
// Page represents a page.
Page = pages.Page
// Layout represents a layout.
Layout = pages.Layout
// Snippet represents a page snippet.
Snippet = pages.Snippet
// Reader provides methods to read Mendix project files.
Reader = mpr.Reader
// Writer provides methods to write Mendix project files.
Writer = mpr.Writer
)
// Open opens an MPR file for reading.
func Open(path string) (*Reader, error) {
return mpr.Open(path)
}
// OpenForWriting opens an MPR file for reading and writing.
func OpenForWriting(path string) (*Writer, error) {
return mpr.NewWriter(path)
}
// NewStringAttribute creates a new string attribute.
func NewStringAttribute(name string, length int) *Attribute {
return &Attribute{
Name: name,
Type: &domainmodel.StringAttributeType{Length: length},
}
}
// NewIntegerAttribute creates a new integer attribute.
func NewIntegerAttribute(name string) *Attribute {
return &Attribute{
Name: name,
Type: &domainmodel.IntegerAttributeType{},
}
}
// NewDecimalAttribute creates a new decimal attribute.
func NewDecimalAttribute(name string) *Attribute {
return &Attribute{
Name: name,
Type: &domainmodel.DecimalAttributeType{},
}
}
// NewBooleanAttribute creates a new boolean attribute.
func NewBooleanAttribute(name string) *Attribute {
return &Attribute{
Name: name,
Type: &domainmodel.BooleanAttributeType{},
}
}
// NewDateTimeAttribute creates a new date/time attribute.
func NewDateTimeAttribute(name string, localize bool) *Attribute {
return &Attribute{
Name: name,
Type: &domainmodel.DateTimeAttributeType{LocalizeDate: localize},
}
}
// NewEnumerationAttribute creates a new enumeration attribute.
func NewEnumerationAttribute(name string, enumerationID ID) *Attribute {
return &Attribute{
Name: name,
Type: &domainmodel.EnumerationAttributeType{EnumerationID: enumerationID},
}
}
// NewEntity creates a new persistable entity.
func NewEntity(name string) *Entity {
return &Entity{
Name: name,
Persistable: true,
}
}
// NewNonPersistableEntity creates a new non-persistable entity.
func NewNonPersistableEntity(name string) *Entity {
return &Entity{
Name: name,
Persistable: false,
}
}
// NewAssociation creates a new reference association.
func NewAssociation(name string, parentID, childID ID) *Association {
return &Association{
Name: name,
ParentID: parentID,
ChildID: childID,
Type: domainmodel.AssociationTypeReference,
Owner: domainmodel.AssociationOwnerDefault,
}
}
// NewReferenceSetAssociation creates a new reference set association.
func NewReferenceSetAssociation(name string, parentID, childID ID) *Association {
return &Association{
Name: name,
ParentID: parentID,
ChildID: childID,
Type: domainmodel.AssociationTypeReferenceSet,
Owner: domainmodel.AssociationOwnerDefault,
}
}
// NewMicroflow creates a new microflow.
func NewMicroflow(name string) *Microflow {
return &Microflow{
Name: name,
}
}
// NewNanoflow creates a new nanoflow.
func NewNanoflow(name string) *Nanoflow {
return &Nanoflow{
Name: name,
}
}
// NewPage creates a new page.
func NewPage(name string) *Page {
return &Page{
Name: name,
}
}
// GenerateID generates a new unique ID for model elements.
func GenerateID() ID {
return ID(mpr.GenerateID())
}