Functions:SQL - bettyblocks/cli GitHub Wiki

SQL

With the sql helper, you can connect with an external database and execute raw SQL.

WARNING: Please be aware that using this helper function requires extra CAUTION in terms of the security and integrity of your database. Take the necessary safety measures to avoid threats like SQL injection or unwanted table or records deletions. Use at own risk.

The sql helper accepts the following arguments:

interface DBCredentials {
  host: string;
  port: number;
  user: string;
  password: string;
  database: string;
  options?: {
    encrypt?: boolean;
  }
}

interface Options {
  client?: string; // default mysql
  raw: string;
  timeout?: number; // default 20s
}

const result = await sql(credentials: DBCredentials, options: Options);

This helper makes use of KnexJS. We accept the following database libraries:

  • pg
  • mysql
  • mysql2
  • oracledb
  • tedious

For more information check the following link: Installation | Knex.js.

Example function:

const queryMsSQL = async () => {
  const result = await sql(
    {
      host: '127.0.0.1',
      port: 3306,
      user: 'your_database_user',
      password: 'your_database_password',
      database: 'myapp_test',
    },
    {
      raw: 'SELECT * FROM empDepartment;',
      client: 'mssql',
    }
  );

  return {
    result,
  };
};

export default queryMsSQL;