Creating new migration files on with the cluster environment is hard, because it automatically runs migraitons.
- Rename
.env.example
->.env
- In
.env
setupDATABASE_URL=postgres://localhost/headless_lms_dev
bin/local-dev-db-create-user
bin/local-dev-db-create
- Run
bin/sqlx-migrate-run
- (Optional)
bin/seed-local
- If migrations succeed, run
bin/dev
Creating new SQL queries in headless-lms using Sqlx requires running bin/sqlx-prepare
so that it builds.
In the headless_lms folder run:
bin/sqlx-migrate-add migration_name
Then write your migration in migrations/<>.up.sql
and write the reverse migration in migrations/<>.down.sql
.
Run migrations with bin/sqlx-migrate-run
or bin/sqlx-migrate-revert
.
When creating a new endpoint, please bear in mind that it should follow the Separation of Concerns.
Walkthrough how to create an endpoint for LMS.
As an example, let's create an endpoint /api/v0/bogus/foo/
, where bogus is our microservice, foo is our db model and endpoint.
This said, the endpoint should be used for the bogus
microservice and endpoints should return foo
s or take foo_id
as parameter.
- Create
foo.rs
in foldersrc/models/
, if not present - Create
foo.rs
in foldersrc/controllers/bogus/
, if not present - In
src/controllers/mod.rs
add the new microservice toconfigure_controllers
->.service(web::scope("/bogus").configure(_add_bogus_routes))
, if not present. - Write the new
_add_bogus_routes
function in thesrc/controllers/bogus/mod.rs
file and create necessary submodules. (Hint: See existing, example below).
/*!
Handlers for HTTP requests to `/api/v0/bogus`.
This documents all endpoints. Select a module below for a category.
*/
pub mod foo;
use actix_web::web::{self, ServiceConfig};
use self::{foo::_add_foo_routes};
/// Add controllers from all the submodules.
pub fn add_bogus_routes(cfg: &mut ServiceConfig) {
cfg.service(web::scope("/foo").configure(_add_foo_routes));
}
- In
src/controllers/bogus/foo.rs
add the routes in e.g._add_foo_routes
, so if you would like to create a CRUD forfoo
, you would add 4 routes as following:
pub fn _add_foo_routes(cfg: &mut ServiceConfig) {
cfg.route("", web::get().to(get_all_foos))
.route("", web::post().to(post_new_foo))
.route("/{foo_id}", web::put().to(update_foo))
.route("/{foo_id}", web::delete().to(delete_foo))
}
The name starts with an underline in order to appear before other functions in the module documentation.
We add the routes by calling the route method instead of using the route annotations, because this method preserves the function signatures for documentation.
- Write the CRUD route methods (get_all_foos, post_new_foo etc.) in the same file.
- Write the logic of what the endpoint does in
src/models/foo.rs
and if needed, in other models as well if more complex endpoint. - If needed, create a seed at
headless-lms/db/seed.sql
and seed your database. - Using
headless-lms/request.rest
ensure that your endpoint works and document manually the request/response above the function insrc/controllers/bogus/foo.rs
.
Passing enum values as parameters to SQL queries: https://docs.rs/sqlx/0.5.5/sqlx/macro.query.html#type-overrides-bind-parameters-postgres-only
- Convert the string to a raw string
- Move the statement into its own line
- Select the whole lines that contain the sql statement
- Use the
SQLTools: Format Selected Query For Any Document
action fromctrl-shift-p
-menu.