generated from aicore/template-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 1
db API
phoenixide edited this page Aug 15, 2022
·
31 revisions
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.
{
"host": "localhost",
"port": "3306",
"database": "testdb",
"user" : "root",
"password": "1234"
}
// 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)
-
config
config config to configure MySQL
Returns boolean true if connection is successful false otherwise*
This function helps to close the database connection
Returns void
This function helps to create a table in database
import {getMySqlConfigs} from "@aicore/libcommonutils";
const configs = getMySqlConfigs();
init(configs)
const tableName = 'customer';
const nameOfPrimaryKey = 'name';
const nameOfJsonColumn = 'details';
await createTable(tableName, nameOfPrimaryKey, nameOfJsonColumn);
* ```
-
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
.