Skip to main content

Database

Database is responsible for managing database connections and configurations.

Drivers

API Reference

Initializes connections

initConnection()Promise<void>

Initializes the connection by setting up the system connection and adding saved configurations.

Example Basic usage:

const { database } = ecoflow;

await database.initConnection();

Fetch Database Config

getDatabaseConfig([ConnectionName])Promise<DatabaseConnectionConfig[]>

Retrieves the database configuration based on the provided connection name. If no connection name is provided, returns all database connection configurations.

Return: A promise that resolves to an array of database connection configurations.

Example Basic usage:

const { database } = ecoflow;

const dbConfigs = await database.getDatabaseConfig();

console.log(dbConfigs);

Available arguments :

ParameterTypeDescription
[ConnectionName]stringThe name of the database connection to retrieve configuration for.

Fetch Database Connection

getDatabaseConnection(name)any

Retrieves a database connection by name.

Return: The database connection associated with the given name.

Available arguments :

ParameterTypeDescription
namestringThe name of the database connection to retrieve.

Validate Connection

validateConnection(driver, connection)Promise<boolean>

Validates a database connection using the specified driver and connection configuration.

Return: A promise that resolves to true if the connection is successful, false otherwise.

Available arguments :

ParameterTypeDescription
driverDB_DriversThe database driver to use for the connection.
connectionConnectionConfigThe configuration object for the connection.

Add Database Connection

addDatabaseConnection(name, driver, connection, [isSystem])Promise<[status, message]>

Asynchronously adds a database connection with the given parameters.

Return: A promise that resolves to a tuple containing a boolean status and a message string.

Available arguments :

ParameterType (Default)Description
namestringThe name of the database connection.
driverDB_DriversThe driver for the database connection.
connectionConnectionConfigThe configuration details for the connection.
[isSystem]boolean (false)Indicates if the connection is a system connection.

Return arguments types :

ParameterTypeDescription
statusbooleanResult status
messagestringResult message

Remove Database Connection

removeDatabaseConnection(name)Promise<[status, message]>

Asynchronously removes a database connection with the given name.

Return: A promise that resolves to a tuple containing a boolean status and a message string.

Available arguments :

ParameterTypeDescription
namestringThe name of the database connection to remove.

Return arguments types :

ParameterTypeDescription
statusbooleanResult status
messagestringResult message

Update Database Connection

removeDatabaseConnection(name, driver, connection)Promise<[status, message]>

Updates the database connection with the given name, driver, and connection configuration.

Return: A promise that resolves to a tuple containing a boolean status and a message string.

Available arguments :

ParameterTypeDescription
namestringThe name of the database connection to update.
driverDB_DriversThe driver type of the database connection.
connectionConnectionConfigThe new connection configuration to update.

Return arguments types :

ParameterTypeDescription
statusbooleanResult status
messagestringResult message

Driver is Knex

isKnex(connection)connection is DriverKnex

Checks if the given connection is an instance of DriverKnex.

Return: True if the connection is an instance of DriverKnex, false otherwise.

Available arguments :

ParameterTypeDescription
connectionanyThe connection to check.

Driver is Mongoose

isMongoose(connection)connection is DriverMongoose

Checks if the given connection is an instance of DriverMongoose.

Return: True if the connection is an instance of DriverMongoose, false otherwise.

Available arguments :

ParameterTypeDescription
connectionanyThe connection to check.

Connection Lists

connectionList()ConnectionList[]

Returns a list of connections in the form of ConnectionList objects.

Filters out connections that start with an underscore.

Return: An array of ConnectionList objects containing connection name and driver information.

Connection count

counntConnections()number

Get the number of connections in the connection list.

Return: The number of connections in the connection list.

Static Methods

Format Knex DateTime

formatKnexDateTime(dateTime)string

Formats a given Date object into a string in the format 'YYYY-MM-DD HH:mm:ss'.

Return: A formatted string representing the date and time.

Available arguments :

ParameterTypeDescription
dateTimeDateThe Date object to format.

TypeScript Properties

DatabaseConnection

/**
* Represents a database connection that can be either a DriverKnex or a DriverMongoose.
*/
type DatabaseConnection = DriverKnex | DriverMongoose;

DB_Drivers

/**
* Defines a custom type DB_Drivers which can be either KnexDB_Driver or "MONGO".
*/
type DB_Drivers = KnexDB_Driver | "MONGO";

ConnectionConfig

interface ConnectionConfig {
/** The connection string to connect to the database. */
connectionString?: string;

/** The host of the database server. */
host?: string;

/** The port number of the database server. */
port?: number;

/** The username for authentication. */
user?: string;

/** The password for authentication. */
password?: string;

/** The name of the database to connect to. */
database?: string;

/** Whether to use SSL for the connection. */
ssl?: boolean;

/** The filename of the database for SQLite */
filename?: string;

/** */
flags?: Array<any>;

/** Mongoose connection options */
mongooseOptions?: MongooseOptions;
}

DatabaseConnectionConfig

interface DatabaseConnectionConfig {
/** The name of the database connection. */
name: string;

/** The driver to use for the connection. */
driver?: DB_Drivers;

/** The configuration for the connections. */
connections?: ConnectionConfig;
}

ConnectionList

interface ConnectionList {
/** The name of the connection. */
connectionsName: string;

/** The driver used for the connection. */
driver: DB_Drivers;
}