DocEngine

Docs

Storage & versions

DocEngine separates template design (layout + schemas + mapping) from filled documents (values). Hosts choose how to persist JSON; Salesforce adds immutable template versions so completed docs keep the layout they were filled against.

Principles

  1. Template JSON — exportTemplate(): kind: "template", blocks, fieldSchemas, optional fieldMapping, page setup. Values reset to defaults. This is also your config — do not keep a parallel schema object beside it.
  2. Values JSON — exportFields(): kind: "field", usually version: 2 with nested sections (section name → field name). Small enough to store and re-merge onto a pinned template. Do not key by internal field ids or embed full blocks.
  3. Full document — exportDoc(): kind: "document" when you need a self-contained snapshot (blocks + values). Prefer template + values for long-lived storage.
  4. Pin layout on fill — when a user starts or completes a document, bind it to a specific template version so later design edits don’t rewrite history.

Common poor shapes (filled templates, id-keyed values, off-template Mapping) are called out on the Builder & integrator guide.

Salesforce model (versioned)

Production pattern in the managed package:

  • DocEngine_Template__c — mutable head: name, object API, denormalized current Template_JSON__c, Version__c, lookup Current_Version__c.
  • DocEngine_Template_Version__c — immutable snapshots: Version__c + frozen Template_JSON__c. Each Save in the builder inserts a new version and updates Current; old rows are never overwritten.
  • DocEngine_Document__c — filled instance: lookup Template + pinned Template_Version__c, Record_Id__c, Document_JSON__c (values-only preferred), status Draft/Completed.
  • PDF/HTML files — ContentVersion linked to the document and source record (not stored inside the Long Text Area).

First save of a document pins the current version (lazy snapshot if needed). Later saves keep that pin so reopen/PDF use the same layout and Mapping.

Self-hosted website

MVP storage is a local JSON database (data/db.json):

  • Templates — one row per template; data holds the latest template export (including Mapping). No separate version table yet.
  • Documents — optional drafts with templateId + data.

For production parity with Salesforce, add immutable template versions (or content-addressed snapshots) and pin them on documents and API render jobs so Mapping/layout can’t drift under a live token.

IO format versions

  • Template / document exports carry version (IO format; fields export v2 = nested sections).
  • fieldMapping uses kind: "fieldMapping", version: 1.
  • Hosts should treat unknown future IO versions carefully; prefer values-only + pinned template over mutating historical full documents.

Related: Mapping · API & hosts · Field types