Skip to content

Auto Increment Feature In API Maker Schema

In some databases, such as MongoDB, native auto-increment fields are not available.
To provide consistent functionality across all supported databases, API Maker includes a built-in Auto Increment feature.

This feature automatically generates sequential numeric values for designated fields during record creation, ensuring predictable and unique identifiers without requiring manual number management.


Enabling Auto Increment in MongoDB, MySQL, PostgreSQL, Microsoft SQL Server, Oracle DB & More..

Auto increment is configured in the schema definition for a field by using the isAutoIncrementByAM property.

Basic Configuration

To enable auto increment with default settings (start at 1, step by 1):

user_id: <ISchemaProperty>{
    type: EType.number,
    isAutoIncrementByAM: true
}

Custom Configuration

You can specify a starting value and an increment step by providing an object:

user_id: <ISchemaProperty>{
    type: EType.number,
    isAutoIncrementByAM: {
        start: 1000,
        step: 5
    }
}

If not specified:

  • start defaults to 1
  • step defaults to 1

Value Generation Process

When a new record is inserted, API Maker determines the next value as follows:

  1. If an internal counter exists for the field
    The next value is calculated as:

    next_value = last_value + step
    

  2. If no internal counter exists

    • API Maker queries the database for the highest existing value of the field.
    • If a value is found, the next value is:

      next_value = max_db_value + step
      

    • If no value is found (empty table/collection), the next value is:

      next_value = start
      

This ensures that even when data already exists, the generated sequence continues without conflicts.


Examples

Scenario Start Step Database Max Value Last Counter Next Value
Counter exists 1000 5 1020 1025
No counter, DB max exists 1000 5 1100 1105
Empty DB 1000 5 1000
Defaults used 1 1 1

Managing Auto Increment Counters

API Maker provides an Auto Increment Utility Page to view and manage all counters for the current environment.

This page displays:

  • Instance name
  • Database name
  • Table or collection name
  • Field name
  • Last incremented value

From this interface, you can manually update the last incremented value. The updated value will be used for the next inserted record.

Counters are environment-specific and are not stored in Git. If you migrate to a new environment, counters must be backed up and restored separately.

auto_increment_page_screenshot.png


Operational Behavior

  • Create operations automatically apply the next sequential value to configured fields.
  • Read operations return the stored values without additional processing.
  • Update operations do not automatically change the value unless explicitly updated.
  • Delete operations do not reset counters.
  • Counters can be migrated between environments as part of backup and restore procedures.
  • If data is incremented outside of API Maker, it will automatically adjust next generated auto increment value.

Best Practices

For predictable sequences:

  • Define both start and step explicitly in the schema.
  • Use the Utility Page to realign counters after bulk data imports or manual changes in the database.
  • Avoid inserting values manually into auto increment fields unless necessary.
  • Keep in mind that changing a counter value affects only the next inserted record.

Limitations

  • Auto increment values are stored internally by API Maker inside internal redis, and are not part of the schema file.