-
-
Couldn't load subscription status.
- Fork 2
Home
Here's for a quick overview of the Linked QL API:
Here we talk about the client.query() method in more detail along with other Linked QL APIs that essentially let you do the same things possible with client.query(), but this time, programmatically.
As an example of one of these APIs, a CREATE DATABASE operation...
const savepoint = await client.query('CREATE DATABASE IF NOT EXISTS database_1');could be programmatically achieved as:
const savepoint = await client.createDatabase('database_1', { ifNotExists: true });That said, while the createDatabase() method is associated with the base Client object, the different programmatic query APIs in Linked QL are actually organized into three hierarchical scopes:
-
the top-level scope (represented by the
Clientinterface), featuring methods such as:createDatabase(),alterDatabase(),dropDatabase(),hasDatabase(),describeDatabase() -
the database-level scope (represented by a certain
Databaseinterface), featuring methods such as:createTable(),alterTable(),dropTable(),hasTable(),describeTable() -
the table-level scope (represented by a certain
Tableinterface), featuring methods such as:select(),insert(),upsert(),update(),delete()
Each object provides a way to narrow in to the next; e.g. from the top-level scope to a database scope...
const database_1 = client.database('database_1');and from there to a table scope:
const table_1 = database.table('table_1');These APIs at play would look something like:
// Create database "database_1"
await client.createDatabase('database_1', { ifNotExists: true });// Enter "database_1" and create a table
await client.database('database_1').createTable({
name: 'table_1', columns: [
{ name: 'column_1', type: 'int', identity: true, primaryKey: true },
{ name: 'column_2', type: 'varchar' },
{ name: 'column_3', type: 'varchar' },
]
});// Enter "table_1" and insert data
await client.database('database_1').table('table_1').insert({
column_2: 'Column 2 test content',
column_3: 'Column 3 test content',
});These APIs and more are what's covered in this section.
Click on a definition to expand.
Client is the top-level object for the individual database kinds 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
Savepointinstance when it's aCREATE,ALTER, orDROPoperation, an array (the result set) when it's aSELECTquery or when it's anINSERT,UPDATE, orDELETEoperation that has aRETURNINGclause, but a number (indicating number of rows processed by the query) when not having aRETURNINGclause. 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(); // trueor a SELECT query, and get back a result set:
const rows = await client.query('SELECT * FROM users WHERE id = 4');
console.log(rows.length); // 1or 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); // 1or 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); // 1Some 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,DROPoperation 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,DROPoperation.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
Savepointinstance.
⚽️ 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
Savepointinstance.
⚽️ 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
Savepointinstance.
⚽️ 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 the schema structure for a database.
client.describeDatabase(databaseName: string): Promise<DatabaseSchemaSpec>
client.describeDatabase(databaseName: string): Promise<DatabaseSchemaSpec>⚙️ Spec:
-
databaseName(string): the database name. - Return value: an object corresponding to
DatabaseSchemaSpec; the requested schema.
⚽️ Usage:
const schema = await client.describeDatabase('database_1');
console.log(schema.name);
console.log(schema.tables);
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
Databaseinstance.
⚽️ Usage:
const database = client.database('database_1');Database is the API for database-level operations. This object is obtained via client.database()
See content
The name associated with the Database instance.
database.name: (string, readonly)
database.name: (string, readonly)⚽️ Usage:
const database = client.database('test_db');
console.log(database.name); // test_db
Dynamically run a CREATE TABLE operation.
database.createTable(tableJson: TableSchemaSpec, options?: Options): Promise<Savepoint>
database.createTable(tableJson: TableSchemaSpec, options?: Options): Promise<Savepoint>⚙️ Spec:
-
tableJson(TableSchemaSpec): an object specifying the intended table structure to create. -
options(Options, optional): as described inquery(). - Return value: a
Savepointinstance.
⚽️ Usage:
const savepoint = await database.createTable({
name: 'table_1'
columns: [
{ name: 'column_1', type: 'int' },
{ name: 'column_2', type: 'time' }
]
}, { description: 'Just testing table creation' });Some additional parameters via options:
-
ifNotExists(boolean, optional): a flag to conditionally create the table.const savepoint = await database.createTable({ name: 'table_1' columns: [ ... ] }, { ifNotExists: true, description: 'Just testing table creation' });
Dynamically run an ALTER TABLE operation.
database.alterTable(tableName: string, callback: (tableSchemaApi: TableSchemaAPI) => void, options?: Options): Promise<Savepoint>
database.alterTable(tableName: string, callback: (tableSchemaApi: TableSchemaAPI) => void, options?: Options): Promise<Savepoint>⚙️ Spec:
-
tableName(string): the table name. -
callback((tableSchemaApi:TableSchemaAPI) => void): a function that is called with the requested table schema. This can be async. -
options(Options, optional): as described inquery(). - Return value: a
Savepointinstance.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
tableSchemaApi.name('table_1_new');
tableSchemaApi.column('column_1').type('int');
tableSchemaApi.column('column_2').drop();
}, { description: 'Renaming for testing purposes' });
Dynamically run a DROP TABLE operation.
database.dropTable(tableName: string, options?: Options): Promise<Savepoint>
database.dropTable(tableName: string, options?: Options): Promise<Savepoint>⚙️ Spec:
-
tableName(string): the table name. -
options(Options, optional): as described inquery(). - Return value: a
Savepointinstance.
⚽️ Usage:
const savepoint = await database.dropTable('table_1', { description: 'Dropping for testing purposes' });Some additional parameters via options:
-
ifExists(boolean, optional): a flag to conditionally drop the table.const savepoint = await database.dropTable('table_1', { ifExists: true, description: 'Dropping for testing purposes' });
-
cascade(boolean, optional): a flag to force-drop the table along with its dependents.const savepoint = await database.dropTable('table_1', { cascade: true, description: 'Dropping for testing purposes' });
Check if a table exists.
database.hasTable(tableName: string): Promise<Boolean>
database.hasTable(tableName: string): Promise<Boolean>⚙️ Spec:
-
tableName(string): the table name. - Return value: Boolean.
⚽️ Usage:
const exists = await database.hasTable('table_1');
Get the schema structure for a table.
database.describeTable(tableName: string): Promise<TableSchemaSpec>
database.describeTable(tableName: string): Promise<TableSchemaSpec>⚙️ Spec:
-
tableName(string): the table name. - Return value: an object corresponding to
TableSchemaSpec; the requested schema.
⚽️ Usage:
const schema = await database.describeTable('table_1');
console.log(schema.name);
console.log(schema.columns);
Get a list of available tables.
database.tables(): Promise<Array<string>>
database.tables(): Promise<Array<string>>⚙️ Spec:
- Return value: an array of table names.
⚽️ Usage:
const tables = await database.tables();
console.log(tables); // ['table_1', 'table_2', ...]
Obtain a Table instance.
database.table(tableName: string): Table
database.table(tableName: string): Table⚙️ Spec:
-
tableName(string): the table name. - Return value: a
Tableinstance.
⚽️ Usage:
const table = database.table('table_1');
Obtain the next available savepoint for given database.
database.savepoint(options?: { direction: string }): Savepoint
database.savepoint(options?: { direction: string }): Savepoint⚙️ Spec:
-
options({ direction: string }, optional): extra paramters for the method. - Return value: a
Savepointinstance.
⚽️ Usage:
const savepoint = await database.savepoint();
console.log(savepoint.versionTag); // number
await savepoint.rollback(); // trueSome additional parameters via options:
-
direction(string, optional): the direction of lookup - either back in time:backward(the default), or forward in time:forward.const savepoint = await database.savepoint({ direction: 'forward' }); console.log(savepoint.versionTag); // number await savepoint.rollback(); // true
Table is the API for table-level operations. This object is obtained via database.table()
See content
The name associated with the Table instance.
table.name: (string, readonly)
table.name: (string, readonly)⚽️ Usage:
const table = client.database('test_db').table('table_1');
console.log(table.name); // table_1
Count total entries in table.
table.count(expr?: string | Function = *): Promise<number>
table.count(expr?: string | Function = *): Promise<number>⚙️ Spec:
-
expr(string | Function = *, optional): a string denoting column name, or a function that recieves a Field object with which to build an expression. Defaults to*. - Return value: number.
⚽️ Usage:
const rowCount = await table.count();// Number of rows where column_1 isn't null
const rowCount = await table.count('column_1');
Dynamically run a SELECT query.
table.select(fields?: (string | Function)[] = *, where?: number | object | Function | true): Promise<Array<object>>
table.select(where?: number | object | Function): Promise<Array<object>>
table.select(fields?: (string | Function)[] = *, where?: number | object | Function | true): Promise<Array<object>>table.select(where?: number | object | Function): Promise<Array<object>>⚙️ Spec:
-
fields((string | Function)[] = *, optional): an array of fields to select. (A field being either a column name string, or a function that recieves a Field object with which to build an expression.) -
where(number | object | Function | true, optional): a number targeting the primary key value of the target row, or an object specifying some column name/column value conditions, or a function that recieves an Assertion object with which to build the conditions, or the valuetruedenoting all records. Defaults totrue. - Return value: an array (the result set).
⚽️ Usage:
// Select all fields (*) from all records
const result = await table.select();// Select specified fields from the record having primary key value of 4
const result = await table.select(['first_name', 'last_name', 'email'], 4);// Select record by primary key value, ommiting fields (implying all fields)
const result = await table.select(4);// Select record by some column name/column value conditions, ommiting fields (implying all fields)
const result = await table.select({ first_name: 'John', last_name: 'Doe' });
Dynamically run an INSERT operation. (With automatic parameter binding.)
table.insert(payload: object | object[], returnList?: (string | Function)[]): Promise<Array<object> | number>
table.insert(columns: string[], values: any[][], returnList?: (string | Function)[]): Promise<Array<object> | number>
table.insert(payload: object | object[], returnList?: (string | Function)[]): Promise<Array<object> | number>table.insert(columns: string[], values: any[][], returnList?: (string | Function)[]): Promise<Array<object> | number>⚙️ Spec:
-
payload(object | object[]): an object denoting a single entry, or an array of said objects denoting multiple entries. (An entry having the general form:{ [key: string]: string | number | boolean | null | Date | object | any[] }where arrays and objects as values are automatically JSON-stringified.) -
columns(string[]): just column names (as against the key/valuepayloadin the first call pattern). -
values(any[][]): a two-dimensional array of just values (as against the key/valuepayloadin the first call pattern), denoting multiple entries. -
returnList(((string | Function)[] | false), optional): a list of fields, corresponding to a select list, specifying data to be returned from the just inserted row. (Equivalent to Postgres' RETURNING clause, but supported for other DB kinds in Linked QL.) - Return value: a number indicating number of rows processed by the query, or where
returnListwas provided, an array of the processed row(s).
⚽️ Usage:
// Insert single entry
await table.insert({ first_name: 'John', last_name: 'Doe', email: '[email protected]'});// Insert multiple entries
await table.insert([
{ first_name: 'John', last_name: 'Doe', email: '[email protected]'},
{ first_name: 'James', last_name: 'Clerk', email: '[email protected]'},
]);// Insert multiple entries another way
await table.insert(['first_name', 'last_name', 'email'], [
['John', 'Doe', '[email protected]'],
['James', 'Clerk', '[email protected]'],
]);// Insert single entry, obtaining inserted rows - which is itself streamlined to just the "id" column
const insertedRows = await table.insert({ first_name: 'John', last_name: 'Doe', email: '[email protected]'}, ['id']);
Dynamically run an UPSERT operation. (With automatic parameter binding.)
table.upsert(payload: object | object[], returnList?: (string | Function)[]): Promise<Array<object> | number>
table.upsert(columns: string[], values: any[][], returnList?: (string | Function)[]): Promise<Array<object> | number>
table.upsert(payload: object | object[], returnList?: (string | Function)[]): Promise<Array<object> | number>table.upsert(columns: string[], values: any[][], returnList?: (string | Function)[]): Promise<Array<object> | number>⚙️ Spec:
-
payload(object | object[]): as described ininsert(). -
columns(string[]): as described ininsert(). -
values(any[][]): as described ininsert(). -
returnList((string | Function)[], optional): as described ininsert(). - Return value: as described in
insert().
⚽️ Usage:
An UPSERT operation is an INSERT operation that automatically converts to an UPDATE operation where given record already exists. API usage is same as insert() but as upsert().
Dynamically run an UPDATE operation. (With automatic parameter binding.)
table.update(where: number | object | Function | true, payload: object, returnList?: (string | Function)[]): Promise<Array<object> | number>
table.update(where: number | object | Function | true, payload: object, returnList?: (string | Function)[]): Promise<Array<object> | number>⚙️ Spec:
-
where(number | object | Function | true): as described inselect(). -
payload(object): an object having the general form:{ [key: string]: string | number | boolean | null | Date | object | any[] }where arrays and objects as values are automatically JSON-stringified. -
returnList((string | Function)[], optional): as described ininsert(). - Return value: as described in
insert().
⚽️ Usage:
// Update the record having primary key value of 4
await table.update(4, { first_name: 'John', last_name: 'Doe' });// Update the record having specified email value, obtaining the updated rows
const updatedRows = await table.update({ email: '[email protected]' }, { first_name: 'John', last_name: 'Doe' }, ['*']);// Update all records
await table.update(true, { updated_at: new Date });
Dynamically run a DELETE operation. (With automatic parameter binding.)
table.delete(where: number | object | Function | true, returnList?: (string | Function)[]): Promise<Array<object> | number>
table.delete(where: number | object | Function | true, returnList?: (string | Function)[]): Promise<Array<object> | number>⚙️ Spec:
-
where(number | object | Function | true): as described inselect(). -
returnList((string | Function)[], optional): as described ininsert(). - Return value: as described in
insert().
⚽️ Usage:
// Delete the record having primary key value of 4
await table.delete(4);// Delete the record having specified email, obtaining the deleted row
const deletedRow = await table.delete({ email: '[email protected]' });// Delete all records
await table.delete(true);Savepoint is an object representation of a database's savepoint. This object is obtained either via database.savepoint() or via a CREATE, ALTER, or DROP operation.
See content
The UUID associated with the savepoint.
savepoint.id: (UUID, readonly)
savepoint.id: (UUID, readonly)⚽️ Usage:
const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.id); // f740d66a-df5f-4a34-a281-8ef3ba6fe754
The subject database's generic identifier that transcends name changes.
savepoint.databaseTag: (string, readonly)
savepoint.databaseTag: (string, readonly)⚽️ Usage:
Consider a database's generic identifier before and after a name change:
// Before name change
const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.databaseTag); // db:18m6z// Name change
await client.alterDatabase('test_db', schema => schema.name('test_db_new'));// Now even after name change
const savepoint = await client.database('test_db_new').savepoint();
console.log(savepoint.databaseTag); // db:18m6z
The savepoint's version tag.
savepoint.versionTag: (number, readonly)
savepoint.versionTag: (number, readonly)⚽️ Usage:
// Version 1
const savepoint = await client.createDatabase({
name: 'test_db',
tables: [{
name: 'test_tbl1',
columns: [],
}]
});
console.log(savepoint.versionTag); // 1// Version 2
const savepoint = await client.database('test_db').createTable({
name: 'test_tbl2',
columns: [],
});
console.log(savepoint.versionTag); // 2// Version 2 currently
const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.versionTag); // 2
The database's peak version regardless of its current rollback level.
savepoint.versionMax: (number, readonly)
savepoint.versionMax: (number, readonly)⚽️ Usage:
const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.versionTag); // 2
console.log(savepoint.versionMax); // 2await savepoint.rollback();const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.versionTag); // 1
console.log(savepoint.versionMax); // 2
The savepoint's current level in the database's list of available savepoints.
savepoint.cursor: (string, readonly)
savepoint.cursor: (string, readonly)⚽️ Usage:
const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.cursor); // 1/2
The description for the changes associated with the savepoint.
savepoint.description: (string, readonly)
savepoint.description: (string, readonly)⚽️ Usage:
const savepoint = await client.database('test_db').createTable({
name: 'test_tbl2',
columns: [],
}, { description: 'Create test_tbl2' });
console.log(savepoint.description); // Create test_tbl2const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.description); // Create test_tbl2
The savepoint's creation date.
savepoint.savepointDate: (Date, readonly)
savepoint.savepointDate: (Date, readonly)⚽️ Usage:
const savepoint = await client.database('test_db').savepoint();
console.log(savepoint.savepointDate); // 2024-07-20T15:31:06.096Z
The savepoint's rollback date.
savepoint.rollbackDate: (Date, readonly)
savepoint.rollbackDate: (Date, readonly)⚽️ Usage:
const savepoint = await client.database('test_db').createTable({
name: 'test_tbl2',
columns: [],
}, { description: 'Create test_tbl2' });
console.log(savepoint.rollbackDate); // nullawait savepoint.rollback();
console.log(savepoint.rollbackDate); // 2024-07-20T15:31:06.096Z// Find the same savepoint with a forward lookup
const savepoint = await client.database('test_db').savepoint({ direction: 'forward' });
console.log(savepoint.rollbackDate); // 2024-07-20T15:31:06.096Z
A single-word summary of the effect that rolling back to this savepoint will have on subject DB.
savepoint.rollbackEffect: (string, readonly)
savepoint.rollbackEffect: (string, readonly)⚽️ Usage:
Will rolling back to given savepoint mean dropping or re-creating the subject database?:
For a create operation...
const savepoint = await client.createDatabase('test_db', { descripton: 'Create db' });Rolling back will mean dropping the DB:
console.log(savepoint.descripton); // Create db
console.log(savepoint.rollbackEffect); // DROP// Drop DB
console.log(savepoint.rollbackEffect); // DROP
await savepoint.rollback();Having rolled back, rolling forward will mean a re-creation of the DB:
// Find the same savepoint with a forward lookup
const savepoint = await client.database('test_db').savepoint({ direction: 'forward' });
// Now rolling back will mean re-creating the DB
console.log(savepoint.descripton); // Create db
console.log(savepoint.rollbackEffect); // CREATEBut note that table-level create/drop operations always only have an ALTER effect on parent DB:
// Create table - which translates to a DB "alter" operation
const savepoint = await client.database('test_db').createTable({
name: 'test_tbl2',
columns: [],
}, { description: 'Create test_tbl2' });
// Rolling back will mean dropping the table - which will still translate to a DB "alter" operation
console.log(savepoint.descripton); // Create test_tbl2
console.log(savepoint.rollbackEffect); // ALTER// Drop DB
await savepoint.rollback();
console.log(savepoint.rollbackEffect); // ALTER// Find the same savepoint with a forward lookup
const savepoint = await client.database('test_db').savepoint({ direction: 'forward' });
// Now rolling back will mean re-creating the table - which will still translate to a DB "alter" operation
console.log(savepoint.descripton); // Create test_tbl2
console.log(savepoint.rollbackEffect); // ALTER
A query preview of the rollback.
savepoint.rollbackQuery: ({ toString(): string }, readonly)
savepoint.rollbackQuery: ({ toString(): string }, readonly)⚽️ Usage:
You get a query instance that is toString()able:
For a create operation...
const savepoint = await client.createDatabase('test_db', { descripton: 'Create db' });Rolling back will mean dropping the DB:
console.log(savepoint.rollbackQuery.toString()); // DROP SCHEMA test_db CASCADE
Check if the savepoint is the next actual point in time for the database.
savepoint.isNextPointInTime(): Promise<boolean>
savepoint.isNextPointInTime(): Promise<boolean>⚙️ Spec:
- Return value: boolean.
⚽️ Usage:
For a new operation, that would be true:
const dbCreationSavepoint = await client.createDatabase('test_db');
console.log(await dbCreationSavepoint.isNextPointInTime()); // trueBut after having performed more operations, that wouldn't be:
const tblCreationSavepoint = await client.database('test_db').createTable({
name: 'test_tbl',
columns: [{
name: 'id',
type: 'int'
}]
});
console.log(await tblCreationSavepoint.isNextPointInTime()); // true
console.log(await dbCreationSavepoint.isNextPointInTime()); // falseRollback table creation and test dbCreationSavepoint's position again:
await tblCreationSavepoint.rollback();
console.log(await tblCreationSavepoint.isNextPointInTime()); // false
console.log(await dbCreationSavepoint.isNextPointInTime()); // true
Rollback all changes associated with given savepoint.
savepoint.rollback(): Promise<boolean>
savepoint.rollback(): Promise<boolean>⚙️ Spec:
- Return value: boolean.
⚽️ Usage:
Create database and rollback:
// Create DB
const savepoint = await client.createDatabase('test_db', { descripton: 'Create db' });
// Roll back - which means drop the DB
await savepoint.rollback();Undo the rollback; i.e. roll forward:
// Find the same savepoint with a forward lookup
const savepoint = await client.database('test_db').savepoint({ direction: 'forward' });
// Roll back - which means re-create the DB
await savepoint.rollback();
Get a plain object representation of the savepoint.
savepoint.toJson(): object
savepoint.toJson(): object⚙️ Spec:
- Return value: an object of the form
{ id: string, name: string, databaseTag: string, versionTag: number, versionMax: number, cursor: string, description: string, savepointDate: Date, rollbackDate: Date | null }.
⚽️ Usage:
const savepoint = await client.createDatabase('test_db', { descripton: 'Create db' });
console.log(savepoint.toJson());
Get the subject DB's schema snapshot at this point in time.
savepoint.schema(): object
savepoint.schema(): object⚙️ Spec:
- Return value: an object corresponding to
DatabaseSchemaSpec(in schema.json).
⚽️ Usage:
const savepoint = await client.database('test_db').createTable({
name: 'test_tbl',
columns: [{
name: 'id',
type: 'int'
}]
});
console.log(savepoint.schema());const savepoint = await client.database('test_db').savepoint();
await savepoint.schema();
Get the subject database's name.
savepoint.name(postRollback?: boolean): string
savepoint.name(postRollback?: boolean): string⚙️ Spec:
-
postRollback(boolean, optional): in case a name change was captured in the savepoint, whether to return the database's post-rollback name. Otherwise the database's active, pre-rollback name is returned. - Return value: the database name.
⚽️ Usage:
// Name change
const savepoint = await client.alterDatabase('test_db', schema => schema.name('test_db_new'));
// The database's active, pre-rollback name
console.log(savepoint.name()); // test_db_new
// The database's post-rollback name
console.log(savepoint.name(true)); // test_dbDatabaseSchemaAPI is the programmatic interface to DatabaseSchemaSpec (in schema.json). This object is obtained via client.alterDatabase()'s callback function.
DatabaseSchemaAPI inherits from AbstractSchemaAPI.
See content
Set or get the database name. (Overrides abstractSchemaApi.name().)
databaseSchemaApi.name(name?: string): this
databaseSchemaApi.name(name?: string): this⚙️ Spec:
-
name(string, optional): when provided, sets the database name. When ommitted, gets the database name returned. - Return value:
Identifier- the current database name, orthis- thedatabaseSchemaApiinstance.
⚽️ Usage:
Rename the database:
const savepoint = await client.alterDatabase('database_1', databaseSchemaApi => {
// Inspect existing name
console.log(databaseSchemaApi.name().toJson()); // database_1
// Rename
databaseSchemaApi.name('new_database_1');
}, { description: 'Renaming for testing purposes' });
Add a table to the database or get an existing one.
databaseSchemaApi.table(tableNameOrJson: string | TableSchemaSpec): TableSchemaAPI
databaseSchemaApi.table(tableNameOrJson: string | TableSchemaSpec): TableSchemaAPI⚙️ Spec:
-
tableNameOrJson(string |TableSchemaSpec): when a string, the name of a table to get. When an object, an object that defines a new table to create. - Return value:
TableSchemaAPI- the table schema requested or the one just added.
⚽️ Usage:
const savepoint = await client.alterDatabase('database_1', databaseSchemaApi => {
// Drop existing table_1
databaseSchemaApi.table('table_1').drop();
// Add table_2
databaseSchemaApi.table({
name: 'table_2',
columns: [],
});
}, { description: 'Altering for testing purposes' });TableSchemaAPI is the programmatic interface to TableSchemaSpec (in schema.json). This object is obtained via databaseSchemaApi.table() and database.alterTable()'s callback function.
TableSchemaAPI inherits from AbstractSchemaAPI.
See content
Set or get the table name. (Overrides abstractSchemaApi.name().)
tableSchemaApi.name(name?: string | string[]): this
tableSchemaApi.name(name?: string | string[]): this⚙️ Spec:
-
name(string | string[], optional): when provided, sets the table name. Accepts a two-part array for a fully-qualified table name. When ommitted, gets the table name returned. - Return value:
Identifier- the current table name, orthis- thetableSchemaApiinstance.
⚽️ Usage:
Rename the table:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Inspect existing name
console.log(tableSchemaApi.name().toJson()); // table_1
// Rename
tableSchemaApi.name('new_table_1');
}, { description: 'Renaming for testing purposes' });Rename the table - fully-qualified:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
tableSchemaApi.name(['database_1', 'new_table_1']);
}, { description: 'Renaming for testing purposes' });Change the qualifier - moving the table to a different database:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
tableSchemaApi.name(['database_4', 'new_table_1']);
}, { description: 'Renaming for testing purposes' });
Add a column to the table or get an existing one.
tableSchemaApi.column(columnNameOrJson: string | ColumnSchemaSpec): ColumnSchemaAPI
tableSchemaApi.column(columnNameOrJson: string | ColumnSchemaSpec): ColumnSchemaAPI⚙️ Spec:
-
columnNameOrJson(string |ColumnSchemaSpec): when a string, the name of a column to get. When an object, an object that defines a new column to create. - Return value:
ColumnSchemaAPI- the column requested or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Obtain existing column_1 and modify its type attribute
tableSchemaApi.column('column_1').type('int');
// Add column_2
tableSchemaApi.column({
name: 'column_2',
type: ['varchar', 50],
});
}, { description: 'Altering for testing purposes' });
Add a Primary Key constraint to the table or get the existing one. (Translates to the SQL PRIMARY KEY constraint.)
tableSchemaApi.primaryKey(constraintJson?: TablePrimaryKeySchemaSpec): TablePrimaryKeySchemaAPI
tableSchemaApi.primaryKey(constraintJson?: TablePrimaryKeySchemaSpec): TablePrimaryKeySchemaAPI⚙️ Spec:
-
constraintJson(TablePrimaryKeySchemaSpec, optional): when provided, an object that defines a new Primary Key to create, specifying the intended Primary Key column(s), and optionally, a constraint name. When ommitted, gets thePRIMARY_KEYinstance on the table returned if exists. - Return value:
TablePrimaryKeySchemaAPI- the existingPRIMARY_KEYinstance requested or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// See if there's one set and undo that
if (!tableSchemaApi.primaryKey()) {
// Add a Primary Key constraint on columns 2 and 3
tableSchemaApi.primaryKey({ columns: ['column_2', 'column_3'] });
}
}, { description: 'Altering for testing purposes' });
Add a Primary Key, Foreign Key, Unique Key, or Check constraint to the table or get an existing one. (Provides a unified way to set/get table constraints.)
tableSchemaApi.constraint(constraintNameOrJson: string | TableConstraintSchemaType): TableConstraintSchemaAPI
tableSchemaApi.constraint(constraintNameOrJson: string | TableConstraintSchemaType): TableConstraintSchemaAPI⚙️ Spec:
-
constraintNameOrJson(string |TableConstraintSchemaType): when a string, the name of a constraint to get. When an object, an object that defines a new constraint to create. - Return value:
TableConstraintSchemaAPI- the constraint requested or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Obtain existing constraint_1 and modify its columns list
tableSchemaApi.constraint('constraint_1').columns(['id', 'bio']);
// Add constraint_2
tableSchemaApi.constraint({
type: 'PRIMARY_KEY',
name: 'constraint_2',
columns: ['id'],
});
}, { description: 'Altering for testing purposes' });Note that when a constraint name is ommitted, one is automatically generated for you:
// Add an anonymous constraint
const constraint = tableSchemaApi.constraint({
type: 'PRIMARY_KEY',
columns: ['id'],
});
// Inspect is auto-generated name
console.log(constraint.name()); // auto_name_25kjd
Add a Fulltext or Spartial Index to the table or get an existing one.
tableSchemaApi.index(indexNameOrJson: string | IndexSchemaSpec): IndexSchema
tableSchemaApi.index(indexNameOrJson: string | IndexSchemaSpec): IndexSchema⚙️ Spec:
-
indexNameOrJson(string |IndexSchemaSpec): when a string, the name of an index to get. When an object, an object that defines a new index to create. - Return value:
IndexSchema- the index requested or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Obtain existing index_1 and modify its columns list
tableSchemaApi.index('index_1').columns(['id', 'bio']);
// Add index_2
tableSchemaApi.index({
type: 'FULLTEXT',
name: 'index_2',
columns: ['id'],
});
}, { description: 'Altering for testing purposes' });Note that when an index name is ommitted, one is automatically generated for you:
// Add an anonymous index
const index = tableSchemaApi.index({
type: 'FULLTEXT',
columns: ['id'],
});
// Inspect is auto-generated name
console.log(index.name()); // auto_name_4gkbcThe getter/setter APIs to the various table-level constraints.
type TableConstraintSchemaAPI = TablePrimaryKeySchemaAPI | TableForeignKeySchemaAPI | TableUniqueKeySchemaAPI | TableCheckConstraintSchemaAPISee details
interface TablePrimaryKeySchemaAPI extends PrimaryKeySchemaAPI {
// Set/get the constraint columns
columns(value?: string[]): Array;
}Jump to
PrimaryKeySchemaAPI
interface TableForeignKeySchemaAPI extends ForeignKeySchemaAPI {
// Set/get the constraint columns
columns(value?: string[]): Array;
}Jump to
ForeignKeySchemaAPI
interface TableUniqueKeySchemaAPI extends UniqueKeySchemaAPI {
// Set/get the constraint columns
columns(value?: string[]): Array;
}Jump to
UniqueKeySchemaAPI
interface TableCheckConstraintSchemaAPI extends CheckConstraintSchemaAPI {
// Get the constraint columns
columns(): Array;
}Jump to
CheckConstraintSchemaAPI
ColumnSchemaAPI is the programmatic interface to ColumnSchemaSpec (in schema.json). This object is obtained via tableSchemaApi.column().
ColumnSchemaAPI inherits from AbstractSchemaAPI.
See content
-
Inherited:
Set the column type or get the current value.
tableSchemaApi.type(typeJson?: string | string[]): ColumnTypeSchema
tableSchemaApi.type(typeJson?: string | string[]): ColumnTypeSchema⚙️ Spec:
-
typeJson(string | string[], optional): when provided, sets the column type. Accepts a two-part array for a fully-qualified type. When ommitted, gets the current column type returned. - Return value:
ColumnTypeSchema- the current column type, orthis- thecolumnSchemaApiinstance.
⚽️ Usage:
Obtain a column and change its type:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// New type
tableSchemaApi.column('column_1').type(['varchar', 255]);
// Current type as JSON
console.log(tableSchemaApi.column('column_1').type().toJson()); // ['varchar', 255]
// Current type as SQL
console.log(tableSchemaApi.column('column_1').type().toString()); // varchar(255)
}, { description: 'Altering for testing purposes' });
Designate the column as Primary Key for the table or get the column's current PRIMARY_KEY instance. (Translates to the SQL PRIMARY KEY constraint.)
columnSchemaApi.primaryKey(constraintToggleOrJson?: boolean | PrimaryKeySchemaSpec): PrimaryKeySchemaAPI
columnSchemaApi.primaryKey(constraintToggleOrJson?: boolean | PrimaryKeySchemaSpec): PrimaryKeySchemaAPI⚙️ Spec:
-
constraintToggleOrJson(boolean |PrimaryKeySchemaSpec, optional): when a boolean, toggles the designation of the column as Primary Key for the table. When an object, an object that specifies a constraint name. When ommitted, gets the column'sPRIMARY_KEYinstance returned if exists. - Return value:
PrimaryKeySchemaAPI- the existingPRIMARY_KEYinstance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').primaryKey()) {
// Add a Primary Key constraint on column_1
tableSchemaApi.column('column_1').primaryKey(true);
}
});Note that when a constraint name is ommitted, one is automatically generated for you:
// Inspect the auto-generated name
console.log(tableSchemaApi.column('column_1').primaryKey().name()); // auto_name_25kjd
Add the FOREIGN_KEY constraint type to the column or get the column's current FOREIGN_KEY instance. (Translates to the SQL FOREIGN KEY constraint.)
columnSchemaApi.foreignKey(constraintJson?: ForeignKeySchemaSpec): ForeignKeySchemaAPI
columnSchemaApi.foreignKey(constraintJson?: ForeignKeySchemaSpec): ForeignKeySchemaAPI⚙️ Spec:
-
constraintJson(ForeignKeySchemaSpec, optional): when provided, an object that defines a new Foreign Key to create, specifying, among other things, the target table and target columns, and optionally, a constraint name. When ommitted, gets the column'sFOREIGN_KEYinstance returned if exists. - Return value:
ForeignKeySchemaAPI- the existingFOREIGN_KEYinstance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').foreignKey()) {
// Add a Foreign Key constraint on column_1
tableSchemaApi.column('column_1').foreignKey({
targetTable: 'table_2',
targetColumns: ['id'],
updateRule: 'CASCADE',
});
}
});Note that when a constraint name is ommitted, one is automatically generated for you:
// Inspect the auto-generated name
console.log(tableSchemaApi.column('column_1').foreignKey().name()); // auto_name_25kjd
Add the UNIQUE_KEY constraint type to the column or get the column's current UNIQUE_KEY instance. (Translates to the SQL UNIQUE constraint.)
columnSchemaApi.uniqueKey(constraintToggleOrJson?: boolean | UniqueKeySchemaSpec): UniqueKeySchemaAPI
columnSchemaApi.uniqueKey(constraintToggleOrJson?: boolean | UniqueKeySchemaSpec): UniqueKeySchemaAPI⚙️ Spec:
-
constraintToggleOrJson(boolean |UniqueKeySchemaSpec, optional): when a boolean, toggles the existence of theUNIQUE_KEYconstraint on the column. When an object, an object that defines a new constraint to create, specifying a constraint name. When ommitted, gets the column'sUNIQUE_KEYinstance returned if exists. - Return value:
UniqueKeySchemaAPI- the existingUNIQUE_KEYinstance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').uniqueKey()) {
// Add a Unique Key constraint on column_1
tableSchemaApi.column('column_1').uniqueKey(true);
}
});Note that when a constraint name is ommitted, one is automatically generated for you:
// Inspect the auto-generated name
console.log(tableSchemaApi.column('column_1').uniqueKey().name()); // auto_name_25kjd
Add the CHECK constraint type to the column or get the column's current CHECK constraint instance. (Translates to the SQL CHECK constraint.)
columnSchemaApi.check(constraintJson?: CheckConstaintSpec): CheckConstraintSchemaAPI
columnSchemaApi.check(constraintJson?: CheckConstaintSpec): CheckConstraintSchemaAPI⚙️ Spec:
-
constraintJson(CheckConstraintSchemaSpec, optional): when provided, an object that defines a new constraint to create, specifying the intended SQL expression, and, optionally, a constraint name. When ommitted, gets the column'sCHECKconstraint instance returned if exists. - Return value:
CheckConstraintSchemaAPI- the existingCHECKconstraint instance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').check()) {
// Add a Check constraint on column_1
tableSchemaApi.column('column_1').check({ expr: 'column_1 IS NOT NULL' });
}
});Note that when a constraint name is ommitted, one is automatically generated for you:
// Inspect the auto-generated name
console.log(tableSchemaApi.column('column_1').check().name()); // auto_name_25kjd
Add the DEFAULT constraint type to the column or get the column's current DEFAULT constraint instance. (Translates to the SQL DEFAULT constraint.)
columnSchemaApi.default(constraintJson?: DefaultConstraintSchemaSpec): DefaultConstraintSchemaAPI
columnSchemaApi.default(constraintJson?: DefaultConstraintSchemaSpec): DefaultConstraintSchemaAPI⚙️ Spec:
-
constraintJson(DefaultConstraintSchemaSpec, optional): when provided, an object that defines a new constraint to create, specifying the intended SQL expression, and, optionally, a constraint name. When ommitted, gets the column'sDEFAULTconstraint instance returned if exists. - Return value:
DefaultConstraintSchemaAPI- the existingDEFAULTconstraint instance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').default()) {
// Add a Default constraint on column_1
tableSchemaApi.column('column_1').default({ expr: 'now()' });
}
});
Add the EXPRESSION constraint type to the column or get the column's current EXPRESSION instance. (Translates to the SQL GENERATED COLUMN type.)
columnSchemaApi.expression(constraintJson?: ExpressionConstraintSchemaSpec): ExpressionConstraintSchemaAPI
columnSchemaApi.expression(constraintJson?: ExpressionConstraintSchemaSpec): ExpressionConstraintSchemaAPI⚙️ Spec:
-
constraintJson(ExpressionConstraintSchemaSpec, optional): when provided, an object that defines a new constraint to create, specifying the intended SQL expression, and, optionally, a constraint name. When ommitted, gets the column'sEXPRESSIONconstraint instance returned if exists. - Return value:
ExpressionConstraintSchemaAPI- the existingEXPRESSIONconstraint instance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').expression()) {
// Add an Expression constraint on column_1
tableSchemaApi.column('column_1').expression({ expr: 'column_1 * 2', stored: true });
}
});
Add the IDENTITY constraint type to the column or get the column's current IDENTITY constraint instance. (Translates to the SQL IDENTITY COLUMN type.)
columnSchemaApi.identity(constraintToggleOrJson?: boolean | IdentityConstraintSchemaSpec): IdentityConstraintSchemaAPI
columnSchemaApi.identity(constraintToggleOrJson?: boolean | IdentityConstraintSchemaSpec): IdentityConstraintSchemaAPI⚙️ Spec:
-
constraintToggleOrJson(boolean |IdentityConstraintSchemaSpec, optional): when boolean, toggles the existence of theIDENTITYconstraint on the column. When an object, an object that defines a new constraint to create, specifying analwaysrule. When ommitted, gets the column'sIDENTITYconstraint instance returned if exists. - Return value:
IdentityConstraintSchemaAPI- the existingIDENTITYconstraint instance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').identity()) {
// Add an Identity constraint on column_1
tableSchemaApi.column('column_1').identity({ always: false });
}
});
Add the NOT_NULL constraint type to the column or get the column's current NOT_NULL constraint instance. (Translates to the SQL NOT NULL constraint.)
columnSchemaApi.notNull(constraintToggle?: boolean): NotNullConstraintSchemaAPIBuilder
columnSchemaApi.notNull(constraintToggle?: boolean): NotNullConstraintSchemaAPIBuilder⚙️ Spec:
-
constraintToggle(boolean, optional): when provided, toggles the existence of theNOT_NULLconstraint on the column. When ommitted, gets the column'sNOT_NULLconstraint instance returned if exists. - Return value:
NotNullConstraintSchemaAPIBuilder- the existingNOT_NULLconstraint instance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').notNull()) {
// Add an notNull constraint on column_1
tableSchemaApi.column('column_1').notNull(true);
}
});
Add the NULL constraint type to the column or get the column's current NULL constraint instance. (Translates to the SQL NULL constraint.)
columnSchemaApi.null(constraintToggle?: boolean): NullConstraintSchemaAPI
columnSchemaApi.null(constraintToggle?: boolean): NullConstraintSchemaAPI⚙️ Spec:
-
constraintToggle(boolean, optional): when provided, toggles the existence of theNULLconstraint on the column. When ommitted, gets the column'sNULLconstraint instance returned if exists. - Return value:
NullConstraintSchemaAPI- the existingNULLconstraint instance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').null()) {
// Add an null constraint on column_1
tableSchemaApi.column('column_1').null(true);
}
});
Add the AUTO_INCREMENT constraint type to the column or get the column's current AUTO_INCREMENT constraint instance. (Translates to the MySQL-specific AUTO_INCREMENT constraint.)
columnSchemaApi.autoIncrement(constraintToggle?: boolean): AutoIncrementConstraintSchemaAPI
columnSchemaApi.autoIncrement(constraintToggle?: boolean): AutoIncrementConstraintSchemaAPI⚙️ Spec:
-
constraintToggle(boolean, optional): when provided, toggles the existence of theAUTO_INCREMENTconstraint on the column. When ommitted, gets the column'sAUTO_INCREMENTconstraint instance returned if exists. - Return value:
AutoIncrementConstraintSchemaAPI- the existingAUTO_INCREMENTconstraint instance on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaAPI => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').autoIncrement()) {
// Add an autoIncrement constraint on column_1
tableSchemaApi.column('column_1').autoIncrement(true);
}
});
Add the ON_UPDATE clause to the column or get the column's current ON_UPDATE constraint instance. (Translates to the MySQL-specific ON UPDATE clause for timestamp/datetime columns.)
columnSchemaApi.onUpdate(constraintToggle?: OnUpdateClauseSpec): OnUpdateClauseSchemaAPI
columnSchemaApi.onUpdate(constraintToggle?: OnUpdateClauseSpec): OnUpdateClauseSchemaAPI⚙️ Spec:
-
constraintToggle(OnUpdateClauseSpec, optional): when provided, an object that defines a new constraint to create, specifying the intended SQL expression. When ommitted, gets theON_UPDATEclause returned if exists. - Return value:
OnUpdateClauseSchemaAPI- the existingON_UPDATEclause on the column or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Be sure that this doesn't already exist on column_1
if (!tableSchemaApi.column('column_1').onUpdate()) {
// Add an autoIncrement constraint on column_1
tableSchemaApi.column('column_1').onUpdate('CURRENT_TIMESTAMP');
}
});
Add a Primary Key, Foreign Key, Unique Key, Check, or other constraint, to the column or get an existing one. (Provides a unified way to set/get column constraints.)
columnSchemaApi.constraint(constraintType: string, constraintToggleOrJson?: boolean | object): ColumnConstraintSchemaAPI
columnSchemaApi.constraint(constraintJson: ColumnConstraintSchemaType): ColumnConstraintSchemaAPI
columnSchemaApi.constraint(constraintType: string, constraintToggleOrJson?: boolean | object): ColumnConstraintSchemaAPIcolumnSchemaApi.constraint(constraintJson: ColumnConstraintSchemaType): ColumnConstraintSchemaAPI⚙️ Spec:
-
constraintType(string): One ofPRIMARY_KEY,FOREIGN_KEY,UNIQUE_KEY,CHECK,DEFAULT,EXPRESSION,NOT_NULL,NULL,IDENTITY,AUTO_INCREMENT,ON_UPDATE. When provided as only argument, gets the existing constraint on the column returned. When in conjucntion withconstraintToggleOrJson, gets the constraint added to the column. -
constraintToggleOrJson(boolean | ColumnConstraintSchemaType, optional): as explained forconstraintToggle/constraintJsonin the individual constraint sections above. -
constraintJson(ColumnConstraintSchemaType): as explained forconstraintJsonin the individual constraint sections above. - Return value:
ColumnConstraintSchemaAPI- the constraint requested or the one just added.
⚽️ Usage:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
const col1 = tableSchemaApi.column('column_1');
// See if we already have a PRIMARY_KEY constraint on the column. Create one if not
if (!col1.constraint('PRIMARY_KEY')) {
// Add PRIMARY_KEY
col1.constraint('PRIMARY_KEY', true);
// Or: col1.constraint({ type: 'PRIMARY_KEY' });
}
});The getter/setter APIs to the various column-level constraints.
type ColumnConstraintSchemaAPI = PrimaryKeySchemaAPI | ForeignKeySchemaAPI | UniqueKeySchemaAPI | CheckConstraintSchemaAPI | DefaultConstraintSchemaAPI | ExpressionConstraintSchemaAPI | IdentityConstraintSchemaAPI | NotNullConstraintSchemaAPI | NullConstraintSchemaAPI | AutoIncrementConstraintSchemaAPI | OnUpdateClauseSchemaAPISee details
interface PrimaryKeySchemaAPI extends AbstractSchemaAPI {}interface ForeignKeySchemaAPI extends AbstractSchemaAPI {
// Set/get the target table
targetTable(value?: string | string[]): Identifier;
// Set/get the target columns
targetColumns(value?: string[]): Array;
// Set/get the match rule
matchRule(value?: string): string;
// Set/get the update rule
updateRule(value?: string | { rule: string, columns: string[] }): string | { rule: string, columns: string[] };
// Set/get the delete rule
deleteRule(value?: string | { rule: string, columns: string[] }): string | { rule: string, columns: string[] };
}interface UniqueKeySchemaAPI extends AbstractSchemaAPI {}interface CheckConstraintSchemaAPI extends AbstractSchemaAPI {
// Set/get the SQL expression
expr(value?: string): string;
}interface DefaultConstraintSchemaAPI extends AbstractSchemaAPI {
// Set/get the SQL expression
expr(value?: string): string;
}interface ExpressionConstraintSchemaAPI extends AbstractSchemaAPI {
// Set/get the SQL expression
expr(value?: string): string;
// Set/get the "stored" false
stored(value?: boolean): boolean;
}interface IdentityConstraintSchemaAPI extends AbstractSchemaAPI {
// Set/get the "always" rule
always(value?: boolean): boolean;
}interface NotNullConstraintSchemaAPI extends AbstractSchemaAPI {}interface NullConstraintSchemaAPI extends AbstractSchemaAPI {}interface AutoIncrementConstraintSchemaAPI extends AbstractSchemaAPI {}interface OnUpdateClauseSchemaAPI extends AbstractSchemaAPI {
// Set/get the SQL expression
expr(value?: string): string;
}Jump to
AbstractSchemaAPI
AbstractSchema is a base class inheritted by all Schema APIs - e.g. DatabaseSchemaAPI, TableSchemaAPI, ColumnSchemaAPI.
See content
Set or get the name the schema instance.
instance.name(value?: string): string | this
instance.name(value?: string): string | this⚙️ Spec:
-
value(string, optional): when provided, the name of the schema instance. When ommitted, returns the current name. - Return value:
string- the current name, orthis- the schema instance.
⚽️ Usage:
Set or get the name of a ColumnSchemaAPI instance:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
// Get the name
console.log(tableSchemaApi.column('column_1').name()); // column_1
// Rename
tableSchemaApi.column('column_2').name('new_column_2');
});
Render the Schema instance to a JSON object.
instance.toJson(): object
instance.toJson(): object⚙️ Spec:
- Return value: an object corresponding to the instance's JSON equivalent in
schema.json.
⚽️ Usage:
Render a TableSchemaAPI to JSON:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
tableSchemaApi.column('column_1').primaryKey(true); // Designate existing column "column_1" as primary key
tableSchemaApi.column('column_2'); // Drop index_2
// Now inspect what you've done so far
console.log(tableSchemaApi.toJson());
});
Render the Schema instance to SQL.
instance.toString(): string
instance.toString(): string⚙️ Spec:
- Return value: an SQL representation of the instance.
⚽️ Usage:
Render a TableSchemaAPI to SQL:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
tableSchemaApi.column('column_1').primaryKey(true); // Designate existing column "column_1" as primary key
tableSchemaApi.column('column_2'); // Drop index_2
// Now inspect what you've done so far
console.log(tableSchemaApi.toString());
});
Specify whether to keep or drop the schema instance, or get the current keep status.
instance.keep(toggle?: boolean): this
instance.keep(toggle?: boolean): this⚙️ Spec:
-
toggle(boolean, optional): when provided, toggles the keep status of the schema. When ommitted returns the current keep status of the schema. - Return value:
boolean- the current status, orthis- the schema instance.
⚽️ Usage:
Drop a Column:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
tableSchemaApi.column('column_2').keep(false);
});
Set the schema instance to the keep === false state.
instance.drop(): this
instance.drop(): this⚙️ Spec:
- Return value:
this- the schema instance.
⚽️ Usage:
Drop a Column:
const savepoint = await database.alterTable('table_1', tableSchemaApi => {
tableSchemaApi.column('column_2').drop();
});