Skip to content

Table/Collection schema

  • Schema is generated as per the collection data.

Supported schema properties

__type

API Maker schema supports the below data types.

  • boolean
  • date
  • file
  • files
  • number
  • objectId
  • string
let schema: ISchemaType = {
    first_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            required: true
        }
    },
    last_update: <ISchemaProperty>{
        __type: EType.date,
        validations: <IPropertyValidation>{
            required: true
        }
    },
    phone: <ISchemaProperty>{
        __type: EType.number,
        validations: <IPropertyValidation>{
            required: true
        }
    }
}

validations

Supported validations properties

  • required
  • email
  • max
  • min
  • minLength
  • maxLength
  • unique
  • validatorFun
  • enum [v1.11+]

required

  • It has boolean value true/false.
  • If 'required: true' is not present in validations, by default, it's 'required: false'.
1
2
3
4
5
6
7
8
let schema: ISchemaType = {
    first_name: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            required: true
        }
    }
}

email

  • It has been a boolean value true/false.
  • If it has true value then it's validate field value for email.
  • If 'email: true' is not present in validations, by default, it's 'email: false'.
1
2
3
4
5
6
7
8
9
let schema: ISchemaType = {
    mail_id: <ISchemaProperty>{
        __type: EType.string,
        validations: <IPropertyValidation>{
            required: true,
            email: true
        }
    }
}

min & max

  • min and max validations are applied on the number column type.
1
2
3
4
5
6
7
8
registration_number: <ISchemaProperty>{
    __type: EType.number,
    validations: <IPropertyValidation>{
        required: true,
        min: 4,
        max: 10
    }
}

minLength & maxLength

  • minLength and maxLength validations are applied to the string column type.
1
2
3
4
5
6
7
8
address: <ISchemaProperty>{
    __type: EType.string,
    validations: <IPropertyValidation>{
        required: true,
        minLength: 20,
        maxLength: 100
    }
}

unique

  • If any field has 'unique: true' validation it can not save with duplicate value.
1
2
3
4
5
6
7
phone: <ISchemaProperty>{
    __type: EType.number,
    validations: <IPropertyValidation>{
        required: true,
        unique: true
    }
}
  • Request payload has three objects in which the last object phone number is not unique. All the other objects were not saved too in both 'save single | multiple' and 'master save | update' APIs.

Request payload

[
    {
        "customer_id": 94729,
        "first_name": "Elva",
        "last_name": "Giles",
        "last_update": "2023-07-26T04:16:57.402Z",
        "phone": "9321735955",
        "pincode": 9393,
        "shipping_id": 30478
    },
    {
        "customer_id": 24573,
        "first_name": "Sigmund",
        "last_name": "Jacinthe",
        "last_update": "2023-07-25T17:17:45.706Z",
        "phone": "9854785858",
        "pincode": 25433,
        "shipping_id": 30185
    },
    {
        "customer_id": 88945,
        "first_name": "Mahesh",
        "last_name": "Sathwara",
        "last_update": "2023-07-25T17:17:45.706Z",
        "phone": "123456789",
        "pincode": 25433,
        "shipping_id": 30185
    }
]

validatorFun

  • Write custom function code for more validations on a particular field.
  • We have field value and the full object of the body.
  • validatorFun will be executed last. So, if you modify data in those functions that are invalid, and try to save/update, it will pass through.
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;
        }
    }
}

enum [v1.11+]

  • Conversion will run before validation
  • If value is present, it should be from given array.
  • Validation will not be performed when enum array is empty.
  • Generate Data will take values from enum instead of random values. It will work in swagger docs and all the relevant APIs.
1
2
3
4
5
6
country: {
    __type: EType.string,
        validations: {
        enum: ['India', 'Africa']
    }
},

conversions

  • To convert any payload column value use 'conversions'.
  • We need to write the conversion function here as 'conversionFun'.

The 'conversions' supports below properties.

  • trimStart: boolean - To remove space from the start of the value.
  • trimEnd: boolean - To remove space from the end of the value.
  • trim: boolean - To remove space from the start and end of the value.
  • toLowerCase: boolean - Convert all characters of the value to lowercase.
  • toUpperCase: boolean - Convert all characters of the value to upper case.
  • conversionFun: function - Write a custom function to change the given value. (v1.11.0+) It will be executed even if value is not present in payload.
  • encryption: boolean - If it's true it will encrypt the value and save.
  • hashing: boolean - It will create a hash of the value and save.
  • defaults [v1.11+]: It is used to set default values.
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,
    }
}
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();
        },
    }
},
active: {
    __type: EType.boolean,
        conversions: {
        defaults: {
            defaultValue: true,
            shouldReplaceEmptyStringWithDefault: true,
            shouldReplaceNullWithDefault: true,
        }
    }
},

isPrimaryKey

  • The database table has the column as the primary key then it will add 'isPrimaryKey: true' in the schema.
1
2
3
4
5
6
7
customer_id: <ISchemaProperty>{
    __type: EType.number,
    isPrimaryKey: true,
    validations: <IPropertyValidation>{
        required: true,
    }
}

isAutoIncrementByAM

  • Add 'isAutoIncrementByAM: true' if we make any column auto increment. It makes a field auto increment from the API Maker not from the database.
1
2
3
4
5
6
7
customer_id: <ISchemaProperty>{
    __type: EType.number,
    isPrimaryKey: true,
    validations: <IPropertyValidation>{
        required: true,
    }
}

isAutoIncrementByDB

  • Add 'isAutoIncrementByDB: true' if the column is an auto increment from the database side.
1
2
3
4
5
6
7
id: <ISchemaProperty>{
    __type: EType.number,
    isAutoIncrementByDB: true,
    validations: <IPropertyValidation>{
        required: true,
    }
}

collection/table

  • To save, get and update nested deep data from any collection/table, use 'collection or table'.
  • You must have to provide 'column'.
1
2
3
4
5
owner_id: <ISchemaProperty>{
    __type: EType.number,
    collection: "COLLECTION_NAME",
    column: "COLUMN_NAME"
}

database

  • To save, get and update nested deep data from any database's collection, use 'database'.
  • If 'database' is not there API Maker takes the same database in which the schema right now you are editing.
1
2
3
4
5
6
owner_id: <ISchemaProperty>{
    __type: EType.number,
    column: "COLUMN_NAME",
    collection: "COLLECTION_NAME",
    database: "DATABASE_NAME",
}

instance

  • To save, get and update nested deep data from any instance, any database, and any collection, use 'instance'.
  • If 'instance' is not there API Maker takes the same instance in which the schema right now you are editing.
1
2
3
4
5
6
7
owner_id: <ISchemaProperty>{
    __type: EType.number,
    column: "COLUMN_NAME",
    instance: "INSTANCE_NAME",
    collection: "COLLECTION_NAME",
    database: "DATABASE_NAME",
}

defaultFun

  • defaultValue has higher priority compare to defaultFun.
  • If defaultValue is not present, system will execution this function and returned value will be set.
  • While saving new record, if value is not provided then this function will be executed to generate default value.
createdAt: {
    __type: EType.date,
        conversions: {
        defaults: {
            defaultFun: () => {
                return new Date();
            },
        }
    }
},