Schema
Schema is a simple, yet powerful tool for generating database migrations from JSON data. Instead of dealing with the stress of writing your database migrations from scratch and thinking about all the types of your data, you can simply create a JSON file with sample data and let Leaf do the rest.
Writing your schema
Schema can be found in the app/database/schema
folder. To get started, create a new JSON file in the the schemas directory. You can name it anything you want, but it's best to name it after the table you're creating as that is what Leaf will expect unless you specify otherwise.
We can start off by creating a users.json
file. All that this file should contain is an example of what your data should look like. For example:
{
"id": 1,
"username?": "username",
"name": "Full Name",
"created_at": "",
"updated_at": ""
}
Using your schema
To use your schema, you can call Leaf\Schema::build
in your migration. It takes in the name of the schema file to build your migrations with.
...
use Leaf\Schema;
class CreateUsers extends Database {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::build("users");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$this->capsule::schema()->dropIfExists("users");
}
}
In the example above, the users
schema will be used to generate the migration. This means that the users
table will be created in your database with the fields specified in the schema. To actually run the migration, you can use the db:migrate
command.
php leaf db:migrate
Read more about migrations.
Data Types
Leaf Schema is flexible and allows you to specify the type of data you want to store in your database. For the most part, Leaf Schema will automatically detect the type of data you want to store, but you can also specify the type of data you want to store.
Automatic Types
Leaf Schema will automatically detect the type of data you want to store in your database. For example, if you want to store a string in your database, you can simply specify the string in your schema.
{
...
"username": "username"
}
In the example above, the username
field will be set to $table->string
in the migration. This is the same as using $table->string('username')
in your migration.
Automatic types are supported for the following types of data:
- string
- integer
- boolean
- float
- array (will be converted to
enum
in the migration) - json (should be a stringified json object)
Manually Adding Types
Leaf Schema supports all the types supported by Laravel's Schema Builder. You can specify the type of data you want to store in your database by using the type as the value of the field.
{
...
"username:text": "username"
}
In the example above, the username
field will be set to $table->text
in the migration. This is the same as using $table->text('username')
in your migration.
bigIncrements bigInteger binarybooleanchardateTimeTzdateTimedatedecimaldoubleenumfloatforeignIdforeignIdForforeignUlidforeignUuidgeometryCollectiongeometryidincrementsintegeripAddressjsonjsonblineStringlongTextmacAddressmediumIncrementsmediumIntegermediumTextmorphsmultiLineStringmultiPointmultiPolygonnullableMorphsnullableTimestampsnullableUlidMorphsnullableUuidMorphspointpolygonrememberTokensetsmallIncrementssmallIntegersoftDeletesTzsoftDeletesstringtexttimeTztimetimestampTztimestamptimestampsTztimestampstinyIncrementstinyIntegertinyTextunsignedBigIntegerunsignedDecimalunsignedIntegerunsignedMediumIntegerunsignedSmallIntegerunsignedTinyIntegerulidMorphsuuidMorphsuliduuidyear
Nullable Fields
If you want to specify that a field should be nullable, you can use the ?
symbol after the field name. This is the same as using $table->nullable()
in your migration.
{
...
"username?": "username"
}
Nullable + Types
If you want to specify that a field should be nullable and also specify the type of data you want to store, you can use the ?
symbol after the field name and also specify the type of data you want to store after the ?
symbol, using :
.
{
...
"username?:string": "username"
}
id
The id
type is used to specify that the field is an auto-incrementing primary key. This is the same as using $table->bigIncrements('id')
in your migration. Setting a field to id
will automatically set the field to be an auto-incrementing primary key.
{
"id": 1,
...
}
If you want to set a field to be an auto-incrementing primary key, but you don't want to set the field to id
, you can use the id
type in the key of the field using :
.
{
"user_id : id": 1,
...
}
In the example above, the user_id
field will be set to $table->bigIncrements
in the migration. This is the same as using $table->bigIncrements('user_id')
in your migration. Just as with the id
type, Leaf Schema allows you to specify the name of the field with the type using :
.
The spaces around the :
are optional, so you can also use user_id:id
.
timestamps
The timestamps
type is used to specify that the table should have created_at
and updated_at
fields. This is the same as using $table->timestamps()
in your migration.
{
...
"timestamps": ""
}
Foreign Keys
The *
type is used to specify that the field should be a foreign key. This is the same as using $table->foreignId
in your migration.
{
...
"user_id*": 1
}
Soft Deletes
To specify that a table should have soft deletes, you can use the softDeletes
key. This is the same as using $table->softDeletes()
in your migration.
{
...
"softDeletes": ""
}
Remember Tokens
To specify that a table should have a remember token, you can use the rememberToken
key. This is the same as using $table->rememberToken()
in your migration.
{
...
"rememberToken": ""
}