Confused about Models and factories #1744
-
I have followed the docs and was able to create a controller, a simple test response and db migration. What I am missing is how to tie all of the pieces together with fake data. My rest api template has files in App/Models. I can't find it documented anywhere. Do I need it? Here's my controller async show({ params, request, response, view }) {
response.json({ content: "cool" + params.id });
// what steps do I need to generate fake data and return them here?
// migration code already creates Foos table
}
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Okay. Let's do it step by step. ModelsMake sure you have models created inside await User.all() // select * from users;
await User.findBy(params.id) // select * from users where id = ?; Fake dataFake data can come from anywhere. For example: You can insert fake data from a JSON file to your database. Or you can use factories to do that. Now of course adding fake data to the database action has to happen without creating any route (HTTP endpoint). So we will use seeders for that. Here's the documentation on seeders https://preview.adonisjs.com/guides/database/seeds Things to keep in mind
|
Beta Was this translation helpful? Give feedback.
-
Thanks. Eventually I found out about Model in the Lucid section further down. I think what I (or many newbies) need is a task-oriented Getting Started guide without being exposed to too many advanced features and options. Right what I need to know to build an end-to-end API endpoint connecting to a Postgres db, the Adonis way, is sprinkled throughout multiple sections. Just some constructive feedback. When I get a better handle of the framework I'd be happy to contribute a simple tutorial. |
Beta Was this translation helpful? Give feedback.
Okay. Let's do it step by step.
Models
Make sure you have models created inside
app/Models
directory. The job of a model is to query a single database table. For example: TheUser
will conventionally query theusers
database table. For Example:Fake data
Fake data can come from anywhere. For example: You can insert fake data from a JSON file to your database. Or you can use factories to do that.
Now of course adding fake data to the database action has to happen without creating any route (HTTP endpoint). So we will use seeders for that. Here's the documentation on seeders https://pr…