Skip to content

Commit a8f8dc1

Browse files
committed
feat: add documentation for db files
1 parent dda425e commit a8f8dc1

File tree

5 files changed

+918
-1
lines changed

5 files changed

+918
-1
lines changed

future/files.md

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
# Database Files
2+
3+
Leaf MVC inherited all the teachings of Laravel and Ruby on Rails, including the use of migrations, seeders, and factories which made creating, testing and seeding databases a breeze. On top of that, Leaf MVC also introduced a new concept called schema files which allowed you to generate migrations from JSON data. While this was a great feature, it was a bit too much for a lot of developers and added to the growing hell of files in your app. To solve this, we've decided to move away from the Rails/Laravel way of doing things and introduce a new way of handling database files in Leaf MVC.
4+
5+
## What are Database Files?
6+
7+
Database files are a way to handle migrations, seeders, and factories in a single file. This way, you can easily manage your database structure without having to create multiple files for each operation and without having to repeat yourself all over your app. Database files are written in yaml which makes them incredibly easy to read and write.
8+
9+
```yml [flights.yml]
10+
defaultId: true
11+
timestamps: true
12+
columns:
13+
to: string
14+
from: string
15+
identifier: string
16+
17+
seeds:
18+
count: 10
19+
data:
20+
to: faker.city
21+
from: faker.city
22+
identifier: faker.uuid
23+
```
24+
25+
## Creating a Database File
26+
27+
Aloe comes with a `g:db` command that you can use to generate a database file. You can generate a database file by running:
28+
29+
```bash:no-line-numbers
30+
php leaf g:db users
31+
```
32+
33+
This will create a database file at `app/database/users.yml` which looks like this:
34+
35+
```yml [users.yml]
36+
defaultId: true
37+
timestamps: true
38+
columns:
39+
name: string
40+
email:
41+
type: string
42+
length: 255
43+
unique: true
44+
password: string
45+
email_verified_at: timestamp
46+
47+
seeds:
48+
count: 10
49+
data:
50+
name: faker.name
51+
email: faker.email
52+
password: "{{ bcrypt('password') }}"
53+
```
54+
55+
Breaking down this file, we have:
56+
57+
- `defaultId`: This is used to set the default id of the table. If set to `true`, the table will have an auto-incrementing id. If set to `false`, the table will not have an id.
58+
59+
- `timestamps`: This is used to set timestamps on the table. If set to `true`, the table will have `created_at` and `updated_at` columns. If set to `false`, the table will not have timestamps.
60+
61+
- `columns`: This is used to set the columns of the table. The key is the column name and the value is a key value pair of column properties. The available properties are:
62+
- `type`: The type of the column. This can be `string`, `integer`, `timestamp` or any type supported by Laravel's Eloquent.
63+
- `length`: The length of the column. This is only used for `string` columns.
64+
- `unique`: This is used to set the column as unique.
65+
- `nullable`: This is used to set the column as nullable.
66+
- `default`: This is used to set the default value of the column.
67+
- `autoIncrement`: This is used to set the column as auto-incrementing.
68+
- `unsigned`: This is used to set the column as unsigned.
69+
- `index`: This is used to set the column as an index.
70+
- `primary`: This is used to set the column as the primary key.
71+
- `foreign`: This is used to set the column as a foreign key. The value of this key is the table and column the column is a foreign key to.
72+
- `onDelete`: This is used to set the `ON DELETE` constraint of the foreign key.
73+
- `onUpdate`: This is used to set the `ON UPDATE` constraint of the foreign key.
74+
75+
- `seeds`: This is used to set the seeders of the table. The available properties are:
76+
- `count`: This is used to set the number of seeds to generate.
77+
- `data`: This is used to set the data of the seeds. The key is the column name and the value is the value of the column. You can use `faker.[value]` to generate fake data for the column. You can also use `{{ [value] }}` to use PHP code.
78+
- `truncate`: This is used to truncate the table before seeding.
79+
80+
## DB Migrations
81+
82+
Traditionally, migrations are used to create database tables and modify them. In Leaf MVC, you can create migrations in your database files. The `columns` key in your database file is used to create migrations. Here's an example of a migration:
83+
84+
```yml [users.yml]
85+
columns:
86+
name: string
87+
email:
88+
type: string
89+
length: 255
90+
unique: true
91+
password: string
92+
email_verified_at: timestamp
93+
```
94+
95+
In this example, we create a migration that creates a `users` table with `name`, `email`, `password`, and `email_verified_at` columns. To run your migrations, you can use the `db:migrate` command:
96+
97+
```bash:no-line-numbers
98+
php leaf db:migrate
99+
```
100+
101+
<!-- ## DB File Scripts
102+
103+
We understand that you might have some complicated functionality that you would want to run when migrating your database, which is why we allow you to run PHP scripts in your database files. This way, you can run any PHP code you want when migrating your database.
104+
105+
```yml [users.yml]
106+
```
107+
108+
Now you need to create the PHP script that will run when migrating your database. You can create a PHP script at `app/database/scripts/users.php`:
109+
110+
```php [users.php]
111+
```
112+
113+
In this example, we're running a PHP script that creates a new table in the database, but checks if particular columns exist before creating the table. -->
114+
115+
## DB Seeders
116+
117+
Seeders are used to populate your database with dummy data. In Leaf MVC, you can create seeders in your database files. The `seeders` key in your database file is used to create seeders. Here's an example of a seeder:
118+
119+
```yml [users.yml]
120+
seeds:
121+
data:
122+
- name: 'Example User'
123+
124+
password: "{{ bcrypt('password') }}"
125+
```
126+
127+
In this example, we create a seeder that seeds the `users` table with an example user. We are passing an array of seeds to the `data` key, each seed being a key value pair of column name and value.
128+
129+
If you want to generate multiple seeds, you can pass an object to the `data` key instead of an array together with a `count` key:
130+
131+
```yml [users.yml]
132+
seeds:
133+
count: 10
134+
data:
135+
name: 'Example User'
136+
137+
password: "{{ bcrypt('password') }}"
138+
```
139+
140+
After creating your seeder, you can run your seeders using the `db:seed` command:
141+
142+
```bash:no-line-numbers
143+
php leaf db:seed
144+
```
145+
146+
This will generate 10 seeds for the `users` table with the same data which is not very useful. To generate multiple fake seeds, you can use what other frameworks call a factory.
147+
148+
In Leaf MVC, factories and seeders are the same thing as we believe this confusion is unnecessary. If you want to generate fake data for your seeders, you can add `faker.[value]` as the value of a column in your seeder. Here's an example:
149+
150+
```yml{4,5} [users.yml]
151+
seeds:
152+
count: 10
153+
data:
154+
name: faker.name
155+
email: faker.email
156+
password: "{{ bcrypt('password') }}"
157+
```
158+
159+
In this example, we're generating 10 fake seeds for the `users` table.

src/docs/database/factories.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,101 @@
11
# DB Factories
2+
3+
Factories are a way to conveniently generate large amounts of database records. Instead of manually specifying the attributes for each model seed, you can use factories to define the attributes for each model. This way, you can easily generate a large number of records with random data.
4+
5+
## Creating a Factory
6+
7+
To create a factory, you can use the `g:factory` command. This command will create a new factory class in the `app/database/factories` directory.
8+
9+
```bash
10+
php leaf g:factory UserFactory
11+
```
12+
13+
This will create a new factory class in the `app/database/factories` directory. The factory class will contain a `definition` method that returns an array of attributes for the model. You can define the attributes for the model in this method.
14+
15+
```php
16+
<?php
17+
18+
namespace App\Database\Factories;
19+
20+
use App\Models\User;
21+
use Leaf\Factory;
22+
23+
class UserFactory extends Factory
24+
{
25+
/**
26+
* The name of the factory's corresponding model.
27+
*
28+
* @var string
29+
*/
30+
public $model = User::class;
31+
32+
/**
33+
* Define the model's default state.
34+
*
35+
* @return array
36+
*/
37+
public function definition()
38+
{
39+
return [
40+
'username' => strtolower($this->faker->firstName),
41+
'name' => $this->faker->name,
42+
'email' => $this->faker->unique()->safeEmail,
43+
'email_verified_at' => \Leaf\Date::now(),
44+
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
45+
'remember_token' => 'random string',
46+
];
47+
}
48+
}
49+
```
50+
51+
This factory class defines the attributes for the `User` model. The `definition` method returns an array of attributes for the model. You can use the `faker` property to generate random data for the attributes.
52+
53+
## Using a Factory
54+
55+
To use a factory to generate records, you can use the `create` method on the factory class. This method will create a new record in the database using the attributes defined in the factory.
56+
57+
```php
58+
(new UserFactory)->create(20)->save();
59+
```
60+
61+
You would usually do this in your seeders to generate records for your database. You can use the `create` method to generate a single record or pass in the number of records you want to generate.
62+
63+
```php
64+
<?php
65+
66+
namespace App\Database\Seeds;
67+
68+
use App\Database\Factories\UserFactory;
69+
use Illuminate\Database\Seeder;
70+
71+
class UsersSeeder extends Seeder
72+
{
73+
/**
74+
* Run the database seeds.
75+
*
76+
* @return void
77+
*/
78+
public function run()
79+
{
80+
(new UserFactory)->create(20)->save();
81+
}
82+
}
83+
```
84+
85+
This will generate 20 records in the `users` table using the attributes defined in the `UserFactory` class.
86+
87+
## Getting Faker Data
88+
89+
In some cases, you may not want to save the generated records to the database. You can use the `get()` method to get the generated data without saving it to the database.
90+
91+
```php
92+
$users = (new UserFactory)->create(20)->get();
93+
```
94+
95+
## Running Factories
96+
97+
You don't need to run factories manually. You can use the `db:seed` command to run your seeders, which will in turn run your factories.
98+
99+
```bash
100+
php leaf db:seed
101+
```

0 commit comments

Comments
 (0)