Functions into dbmodels, introduced air

This commit is contained in:
Simon Martens
2025-02-11 13:10:48 +01:00
parent ad91cbd4c6
commit 420fff6daa
16 changed files with 237 additions and 140 deletions

60
.air.toml Normal file
View File

@@ -0,0 +1,60 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"
[build]
args_bin = []
full_bin = "./tmp/musenalm --dir ./pb_data serve"
cmd = "go build -tags=\"dev\" -o ./tmp/musenalm ."
delay = 400
exclude_dir = [
"views/assets",
"views/node_modules",
"tmp",
"vendor",
"testdata",
"data_git",
"cache_gnd",
"cache_geonames",
"pb_data",
]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html", "gohtml", "js", "css", "xsl"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = ["npm --prefix views/ run build"]
rerun = false
rerun_delay = 250
send_interrupt = true
stop_on_error = true
[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"
[log]
main_only = false
time = false
[misc]
clean_on_exit = true
[proxy]
app_port = 8080
enabled = true
proxy_port = 8081
[screen]
clear_on_rebuild = true
keep_scroll = true

View File

@@ -1,5 +1,7 @@
package dbmodels
import "github.com/pocketbase/pocketbase/tools/types"
var EDITORSTATE_VALUES = []string{"Unknown", "ToDo", "Seen", "Partially Edited", "Waiting", "Review", "Edited"}
var ITEM_TYPE_VALUES = []string{
@@ -410,10 +412,10 @@ var SERIES_RELATIONS = []string{
"In anderer Sprache",
}
const (
PUBLIC_VIEW_RULE = ""
PUBLIC_LIST_RULE = ""
var PUBLIC_VIEW_RULE = types.Pointer("")
var PUBLIC_LIST_RULE = types.Pointer("")
const (
PLACES_TABLE = "places"
AGENTS_TABLE = "agents"
SERIES_TABLE = "series"
@@ -486,7 +488,3 @@ const (
ITEMS_CONDITION_FIELD = "condition"
ITEMS_IDENTIFIER_FIELD = "identifier"
)
func RelationTableName(collection1, collection2 string) string {
return "R_" + collection1 + "_" + collection2
}

66
dbmodels/functions.go Normal file
View File

@@ -0,0 +1,66 @@
package dbmodels
import "github.com/pocketbase/pocketbase/core"
func SetBasicPublicRules(collection *core.Collection) {
collection.ViewRule = PUBLIC_VIEW_RULE
collection.ListRule = PUBLIC_LIST_RULE
}
func SetMusenalmIDField(fieldlist *core.FieldsList) {
fieldlist.Add(&core.TextField{Name: MUSENALMID_FIELD, Max: 64, Required: false})
}
func SetEditorStateField(fieldlist *core.FieldsList) {
fieldlist.Add(&core.SelectField{Name: EDITSTATE_FIELD, Required: false, Values: EDITORSTATE_VALUES})
}
func SetNotesAndAnnotationsField(fieldlist *core.FieldsList) {
fieldlist.Add(&core.EditorField{Name: ANNOTATION_FIELD, Required: false, ConvertURLs: false})
fieldlist.Add(&core.EditorField{Name: COMMENT_FIELD, Required: false, ConvertURLs: false})
}
func AddMusenalmIDIndex(collection *core.Collection) {
AddIndex(collection, MUSENALMID_FIELD, true)
}
func AddIndex(collection *core.Collection, field string, unique bool) {
name := collection.Name
collection.AddIndex("idx_"+name+"_"+field, unique, field, "")
}
func RelationTableName(collection1, collection2 string) string {
return "R_" + collection1 + "_" + collection2
}
func BasicRelationCollection(app core.App, sourcetablename, targettablename string, relations []string) (*core.Collection, error) {
stable, err := app.FindCollectionByNameOrId(sourcetablename)
if err != nil {
return nil, err
}
ttable, err := app.FindCollectionByNameOrId(targettablename)
if err != nil {
return nil, err
}
collection := core.NewBaseCollection(RelationTableName(stable.Name, ttable.Name))
SetBasicPublicRules(collection)
fields := core.NewFieldsList(
&core.RelationField{Name: stable.Name, Required: true, CollectionId: stable.Id, MinSelect: 1, MaxSelect: 1},
&core.RelationField{Name: ttable.Name, Required: true, CollectionId: ttable.Id, MinSelect: 1, MaxSelect: 1},
&core.SelectField{Name: RELATION_TYPE_FIELD, Required: true, Values: relations, MaxSelect: 1},
&core.BoolField{Name: RELATION_CONJECTURE_FIELD, Required: false},
&core.BoolField{Name: RELATION_UNCERTAIN_FIELD, Required: false},
)
SetNotesAndAnnotationsField(&fields)
collection.Fields = fields
AddIndex(collection, stable.Name, false)
AddIndex(collection, ttable.Name, false)
AddIndex(collection, RELATION_TYPE_FIELD, false)
return collection, nil
}

View File

@@ -25,7 +25,7 @@ func init() {
func placesTable() *core.Collection {
collection := core.NewBaseCollection(dbmodels.PLACES_TABLE)
setBasicPublicRules(collection)
dbmodels.SetBasicPublicRules(collection)
return collection
}
@@ -37,15 +37,15 @@ func placesFields() core.FieldsList {
&core.URLField{Name: dbmodels.URI_FIELD, Required: false, OnlyDomains: []string{"geonames.org"}},
)
setMusenalmIDField(&fields)
setEditorStateField(&fields)
setNotesAndAnnotationsField(&fields)
dbmodels.SetMusenalmIDField(&fields)
dbmodels.SetEditorStateField(&fields)
dbmodels.SetNotesAndAnnotationsField(&fields)
return fields
}
func placesIndexes(collection *core.Collection) {
addMusenalmIDIndex(collection)
addIndex(collection, dbmodels.PLACES_NAME_FIELD, false)
addIndex(collection, dbmodels.URI_FIELD, false)
dbmodels.AddMusenalmIDIndex(collection)
dbmodels.AddIndex(collection, dbmodels.PLACES_NAME_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.URI_FIELD, false)
}

View File

@@ -25,7 +25,7 @@ func init() {
func agentsTable() *core.Collection {
collection := core.NewBaseCollection(dbmodels.AGENTS_TABLE)
setBasicPublicRules(collection)
dbmodels.SetBasicPublicRules(collection)
return collection
}
@@ -41,15 +41,15 @@ func agentsFields() core.FieldsList {
&core.TextField{Name: dbmodels.REFERENCES_FIELD, Required: false},
)
setMusenalmIDField(&fields)
setEditorStateField(&fields)
setNotesAndAnnotationsField(&fields)
dbmodels.SetMusenalmIDField(&fields)
dbmodels.SetEditorStateField(&fields)
dbmodels.SetNotesAndAnnotationsField(&fields)
return fields
}
func agentsIndexes(collection *core.Collection) {
addMusenalmIDIndex(collection)
addIndex(collection, dbmodels.AGENTS_NAME_FIELD, false)
addIndex(collection, dbmodels.URI_FIELD, false)
dbmodels.AddMusenalmIDIndex(collection)
dbmodels.AddIndex(collection, dbmodels.AGENTS_NAME_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.URI_FIELD, false)
}

View File

@@ -25,7 +25,7 @@ func init() {
func seriesTable() *core.Collection {
collection := core.NewBaseCollection(dbmodels.SERIES_TABLE)
setBasicPublicRules(collection)
dbmodels.SetBasicPublicRules(collection)
return collection
}
@@ -37,14 +37,14 @@ func seriesFields() core.FieldsList {
&core.TextField{Name: dbmodels.SERIES_FREQUENCY_FIELD, Required: false},
)
setMusenalmIDField(&fields)
setEditorStateField(&fields)
setNotesAndAnnotationsField(&fields)
dbmodels.SetMusenalmIDField(&fields)
dbmodels.SetEditorStateField(&fields)
dbmodels.SetNotesAndAnnotationsField(&fields)
return fields
}
func seriesIndexes(collection *core.Collection) {
addMusenalmIDIndex(collection)
addIndex(collection, dbmodels.SERIES_TITLE_FIELD, false)
dbmodels.AddMusenalmIDIndex(collection)
dbmodels.AddIndex(collection, dbmodels.SERIES_TITLE_FIELD, false)
}

View File

@@ -32,7 +32,7 @@ func init() {
func entriesTable() *core.Collection {
collection := core.NewBaseCollection(dbmodels.ENTRIES_TABLE)
setBasicPublicRules(collection)
dbmodels.SetBasicPublicRules(collection)
return collection
}
@@ -106,24 +106,24 @@ func entriesFields(app core.App) *core.FieldsList {
&core.JSONField{Name: dbmodels.MUSENALM_DEPRECATED_FIELD, Required: false},
)
setMusenalmIDField(&fields)
setEditorStateField(&fields)
setNotesAndAnnotationsField(&fields)
dbmodels.SetMusenalmIDField(&fields)
dbmodels.SetEditorStateField(&fields)
dbmodels.SetNotesAndAnnotationsField(&fields)
return &fields
}
func entriesIndexes(collection *core.Collection) {
addMusenalmIDIndex(collection)
addIndex(collection, dbmodels.PREFERRED_TITLE_FIELD, false)
addIndex(collection, dbmodels.VARIANT_TITLE_FIELD, false)
addIndex(collection, dbmodels.PARALLEL_TITLE_FIELD, false)
addIndex(collection, dbmodels.TITLE_STMT_FIELD, false)
addIndex(collection, dbmodels.SUBTITLE_STMT_FIELD, false)
addIndex(collection, dbmodels.INCIPIT_STMT_FIELD, false)
addIndex(collection, dbmodels.RESPONSIBILITY_STMT_FIELD, false)
addIndex(collection, dbmodels.PLACE_STMT_FIELD, false)
addIndex(collection, dbmodels.PUBLICATION_STMT_FIELD, false)
addIndex(collection, dbmodels.YEAR_FIELD, false)
addIndex(collection, dbmodels.EDITION_FIELD, false)
dbmodels.AddMusenalmIDIndex(collection)
dbmodels.AddIndex(collection, dbmodels.PREFERRED_TITLE_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.VARIANT_TITLE_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.PARALLEL_TITLE_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.TITLE_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.SUBTITLE_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.INCIPIT_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.RESPONSIBILITY_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.PLACE_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.PUBLICATION_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.YEAR_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.EDITION_FIELD, false)
}

View File

@@ -25,7 +25,7 @@ func init() {
func itemsTable() *core.Collection {
collection := core.NewBaseCollection(dbmodels.ITEMS_TABLE)
setBasicPublicRules(collection)
dbmodels.SetBasicPublicRules(collection)
return collection
}
@@ -59,16 +59,16 @@ func itemsFields(app core.App) core.FieldsList {
&core.URLField{Name: dbmodels.URI_FIELD, Required: false, Presentable: false},
)
setNotesAndAnnotationsField(&fields)
setEditorStateField(&fields)
dbmodels.SetNotesAndAnnotationsField(&fields)
dbmodels.SetEditorStateField(&fields)
return fields
}
func itemsIndexes(collection *core.Collection) {
addIndex(collection, dbmodels.ITEMS_CONDITION_FIELD, false)
addIndex(collection, dbmodels.ITEMS_OWNER_FIELD, false)
addIndex(collection, dbmodels.ITEMS_LOCATION_FIELD, false)
addIndex(collection, dbmodels.ITEMS_IDENTIFIER_FIELD, false)
addIndex(collection, dbmodels.URI_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.ITEMS_CONDITION_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.ITEMS_OWNER_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.ITEMS_LOCATION_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.ITEMS_IDENTIFIER_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.URI_FIELD, false)
}

View File

@@ -8,7 +8,7 @@ import (
func init() {
m.Register(func(app core.App) error {
collection, err := basicRelationCollection(app, dbmodels.ENTRIES_TABLE, dbmodels.SERIES_TABLE, dbmodels.SERIES_RELATIONS)
collection, err := dbmodels.BasicRelationCollection(app, dbmodels.ENTRIES_TABLE, dbmodels.SERIES_TABLE, dbmodels.SERIES_RELATIONS)
if err != nil {
return err
}

View File

@@ -34,7 +34,7 @@ func init() {
func partialsTable() *core.Collection {
collection := core.NewBaseCollection(dbmodels.CONTENTS_TABLE)
setBasicPublicRules(collection)
dbmodels.SetBasicPublicRules(collection)
return collection
}
@@ -114,23 +114,23 @@ func partialsFields(app core.App) *core.FieldsList {
},
)
setMusenalmIDField(&fields)
setEditorStateField(&fields)
setNotesAndAnnotationsField(&fields)
dbmodels.SetMusenalmIDField(&fields)
dbmodels.SetEditorStateField(&fields)
dbmodels.SetNotesAndAnnotationsField(&fields)
return &fields
}
func partialsIndexes(collection *core.Collection) {
addMusenalmIDIndex(collection)
addIndex(collection, dbmodels.PREFERRED_TITLE_FIELD, false)
addIndex(collection, dbmodels.VARIANT_TITLE_FIELD, false)
addIndex(collection, dbmodels.PARALLEL_TITLE_FIELD, false)
addIndex(collection, dbmodels.TITLE_STMT_FIELD, false)
addIndex(collection, dbmodels.SUBTITLE_STMT_FIELD, false)
addIndex(collection, dbmodels.INCIPIT_STMT_FIELD, false)
addIndex(collection, dbmodels.RESPONSIBILITY_STMT_FIELD, false)
addIndex(collection, dbmodels.PLACE_STMT_FIELD, false)
addIndex(collection, dbmodels.PUBLICATION_STMT_FIELD, false)
addIndex(collection, dbmodels.YEAR_FIELD, false)
dbmodels.AddMusenalmIDIndex(collection)
dbmodels.AddIndex(collection, dbmodels.PREFERRED_TITLE_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.VARIANT_TITLE_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.PARALLEL_TITLE_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.TITLE_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.SUBTITLE_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.INCIPIT_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.RESPONSIBILITY_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.PLACE_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.PUBLICATION_STMT_FIELD, false)
dbmodels.AddIndex(collection, dbmodels.YEAR_FIELD, false)
}

View File

@@ -8,7 +8,7 @@ import (
func init() {
m.Register(func(app core.App) error {
collections, err := basicRelationCollection(app, dbmodels.CONTENTS_TABLE, dbmodels.AGENTS_TABLE, dbmodels.AGENT_RELATIONS)
collections, err := dbmodels.BasicRelationCollection(app, dbmodels.CONTENTS_TABLE, dbmodels.AGENTS_TABLE, dbmodels.AGENT_RELATIONS)
if err != nil {
return err
}

View File

@@ -8,7 +8,7 @@ import (
func init() {
m.Register(func(app core.App) error {
collections, err := basicRelationCollection(app, dbmodels.ENTRIES_TABLE, dbmodels.AGENTS_TABLE, dbmodels.AGENT_RELATIONS)
collections, err := dbmodels.BasicRelationCollection(app, dbmodels.ENTRIES_TABLE, dbmodels.AGENTS_TABLE, dbmodels.AGENT_RELATIONS)
if err != nil {
return err
}

View File

@@ -1,65 +0,0 @@
package migrations
import (
"github.com/Theodor-Springmann-Stiftung/musenalm/dbmodels"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/types"
)
func setBasicPublicRules(collection *core.Collection) {
collection.ViewRule = types.Pointer(dbmodels.PUBLIC_VIEW_RULE)
}
func setMusenalmIDField(fieldlist *core.FieldsList) {
fieldlist.Add(&core.TextField{Name: dbmodels.MUSENALMID_FIELD, Max: 64, Required: false})
}
func setEditorStateField(fieldlist *core.FieldsList) {
fieldlist.Add(&core.SelectField{Name: dbmodels.EDITSTATE_FIELD, Required: false, Values: dbmodels.EDITORSTATE_VALUES})
}
func setNotesAndAnnotationsField(fieldlist *core.FieldsList) {
fieldlist.Add(&core.EditorField{Name: dbmodels.ANNOTATION_FIELD, Required: false, ConvertURLs: false})
fieldlist.Add(&core.EditorField{Name: dbmodels.COMMENT_FIELD, Required: false, ConvertURLs: false})
}
func addMusenalmIDIndex(collection *core.Collection) {
addIndex(collection, dbmodels.MUSENALMID_FIELD, true)
}
func addIndex(collection *core.Collection, field string, unique bool) {
name := collection.Name
collection.AddIndex("idx_"+name+"_"+field, unique, field, "")
}
func basicRelationCollection(app core.App, sourcetablename, targettablename string, relations []string) (*core.Collection, error) {
stable, err := app.FindCollectionByNameOrId(sourcetablename)
if err != nil {
return nil, err
}
ttable, err := app.FindCollectionByNameOrId(targettablename)
if err != nil {
return nil, err
}
collection := core.NewBaseCollection(dbmodels.RelationTableName(stable.Name, ttable.Name))
setBasicPublicRules(collection)
fields := core.NewFieldsList(
&core.RelationField{Name: stable.Name, Required: true, CollectionId: stable.Id, MinSelect: 1, MaxSelect: 1},
&core.RelationField{Name: ttable.Name, Required: true, CollectionId: ttable.Id, MinSelect: 1, MaxSelect: 1},
&core.SelectField{Name: dbmodels.RELATION_TYPE_FIELD, Required: true, Values: relations, MaxSelect: 1},
&core.BoolField{Name: dbmodels.RELATION_CONJECTURE_FIELD, Required: false},
&core.BoolField{Name: dbmodels.RELATION_UNCERTAIN_FIELD, Required: false},
)
setNotesAndAnnotationsField(&fields)
collection.Fields = fields
addIndex(collection, stable.Name, false)
addIndex(collection, ttable.Name, false)
addIndex(collection, dbmodels.RELATION_TYPE_FIELD, false)
return collection, nil
}

View File

@@ -5,7 +5,6 @@ import (
"github.com/Theodor-Springmann-Stiftung/musenalm/pagemodels"
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/tools/types"
)
var bilder_fields = core.NewFieldsList(
@@ -70,8 +69,8 @@ func init() {
func bilderCollection() *core.Collection {
c := core.NewBaseCollection(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_BILDER))
c.ListRule = types.Pointer("")
c.ViewRule = types.Pointer("")
c.ListRule = dbmodels.PUBLIC_LIST_RULE
c.ViewRule = dbmodels.PUBLIC_VIEW_RULE
c.Fields = bilder_fields
return c
}
@@ -79,8 +78,8 @@ func bilderCollection() *core.Collection {
func texteCollection() *core.Collection {
c := core.NewBaseCollection(
pagemodels.GeneratePageTableName(pagemodels.P_INDEX_NAME, pagemodels.T_INDEX_TEXTE))
c.ListRule = types.Pointer("")
c.ViewRule = types.Pointer("")
c.ListRule = dbmodels.PUBLIC_LIST_RULE
c.ViewRule = dbmodels.PUBLIC_VIEW_RULE
c.Fields = texte_fields
return c
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,39 @@
<!doctype html>
<html class="w-full h-full" {{ if .lang }}lang="{{ .lang }}"{{ end }}>
<head>
<meta charset="UTF-8" />
{{ block "head" . }}
<!-- Default Head elements -->
{{ end }}
{{ if .isDev }}
<link rel="icon" href="/assets/logo/dev_favicon.png" />
{{ else }}
<link rel="icon" href="/assets/logo/favicon.png" />
{{ end }}
<link href="/assets/css/remixicon.css" rel="stylesheet" />
<script src="/assets/js/alpine.min.js" defer></script>
<script src="/assets/js/htmx.min.js" defer></script>
<script src="/assets/js/htmx-response-targets.js" defer></script>
<script src="/assets/js/client-side-templates.js" defer></script>
<link rel="stylesheet" type="text/css" href="/assets/css/fonts.css" />
<link rel="stylesheet" type="text/css" href="/assets/style.css" />
<script type="module">
import { setup } from "/assets/scripts.js";
setup();
</script>
</head>
<body class="w-full" hx-ext="response-targets" hx-boost="true">
{{ block "body" . }}
<!-- Default app body... -->
{{ end }}
</div>
</body>
</html>