Overview
The Document system is the foundation of data management in Loopar. Documents represent data models with automatic database mapping, validation, lifecycle hooks, and field management. Every data entity in Loopar extends from the Document base classes.Document Hierarchy
Loopar documents follow a three-tier inheritance structure:- CoreDocument: Core field management, validation, and lifecycle methods
- BaseDocument: List views, filtering, and query building
- Custom Document: Your business logic and custom methods
CoreDocument
TheCoreDocument class provides the fundamental document functionality:
packages/loopar/core/document/core-document.js
Key Features
Dynamic Fields
Fields are automatically created from entity metadata
Validation
Built-in validation for all field types
Lifecycle Hooks
onLoad, validate, save, delete hooks
Child Tables
Support for parent-child relationships
BaseDocument
TheBaseDocument extends CoreDocument with querying and list functionality:
packages/loopar/core/document/base-document.js
Creating Documents
There are two ways to work with documents:Creating New Documents
- New Document
- Get Existing
Using the Document API
packages/loopar/core/loopar/document.js
Field Management
Fields are dynamically created based on the entity’sdoc_structure:
Fields are created as JavaScript properties with getters and setters, allowing natural property access like
user.email = '[email protected]'.Lifecycle Hooks
Documents support various lifecycle hooks:Available Hooks
onLoad()
onLoad()
Called after document initialization. Use for setup logic:
validate()
validate()
Called before saving. Add custom validation:
save()
save()
Saves document to database with validation:
delete()
delete()
Deletes document with connection checking:
Validation
Documents include automatic field validation based on field types:Child Documents
Support for parent-child table relationships:Querying Documents
List with Filters
Building Conditions
Document History
Automatic tracking of document changes:Custom Documents
Create custom document classes for specific entities:Best Practices
Document Parsing Utility
TheparseDocument function processes document data and renders markdown fields based on the document structure definition.
- Reads the document structure from the entity configuration
- Identifies fields with
MARKDOWN_INPUTelement type - Renders markdown content to HTML using the markdown renderer
- Returns the processed document
Next Steps
Controllers
Learn how controllers interact with documents
Components
Build forms for document data entry