Skip to main content

Knex

DriverKnex represents a class that implements the DriverKnex interface for interacting with a database using Knex.js.

API Reference

tip

Knex.js official guide can be found here

Create Connection

createConnection(config)Promise<void>

Creates a database connection using the provided configuration.

Return: A promise that resolves once the connection is established.

Available arguments :

ParameterTypeDescription
configDBConfigThe configuration object for the database connection.

SchemaBuilder

schemaBuilderKnex.SchemaBuilder

Getter method that returns the Knex SchemaBuilder associated with the connection.

Return: The Knex SchemaBuilder object for the connection.

QueryBuilder

queryBuilder([tableName], [options])Knex.QueryBuilder

Builds a query using Knex for the specified table name and options.

Return: A Knex query builder object.

Available arguments :

ParameterTypeDescription
[tableName]Knex.TableDescriptor | Knex.AliasDict | undefinedThe name of the table or an alias dictionary.
[options]objectAdditional options for the query, such as 'only'

RawBuilder

rawBuilder(value, [binding])Knex.Raw<any>

Builds a raw Knex query using the provided value and optional binding.

Return: A raw Knex query object.

Available arguments :

ParameterTypeDescription
valueKnex.Value | stringThe value to be used in the raw query or string to be used in the raw query.
[binding]Knex.RawBinding | (readonly Knex.RawBinding[] | Knex.ValueDict)Optional binding for the raw query

RefBuilder

refBuilder(value)Knex.Ref<string, { [x: string]: string }>

Builds a reference to a column in a table using the provided value.

Return: A reference to a column in a table.

Available arguments :

ParameterTypeDescription
valuestringThe value to build the reference with.

Column Info

getColumnInfo(name)Promise<any>

Asynchronously retrieves column information for a given table name using the query builder.

Return: A promise that resolves with the column information.

Available arguments :

ParameterTypeDescription
namestringThe name of the table to retrieve column information for.

List Tables

listTables()Promise<string[]>

Asynchronously lists tables based on the dialect of the database client.

Return: A Promise that resolves to an array of strings representing table names.

Knex FunctionHelper

functionHelperKnex.FunctionHelper

Get the Knex FunctionHelper from the connection object.

Return: The Knex FunctionHelper object.

Fetch Client

clientKnexDB_Driver

Get the database client type based on the connection configuration.

Return: The database client type (MYSQL, PGSQL, SQLite).

Instance of Knex

knexKnex

Get the instance of Knex for performing database operations.

Return: The instance of Knex for database operations.

TypeScript Properties

KnexDB_Driver

/**
* Defines a custom type for specifying the driver type for Knex database connections.
* The driver can be one of the following: "MYSQL", "PGSQL", or "SQLite".
*/
type KnexDB_Driver = "MYSQL" | "PGSQL" | "SQLite";

DBConfig

interface DBConfig extends Knex.Config {
/** The type of database client to be used, can be "mysql", "pg", "sqlite3", or a custom client type. */
client?: "mysql" | "pg" | "sqlite3" | typeof Client;
}

DatabaseTableTypes

/**
* Defines the possible types for a database table column.
* It can be one of the following types: "string", "integer", "boolean", "json", "datetime".
*/
type DatabaseTableTypes =
| "string"
| "integer"
| "boolean"
| "json"
| "datetime";

DatabaseTableAlias

/**
* Represents the possible types for a database table alias.
* Can be one of: "Text", "Number", "Boolean", "Json", "Date".
*/
type DatabaseTableAlias = "Text" | "Number" | "Boolean" | "Json" | "Date";

DatabaseColumnInfo

interface DatabaseColumnInfo {
/** The name of the column. */
name: string;

/** The type of the column. */
type: DatabaseTableTypes;

/** The alias of the column. */
alias: DatabaseTableAlias;

/** Additional data related to the column. */
actualData?: {
/** The type of the column's actual data. */
type?: DatabaseTableTypes;

/** The data model for creating/editing the column. */
columnData?: DatabaseCreateEditModel;
};
}

DatabaseCreateEditModel

interface DatabaseCreateEditModel {
/** The name of the column in the database. */
columnName: string;

/** The text format of the column. */
textFormat: "varchar" | "text" | null;

/** The number format of the column. */
numberFormat: "int" | "bigInt" | "dec" | "float" | null;

/** The date and time format of the column. */
dateTimeFormat: "date" | "time" | "datetime" | null;

/** The default value for the column. */
defaultValue: any;

/** Indicates if the column value should not be not null. */
notNull: boolean;
}