Skip to content
phoenixide edited this page Aug 15, 2022 · 31 revisions

init

This function helps to initialize MySql Client This function should be called before calling any other functions in this library

Best practice is to import @aicore/libcommonutils and call getMySqlConfigs() api to read values from of configs from environment variables.

Sample config

    {
    "host": "localhost",
    "port": "3306",
    "database": "testdb",
    "user" : "root",
    "password": "1234"
    }

Sample initialization code

// set  following  environment variables to access database securely
// set MY_SQL_SERVER for mysql server
// set MY_SQL_SERVER_PORT to set server port
// set MY_SQL_SERVER_DB to specify database where database operations are conducted
// set MY_SQL_USER to specify database user
// set MY_SQL_PASSWORD to set mysql password

import {getMySqlConfigs} from "@aicore/libcommonutils";

const configs = getMySqlConfigs();
init(configs)

Parameters

  • config config config to configure MySQL

Returns boolean true if connection is successful false otherwise*

close

This function helps to close the database connection

Returns void

createTable

This function helps to create a table in database

How to use this function?.

import {getMySqlConfigs} from "@aicore/libcommonutils";

const configs = getMySqlConfigs();
init(configs)
const tableName = 'customer';
const nameOfPrimaryKey = 'name';
const nameOfJsonColumn = 'details';
await createTable(tableName, nameOfPrimaryKey, nameOfJsonColumn);
* ```

Parameters

  • tableName string name of table to create
  • nameOfPrimaryKey string name of primary key
  • nameOfJsonColumn string name of JsonColumn

Returns Promise returns a Promise await on Promise to get status of createTable on success await will return true. on failure await will throw an exception.

Clone this wiki locally