Skip to main content

Instance

Holds connection data and is the gateway to performing queries.

Instances can be managed via the default export awSQL

Methods

connect()

Promise → () → String

Connects the instance.

Possible crash

Throws an error whenever the connection fails with an error

Returns

String - A connection string in the following format: "Connected to host with user user"


destroy()

→ () → true

Destroys the connection

Returns

true


queryRaw()

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

Performs a raw query with the given sql-string.

To prevent sql-injections use ? and push your values in order into the values array.

Parameters

ParameterTypeDescription
queryStringStringThe sql-query to perform
valuesArray<any>? in the query string will be replaced by this values in order

Returns

any - Whatever the query returns


getDatabases()

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

Returns a list of database names the user has access to

Parameters

ParameterTypeDescription
excludeSchema optionBooleanWhether to exclude the default database 'information_schema'

Returns

Array<String>

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

Selects a default database for future queries

Parameters

ParameterTypeDescription
nameStringThe database to select

Returns

this


getTables()

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

Returns a list of tables for the selected database

Possible crash

options.multipleStatemens must have been set to true at creation of this instance for this to work. If not this will throw an error.

Parameters

ParameterTypeDescription
database optionalStringDatabase to get tables of. Can be empty as long as a default database was set with 'selectDatabase'

Returns

Array<String>

Example

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

select()

→ (from = String, ...columns? = String) → Select

Prepares a new select query.

Parameters

ParameterTypeDescription
fromStringName of the table to select from
...columns optionalStringNames of the columns to include in the query. Leave empty to select all (*)

Returns

Select


insert()

→ (into = String) → Insert

Prepares a new query to insert data.

Parameters

ParameterTypeDescription
intoStringName of the table to insert into

Returns

Insert


delete()

→ (from = String) → Delete

Prepares a new query to delete data.

Parameters

ParameterTypeDescription
fromStringName of the table to delete from

Returns

Delete


update()

→ (table = String) → Update

Prepares a new query to update data.

Parameters

ParameterTypeDescription
tableStringName of the table to update data of

Returns

Update


dropDatabase()

async → (database = String) → OkPacket

Drops a whole database

  • Requires admin privileges

Parameters

ParameterTypeDescription
databaseStringThe name of the database to drop

Returns

OkPacket


dropTable()

async → (table = String) → OkPacket

Drops a whole table.

Possible crash

A default database must be set with selectDatabase()

Parameters

ParameterTypeDescription
tableStringThe name of the table to drop

Returns

OkPacket


createDatabase()

async → (name = String) → OkPacket

Creates a new database.

Possible crash
  • Requires admin privileges, crashes otherwise
  • Crashes if the database already exists

Parameters

ParameterTypeDescription
nameStringThe name of the database to create

Returns

OkPacket


createTable()

→ (name = String) → CreateTable

Prepares to create a new table.

Possible crash

Crashes if the table already exists

Parameters

ParameterTypeDescription
nameStringThe name of the table to create

Returns

CreateTable


alterTable()

→ (name = String) → AlterTable

Prepares to alter a table.

Parameters

ParameterTypeDescription
nameStringThe name of the table to alter

Returns

AlterTable


createStructure()

→ () → Structure

Creates a new structure.

Returns

Structure


getStructure()

async → (table = String, database? = String) → Structure

Returns the structure object of a table.

Parameters

ParameterTypeDescription
tableStringThe name of the table to get structure of
database optionalStringName of the underlying database. Can be empty when a default database was set with selectDatabase()

Returns

Structure


checkStructure()

async → (table = String, desiredStructure = Structure, database? = String) → Object

Checks the structure of a table against a given structure.

Parameters

ParameterTypeDescription
tableStringThe name of the table to check
desiredStructureStructureThe structure to check against
database optionalStringName of the underlying database. Can be empty when a default database was set with selectDatabase()

Returns

Object

{
errors: [<...String>], // Holds error messages
passed: [<...String>] // Holds success messages
}
info

If errors.length is 0 the structure is correct


total()

async → (table = String) → Number

Returns the total amount of rows of a table.

  • A default database must be set

Parameters

ParameterTypeDescription
tableStringThe name of the table to check.

Returns

Number


isConnected()

→ () → Boolean

Returns whether the connection has been established.

Returns

Boolean