adds db seed command

This commit is contained in:
Christian Beutel
2024-03-10 02:58:51 +01:00
parent 01e1053569
commit 4b8e740730
9 changed files with 29 additions and 3 deletions

View File

@@ -13,6 +13,7 @@ COPY *.go ./
RUN go build -o /pocketbase
RUN /pocketbase migrate
RUN /pocketbase seed
ENV MEILI_URL=http://localhost:7700
ENV MEILI_MASTER_KEY=

View File

@@ -4,12 +4,16 @@ import (
"errors"
"log"
"os"
"strings"
"github.com/meilisearch/meilisearch-go"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/forms"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/plugins/migratecmd"
"github.com/pocketbase/pocketbase/tools/filesystem"
"github.com/spf13/cobra"
_ "pocketbase/migrations"
)
@@ -51,6 +55,26 @@ func main() {
Automigrate: true,
})
app.RootCmd.AddCommand(&cobra.Command{
Use: "seed",
Run: func(cmd *cobra.Command, args []string) {
collection, _ := app.Dao().FindCollectionByNameOrId("categories")
categories := []string{"Hiking", "Walking", "Climbing", "Skiing", "Canoeing", "Biking"}
for _, element := range categories {
record := models.NewRecord(collection)
form := forms.NewRecordUpsert(app, record)
form.LoadData(map[string]any{
"name": element,
})
f, _ := filesystem.NewFileFromPath("migrations/initial_data/" + strings.ToLower(element) + ".jpg")
form.AddFiles("img", f)
form.Submit()
}
},
})
client := meilisearch.NewClient(meilisearch.ClientConfig{
Host: os.Getenv("MEILI_URL"),
APIKey: os.Getenv("MEILI_MASTER_KEY"),

Binary file not shown.

After

Width:  |  Height:  |  Size: 573 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 664 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 453 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 432 KiB

View File

@@ -43,9 +43,10 @@ services:
web:
build: ./web
environment:
PUBLIC_MEILISEARCH_URL: http://localhost:7700
PUBLIC_MEILISEARCH_API_TOKEN:
PUBLIC_POCKETBASE_URL: http://127.0.0.1:8090
ORIGIN: http://localhost:8080
PUBLIC_MEILISEARCH_URL: http://search:7700
PUBLIC_MEILISEARCH_API_TOKEN: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhcGlLZXlVaWQiOiIyMjk3NDg3Yy0zMjFlLTQ5MmEtYmRiNS1iZmYwZGYzZjc2NTYiLCJzZWFyY2hSdWxlcyI6eyJ0cmFpbHMiOnsiZmlsdGVyIjoicHVibGljPXRydWUifSwiY2l0aWVzNTAwIjp7fX0sImV4cCI6bnVsbH0.CuIlkiWAec8TGyZI5X37JTjQvcnvFAFZAuA4xpbXJ8o
PUBLIC_POCKETBASE_URL: http://db:8090
ports:
- "8080:3000"
networks: