Skip to content

Commit 7ee3a62

Browse files
committed
feat: add schema file docs
1 parent 095b144 commit 7ee3a62

File tree

2 files changed

+58
-33
lines changed

2 files changed

+58
-33
lines changed

.vitepress/config/sidebar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ const sidebar = [
156156
{ text: 'Models', link: '/docs/database/models' },
157157
{ text: 'Migrations', link: '/docs/database/migrations' },
158158
{ text: 'JSON Schema', link: '/docs/database/schema' },
159-
// { text: 'Schema Files', link: '/docs/database/files' },
159+
{ text: 'Schema Files', link: '/docs/database/files' },
160160
{ text: 'Seeders', link: '/docs/database/seeders' },
161161
{ text: 'Factories', link: '/docs/database/factories' },
162162
{ text: 'Writing Commands', link: '/docs/mvc/commands' },

.vitepress/future/files.md renamed to src/docs/database/files.md

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
# Schema Files
1+
# Schema Files <Badge type="warning">ALPHA v4+</Badge>
22

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. It even introduced schema files, allowing you to generate migrations from JSON data. While helpful, this added complexity and clutter to projects. To simplify things, we’re moving away from the Rails/Laravel approach and creating a more streamlined, Leaf-like solution.
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. While this is great and has been tried and tested over the years, having multiple files for a single database table can be a bit of a hassle. This is why we introduced Schema Files in Leaf MVC v4.
44

55
## What are Schema Files?
66

77
Schema files build on the JSON schema idea we introduced in earlier Leaf MVC versions, but they take things further. Instead of juggling separate files for migrations, seeders, and factories, you can handle everything in one place. They’re written in YAML, so they’re easy to read and work with—no extra hassle, no repeating yourself.
88

99
```yml [flights.yml]
10-
increments: true
11-
timestamps: true
1210
columns:
1311
to: string
1412
from: string
@@ -17,9 +15,9 @@ columns:
1715
seeds:
1816
count: 10
1917
data:
20-
to: 'faker:city'
21-
from: 'faker:city'
22-
identifier: 'faker:uuid'
18+
to: '@faker.city'
19+
from: '@faker.city'
20+
identifier: '@faker.uuid'
2321
```
2422
2523
## Creating a Schema File
@@ -33,36 +31,41 @@ php leaf g:schema <table-name>
3331
Remember, every schema file is tied to a table in your database. When you run the command above, Leaf will create a schema file in your `app/database` directory with the name `<table-name>.yml`. Here’s an example:
3432

3533
```bash:no-line-numbers
36-
php leaf g:schema users
34+
php leaf g:schema posts
3735
```
3836

39-
This will create a schema file at `app/database/users.yml` which looks like this:
37+
This will create a schema file at `app/database/posts.yml` which looks like this:
4038

41-
```yml [users.yml]
42-
increments: true
43-
timestamps: true
39+
```yml [posts.yml]
40+
# schema files add auto-increments and timestamps by default
41+
42+
# you can add all the columns you want under the columns key
4443
columns:
4544
name: string
46-
email:
45+
identifier:
4746
type: string
48-
length: 255
4947
unique: true
50-
password: string
51-
email_verified_at: timestamp
48+
verified_at:
49+
type: timestamp
50+
nullable: true
5251
52+
# you can add foreign ids for other models under the relationships key key
53+
relationships:
54+
- User
55+
56+
# seeds are optional and can be used to populate the database with dummy data
5357
seeds:
54-
count: 10
58+
count: 5
59+
truncate: true
5560
data:
5661
name: 'faker:name'
57-
email: 'faker:email'
62+
email: 'faker:unique:safeEmail'
63+
email_verified_at: 'tick:now'
5864
password: 'hash:password'
65+
remember_token: 'randomString:10'
5966
```
6067

61-
Breaking down this file, we have:
62-
63-
- `increments`: 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, and you can set your own primary key.
64-
65-
- `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.
68+
Breaking this file down, there are three main sections:
6669

6770
- `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:
6871
- `type`: The type of the column. This can be `string`, `integer`, `timestamp` or any type supported by Laravel's Eloquent.
@@ -80,9 +83,31 @@ Breaking down this file, we have:
8083

8184
- `seeds`: This is used to set the seeders of the table. The available properties are:
8285
- `count`: This is used to set the number of seeds to generate.
83-
- `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, but this is a separate PHP thread which means you can't use variables from the current scope. -->
86+
- `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, but this is a separate PHP thread which means you can't use variables from the current scope. -->
8487
- `truncate`: This is used to truncate the table before seeding.
8588

89+
- `relationships`: This is used to set the relationships of the table. The value is an array of models the table is related to. This is used to generate foreign keys for the table.
90+
91+
Besides these, Schema files also set a lot of defaults for you. For instance, the `id` column is set as the primary key and auto-incrementing by default. Timestamps are also added by default. You can override these defaults by adding the `id` and `timestamps` keys to your schema file. Here's an example:
92+
93+
```yml:no-line-numbers [posts.yml]
94+
increments: false # this will remove the auto-incrementing id column
95+
timestamps: false # this will remove the timestamps columns
96+
```
97+
98+
Once you turn off auto-increments, you can add your own `id` column. Here's an example:
99+
100+
```yml:no-line-numbers [posts.yml]
101+
increments: false
102+
103+
columns:
104+
id:
105+
type: integer
106+
primary: true
107+
108+
...
109+
```
110+
86111
## Database tables
87112

88113
Traditionally, migrations are used to create database tables and modify them. In Leaf MVC, every schema file is tied to a particular table which is the name of the file. All you need to do is modify the columns of the table using the `columns` key in your schema file. Here's an example:
@@ -162,17 +187,17 @@ In the end, this means you can continue to use `php leaf db:rollback` to roll ba
162187

163188
Database seeds are a way to populate a database with initial data. This initial data can be used to set up default values or pre-populate a database with test data. Database seeds typically contain small amounts of data, such as default settings, test data, or sample records.
164189

165-
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:
190+
Seeders are used to populate your database with dummy data. In Leaf MVC, you can create seeders in your database files. The `seeds` key in your database file is used to create seeders. Here's an example of a seeder:
166191

167192
```yml [users.yml]
168193
seeds:
169194
data:
170195
- name: 'Example User'
171196
172-
password: 'hash:password'
197+
password: '@hash:passwordForThisUser' # @hash requires leafs/password
173198
- name: 'Another User'
174199
175-
password: 'hash:password'
200+
password: '@hash:passwordForThisUser' # @hash requires leafs/password
176201
```
177202

178203
In this example, we create a seeder that seeds the `users` table with two example users. We are passing an array of seeds to the `data` key, each seed being a key value pair of column name and value.
@@ -185,7 +210,7 @@ seeds:
185210
data:
186211
name: 'Example User'
187212
188-
password: 'hash:password'
213+
password: '@hash:password'
189214
```
190215

191216
After creating your seeder, you can run your seeders using the `db:seed` command:
@@ -196,15 +221,15 @@ php leaf db:seed
196221

197222
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.
198223

199-
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:
224+
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:
200225

201226
```yml{4,5} [users.yml]
202227
seeds:
203228
count: 10
204229
data:
205-
name: 'faker:name'
206-
email: 'faker:email'
207-
password: 'hash:password'
230+
name: '@faker.name'
231+
email: '@faker.email'
232+
password: '@hash:password'
208233
```
209234

210235
In this example, we're generating 10 fake seeds for the `users` table.

0 commit comments

Comments
 (0)