Saving to Database
Saving on every keystroke is expensive and unstable. Use `onDebouncedUpdate` for persistence.Why debounced save
- Reduces database write frequency.
- Avoids race conditions in distributed systems.
- Preserves responsive typing experience.
Example integration
import { HybricEditor } from 'hybricmark'
async function saveToAPI(json: unknown) {
const res = await fetch('/api/docs/save', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ document: json }),
})
if (!res.ok) {
throw new Error('Failed to save document')
}
}
export function DocumentEditor({ initialDoc }: { initialDoc: unknown }) {
return (
<HybricEditor
content={initialDoc}
debounceMs={800}
onDebouncedUpdate={({ editor }) => {
void saveToAPI(editor.getJSON())
}}
/>
)
}Operational advice
- Start at `debounceMs=800` and tune with real traffic.
- Include `updatedAt` and revision/version fields in payload.
- Keep saves idempotent (`upsert` with revision checks).
- Add retry logic for transient network failures.
HTML vs JSON storage
| Format | Pros | Cons | Recommended use |
|---|---|---|---|
| HTML | Easy direct rendering in web views. | Weak block identity, hard to patch by block. | Read-only rendering/export snapshots. |
| JSON | Stable structure, preserves block IDs and semantic nodes. | Needs renderer/transformer for non-editor channels. | Primary database format for product features. |
For HybricMark products, persist JSON as source of truth, and derive HTML only when needed.