SQL Query Execution
IMDOS UI is designed to make creating backend-ready admin panels easier and more efficient. One of its powerful features is the ability to execute SQL queries directly from your application using the imdos.request() function. This guide will walk you through how to use this function to fetch data from a MySQL database, including fetching all records, fetching records based on conditions, and fetching the latest record.
Overview of imdos.request()
The imdos.request() function is a versatile method for executing SQL queries. It accepts three arguments:
queryA string representing the SQL query to be executedvaluesAn array of values to be used in the query (optional)optionsAn object containing additional options for the query (optional)
Basic Usage
Import IMDOS
import imdos from '@/lib/imdos';
Fetching All Records
To fetch all records from a table, you can use a simple SELECT statement:
const records = await imdos.request('SELECT * FROM records');
This will execute the query and return an array containing all the data from the records table.
Fetching Records with Conditions
To fetch records based on specific conditions, you can use parameterized queries. This helps prevent SQL injection attacks by safely including user input in your queries.
For example, to fetch records where the code is 123, you can do the following:
const recordCode = 123;
const recordsBasedOnConditions = await imdos.request('SELECT * FROM records WHERE code = ?', [recordCode]);
In this case, the ? is a placeholder for the value in the values array. The function will safely insert the value of recordCode into the query.
Fetching the Latest Record
Sometimes, you may only want to fetch the latest record that matches a condition. You can achieve this by using the options parameter with a first option set to true.
For example, to fetch records where the code is 123, you can do the following:
const recordCode = 123;
const latestRecord = await imdos.request('SELECT * FROM records WHERE code = ?', [recordCode], { first: true });
Setting first to true tells the function to return only the first matching record as an object, instead of an array of records.
Error Handling
It's important to handle errors that may occur during the execution of SQL queries. You can use try...catch blocks to catch and handle any errors:
try {
const allRecords = await imdos.request('SELECT * FROM records');
console.log(allRecords);
} catch (error) {
console.error('Error fetching all records:', error);
}
try {
const recordsBasedOnCode = await imdos.request('SELECT * FROM records WHERE code = ?', [recordCode]);
console.log(recordsBasedOnCode);
} catch (error) {
console.error('Error fetching records based on code:', error);
}
try {
const latestRecord = await imdos.request('SELECT * FROM records WHERE code = ?', [recordCode], { first: true });
console.log(latestRecord);
} catch (error) {
console.error('Error fetching the latest record:', error);
}
The imdos.request() function is a powerful tool for executing SQL queries in your IMDOS UI-based admin panels. By understanding how to use this function to fetch all records, fetch records based on conditions, and fetch the latest record, you can build more dynamic and responsive applications. With proper error handling, you can ensure that your application remains robust and user-friendly.