Register a schema for an entity and every write is validated before it lands. MQDB checks types (string, number, boolean, array, object), enforces required fields, and fills in any default values you declare.
db.addSchema(entity, { fields: [...] }) registers a field list. Each field has name, type, optional required, optional default.create/update, MQDB walks the fields: required-but-missing → reject; wrong type → reject; missing-with-default → fill the default.UNIQUE or FOREIGN KEY rules run after schema validation passes.addSchema again with the new list; it replaces the previous one.
flowchart TD
W["db.create(entity, data)"] --> S{schema?}
S -- no schema --> OK[write]
S -- has schema --> V{per-field
validate}
V -- required missing --> R1[reject]
V -- wrong type --> R2[reject]
V -- missing
(has default) --> F[fill default] --> OK
V -- all present,
types ok --> OK
style R1 fill:#ffcdd2,stroke:#c62828
style R2 fill:#ffcdd2,stroke:#c62828
style OK fill:#c8e6c9,stroke:#2e7d32
style F fill:#fff3e0,stroke:#ef6c00
name, email, age, active). Click Add schema.+ Add field (e.g. role, type string, default 'user'), click Add schema again, then re-run the Default value preset — the record gets role: 'user' automatically.Run all tests to see the 5 automated checks.Each preset uses the users schema above. Run them one after another to see validator behaviour.
{ name: "Alice", email: "alice@example.com", age: 30 }
{ name: "Bob", email: "bob@x.com", age: "thirty" }
{ name: "Charlie" } // no email
{ name: "Diana", email: "diana@x.com" } // → active=true
No schemas yet.
Run all tests creates a test_users schema and runs these 5 steps:
age as string) is rejectedemail) is rejectedgetSchema returns the exact 4-field definition