Database export restrictions description
IMPORTANT: When using schema-dsl to export the database schema feature, please read this document carefully to understand which features can be exported and which are not supported.
core principles
schema-dsl's database export function follows the following principles:
- ✅ Static structure first: Only export fixed, static Schema definitions
- ❌ Dynamic logic is not exported: Runtime conditional logic, dynamic calculations, etc. cannot be converted into database constraints
- ⚠️ Limited constraint mapping: The database’s native constraint capabilities are limited, and some advanced constraints will be ignored or simplified
- 🎯 Type mapping is the main: Mainly focus on type definition and basic constraints (length, range, required, etc.)
Exported features are not supported
The following schema-dsl attributes cannot be exported to a database schema (will be ignored):
1. Conditional validation logic ❌
s.match() - conditional field mapping
Cause: The database does not support the dynamic constraint of "determine B field type based on A field value".
Alternatives:
- Export as loosest type (
VARCHAR(255)) - Validation logic remains at the application layer (using the schema-dsl validator)
s.if() - Conditional validation
Cause: Same as above, the database does not support conditional constraints.
2. Complex JSON Schema keywords ❌
The following JSON Schema advanced features cannot be exported:
Example:
Current Behavior:
ipv4 | ipv6All branches of this type are ultimately mapped to unions of the same SQL column type and can still be exportedstring | numberThis type will fall into the union of different SQL column types. The MySQL / PostgreSQL exporter will explicitly throw an error instead of silently fetching the first item
3. Custom validator ❌
Cause: The database cannot execute the JavaScript function.
Alternatives:
- Use
patternregular expression if expressible - Validation logic remains in the application layer
4. Customize error messages ❌
Export Behavior:
- ✅
label()will be exported asCOMMENT(MySQL/PostgreSQL) - ❌
messages()will be ignored (the database does not store error messages)
5. Depth constraints of nested objects ⚠️
Export Behavior:
- MongoDB: ✅ Full support for nested validation
- MySQL: ❌ exported as
JSONtype, internal constraints are lost - PostgreSQL: ❌ exported as
JSONBtype, internal constraints lost
Partially supported features
The following features are supported to varying degrees in different databases:
1. Regular expression constraints ⚠️
Note: MySQL and PostgreSQL do not have native regular constraints and need to be validated at the application layer.
2. Numerical range constraints ⚠️
3. String length constraint ⚠️
4. Enumeration constraints ⚠️
5. Array constraints ⚠️
Fully supported features
The following properties export well in all databases:
✅ Basic type
All databases support type mapping.
✅Required constraints
Export as:
- MongoDB:
required: ['email'] - MySQL/PostgreSQL:
NOT NULL/NULL
✅ Default value (MySQL/PostgreSQL only)
Export as:
- MongoDB: ❌ Not supported
default - MySQL/PostgreSQL: ✅
DEFAULT false
✅ Field description
Export as:
- MongoDB:
description: "User login name" - MySQL:
COMMENT 'User login name' - PostgreSQL:
COMMENT ON COLUMN... IS 'User login name'
Database specific restrictions
MongoDB
MySQL
PostgreSQL
Best practice recommendations
1. Hierarchical validation strategy 🎯
in principle:
- Database: The last line of defense against data corruption
- Application layer: complete business logic validation
2. Clarify expectations before exporting 📋
Before using the export function, please check whether the Schema contains unsupported features:
3. Use descriptions to illustrate constraints 📝
For constraints that cannot be exported, use description() to leave a description in the database:
Export as:
4. Keep the complete Schema definition 💾
5. Documented incompatible features 📖
Clearly state in the project documentation which validation logic does not take effect at the database layer:
FAQ
Q1: Why can't s.match() be exported?
A: The database does not support the dynamic constraint of "determining the type of field B based on the value of field A". The database schema is fixed when it is created and cannot be changed at runtime.
Solution:
- Export to the loosest type (e.g.
VARCHAR(255)) - Application layer uses full Schema validation
Q2: MySQL does not support regular expressions, what should I do?
A: MySQL's CHECK constraint does not support regular expressions.
Solution:
- Application layer authentication (recommended)
- Use triggers (not recommended, complex and difficult to maintain)
- Specify constraint rules in
COMMENT
Q3: Constraints lost after nested objects are exported?
A: MySQL/PostgreSQL exports nested objects as JSON/JSONB types, and internal constraints cannot be expressed.
Solution:
- MongoDB: Full support for nested validation
- MySQL/PostgreSQL: Application Layer Authentication
Q4: How to check whether the Schema is suitable for export?
A: The following features are not suitable for export:
Features suitable for export:
Summarize
Related documents
Corresponding sample file
Example entry: export-limitations.ts Description: Shows the division of labor between "complete application layer schema" and "database export-specific simplified schema", as well as the implementation results of the three types of exporters on static schemas.