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

  • It is done by API Maker so in case of concurrent insert/updates it will not work.
  • Please create unique index at database level to get perfect unique result, and do not use this 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']
    }
},

Validation Examples

Validate field with required
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            required: true
        }
    }
Validate field with min
1
2
3
4
5
6
    pincode: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            min: 6
        }
    }
Validate field with max
1
2
3
4
5
6
    salary: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            max: 9
        }
    }
Validate field with minLength
1
2
3
4
5
6
    firstName: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            minLength: 3
        }
    }
Validate field with maxLength
1
2
3
4
5
6
    firstName: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            maxLength: 15
        }
    }
Validate field with unique
1
2
3
4
5
6
    firstName: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            unique: true
        }
    }
Validate field with email
1
2
3
4
5
6
    mailId: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            email: true
        }
    }
Validate field with validatorFun
    country_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            validatorFun: <any><IFunctionJSON> {
                hash: '12345645616148',
                code: `(str) => {
                   if (str && ['AHMEDABAD', 'SURAT'].includes(str)) throw new Error("You can not save country_name as '"+ str+ "'");
                   else return true;
                }`,
           }
        }
    }
Multiple validation in single field
1
2
3
4
5
6
7
8
9
    _id: EType.objectId,
    first_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            required: true,
            minLength: 3,
            maxLength: 15
        }
    }
All in one
    _id: EType.objectId,
    first_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            required: true
        }
    },
    area_pincode: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            min: 6
        }
    },
    monthly_salary: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            max: 9
        }
    },
    last_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            minLength: 3
        }
    },
    last_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            maxLength: 15
        }
    },
    mobile_no: <ISchemaProperty>{
        __type: EType.number,
        validations: <IPropertyValidation>{
            unique: true
        }
    },
    mail_Id: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            email: true
        }
    },
    country_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            validatorFun: <any><IFunctionJSON> {
                hash: '12345645616148',
                code: `(str) => {
                   if (str && ['AHMEDABAD', 'SURAT'].includes(str)) throw new Error("You can not save country_name as '"+ str+ "'");
                   else return true;
                }`,
           }
        }
    }

Conversion Examples

trimStart
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversion: <IPropertyConversion>{
            trimStart: true
        }
    }
trimEnd
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversion: <IPropertyConversion>{
            trimEnd: true
        }
    }
trim
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversion: <IPropertyConversion>{
            trim: true
        }
    }
toLowerCase
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversion: <IPropertyConversion>{
            toLowerCase: true
        }
    }
toUpperCase
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversion: <IPropertyConversion>{
            toUpperCase: true
        }
    }
conversionFun
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversions: <IPropertyConversion>{
            conversionFun: (firstName, fullObj) => {
                if (firstName) return (firstName + "_IND");
                else return (firstName);
            },
        }
    }
encryption
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversion: <IPropertyConversion>{
            encryption: true
        }
    }
hashing
1
2
3
4
5
6
7
    _id: EType.objectId,
    firstName: <ISchemaProperty>{
        __type: EType.string,
        conversion: <IPropertyConversion>{
            hashing: true
        }
    }
  • To understand Schema in more details click here.