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;
        }
    }
}

conversionFun example 1

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,
    }
}

conversionFun example 2

updatedAt: {
    __type: EType.date,
        conversions: {
        conversionFun: (val, data) => {
            // Every time generate new value on update or save
            return new Date();

            // If it is present in payload, take that value and set in db.
            // if (data.updatedAt) return data.updatedAt;
            // else return new Date();
        },
    }
}

defaults [v1.11+]

active: {
    __type: EType.boolean,
        conversions: {
        defaults: {
            defaultValue: true,
            shouldReplaceEmptyStringWithDefault: true,
            shouldReplaceNullWithDefault: true,
        }
    }
},

defaultFun [v1.11+]

createdAt: {
    __type: EType.date,
        conversions: {
        defaults: {
            defaultFun: () => {
                return new Date();
            },
        }
    }
},

isPrimaryKey

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

enum

1
2
3
4
5
6
country: {
    __type: EType.string,
    validations: {
        enum: ['India', 'Africa']
    }
},
  • To understand Schema in more details click here.