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.
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>
Example
const databases = await instance.getDatabases();
console.log(databases); // ['awSQL_dev','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
| 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
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>
Example
const tables = await instance.getTables("awSQL_dev"); // Getting tables of database "awSQL_dev"
[
'Categories',
'Customers',
'Employees',
'OrderDetails',
'Orders',
'Products',
'Shippers',
'Suppliers'
]
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) → OkPacket
Drops a whole database
- Requires admin privileges
Parameters
| Parameter | Type | Description |
|---|---|---|
database | String | The name of the database to drop |
Returns
→ OkPacket
dropTable()
async → (table = String) → OkPacket
Drops a whole table.
A default database must be set with selectDatabase()
Parameters
| Parameter | Type | Description |
|---|---|---|
table | String | The name of the table to drop |
Returns
→ OkPacket
createDatabase()
async → (name = String) → OkPacket
Creates a new database.
- Requires admin privileges, crashes otherwise
- Crashes if the database already exists
Parameters
| Parameter | Type | Description |
|---|---|---|
name | String | The name of the database to create |
Returns
→ OkPacket
createTable()
→ (name = String) → CreateTable
Prepares to create a new table.
Crashes if the table already exists
Parameters
| Parameter | Type | Description |
|---|---|---|
name | String | The name of the table to create |
Returns
alterTable()
→ (name = String) → AlterTable
Prepares to alter a table.
Parameters
| Parameter | Type | Description |
|---|---|---|
name | String | The name of the table to alter |
Returns
createStructure()
→ () → Structure
Creates a new structure.
Returns
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
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
}
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