Schemas: Required

Ensuring that a field is mandatory in the MerlinDB schema.

🪶 This property is used to ensure that a field is mandatory.

1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.min.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "MY-STORE-DATABASE"
  • )
  • ;
    6
    7
  • const
  • ClientSchema = Schema({
  • //Or merlin.Schema(<your-Schema>)
  • 8  
  • name:
  • {
  • type:
  • String
  • ,
  • required:
  • true
  • }
  • ,
    9  
  • //Or defining a message
  • 10  
  • name:
  • {
  • 11      
  • type:
  • String
  • ,
    12      
  • required:
  • [
  • true
  • , 'Name is required!' ]
    13  
  • }
  • ,
    14  
  • //Or doing validations
  • 15  
  • name:
  • {
  • 16      
  • type:
  • String
  • ,
    17      
  • required:
  • function (
  • )
  • {
  • 18         this.$message = "Too young, your birth is required!"
    19         return this.age <
  • 18
  • ;
    20      
  • }
  • ,
    21  
  • }
  • ,
    22    
    23
  • }
  • )
  • ;
    24
    25
  • var
  • clientModel = merlin
  • .model(
  • "Users", ClientSchema
  • )
  • ;
    26
    27
  • //Inserting multiple data
  • 28clientModel
  • .insert(
  • [
    29   {},
  • //name is required here
  • 30  
  • {
  • name:
  • "Michelle"
  • }
  • ,
    31]
  • )
  • .then(
  • e =>
  • {
  • 32  
  • /*  OK...  */
  • 33
  • }
  • )
  • .catch(
  • e =>
  • {
  • 34   console
  • .log(
  • e
  • )
  • ;
    35  
  • //returns Name is required!
  • 36
  • }
  • )
  • ;