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

Parameter | Type | Description
--- | --- | ---
`queryString` | String | The sql-query to perform
`values` | Array<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

Parameter | Type | Description
--- | --- | ---
`excludeSchema` option | Boolean | Whether to exclude the default database 'information_schema'

Returns

→ Array<String>

### selectDatabase()​

→ (`name` = String) → `this`

Selects a default database for future queries

Parameters

Parameter | Type | Description
--- | --- | ---
`name` | String | The 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

Parameter | Type | Description
--- | --- | ---
`database` optional | String | Database to get tables of. Can be empty as long as a default database was set with 'selectDatabase'

Returns

→ Array<String>

### select()​

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

Prepares a new select query.

Parameters

Parameter | Type | Description
--- | --- | ---
`from` | String | Name of the table to select from
`...columns` optional | String | Names 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

Parameter | Type | Description
--- | --- | ---
`into` | String | Name of the table to insert into

Returns

→ Insert

### delete()​

→ (`from` = String) → Delete

Prepares a new query to delete data.

Parameters

Parameter | Type | Description
--- | --- | ---
`from` | String | Name of the table to delete from

Returns

→ Delete

### update()​

→ (`table` = String) → Update

Prepares a new query to update data.

Parameters

Parameter | Type | Description
--- | --- | ---
`table` | String | Name of the table to update data of

Returns

→ Update

### dropDatabase()​

`async` → (`database` = String) → any

Drops a whole database

- Requires admin privileges

Parameters

Parameter | Type | Description
--- | --- | ---
`database` | String | The name of the database to drop

Returns

→ any - Whatever the query result is

### dropTable()​

`async` → (`table` = String) → any

Drops a whole table.

Possible crash

A default database must be set with selectDatabase()

Parameters

Parameter | Type | Description
--- | --- | ---
`table` | String | The name of the table to drop

Returns

→ any - Whatever the query result is

### createDatabase()​

`async` → (`name` = String) → any

Creates a new database.

- Requires admin privileges

Parameters

Parameter | Type | Description
--- | --- | ---
`name` | String | The name of the database to create

Returns

→ any - Whatever the query result is

### createTable()​

→ (`name` = String) → CreateTable

Prepares to create a new table.

Parameters

Parameter | Type | Description
--- | --- | ---
`name` | String | The name of the table to create

Returns

→ CreateTable

### alterTable()​

→ (`name` = String) → AlterTable

Prepares to alter a table.

Parameters

Parameter | Type | Description
--- | --- | ---
`name` | String | The 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

Parameter | Type | Description
--- | --- | ---
`table` | String | The name of the table to get structure of
`database` optional | String | Name 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

Parameter | Type | Description
--- | --- | ---
`table` | String | The name of the table to check
`desiredStructure` | Structure | The structure to check against
`database` optional | String | Name 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

Parameter | Type | Description
--- | --- | ---
`table` | String | The name of the table to check.

Returns

→ Number

### isConnected()​

→ () → Boolean

Returns whether the connection has been established.

Returns

→ Boolean