File tree Expand file tree Collapse file tree 1 file changed +45
-2
lines changed
Expand file tree Collapse file tree 1 file changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -87,10 +87,53 @@ Apply the same information like you see below in the screenshot.
8787
8888![ console] ( console.png )
8989
90- > Now you have created a complete new endpoint in your api for the resource pets
90+ > Now you have created a complete new endpoint in your api for the resource pets.
91+
92+ We have to add the relationship between users an pets. Open the created migration file and replace the user property with these lines.
93+ ```
94+ table.integer('user_id').unsigned();
95+ table.foreign('user_id').references('id').inTable('users').onDelete('cascade');
96+ ```
97+
98+ Next we have to add this relationship also in the pets model.
99+ ```
100+ public user(): User {
101+ return this.belongsTo(User);
102+ }
103+ ```
104+
105+ > The relationship between the users and pets are set and ready. So you can migrate your database with ` npm run db:migrate `
91106
92107### Step 5: Create a Seeder
93- TODO
108+ To generate some useful test data we need a smart factory for the pets. So open the ./src/database/factories/index.ts and add this code.
109+ ```
110+ /**
111+ * PET - Factory
112+ */
113+ factory.define(Pet, (faker: Faker.FakerStatic, args: any[]) => {
114+ const type = args[0];
115+ return {
116+ name: faker.name.firstName(),
117+ type: type || 'dog',
118+ userId: factory.get(User).returning('id')
119+ };
120+ });
121+ ```
122+
123+ > So you have a pet factory to create sample data, but we need a seeder to run this awesome factory.
124+
125+ Run this command in your terminal and call the new seeder "create pets".
126+ ```
127+ npm run console make:seed
128+ ```
129+
130+ Open the file and place this code into it.
131+ ```
132+ await factory.get(Pet)
133+ .create(10);
134+ ```
135+
136+ > Now we can seed some nice cats into the database with ` npm run db:seed ` .
94137
95138## Scripts / Tasks
96139All script are defined in the package.json file, but the most important ones are listed here.
You can’t perform that action at this time.
0 commit comments