Swiftybase
Function StackDatabaseMysql

MySQL Execute

Execute a MySQL query and retrieve the result.

Configuration:

  • Type – Defines the type of query:
    1. Insert: Insert values into a table.
    2. Insert Bulk: Insert multiple values in a single query.
    3. Select: Retrieve multiple rows from the result set.
    4. First: Return the first row of the result or an error if no row exists.
    5. First Or Null: Return the first row of the result or null if no row exists.
    6. Update: Update a row and return the result.
    7. Delete: Delete a row and return the result.
  • Query – The raw MySQL query to be executed.
  • Arguments – A list of values required by your query. For example, if you have a query like:
    SELECT id, name FROM users WHERE id = ?
    You would pass the parameter list as [1] or ["$userIdParam"] if literal arguments are enabled.
  • Literal Arguments – Enables dynamic creation of a literal list, where the values can be variables.
  • Affected Rows – Optionally enabled to return the number of affected rows from the query, if applicable.
  • Model – Specifies the type of data returned by the query. For a single row, use a primitive type. For multiple rows, define a struct model.
  • Field Mapper – A list of strings that map returned columns from the query to the specified model. For example, with the query:
    SELECT id, name FROM users WHERE id = ?
    You would define a struct with Id and Name fields, and use a mapper like ["Id", "Name"] to map the results to a User model.
  • Expected Result Set – Allocates a specific size to hold the query result.
  • Database – The name of the database dependency registered globally for use across your backend stack. If not provided, it defaults to MySqlDb, but you can specify a custom database name.
  • Cache Dependency – Allows caching of the database instance when the function is called for the first time, based on a specified caching policy.

Ensure that your server hook connects to the database once and registers it as MySqlDb or any custom name to ensure consistent access across your API.

Outputs:

  • Returns a Res<SqlRes>, which contain result of the execution.

Check in detail with examples:

  • Insert Query: Add a new record to your MySQL table.
  • Select: Retrieve a list of rows that match your SQL query.
  • First: Fetch the first row from the result set (must exist).
  • First Or Null: Fetch the first row, or return null if nothing is found.
  • Update: Modify existing records and return the affected row count.
  • Delete: Remove records that match the condition from your table.

On this page