Schemas: Creating a Schema

Creating a schema for your models..

🪶 We've reached a cool part, well as described in the Mongoose guide, in MerlinDB everything also starts with a Schema, even MerlinDB was also based on Mongoose.

First I will show a complete example with all the Schema properties and then I will explain each one:

1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.min.js' just call 'new MerlinDB()';
  • 3
    4
  • var
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5
    6
  • const
  • ClientSchema = merlin.Schema({
  • //Or Schema(<your-Schema>)
  • 7  
  • name:
  • {
  • 8      
  • type:
  • [
  • String
  • ],
    9      
  • unique:
  • true
  • ,
    10      
  • required:
  • [
  • true
  • , 'name is required!' ],
    11      
  • maxLength:
  • 100
  • ,
    12      
  • minLength:
  • [
  • 3
  • , '
  • {
  • VALUE
  • }
  • must have at least three characters!' ],
    13  
  • }
  • ,
    14  
  • age:
  • {
  • 15      
  • type:
  • [
  • Number
  • ],
    16      
  • min:
  • [
  • 3
  • , 'The minimum for age is
  • 3
  • ' ],
    17      
  • max:
  • [
  • 65
  • , '
  • {
  • VALUE
  • }
  • needs to be less than
  • 65
  • year old!' ]
    18  
  • }
  • ,
    19  
  • streets:
  • Object
  • ,
    20  
  • email:
  • {
  • 21      
  • type:
  • String
  • ,
    22      
  • unique:
  • [
  • true
  • , "'
  • {
  • VALUE
  • }
  • ' email, already exists man!" ],
    23      
  • required:
  • true
  • ,
    24      
  • validateEmail:
  • [
  • true
  • , "Invalid email!",
  • /*  /[a-z]/g  */
  • ]
    25  
  • }
  • ,
    26  
  • drink:
  • {
  • 27      
  • type:
  • String
  • ,
    28      
  • required:
  • true
  • ,
    29      
  • enum:
  • {
  • 30        
  • values:
  • [ 'whiskey', 'vodka' ],
    31        
  • message:
  • 'We dont have a
  • {
  • VALUE
  • }
  • in this bar!'
    32      
  • }
  • 33  
  • }
  • ,
    34  
  • cardNumber:
  • {
  • 35      
  • type:
  • [
  • Array
  • ],
    36      
  • encrypt:
  • {
  • 37        
  • //This parameter must be passed in decrypt if you are going to use it
  • 38        
  • strength:
  • "medium"
    39      
  • }
  • 40  
  • }
  • ,
    41  
  • hobbies:
  • {
  • 42      
  • type:
  • [
  • Array
  • ],
    43      
  • minList:
  • [
  • 4
  • , 'Min
  • 4
  • hobbies!' ],
    44      
  • maxList:
  • 10
  • ,
    45  
  • }
  • 46  
  • city:
  • [
  • String
  • ,
  • Array
  • ],
    47  
  • birth:
  • {
  • 48      
  • type:
  • [ [
  • Date
  • ], 'Invalid BirthDate man' ],
    49      
  • required:
  • function (
  • )
  • {
  • 50         this.$message = "Too young, your birth is required!"
    51         return this.age <
  • 18
  • ;
    52      
  • }
  • ,
    53  
  • }
  • ,
    54  
  • phone:
  • {
  • 55      
  • type:
  • String
  • ,
    56      
  • validate:
  • {
  • 57        
  • validator:
  • function (
  • v
  • )
  • {
  • 58            return /\d
  • {
  • 3
  • }
  • -\d
  • {
  • 3
  • }
  • -\d
  • {
  • 4
  • }
  • $/
  • .test(
  • v
  • )
  • ;
    59        
  • }
  • ,
    60        
  • //message: '{VALUE} is not a number',
  • 61        
  • //message: props => `${props.value} is not a valid phone number!`
  • 62      
  • }
  • 63  
  • }
  • ,
    64
  • }
  • )
  • Let's go, first let's start with the types, see the following post: