This document details APIJSON's conditional operations system and the implementation of the fake delete (soft delete) feature. Conditional operations (INSERT, UPDATE, REPLACE, REMOVE, IF) allow precise control over data modifications based on existence checks and custom conditions. Fake delete enables marking records as deleted without physical removal from the database, preserving data integrity and enabling recovery.
APIJSON provides several conditional operations that execute data modifications only when specific criteria are met. These operations are primarily used in the Request table's structure configuration to define strict validation rules for write operations.
| Operation | Behavior | Use Case | Existence Check |
|---|---|---|---|
INSERT | Add fields only when they don't exist | Prevent duplicate additions | Must not exist |
UPDATE | Force put fields regardless of existence | Upsert behavior | No check |
REPLACE | Replace fields only when they exist | Ensure target exists before modification | Must exist |
REMOVE | Remove fields only when they exist | Safe deletion of optional fields | Must exist |
IF | Execute actions when conditions are met | Synchronize related tables, custom validation | Custom condition |
EXIST | Verify that specified fields exist | Dependency validation | Must exist |
UNIQUE | Verify fields are unique (excluding self) | Conflict prevention | Must be unique |
Sources: APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java139-152 APIJSONORM/src/main/java/apijson/orm/Operation.java11-165
The INSERT operation adds fields to a record only when those fields do not already exist. This is defined in the Operation enum and processed by the verifier.
Sources: APIJSONORM/src/main/java/apijson/orm/Operation.java79-88 APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java142
The UPDATE operation performs a forced put, adding fields if they don't exist or modifying them if they do. This implements standard upsert semantics.
Sources: APIJSONORM/src/main/java/apijson/orm/Operation.java90-99 APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java143
The REPLACE operation modifies fields only when they already exist. This ensures you're updating existing data rather than creating new fields.
Sources: APIJSONORM/src/main/java/apijson/orm/Operation.java101-110 APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java144
The REMOVE operation deletes fields from a record only when they exist. This provides safe field deletion without errors for missing fields.
Sources: APIJSONORM/src/main/java/apijson/orm/Operation.java112-116 APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java147
The IF operation defines conditional logic that executes when specific conditions are met. It is used for event listeners to synchronize data across tables (e.g., updating a userName in a Comment table when the User table's name changes) or to throw custom errors. It requires AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION to be enabled.
Sources: APIJSONORM/src/main/java/apijson/orm/Operation.java118-138 APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java150 APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java17
The following diagram bridges the Natural Language concepts of operations to the Code Entity Space, showing how AbstractVerifier handles these conditions.
Sources: APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java60-152 APIJSONORM/src/main/java/apijson/orm/Operation.java11-165
Fake delete preserves data integrity by marking records as deleted rather than physically removing them. This is implemented by converting DELETE requests into UPDATE requests and filtering read queries.
The implementation relies on AbstractSQLConfig for global key definitions and AbstractVerifier for table-specific mapping.
Key Configuration Components:
KEY_DELETED_KEY: The column name used for soft delete status (defaults to "deletedKey"). APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java57KEY_DELETED_VALUE: The value representing a deleted state (defaults to "deletedValue"). APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java58KEY_NOT_DELETED_VALUE: The value representing an active state (defaults to "notDeletedValue"). APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java59ACCESS_FAKE_DELETE_MAP: A static map in AbstractVerifier that stores fake delete configurations for different tables. APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java115Sources: APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java57-59 APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java115 APIJSONORM/src/main/java/apijson/RequestMethod.java49
When a DELETE request is received (represented by RequestMethod.DELETE), the system checks if the target table is configured in ACCESS_FAKE_DELETE_MAP. If configured, the AbstractSQLConfig logic ensures the SQL generated is an UPDATE statement that sets the field defined by KEY_DELETED_KEY to KEY_DELETED_VALUE.
For GET requests, the system automatically appends a condition where KEY_DELETED_KEY equals KEY_NOT_DELETED_VALUE to filter out soft-deleted records.
Sources: APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java57-59 APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java115 APIJSONORM/src/main/java/apijson/RequestMethod.java49
To prevent catastrophic data loss during soft deletes (which are technically updates), AbstractVerifier.IS_UPDATE_MUST_HAVE_ID_CONDITION is set to true by default. This ensures that every DELETE or PUT request contains a specific id, id{}, or id{}@ condition.
Sources: APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java64-66 APIJSONORM/src/main/java/apijson/RequestMethod.java42-49
Conditional operations are enforced via the Request system table. AbstractVerifier uses the REQUEST_MAP to store and retrieve these structural requirements.
Sources: APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java120 APIJSONORM/src/main/java/apijson/orm/model/Request.java43
The OPERATION_KEY_LIST in AbstractVerifier defines the supported keywords for structure validation, corresponding to the Operation enum:
TYPE: Validates data types (BOOLEAN, NUMBER, STRING, etc.). APIJSONORM/src/main/java/apijson/orm/Operation.java43VERIFY: Validates content patterns using regex or range checks. APIJSONORM/src/main/java/apijson/orm/Operation.java62INSERT, UPDATE, REPLACE, REMOVE: Conditional field modifications. APIJSONORM/src/main/java/apijson/orm/Operation.java79-116EXIST, UNIQUE: Existence and uniqueness constraints. APIJSONORM/src/main/java/apijson/orm/Operation.java64-77MUST, REFUSE: Mandatory or forbidden fields. APIJSONORM/src/main/java/apijson/orm/Operation.java16-22IF: Logic and event synchronization. APIJSONORM/src/main/java/apijson/orm/Operation.java138ALLOW_PARTIAL_UPDATE_FAIL: Permits partial success in batch updates for specific tables configured in ALLOW_PARTIAL_UPDATE_FAIL_TABLE_MAP. APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java94-96 APIJSONORM/src/main/java/apijson/orm/Operation.java158Sources: APIJSONORM/src/main/java/apijson/orm/AbstractVerifier.java139-152 APIJSONORM/src/main/java/apijson/orm/Operation.java11-165
Refresh this wiki