37
loading...
This website collects cookies to deliver better user experience
$ npx tode-cli create-project hello-world
.hello-world
, with the following folder structure:npm i
to install the node packages, then run npm run dev
to serve your app locally. Your app will be served at http://localhost:8080/
.http://localhost:8080/
via a client (web browser, postman, etc) will give the following response:example
route comes with the application. route
files to handle the routing in the app.
$ npx tode-cli add:controller <controller_name>
. This will scaffold a controller(route) file like the following:import { Request, Response, Router } from "express";
module.exports = () => {
const router = Router();
/**
* Create a new Item
*/
router.post("/", async (req, res) => {
return res.send('demo/ - POST');
});
/**
* Get all Items
*/
router.get("/", (req: Request, res: Response) => {
res.send("demo/ - GET");
});
/**
* Get an Item by Id
*/
router.get("/:id", (req: Request, res: Response) => {
res.send("demo/ - GET /id");
});
/**
* Update an Item
*/
router.patch("/:id", (req: Request, res: Response) => {
res.send("demo/ - PATCH /id");
});
return router;
};
demo
. This controller is created with handlers for some basic HTTP request methods such as GET
, POST
, PATCH
on the '/demo'
path. http://localhost:8080/demo
via a client(web browser, postman, etc). We should get the following response:http://localhost:8080
again we will see the demo
route in our list of routes. As you can see here, tode-cli apps offer a level of self-documentation for the routes within the application.$ npx tode-cli add:model <model_name>
. You'll get a generated model like the following:import BaseModel from "../BaseMode";
export class User extends BaseModel {
// Name of table that this model maps back to
// Table name is the only required property.
public static tableName = 'ENTER_TABLE_NAME';
// Example property
public foo!: string;
// Add other table fields (columns) as properties to access them via the model
// Define the relations to other models.
// READ MORE at https://vincit.github.io/objection.js/guide/relations.html
public static relationMappings = () => ({
// specify relation with other modules
})
}
User
. Models on have one required property, tableName
, which holds the name of the database table that the model represents. You can perform a query to fetch all users in your database table using await User.query()
. Read more about objection.js' queries here.relationMappings
is where you define your model's relationship with other models - basically representing your database relationships. Read more about objection.js' relationships here.$ npx tode add:service <service_name>
. When you create a service, a file will be generated like the following.import { ServiceReponse } from "../../config/constants";
class UserService {
private _foo = "foo";
constructor() {
//
}
get foo() {
return this._foo;
}
set foo(val: string) {
this._foo = val;
}
public foobar() {
//
}
}
const UserService = new UserService();
export { UserService };
UserService
. You can then add methods to perform any logic inside this file. You'll then call these methods from within a controller or another service. Services are also where you utilize your Models to perform database queries where necessary.$ npx tode-cli add:auth
.Users
table in your database/auth/login
/auth/register
$ npx knex migrate:latest
. Now authentication is fully integrated.