Add Alias, Add ttl

This commit is contained in:
George Suntres
2026-04-27 12:38:00 -04:00
parent c770d08b7f
commit 40ce8d334b
2 changed files with 32 additions and 3 deletions

View File

@@ -5,7 +5,7 @@
"singular": "event", "singular": "event",
"plural": "events", "plural": "events",
"idPrefix": "e", "idPrefix": "e",
"indexSpecs": [], "indexSpecs": [],
"timeseries": { "timeseries": {
"timeField": "createdAt", "timeField": "createdAt",
"metaField": "name", "metaField": "name",

33
main.go
View File

@@ -51,6 +51,7 @@ type Timeseries struct {
TimeField string `bson:"timeField"` TimeField string `bson:"timeField"`
MetaField string `bson:"metaField"` MetaField string `bson:"metaField"`
Granularity string `bson:"granularity"` Granularity string `bson:"granularity"`
BucketMaxSpan int64 `bson:"bucketMaxSpan"`
} }
type Discriminator struct { type Discriminator struct {
@@ -68,6 +69,7 @@ type CollectionDefinition struct {
IndexSpecs []map[string]any `bson:"indexSpecs"` IndexSpecs []map[string]any `bson:"indexSpecs"`
Schema map[string]any `bson:"schema"` Schema map[string]any `bson:"schema"`
Views map[string]any `bson:"views"` Views map[string]any `bson:"views"`
Ttl int64 `bson:"ttl"`
Timeseries *Timeseries `bson:"timeseries,omitempty"` Timeseries *Timeseries `bson:"timeseries,omitempty"`
Discriminator *Discriminator Discriminator *Discriminator
} }
@@ -86,6 +88,8 @@ type MongoClient struct {
DBPrefix string DBPrefix string
// Registry holds critical information about collection's structure, like schema and indexes // Registry holds critical information about collection's structure, like schema and indexes
Registry map[string]*CollectionDefinition Registry map[string]*CollectionDefinition
// Alias collection names that map to a registered collection
Aliases map[string]string
// Relaxed if set to true will not enforce schema // Relaxed if set to true will not enforce schema
Relaxed bool Relaxed bool
// Context fields to serialize when needed // Context fields to serialize when needed
@@ -100,6 +104,18 @@ type MongoClient struct {
IgnoreAudit []string IgnoreAudit []string
} }
// AddAlias will add the alias only if it doesn't exist.
func (c *MongoClient) AddAlias(alias, name string) {
if c.Aliases == nil {
c.Aliases = make(map[string]string)
}
_, ok := c.Aliases[alias]
if !ok {
log.Printf("Registering alias %s to %s", alias, name)
c.Aliases[alias] = name
}
}
func (c *MongoClient) SetRelaxed() { func (c *MongoClient) SetRelaxed() {
c.Relaxed = true c.Relaxed = true
} }
@@ -142,8 +158,7 @@ func (c *MongoClient) AddDefinition(data map[string]any) {
if cd.Views != nil { if cd.Views != nil {
for k, _ := range cd.Views { for k, _ := range cd.Views {
log.Printf("Registering alias %s to %s", k, cd.Name) c.AddAlias(k, cd.Name)
c.Registry[k] = &cd
} }
} }
} }
@@ -231,7 +246,13 @@ func ApplyTimeSeries(cdef *CollectionDefinition, opts *options.CreateCollectionO
SetMetaField(cdef.Timeseries.MetaField). SetMetaField(cdef.Timeseries.MetaField).
SetGranularity(cdef.Timeseries.Granularity) SetGranularity(cdef.Timeseries.Granularity)
if cdef.Timeseries.BucketMaxSpan > 0 {
tsOpts.SetBucketMaxSpan(time.Duration(cdef.Timeseries.BucketMaxSpan) * time.Second)
}
opts.SetTimeSeriesOptions(tsOpts) opts.SetTimeSeriesOptions(tsOpts)
opts.SetExpireAfterSeconds(cdef.Ttl)
} else { } else {
log.Printf("No timeseries") log.Printf("No timeseries")
} }
@@ -480,6 +501,10 @@ const ADD_DEFINITION_SCHEMA = `
"views": { "views": {
"type": "object" "type": "object"
}, },
"ttl": {
"type": "integer",
"format": "int64"
},
"timeseries": { "timeseries": {
"type": "object", "type": "object",
"properties": { "properties": {
@@ -491,6 +516,10 @@ const ADD_DEFINITION_SCHEMA = `
}, },
"granularity": { "granularity": {
"enum": [ "seconds", "minutes", "hours"] "enum": [ "seconds", "minutes", "hours"]
},
"bucketMaxSpan": {
"type": "integer",
"format": "int64"
} }
}, },
"required": ["timeField", "metaField", "granularity"] "required": ["timeField", "metaField", "granularity"]