Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Attachment ¶ added in v0.9.0
type Attachment struct {
StoragePath string `json:"storage_path" validate:"required,max=1024"`
Mime string `json:"mime" validate:"required,max=100"`
Size int64 `json:"size" validate:"required,min=1"`
SHA256 string `json:"sha256,omitempty" validate:"omitempty,len=64"`
}
Attachment is an embeddable document field that references a single file stored via a den.Storage backend.
Typical use: embed directly when the containing document IS a file, or add it as a named field when a document HAS one or more files.
// IS a file — Attachment embedded
type Media struct {
document.Base
document.Attachment
AltText string
}
// HAS files — Attachment as named fields
type Product struct {
document.Base
Hero document.Attachment
Thumbnail document.Attachment
}
The fields are populated by den.Storage.Store and are never edited by hand afterwards — StoragePath, SHA256, and Size are intrinsic to the stored bytes. Mime may be overridden by application code if the detected type is wrong, but changing it does not re-hash or move the file.
When a document that embeds or contains an Attachment is hard-deleted, Den asks the configured Storage to remove the referenced bytes.
func (Attachment) IsZero ¶ added in v0.9.0
func (a Attachment) IsZero() bool
IsZero reports whether the Attachment has no content — StoragePath unset and Size zero. Used to distinguish "no file attached yet" from a pointer to a real file when the field is embedded by value.
type Base ¶
type Base struct {
ID string `json:"_id"`
CreatedAt time.Time `json:"_created_at"`
UpdatedAt time.Time `json:"_updated_at"`
Rev string `json:"_rev,omitempty"`
}
Base provides the standard fields every Den document carries: ID, creation and update timestamps, and an optional revision token for optimistic concurrency control. Embed this in your document structs.
Combine with document.SoftDelete for soft-delete support and/or document.Tracked for change tracking. The three embeds are orthogonal — pick any subset.
type SoftDelete ¶ added in v0.8.0
type SoftDelete struct {
DeletedAt *time.Time `json:"_deleted_at,omitempty"`
DeletedBy string `json:"_deleted_by,omitempty"`
DeleteReason string `json:"_delete_reason,omitempty"`
}
SoftDelete adds a `_deleted_at` timestamp to a document. Embed it alongside Base to opt into soft-delete semantics:
type Article struct {
document.Base
document.SoftDelete
Title string `json:"title"`
}
When a document with SoftDelete embedded is Delete()d, Den records the deletion timestamp instead of removing the row. QuerySet auto-filters rows with DeletedAt set unless IncludeDeleted() is used. HardDelete() bypasses the soft path and physically removes the row.
Soft-delete participates in revision control: on a document that also opts into UseRevision, Delete verifies and bumps _rev like Update so concurrent writers holding the pre-delete revision fail with ErrRevisionConflict instead of silently clobbering DeletedAt.
DeletedBy and DeleteReason are optional audit fields populated by the SoftDeleteBy and SoftDeleteReason CRUDOptions. Both default to empty so existing data stays compatible; empty values omit from the encoded JSON.
Den detects soft-delete support structurally via the `_deleted_at` JSON field — any type carrying that field (through this embed or otherwise) participates in soft-delete handling.
func (SoftDelete) IsDeleted ¶ added in v0.8.0
func (s SoftDelete) IsDeleted() bool
IsDeleted reports whether the document has been soft-deleted.
type Trackable ¶
Trackable is implemented by documents that carry a byte-level snapshot of their last-saved state. Embed document.Tracked to satisfy this interface; den uses it to populate the snapshot after a load and to detect changes (IsChanged, GetChanges, Revert).
type Tracked ¶ added in v0.8.0
type Tracked struct {
// contains filtered or unexported fields
}
Tracked adds change-tracking support to a document. Embed it alongside Base to opt in:
type Article struct {
document.Base
document.Tracked
Title string `json:"title"`
}
Tracked satisfies the Trackable interface so Den populates the snapshot byte slice after every load/save. Callers can then use IsChanged, GetChanges, and Revert to inspect or undo in-memory modifications.
The snapshot is not serialized — it lives only on the in-memory struct and is restored on the next load.
func (*Tracked) SetSnapshot ¶ added in v0.8.0
SetSnapshot stores the raw JSON bytes for later change detection. Called by Den after a load/save.