mirror of
https://github.com/silverqx/TinyORM.git
synced 2026-04-26 02:39:14 -05:00
added docs for seeding 📃
This commit is contained in:
@@ -17,6 +17,7 @@ The code is well tested with the unit and functional tests. Almost all the query
|
||||
- [Database: Getting Started](database.mdx#database-getting-started)
|
||||
- [Database: Query Builder](query-builder.mdx#database-query-builder)
|
||||
- [Database: Migrations](migrations.mdx#database-migrations)
|
||||
- [Database: Seeding](seeding.mdx#database-seeding)
|
||||
- [TinyORM: Getting Started](tinyorm.mdx#tinyorm-getting-started)
|
||||
- [TinyORM: Relationships](tinyorm-relationships.mdx#tinyorm-relationships)
|
||||
- [Building: TinyORM](building-tinyorm.mdx#building-tinyorm)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
sidebar_position: 9
|
||||
sidebar_position: 10
|
||||
description: Hello world example created in the terminal and QtCreator IDE.
|
||||
---
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
sidebar_position: 10
|
||||
sidebar_position: 11
|
||||
description: How to compile the TinyORM migrations (tom) c++ console application on Windows and Linux.
|
||||
---
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
sidebar_position: 8
|
||||
sidebar_position: 9
|
||||
hide_table_of_contents: true
|
||||
description: How to compile the TinyORM c++ library on Windows and Linux.
|
||||
---
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
---
|
||||
sidebar_position: 6
|
||||
description: Migrations are like version control for your database, allowing your team to define and share the application's database schema definition. Migrations use the Schema facade that provides database agnostic support for creating and manipulating tables across all of TinyORM's supported database systems.
|
||||
---
|
||||
|
||||
# Database: Seeding
|
||||
|
||||
- [Introduction](#introduction)
|
||||
- [Writing Seeders](#writing-seeders)
|
||||
- [Calling Additional Seeders](#calling-additional-seeders)
|
||||
- [Running Seeders](#running-seeders)
|
||||
|
||||
## Introduction
|
||||
|
||||
TinyORM includes the ability to seed your database with data using seed classes. All seed classes should be stored in the `database/seeders` directory. The `DatabaseSeeder` class is considered as the root seeder. From this class, you may use the `call` method to run other seed classes, allowing you to control the seeding order.
|
||||
|
||||
:::tip
|
||||
[Mass assignment protection](tinyorm.mdx#mass-assignment) is automatically disabled during database seeding.
|
||||
:::
|
||||
|
||||
## Writing Seeders
|
||||
|
||||
A seeder class only contains one method by default: `run`. This method is called when the `db:seed` tom command is executed. Within the `run` method, you may insert data into your database however you wish. You may use the [query builder](query-builder.mdx#insert-statements) to manually insert data.
|
||||
|
||||
As an example, let's modify the default `DatabaseSeeder` class and add a database insert statement to the `run` method:
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <tom/seeder.hpp>
|
||||
|
||||
namespace Seeders
|
||||
{
|
||||
|
||||
/*! Main database seeder. */
|
||||
struct DatabaseSeeder : public Seeder
|
||||
{
|
||||
/*! Run the database seeders. */
|
||||
void run() override
|
||||
{
|
||||
DB::table("users")->insert({
|
||||
{{"name", "1. user"}, {"email", "user1@example.com"}},
|
||||
{{"name", "2. user"}, {"email", "user2@example.com"}},
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Seeders
|
||||
|
||||
### Calling Additional Seeders
|
||||
|
||||
Within the `DatabaseSeeder` class, you may use the `call` method to execute additional seed classes. Using the `call` method allows you to break up your database seeding into multiple files so that no single seeder class becomes too large. The `call` method accepts the template parameter pack of seeder classes that should be executed:
|
||||
|
||||
/*! Run the database seeders. */
|
||||
void run() override
|
||||
{
|
||||
call<UserSeeder, PostSeeder, CommentSeeder>();
|
||||
}
|
||||
|
||||
## Running Seeders
|
||||
|
||||
You may execute the `db:seed` tom command to seed your database. By default, the `db:seed` command runs the `Seeders::DatabaseSeeder` class, which may in turn invoke other seed classes. However, you may use the `--class` option to specify a specific seeder class to run individually:
|
||||
|
||||
```bash
|
||||
tom db:seed
|
||||
|
||||
tom db:seed --class=UserSeeder
|
||||
```
|
||||
|
||||
You may also seed your database using the `migrate`, `migrate:fresh` or `migrate:refresh` commands in combination with the `--seed` option. For example the `migrate:fresh` command drops all tables and re-run all of your migrations. This command is useful for completely re-building your database:
|
||||
|
||||
```bash
|
||||
tom migrate:fresh --seed
|
||||
```
|
||||
|
||||
#### Forcing Seeders To Run In Production
|
||||
|
||||
Some seeding operations may cause you to alter or lose data. In order to protect you from running seeding commands against your production database, you will be prompted for confirmation before the seeders are executed in the `production` environment. To force the seeders to run without a prompt, use the `--force` flag:
|
||||
|
||||
```bash
|
||||
tom db:seed --force
|
||||
```
|
||||
@@ -1,5 +1,5 @@
|
||||
---
|
||||
sidebar_position: 7
|
||||
sidebar_position: 8
|
||||
description: TinyORM relationships are defined as methods on your TinyORM model classes. Since relationships also serve as powerful query builders, defining relationships as methods provides powerful method chaining and querying capabilities.
|
||||
---
|
||||
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
---
|
||||
sidebar_position: 6
|
||||
sidebar_position: 7
|
||||
description: TinyORM is an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using TinyORM, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, TinyORM models allow you to insert, update, and delete records from the table as well.
|
||||
---
|
||||
|
||||
|
||||
Reference in New Issue
Block a user