-
-
Notifications
You must be signed in to change notification settings - Fork 2
Client API
Client is the top-level object in Linked QL. Each instance implements the following interface:
See content
Run any SQL query.
client.query(sql: string, options?: Options): Promise<Savepoint | Array<object>>
client.query(sql: string, options?: Options): Promise<Savepoint | Array<object>>
⚙️ Spec:
-
sql
(string): an SQL query. -
options
(Options, optional): extra parameters for the query. - Return value: a
Savepoint
instance when it's aCREATE
,ALTER
, orDROP
operation, an array (the result set) when it's aSELECT
query or when it's anINSERT
,UPDATE
, orDELETE
operation that has aRETURNING
clause, but a number (indicating number of rows processed by the query) when not having aRETURNING
clause. Null in all other cases.
⚽️ Usage:
Run a CREATE
, ALTER
, or DROP
operation and get back a reference to the savepoint associated with it:
const savepoint = await client.query('ALTER TABLE users RENAME TO accounts');
console.log(savepoint.versionTag); // number
await savepoint.rollback(); // true
or a SELECT query, and get back a result set:
const rows = await client.query('SELECT * FROM users WHERE id = 4');
console.log(rows.length); // 1
or an INSERT
, UPDATE
, or DELETE
operation with a RETURNING
clause, and get back a result set:
const rows = await client.query('INSERT INTO users SET name = \'John Doe\' RETURNING id');
console.log(rows.length); // 1
or an INSERT
, UPDATE
, or DELETE
operation without a RETURNING
clause, and ge back a number indicating the number of rows processed by the query:
const rowCount = await client.query('INSERT INTO users SET name = \'John Doe\'');
console.log(rowCount); // 1
Some additional parameters via options
:
-
dialect
(string, optional): the SQL dialect in use:postgres
(the default) ormysql
. (Details soon as to how this is treated by Linked QL.)// Unlock certain dialect-specific clauses or conventions const rows = await client.query('ALTER TABLE users MODIFY COLUMN id int', { dialect: 'mysql' });
-
values
((string | number | boolean | null | Date | object | any[])[], optional): the values for parameters in the query.const rows = await client.query('SELECT * FROM users WHERE id = $1', { values: [4] });
-
description
(string, optional): the description for aCREATE
,ALTER
,DROP
operation and for the underlying savepoint they create.const savepoint = await client.query('DROP DATABASE test', { description: 'No longer needed' });
-
noCreateSavepoint
(boolean, optional): a flag to disable savepoint creation on aCREATE
,ALTER
,DROP
operation.await client.query('DROP DATABASE test', { noCreateSavepoint: true });
Dynamically run a CREATE DATABASE
operation.
client.createDatabase(databaseNameOrJson: string | DatabaseSchemaSpec, options?: Options): Promise<Savepoint>
client.createDatabase(databaseNameOrJson: string | DatabaseSchemaSpec, options?: Options): Promise<Savepoint>
⚙️ Spec:
-
databaseNameOrJson
(string |DatabaseSchemaSpec
): the database name, or an object specifying the intended database structure to create. -
options
(Options, optional): as described inquery()
. - Return value: a
Savepoint
instance.
⚽️ Usage:
Specify database by name:
const savepoint = await client.createDatabase('database_1', { description: 'Just testing database creation' });
or by a schema object, with an optional list of tables to be created along with it. (Each listed table corresponding to TableSchemaSpec
(in schema.json).):
const savepoint = await client.createDatabase({
name: 'database_1',
tables: [{
name: 'table_1'
columns: [{ name: 'column_1', type: 'int' }, { name: 'column_2', type: 'time' }]
}]
}, { description: 'Just testing database creation' });
Some additional parameters via options
:
-
ifNotExists
(boolean, optional): a flag to conditionally create the database.const savepoint = await client.createDatabase('database_1', { ifNotExists: true, description: 'Just testing database creation' });
Dynamically run an ALTER DATABASE
operation.
client.alterDatabase(databaseNameOrJson: string | { name: string, tables?: string[] }, callback: (databaseSchemaApi: DatabaseSchemaAPI) => void, options?: Options): Promise<Savepoint>
client.alterDatabase(databaseNameOrJson: string | { name: string, tables?: string[] }, callback: (databaseSchemaApi: DatabaseSchemaAPI) => void, options?: Options): Promise<Savepoint>
⚙️ Spec:
-
databaseNameOrJson
(string | { name: string, tables?: string[] }): the database name, or an object with the name and, optionally, a list of tables to be altered along with it. -
callback
((databaseSchemaApi:DatabaseSchemaAPI
) => void): a function that is called with the requested schema. This can be async. -
options
(Options, optional): as described inquery()
. - Return value: a
Savepoint
instance.
⚽️ Usage:
Specify database by name:
const savepoint = await client.alterDatabase('database_1', databaseSchemaApi => {
databaseSchemaApi.name('database_1_new');
}, { description: 'Renaming for testing purposes' });
or by an object, with an optional list of tables to be altered along with it:
const savepoint = await client.alterDatabase({ name: 'database_1', tables: ['table_1'] }, databaseSchemaApi => {
databaseSchemaApi.name('database_1_new');
databaseSchemaApi.table('table_1').column('column_1').name('column_1_new');
databaseSchemaApi.table('table_1').column('column_2').type('varchar');
}, { description: 'Renaming for testing purposes' });
Dynamically run a DROP DATABASE
operation.
client.dropDatabase(databaseName: string, options?: Options): Promise<Savepoint>
client.dropDatabase(databaseName: string, options?: Options): Promise<Savepoint>
⚙️ Spec:
-
databaseName
(string): the database name. -
options
(Options, optional): as described inquery()
. - Return value: a
Savepoint
instance.
⚽️ Usage:
const savepoint = await client.dropDatabase('database_1', { description: 'Dropping for testing purposes' });
Some additional parameters via options
:
-
ifExists
(boolean, optional): a flag to conditionally drop the database.const savepoint = await client.dropDatabase('database_1', { ifExists: true, description: 'Dropping for testing purposes' });
-
cascade
(boolean, optional): a flag to force-drop the database along with its dependents.const savepoint = await client.dropDatabase('database_1', { cascade: true, description: 'Dropping for testing purposes' });
Check if a database exists.
client.hasDatabase(databaseName: string): Promise<Boolean>
client.hasDatabase(databaseName: string): Promise<Boolean>
⚙️ Spec:
-
databaseName
(string): the database name. - Return value: Boolean.
⚽️ Usage:
const exists = await client.hasDatabase('database_1');
Get a list of available databases.
client.databases(): Promise<Array<string>>
client.databases(): Promise<Array<string>>
⚙️ Spec:
- Return value: an array of database names.
⚽️ Usage:
const databases = await client.databases();
console.log(databases); // ['public', 'database_1', ...]
Obtain a Database
instance.
client.database(databaseName: string): Database
client.database(databaseName: string): Database
⚙️ Spec:
-
databaseName
(string): the database name. - Return value: a
Database
instance.
⚽️ Usage:
const database = client.database('database_1');