Overview
Last updated
Last updated
QB ships with a schema builder to help you build your database objects. This provides a few benefits:
The syntax is expressive and fluent, making it easy to understand what is being executed
The syntax is database-agnostic. Specific quirks are isolated in a Grammar file, making it easy to migrate between engines.
You start with a SchemaBuilder
object. The SchemaBuilder
takes the same Grammar that a QueryBuilder
takes. It can additionally take a struct of default query options forwarded on to queryExecute
and a defaultSchema
to use when calling hasTable
and hasColumn
. (A schema
argument passed to those methods still takes precendence.)
Note: the
SchemaBuilder
is a transient, and a new one should be created for each operation.
The SchemaBuilder
has four main methods to start your database object creation:
Create a new table in the database.
The majority of the work comes from calling methods on the Blueprint
object. A Blueprint
defines the columns and indexes for your tables.
Example:
SchemaBuilder
SQL (MySQL)
Alter an existing table in the database.
In addition to using the columns and indexes off of the passed-in Blueprint
object, the Blueprint
contains helpers such as addConstraint
, removeConstraint
, addColumn
, renameColumn
, and dropColumn
to assist in altering existing tables.
Example:
SchemaBuilder
SQL (MySQL)
Drop a table from the database.
Example:
SchemaBuilder
SQL (MySQL)
SchemaBuilder
as well:rename
Rename a table from an old name to a new name
Example:
SchemaBuilder
SQL (MySQL)
hasTable
Check if a table exists in the database.
Example:
SchemaBuilder
SQL (MySQL)
hasColumn
Check if a column exists in a table in the database.
Example:
SchemaBuilder
SQL (MySQL)
Argument
Type
Required
Default
Description
table
string
true
The name of the table to create.
callback
function
true
A callback function used to define the table body. It is passed a Blueprint
as the only argument.
options
struct
false
{}
Options to pass to queryExecute
.
execute
boolean
false
true
Run the query immediately after building it.
Argument
Type
Required
Default
Description
table
string
true
The name of the table to alter.
callback
function
true
A callback function used to define the changes to the table. It is passed a Blueprint
as the only argument.
options
struct
false
{}
Options to pass to queryExecute
.
execute
boolean
false
true
Run the query immediately after building it.
Argument
Type
Required
Default
Description
table
string
true
The name of the table to drop.
options
struct
false
{}
Options to pass to queryExecute
.
execute
boolean
false
true
Run the query immediately after building it.
Argument
Type
Required
Default
Description
from
string
true
The old table name.
to
string
true
The new table name.
options
struct
false
{}
Options to pass to queryExecute
.
execute
boolean
false
true
Run the query immediately after building it.
Argument
Type
Required
Default
Description
name
string
true
The name of the table to check.
options
struct
false
{}
Options to pass to queryExecute
.
execute
boolean
false
true
Run the query immediately after building it.
Argument
Type
Required
Default
Description
table
string
true
The name of the table to check for the column in.
column
string
true
The column to check for in the table.
options
struct
false
{}
Options to pass to queryExecute
.
execute
boolean
false
true
Run the query immediately after building it.