Skip to main content

Instance

Handles connection data and serves as the gateway for performing queries.

Instances can be managed globally via awSQL.


Methods


connect()

Promise → () → String

Establishes a connection to the database.

Possible Errors

Throws an error if the connection fails.

Returns

String - Connection confirmation in the format: "Connected to host with user user"


destroy()

→ () → true

Destroys the connection and releases resources.

Returns

true - Always returns true unless an error occurs.


queryRaw()

async → (queryString = String, values? = Array<any>) → any

Executes a raw SQL query.

Security Tip

Use ? placeholders and pass values seperately to prevent SQL injection.

Parameters

ParameterTypeDescription
queryStringStringSQL query to execute.
valuesArray<any>Values for placeholders (?), in left-to-right order.

Returns

any - Query Result


getDatabases()

async → (excludeSchema? = Boolean) → Array<String>

Retrieves a list of databases accessible to the user.

Parameters

ParameterTypeDescription
excludeSchema optionBooleanExcludes information_schema if true

Returns

Array<String> - List of database names.

Example

const databases = await instance.getDatabases();
console.log(databases); // ['awSQL_dev','information_schema']
Excluding default database 'information_schema'
const databasesWithoutSchema = await instance.getDatabases(true);
console.log(databasesWithoutSchema); // ['awSQL_dev']

selectDatabase()

→ (name = String) → this

Sets a default database for future queries.

Parameters

ParameterTypeDescription
nameStringThe database to select

Returns

this


getTables()

→ (database? = String) → Array<String>

Retrieves a list of tables in the selected database.

Possible Errors

Requires options.multipleStatements set to true at instance creation.

Returns

Array<String>

Example Usage

const tables = await instance.getTables("awSQL_dev");
console.log(tables);
Result
[
'Categories',
'Customers',
'Employees',
'OrderDetails',
'Orders',
'Products',
'Shippers',
'Suppliers'
]

Query Builders

These methods return specialized query objects that allow for structured queries:

MethodDescriptionReturns
.select(from, ...columns?)Prepare a SELECT query.Select
.insert(into)Prepare an INSERT query.Insert
.delete(from)Prepare a DELETE query.Delete
.update(table)Prepare an UPDATE query.Update

Database & Table Management

MethodDescriptionReturns
.dropDatabase(database)Deletes an entire database (requires admin privileges).OkPacket
.dropTable(table)Deletes a table (default database must be set).OkPacket
.createDatabase(name)Creates a new database (fails if it already exists).OkPacket
.createTable(name)Prepares a new table creation query.CreateTable
.alterTable(name)Prepares a table modification query.AlterTable
Potential Errors
  • Creating a database requires admin privileges.
  • Dropping a table reqeuires a default database to be selected.

Structure Management

MethodDescriptionReturns
.createStructure()Creates a structure object.Structure
.getStructure(table, database?)Retrieves the structure of a table.Structure
.checkStructure(table, desiredStructure, database?)Compares a table's structure with the expected one.{ errors: [], passed: [] }
✅ If errors.length === 0, the structure is correct.

Utility Methods

MethodDescriptionReturns
.total(table)Counts total rows in a table. (Requires a default database).Number
.isConnected()Checks if the instance is connected.Boolean