@travetto/scaffold
v5.0.21
Published
App Scaffold for the Travetto framework
Downloads
665
Maintainers
Readme
App Scaffold
App Scaffold for the Travetto framework
A simple tool for scaffolding a reference project. To get started, you need to make sure:
Terminal: Setting up the necessary config
$ git config --global.username <Username> #Set your git username
Once the necessary configuration is setup, you can invoke the scaffolding by running
Terminal: Running Generator
$ npx @travetto/scaffold
# or
$ npx @travetto/scaffold@<version-or-tag>
The generator will ask about enabling the following features:
Restful Architecture
The RESTful API provides the necessary integration for exposing restful apis. When selecting the rest
feature, you will need to specify which backend you want to include with your application, the default being express. Currently you can select from:
- express
- koa
- fastify
The code will establish some basic routes, specifically,
GET /
as the root endpoint. This will return the contents of yourpackage.json
as an identification operation.
Additional Rest Features
In addition to the core functionality, the rest
feature has some useful sub-features. Specifically:
OpenAPI Specification support for the restful api. This will automatically expose a openapi.yml
endpoint, and provide the necessary plumbing to support client generation.
Logging support for better formatting, debug like support, and colorized output. This is generally useful for server logs, especially during development.
Authentication
Authentication is also supported on the Restful endpoints by selecting Rest Auth during setup. This will support basic authentication running out of local memory, with user REST Sessions.
Testing
Testing can also be configured out of the box to provide simple test cases for the data model.
Data Modelling and Storage
The Data Modeling Support allows for modeling of application data, and provides mechanisms for storage and retrieval. When setting up your application, you will need to select which database backend you want to use:
Code: Todo Model
import { Model, ModelType } from '@travetto/model';
@Model()
export class Todo implements ModelType {
id: string;
text: string;
completed?: boolean;
userId?: string;
}
Basic tests are also included for the model
to verify that database interaction and functionality is working properly.
Rest + Model
In the case both rest
and model
features are enabled, the code will produce a controller that exposes the Todo model via restful patterns.
Code: Todo controller
import { Controller, Get, Put, Post, Delete } from '@travetto/rest';
import { NotFoundError } from '@travetto/model';
import { Inject } from '@travetto/di';
import { ModelQuery, ModelQueryCrudSupport } from '@travetto/model-query';
import { Schema } from '@travetto/schema';
import { Todo } from './model';
@Schema()
class Query {
q: object = {};
}
/**
* Controller for managing all aspects of the Todo lifecycle
*/
@Controller('/todo')
export class TodoController {
@Inject()
source: ModelQueryCrudSupport;
/**
* Get all Todos
*/
@Get('/')
async getAll(query: Query): Promise<Todo[]> {
query.q ??= {};
return this.source.query(Todo, { where: query.q });
}
/**
* Get Todo by id
*/
@Get('/:id')
async getOne(id: string): Promise<Todo> {
const q: ModelQuery<Todo> = { where: { id } };
if (typeof q.where !== 'string') {
}
return this.source.queryOne(Todo, q);
}
/**
* Create a Todo
*/
@Post('/')
async save(todo: Todo): Promise<Todo> {
return this.source.create(Todo, todo);
}
/**
* Update a Todo
*/
@Put('/:id')
async update(todo: Todo): Promise<Todo> {
return this.source.update(Todo, todo);
}
/**
* Delete a Todo
*/
@Delete('/:id')
async remove(id: string): Promise<void> {
const q: ModelQuery<Todo> = { where: { id } };
if (typeof q.where !== 'string') {
}
if (await this.source.deleteByQuery(Todo, q) !== 1) {
throw new NotFoundError(Todo, id);
}
}
}
Running
Once finished the application will reflect the modules chosen, and will be ready for execution, if you have configured a runnable application. Currently, this requires the rest
feature to be selected.
Terminal: Starting the App
npm start