Documentation
¶
Overview ¶
Package ddml provides types and utilities for defining document database schemas. DDML (Document Database Markup Language) is a schema definition language for document databases, supporting nested structures, arrays, and index configurations.
Index ¶
- type Collection
- func (c *Collection) AddField(field *Field) *Collection
- func (c *Collection) AddIndex(index *Index) *Collection
- func (c *Collection) Generate() string
- func (c *Collection) Validate() error
- func (c *Collection) WithNote(note string) *Collection
- func (c *Collection) WithSetting(key, value string) *Collection
- type Enum
- type Field
- func (f *Field) AddField(nested *Field) *Field
- func (f *Field) Generate() string
- func (f *Field) Validate() error
- func (f *Field) WithDefault(value string) *Field
- func (f *Field) WithEnumRef(enumName string) *Field
- func (f *Field) WithNote(note string) *Field
- func (f *Field) WithPrimaryKey() *Field
- func (f *Field) WithRef(collection, field string) *Field
- func (f *Field) WithRequired() *Field
- func (f *Field) WithUnique() *Field
- type FieldRef
- type FieldType
- type Index
- func (i *Index) Generate() string
- func (i *Index) Validate() error
- func (i *Index) WithName(name string) *Index
- func (i *Index) WithOrder(fieldName string, order IndexOrder) *Index
- func (i *Index) WithSparse() *Index
- func (i *Index) WithTTL(seconds int) *Index
- func (i *Index) WithType(indexType IndexType) *Index
- func (i *Index) WithUnique() *Index
- type IndexField
- type IndexOrder
- type IndexType
- type Schema
- func (s *Schema) AddCollection(collection *Collection) *Schema
- func (s *Schema) AddEnum(enum *Enum) *Schema
- func (s *Schema) FromJSON(data []byte) error
- func (s *Schema) FromYAML(data []byte) error
- func (s *Schema) Generate() string
- func (s *Schema) ToJSON() ([]byte, error)
- func (s *Schema) ToYAML() ([]byte, error)
- func (s *Schema) Validate() error
- func (s *Schema) WithNote(note string) *Schema
- type ValidationError
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Collection ¶
type Collection struct {
Name string
Note *string
Settings map[string]string
Fields []*Field
Indexes []*Index
}
Collection represents a document collection.
func NewCollection ¶
func NewCollection(name string) *Collection
NewCollection creates a new document collection.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
collection := ddml.NewCollection("orders").
AddField(ddml.NewField("_id", ddml.TypeObjectID).WithPrimaryKey()).
AddField(ddml.NewField("customer_id", ddml.TypeString).WithRequired())
fmt.Println(collection.Name)
fmt.Println(len(collection.Fields))
}
Output: orders 2
func (*Collection) AddField ¶
func (c *Collection) AddField(field *Field) *Collection
AddField adds a field to the collection.
func (*Collection) AddIndex ¶
func (c *Collection) AddIndex(index *Index) *Collection
AddIndex adds an index to the collection.
func (*Collection) Generate ¶
func (c *Collection) Generate() string
Generate generates the DDML syntax for a Collection.
func (*Collection) Validate ¶
func (c *Collection) Validate() error
Validate validates a Collection.
func (*Collection) WithNote ¶
func (c *Collection) WithNote(note string) *Collection
WithNote adds a note to the collection.
func (*Collection) WithSetting ¶
func (c *Collection) WithSetting(key, value string) *Collection
WithSetting adds a setting to the collection.
type Enum ¶
Enum represents an enumeration type.
func NewEnum ¶
NewEnum creates a new enum with the given values.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
enum := ddml.NewEnum("order_status", "pending", "processing", "shipped", "delivered").
WithNote("Order status values")
fmt.Println(enum.Name)
fmt.Println(len(enum.Values))
}
Output: order_status 4
type Field ¶
type Field struct {
// For nested documents (Type == TypeObject)
Fields []*Field
// For arrays (Type == TypeArray)
ArrayOf *Field
// For references
Ref *FieldRef
Note *string
Default *string
EnumRef *string // For enums (Type == TypeEnum)
Name string
Type FieldType
Required bool
Unique bool
PrimaryKey bool
}
Field represents a document field (recursive for nesting).
func NewArrayField ¶
NewArrayField creates a new array field with element type.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
// Simple string array
tags := ddml.NewArrayField("tags", ddml.NewField("", ddml.TypeString))
fmt.Println(tags.Name)
fmt.Println(tags.Type)
fmt.Println(tags.ArrayOf.Type)
}
Output: tags array string
Example (WithObjects) ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
// Array of nested objects
items := ddml.NewArrayField("items",
ddml.NewObjectField("").
AddField(ddml.NewField("product_id", ddml.TypeString).WithRequired()).
AddField(ddml.NewField("quantity", ddml.TypeInt).WithRequired()),
)
fmt.Println(items.Name)
fmt.Println(items.ArrayOf.Type)
fmt.Println(len(items.ArrayOf.Fields))
}
Output: items object 2
func NewField ¶
NewField creates a new document field.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
field := ddml.NewField("email", ddml.TypeString).
WithRequired().
WithUnique().
WithNote("User email address")
fmt.Println(field.Name)
fmt.Println(field.Type)
fmt.Println(field.Required)
}
Output: email string true
func NewObjectField ¶
NewObjectField creates a new nested object field.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
address := ddml.NewObjectField("address").
AddField(ddml.NewField("street", ddml.TypeString).WithRequired()).
AddField(ddml.NewField("city", ddml.TypeString).WithRequired()).
AddField(ddml.NewField("postal_code", ddml.TypeString))
fmt.Println(address.Name)
fmt.Println(address.Type)
fmt.Println(len(address.Fields))
}
Output: address object 3
func (*Field) WithDefault ¶
WithDefault sets a default value for the field.
func (*Field) WithEnumRef ¶
WithEnumRef sets the enum reference for an enum field.
func (*Field) WithPrimaryKey ¶
WithPrimaryKey marks the field as a primary key.
func (*Field) WithRequired ¶
WithRequired marks the field as required.
func (*Field) WithUnique ¶
WithUnique marks the field as unique.
type FieldType ¶
type FieldType string
FieldType represents the data type of a document field.
const ( TypeString FieldType = "string" TypeInt FieldType = "int" TypeFloat FieldType = "float" TypeBool FieldType = "bool" TypeDate FieldType = "date" TypeObjectID FieldType = "objectid" TypeObject FieldType = "object" TypeArray FieldType = "array" TypeEnum FieldType = "enum" TypeBinary FieldType = "binary" TypeGeoPoint FieldType = "geopoint" )
Document field types.
type Index ¶
type Index struct {
Fields []IndexField
Name *string
TTL *int
Type *IndexType
Unique bool
Sparse bool
}
Index represents an index definition.
func NewIndex ¶
NewIndex creates a new index on the specified fields.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
idx := ddml.NewIndex("customer_id", "created_at").
WithName("idx_customer_created").
WithUnique()
fmt.Println(len(idx.Fields))
fmt.Println(idx.Unique)
}
Output: 2 true
func (*Index) WithOrder ¶
func (i *Index) WithOrder(fieldName string, order IndexOrder) *Index
WithOrder sets the order for a specific field in the index.
func (*Index) WithSparse ¶
WithSparse marks the index as sparse.
func (*Index) WithUnique ¶
WithUnique marks the index as unique.
type IndexField ¶
type IndexField struct {
Order *IndexOrder
Name string
}
IndexField represents a field in an index.
type IndexOrder ¶
type IndexOrder string
IndexOrder represents the sort order of an index field.
const ( Ascending IndexOrder = "asc" Descending IndexOrder = "desc" )
Index sort orders.
type Schema ¶
type Schema struct {
Collections map[string]*Collection
Enums map[string]*Enum
Name string
Note *string
}
Schema represents a document database schema containing collections and enums.
func NewSchema ¶
NewSchema creates a new document database schema.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
schema := ddml.NewSchema("ecommerce").
WithNote("E-commerce document store")
fmt.Println(schema.Name)
}
Output: ecommerce
func (*Schema) AddCollection ¶
func (s *Schema) AddCollection(collection *Collection) *Schema
AddCollection adds a collection to the schema.
func (*Schema) ToJSON ¶
ToJSON converts a Schema to JSON bytes.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
schema := ddml.NewSchema("test").
AddCollection(
ddml.NewCollection("users").
AddField(ddml.NewField("_id", ddml.TypeObjectID)),
)
data, err := schema.ToJSON()
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println(len(data) > 0)
}
Output: true
func (*Schema) Validate ¶
Validate validates a Schema.
Example ¶
package main
import (
"fmt"
"github.com/zoobzio/ddml"
)
func main() {
schema := ddml.NewSchema("test").
AddCollection(
ddml.NewCollection("users").
AddField(ddml.NewField("_id", ddml.TypeObjectID)),
)
err := schema.Validate()
fmt.Println(err == nil)
}
Output: true
type ValidationError ¶
ValidationError represents a validation error.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string