Skip to content

Commit

Permalink
feat: add index on state
Browse files Browse the repository at this point in the history
  • Loading branch information
toopay committed Oct 25, 2024
1 parent bfbb805 commit 3e9f8c3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/generator/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func BuildRelationTag(r *state.Relation) string {
tags = append(tags, jsonTag)

if r.Index != nil {
tags = append(tags, "indexed")
tags = append(tags, "indexed:true")
}

if r.Action != nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/state/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ func buildTableFromModel(model any) (ei ExtractTableItem) {
}
}

// check index field
indexed := field.Tag.Get("indexed")
if len(indexed) > 0 {
rel.Index = &objects.Index{
Schema: ei.Table.Schema,
Table: rel.TargetTableName,
Name: fmt.Sprintf("ix_%s_%s", rel.TargetTableName, rel.TargetColumnName),
Definition: fmt.Sprintf("CREATE INDEX ix_%s_%s ON %s(%s);", rel.TargetTableName, rel.TargetColumnName, rel.TargetTableName, rel.TargetColumnName),
}
}

ei.Table.Relationships = append(ei.Table.Relationships, rel)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/state/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Submission struct {
Acl string `json:"-" read:"anon" write:"anon"`

// Relations
Candidate *Candidate `json:"candidate,omitempty" join:"joinType:hasOne;primaryKey:id;foreignKey:candidate_id" onUpdate:"cascade" onDelete:"cascade"`
Candidate *Candidate `json:"candidate,omitempty" join:"joinType:hasOne;primaryKey:id;foreignKey:candidate_id" onUpdate:"cascade" onDelete:"cascade" indexed:"true"`
}

type Candidate struct {
Expand Down

0 comments on commit 3e9f8c3

Please sign in to comment.