Skip to content

Functions:SQL

Thomas van Ophem edited this page Jul 17, 2023 · 12 revisions

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:

interfaceDBCredentials{host: string;port: number;user: string;password: string;database: string;options?: {encrypt?: boolean;}}interfaceOptions{client?: string;// default mysqlraw: string;timeout?: number;// default 20s}constresult=awaitsql(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:

constqueryMsSQL=async()=>{constresult=awaitsql({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,};};exportdefaultqueryMsSQL;

Clone this wiki locally