serialize.go
package query
import (
"encoding/json"
"github.com/readysite/readysite/website/models"
)
// DocumentToRecord converts a Document to a flat map for API response.
func DocumentToRecord(doc *models.Document, collection *models.Collection) map[string]any {
record := map[string]any{
"id": doc.ID,
"collectionId": doc.CollectionID,
"collectionName": collection.Name,
"created": doc.CreatedAt.Format("2006-01-02T15:04:05Z"),
"updated": doc.UpdatedAt.Format("2006-01-02T15:04:05Z"),
}
// Flatten data fields into record
if doc.Data != "" {
var data map[string]any
if err := json.Unmarshal([]byte(doc.Data), &data); err == nil {
for k, v := range data {
record[k] = v
}
}
}
return record
}
// ParseDocumentData parses a document's JSON data into a map.
// Returns an empty map if the document has no data or parsing fails.
func ParseDocumentData(doc *models.Document) map[string]any {
data := make(map[string]any)
if doc.Data != "" {
json.Unmarshal([]byte(doc.Data), &data)
}
return data
}