The base route for database operations is /api/records, while a specific route exists for custom queries at /api/query.
Here are the endpoints defined in the code:
1. Custom SQL Query
• Endpoint: POST /api/query
• Function: Executes a raw SQL SELECT statement provided in the request body.
• Details:
◦ The body must be a JSON object containing the query string.
◦ It includes security checks to reject unsafe queries (e.g., INSERT, DELETE, DROP) or those containing multiple statements.
◦ It returns the rows found and the count of affected rows.
2. Read Records (GET)
• Endpoint: GET /api/records/{table}
• Function: Retrieves a list of records from the specified table.
• Details:
◦ This endpoint supports query parameters for filtering (e.g., filter=col,eq,val), ordering (order=col,asc), field selection (include=col1,col2), and paging (page=1&size=20).
◦ It returns the records and the total result count if paging is used.
• Endpoint: GET /api/records/{table}/{id}
• Function: Retrieves a single record by its ID.
• Details: Returns a 404 error if the record is not found.
• Endpoint: GET /api/records/{table}/{id1,id2,...}
• Function: Retrieves multiple specific records based on a comma-separated list of IDs.
• Details: Returns a list of records matching the provided IDs.
3. Create Records (POST)
• Endpoint: POST /api/records/{table}
• Function: Inserts one or more new records into the table.
• Details:
◦ The request body can be a single JSON object (to insert one row) or an array of objects (batch insert).
◦ It returns the affected row count or a list of new IDs.
◦ Requests that include an ID in the URL (e.g., /api/records/table/1) are rejected as "Bad request".
4. Update Records (PUT)
• Endpoint: PUT /api/records/{table}/{id}
• Function: Updates a single record identified by the ID in the URL.
• Details: The body must be a JSON object containing the fields to update.
• Endpoint: PUT /api/records/{table}/{id1,id2,...}
• Function: Updates multiple records identified by the comma-separated IDs in the URL.
• Details:
◦ If the body is a single map, that update is applied to all specified IDs.
◦ If the body is a list, it must match the size of the ID list, applying specific updates to specific IDs.
◦ It performs the updates inside a transaction.
5. Delete Records (DELETE)
• Endpoint: DELETE /api/records/{table}/{id}
• Function: Deletes a single record identified by the ID.
• Details: Returns the number of affected rows.
• Endpoint: DELETE /api/records/{table}/{id1,id2,...}
• Function: Deletes multiple records identified by the comma-separated IDs.
• Details: Uses a transaction to delete the records and returns the results for each operation.
6. CORS Support (OPTIONS)
• Endpoint: OPTIONS (Any Valid Path)
• Function: Handles Cross-Origin Resource Sharing (CORS) preflight checks.
• Details: If EnableCORS is set to true, this returns a 204 success status to the browser.
Authorization
All endpoints check for an API key in the header X-API-Key. If the key does not match the server configuration, the request returns a 401 "Authorization Required" error.