Edits history of script submission #6004 for ' Execute arbitrary Query in Deno (mysql)'

  • deno
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { createPool as createMysqlPool } from "npm:mysql2/promise";
    
    // Define the MySQL resource type as specified
    type Mysql = {
      ssl: boolean,
      host: string,
      port: number,
      user: string,
      database: string,
      password: string
    
    }
    
    // The main function that executes a query on a MySQL database
    export async function main(
      mysqlResource: Mysql,
      query: string,
    ): Promise<any> {
      // Adjust the SSL configuration based on the mysqlResource.ssl value
      const sslConfig = mysqlResource.ssl ? { rejectUnauthorized: true } : false;
    
      // Create a new connection pool using the provided MySQL resource
      const pool = createMysqlPool({
        host: mysqlResource.host,
        user: mysqlResource.user,
        database: mysqlResource.database,
        password: mysqlResource.password,
        port: mysqlResource.port,
        // Use the adjusted SSL configuration
        ssl: sslConfig,
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0,
      });
    
      try {
        // Get a connection from the pool and execute the query
        const [rows] = await pool.query(query);
        return rows;
      } catch (error) {
        // If an error occurs, throw it to be handled by the caller
        throw new Error(`Failed to execute query: ${error}`);
      } finally {
        // Always close the pool after the operation is complete
        await pool.end();
      }
    }

    Submitted by hugo697 369 days ago

  • deno
    import { createPool as createMysqlPool } from "npm:mysql2/promise";
    
    // Define the MySQL resource type as specified
    type Mysql = {
      ssl: boolean,
      host: string,
      port: number,
      user: string,
      database: string,
      password: string
    
    }
    
    // The main function that executes a query on a MySQL database
    export async function main(
      mysqlResource: Mysql,
      query: string,
    ): Promise<any> {
      // Adjust the SSL configuration based on the mysqlResource.ssl value
      const sslConfig = mysqlResource.ssl ? { rejectUnauthorized: true } : false;
    
      // Create a new connection pool using the provided MySQL resource
      const pool = createMysqlPool({
        host: mysqlResource.host,
        user: mysqlResource.user,
        database: mysqlResource.database,
        password: mysqlResource.password,
        port: mysqlResource.port,
        // Use the adjusted SSL configuration
        ssl: sslConfig,
        waitForConnections: true,
        connectionLimit: 10,
        queueLimit: 0,
      });
    
      try {
        // Get a connection from the pool and execute the query
        const [rows] = await pool.query(query);
        return rows;
      } catch (error) {
        // If an error occurs, throw it to be handled by the caller
        throw new Error(`Failed to execute query: ${error}`);
      } finally {
        // Always close the pool after the operation is complete
        await pool.end();
      }
    }

    Submitted by henri186 790 days ago