Skip to content

Schema

Database schema examples

Generate string field schema

1
2
3
4
5
6
firstName: <ISchemaProperty>{
    __type: EType.string,
    validations: <IPropertyValidation>{
        required: true
    }
}

Generate number field schema

1
2
3
4
5
6
EmployyeNo: <ISchemaProperty>{
    __type: EType.number,
    validations: <IPropertyValidation>{
        required: true
    }
}

Generate boolean field schema

1
2
3
4
5
6
isActive: <ISchemaProperty>{
    __type: EType.boolean,
    validations: <IPropertyValidation>{
        required: true
    }
}

Generate objectId field schema"

1
2
3
4
5
6
department: [
    EType.objectId,
    validations: <IPropertyValidation>{
        required: true
    }
]

All in one data types

firstName: <ISchemaProperty>{
    __type: EType.string,
    validations: <IPropertyValidation>{
        required: true
    }
},
LastName: <ISchemaProperty>{
    __type: EType.string
},
EmployyeNo: <ISchemaProperty>{
    __type: EType.number,
    validations: <IPropertyValidation>{
        required: true
    }
},
isActive: <ISchemaProperty>{
    __type: EType.boolean,
    validations: <IPropertyValidation>{
        required: true
    }
},
department: [
    EType.objectId,
    validations: <IPropertyValidation>{
        required: true
    }
]

min & max validation

1
2
3
4
5
6
7
8
registration_number: <ISchemaProperty>{
    __type: EType.number,
    validations: <IPropertyValidation>{
        required: true,
        min: 4,
        max: 10
    }
}

minLength & maxLength validation

1
2
3
4
5
6
7
8
address: <ISchemaProperty>{
    __type: EType.string,
    validations: <IPropertyValidation>{
        required: true,
        minLength: 20,
        maxLength: 100
    }
}

unique validation

1
2
3
4
5
6
7
phone: <ISchemaProperty>{
    __type: EType.number,
    validations: <IPropertyValidation>{
        required: true,
        unique: true
    }
}

validatorFun

city_name: <ISchemaProperty>{
    __type: EType.string,
    validations: <IPropertyValidation>{
        required: true,
        validatorFun: (city_name, fullObj) => {
            if (city_name && ['AHMEDABAD', 'SURAT'].includes(city_name)) throw new Error("You can not save city_name as '"+ city_name+ "'");
            else return true;
        }
    }
}

conversions

city_name: <ISchemaProperty>{
    __type: EType.string,
    conversions: <IPropertyConversion> {
        conversionFun: (city_name, fullObj) => {
            if (city_name) return (city_name + "_IND");
            else return (city_name);
        },
        trimStart: true,
        trimEnd: true
    },
    validations: <IPropertyValidation>{
        required: true,
    }
}

isPrimaryKey

1
2
3
4
5
6
7
customer_id: <ISchemaProperty>{
    __type: EType.number,
    isPrimaryKey: true,
    validations: <IPropertyValidation>{
        required: true,
    }
}
  • To understand Schema in more details click here.