Edits history of script submission #6005 for ' Execute arbitrary Query in Bun (mysql)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { createConnection } from 'mysql';
    
    // 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 will execute a query on a Mysql resource
    export async function main(mysqlResource: Mysql, query: string): Promise<any> {
      // Create a promise to handle the MySQL connection and query execution
      return new Promise((resolve, reject) => {
        // Create a connection to the MySQL database using the resource credentials
        const connection = createConnection({
          host: mysqlResource.host,
          port: mysqlResource.port,
          user: mysqlResource.user,
          password: mysqlResource.password,
          database: mysqlResource.database,
          ssl: mysqlResource.ssl
        });
    
        // Connect to the MySQL database
        connection.connect(err => {
          if (err) {
            reject(err);
            return;
          }
    
          // Execute the query provided as a parameter
          connection.query(query, (error, results) => {
            // Close the connection after the query execution
            connection.end();
    
            if (error) {
              reject(error);
            } else {
              resolve(results);
            }
          });
        });
      });
    }

    Submitted by hugo697 385 days ago

  • bun
    import { createConnection } from 'mysql';
    
    // 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 will execute a query on a Mysql resource
    export async function main(mysqlResource: Mysql, query: string): Promise<any> {
      // Create a promise to handle the MySQL connection and query execution
      return new Promise((resolve, reject) => {
        // Create a connection to the MySQL database using the resource credentials
        const connection = createConnection({
          host: mysqlResource.host,
          port: mysqlResource.port,
          user: mysqlResource.user,
          password: mysqlResource.password,
          database: mysqlResource.database,
          ssl: mysqlResource.ssl
        });
    
        // Connect to the MySQL database
        connection.connect(err => {
          if (err) {
            reject(err);
            return;
          }
    
          // Execute the query provided as a parameter
          connection.query(query, (error, results) => {
            // Close the connection after the query execution
            connection.end();
    
            if (error) {
              reject(error);
            } else {
              resolve(results);
            }
          });
        });
      });
    }

    Submitted by henri186 807 days ago