|
| 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. |
0 commit comments