pcgamedb/db/export.go
nite07 304517e241
Some checks failed
release / goreleaser (push) Failing after 2m23s
docker / prepare-and-build (push) Failing after 2m55s
trans to private
2024-11-20 19:09:04 +08:00

43 lines
892 B
Go

package db
import (
"context"
"encoding/json"
"time"
"pcgamedb/model"
"go.mongodb.org/mongo-driver/bson"
)
func Export() ([]byte, []byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
infos := []model.GameInfo{}
games := []model.GameItem{}
cursor, err := GameInfoCollection.Find(ctx, bson.M{})
if err != nil {
return nil, nil, err
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &infos); err != nil {
return nil, nil, err
}
cursor, err = GameItemCollection.Find(ctx, bson.M{})
if err != nil {
return nil, nil, err
}
defer cursor.Close(ctx)
if err = cursor.All(ctx, &games); err != nil {
return nil, nil, err
}
infoJson, err := json.Marshal(infos)
if err != nil {
return nil, nil, err
}
gameJson, err := json.Marshal(games)
if err != nil {
return nil, nil, err
}
return infoJson, gameJson, nil
}