| field | Description |
|---|---|
| as const | Required for TypeScript to infer literal types instead of string[] |
| First argument | The enum name used in dictionary translations |
| Generated type | Access values via ProductStatus["value"] type (e.g., "active" | "soldOut" | ...) |
Required for TypeScript to infer literal types instead of string[]
The enum name used in dictionary translations
Access values via ProductStatus["value"] type (e.g., "active" | "soldOut" | ...)
| method | Description |
|---|---|
| via((field) => ({...})) | Creates a new class with the specified fields. The field() function takes a type and optional options. |
| field(Type, options?) | Type can be: String, Int, Float, Boolean, Date, JSON, ID, [Array], custom classes, or enums. |
| .optional() | Chain .optional() to make a field nullable. The field can then be omitted or set to null. |
Creates a new class with the specified fields. The field() function takes a type and optional options.
Type can be: String, Int, Float, Boolean, Date, JSON, ID, [Array], custom classes, or enums.
Chain .optional() to make a field nullable. The field can then be omitted or set to null.
| field | Description |
|---|---|
| status | Document lifecycle state, controlled by service methods |
| stock | Inventory count, default to 0, modified by sell operations |
| soldCount | Accumulated sales count for analytics |
Document lifecycle state, controlled by service methods
Inventory count, default to 0, modified by sell operations
Accumulated sales count for analytics
| field | Description |
|---|---|
| Performance Benefits | When fetching 100 products for a list, Light ensures only essential fields are transferred, reducing payload size and database load. |
| Field Selection | Choose fields that appear in list UIs: identifiers, status, key metadata. Exclude: content, files, detailed nested objects. |
| Resolve Function | The (resolve) => ({}) function can add computed/virtual fields that are calculated server-side but not stored in DB. |
When fetching 100 products for a list, Light ensures only essential fields are transferred, reducing payload size and database load.
Choose fields that appear in list UIs: identifiers, status, key metadata. Exclude: content, files, detailed nested objects.
The (resolve) => ({}) function can add computed/virtual fields that are calculated server-side but not stored in DB.
| method | Description |
|---|---|
| Static Methods | Static methods operate on arrays of items. Use for filtering, grouping, or computing derived data from lists. |
| Usage Example | const activeProducts = cnst.Product.getActiveList(productList); |
Static methods operate on arrays of items. Use for filtering, grouping, or computing derived data from lists.
const activeProducts = cnst.Product.getActiveList(productList);
| field | Description |
|---|---|
| { $sum: 1 } | Simple count - adds 1 for each matching document |
| { $sum: "$fieldName" } | Sum of field values across all matching documents |
| { $cond: [...] } | Conditional aggregation - count only documents matching a condition |
Simple count - adds 1 for each matching document
Sum of field values across all matching documents
Conditional aggregation - count only documents matching a condition
| field | Description |
|---|---|
| When to use Scalars | Data that belongs to a single parent (1:N embedded). No need for separate queries or references. Data always accessed with parent document. |
| Scalar Location | Scalars are typically placed in __scalar/ directory to indicate they're embedded types, not standalone collections. |
Data that belongs to a single parent (1:N embedded). No need for separate queries or references. Data always accessed with parent document.
Scalars are typically placed in __scalar/ directory to indicate they're embedded types, not standalone collections.
| method | Description | Example |
|---|---|---|
| Scalar Embedding | Embed scalar objects directly in the document. Best for 1:N relationships where child data is always accessed with parent. | |
| Reference by ID | Store only the ObjectID of related document. Best when you need to query the referenced collection separately. | |
| Light Model Reference | Embed a Light version of another model. Best when you need key fields without full population. | |
Embed scalar objects directly in the document. Best for 1:N relationships where child data is always accessed with parent.
Store only the ObjectID of related document. Best when you need to query the referenced collection separately.
Embed a Light version of another model. Best when you need key fields without full population.
| Option | Type | Default | Description | Example |
|---|---|---|---|---|
| default | any | (() => any) | - | Default value when no value is provided. Can be a static value or a function for dynamic defaults. | |
| nullable | boolean | false | If true, the field can be null or omitted. Equivalent to using .optional() chain method. | |
| ref | string | - | Reference collection name for ID fields. Used for MongoDB population. | |
| refPath | string | - | Dynamic reference path - uses another field's value to determine the referenced collection. | |
| refType | "child" | "parent" | "relation" | - | Type of reference relationship. Affects how population and cascading operations work. | |
| type | "email" | "password" | "url" | - | Preset type that applies default validation and example values for common patterns. | |
| fieldType | "property" | "hidden" | "resolve" | "property" | property: normal field, hidden: backend-only (not sent to frontend), resolve: computed field (not stored in DB). | |
| immutable | boolean | false | If true, field cannot be modified after document creation. Only set during create. | |
| min | number | - | Minimum value constraint for Int or Float fields. Validation runs on save. | |
| max | number | - | Maximum value constraint for Int or Float fields. Validation runs on save. | |
| minlength | number | - | Minimum string length constraint for String fields. | |
| maxlength | number | - | Maximum string length constraint for String fields. | |
| enum | EnumClass | - | Restrict field values to enum values. Typically used with enumOf() defined classes. | |
| select | boolean | true | If false, field is excluded from default queries. Use for sensitive or large fields. | |
| accumulate | object | - | MongoDB aggregation expression for Insight fields. Calculates statistics across matched documents. | |
| example | any | - | Example value for API documentation generation (Swagger/OpenAPI). | |
| of | Type | - | Value type for Map fields. The key is always string, value type is specified by 'of'. | |
| validate | (value, model) => boolean | - | Custom validation function. Receives the field value and full model, returns boolean. | |
| text | "search" | "filter" | - | Enable text indexing for full-text search (search) or filtering (filter) capabilities. | |
| meta | object | - | Custom metadata object for additional field information used by UI components or plugins. | |
Default value when no value is provided. Can be a static value or a function for dynamic defaults.
If true, the field can be null or omitted. Equivalent to using .optional() chain method.
Reference collection name for ID fields. Used for MongoDB population.
Dynamic reference path - uses another field's value to determine the referenced collection.
Type of reference relationship. Affects how population and cascading operations work.
Preset type that applies default validation and example values for common patterns.
property: normal field, hidden: backend-only (not sent to frontend), resolve: computed field (not stored in DB).
If true, field cannot be modified after document creation. Only set during create.
Minimum value constraint for Int or Float fields. Validation runs on save.
Maximum value constraint for Int or Float fields. Validation runs on save.
Minimum string length constraint for String fields.
Maximum string length constraint for String fields.
Restrict field values to enum values. Typically used with enumOf() defined classes.
If false, field is excluded from default queries. Use for sensitive or large fields.
MongoDB aggregation expression for Insight fields. Calculates statistics across matched documents.
Example value for API documentation generation (Swagger/OpenAPI).
Value type for Map fields. The key is always string, value type is specified by 'of'.
Custom validation function. Receives the field value and full model, returns boolean.
Enable text indexing for full-text search (search) or filtering (filter) capabilities.
Custom metadata object for additional field information used by UI components or plugins.
| field | Description |
|---|---|
| property | Normal field - stored in DB, sent to frontend |
| hidden | Backend only - stored in DB but never sent to frontend (e.g., OTP codes) |
| resolve | Virtual field - computed on query, not stored in DB (e.g., view count from other collection) |
Normal field - stored in DB, sent to frontend
Backend only - stored in DB but never sent to frontend (e.g., OTP codes)
Virtual field - computed on query, not stored in DB (e.g., view count from other collection)
| field | Description |
|---|---|
| parent | This document belongs to the referenced document |
| child | Referenced document belongs to this document |
| relation | Many-to-many relationship without ownership |
This document belongs to the referenced document
Referenced document belongs to this document
Many-to-many relationship without ownership
| field | Description |
|---|---|
| Class Ordering | Define in order: Enums → Input → Object → Light → Full → Insight. This prevents forward reference issues. |
| Light Field Selection | Include: id, status, key identifiers, timestamps. Exclude: large content fields, files, detailed nested objects. |
| Default Values | Always provide defaults for Object fields. Use functions for dynamic values like dates: () => dayjs() |
| Static Methods | Add static utility methods to Full class for filtering/grouping logic. Keep them pure (no side effects). |
| Import Paths | Always use direct file imports for other constants to avoid circular references. Never import from barrel files. |
Define in order: Enums → Input → Object → Light → Full → Insight. This prevents forward reference issues.
Include: id, status, key identifiers, timestamps. Exclude: large content fields, files, detailed nested objects.
Always provide defaults for Object fields. Use functions for dynamic values like dates: () => dayjs()
Add static utility methods to Full class for filtering/grouping logic. Keep them pure (no side effects).
Always use direct file imports for other constants to avoid circular references. Never import from barrel files.