App Scaffold for the Travetto framework
npm install @travetto/scaffoldA simple tool for scaffolding a reference project. To get started, you need to make sure:
Terminal: Setting up the necessary config
``bash`
$ git config --global.username
Once the necessary configuration is setup, you can invoke the scaffolding by running
Terminal: Running Generator
`bash
$ npx @travetto/scaffold
$ npx @travetto/scaffold@
`
The generator will ask about enabling the following features:
as the root endpoint. This will return the contents of your package.json as an identification operation.$3
In addition to the core functionality, the web feature has some useful sub-features. Specifically:OpenAPI Specification support for the web 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 Web endpoints by selecting Web Auth during setup. This will support basic authentication running out of local memory.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:
* elasticsearch
* mongodb
* Postgres
* MySQL
* SQLite
* DynamoDB
* FirestoreA default model is constructed, a Todo class:
Code: Todo Model
`typescript
import { Model, type 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.Web + Model
In the case both web and model features are enabled, the code will produce a controller that exposes the Todo model via web patterns.Code: Todo controller
`typescript
import { Controller, Get, Put, Post, Delete } from '@travetto/web';
import { NotFoundError } from '@travetto/model';
import { Inject } from '@travetto/di';
import type { ModelQuery, ModelQueryCrudSupport } from '@travetto/model-query';
import { Schema } from '@travetto/schema';import { Todo } from './model.ts';
@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 {
query.q ??= {};
return this.source.query(Todo, { where: query.q });
}
/**
* Get Todo by id
*/
@Get('/:id')
async getOne(id: string): Promise {
const query: ModelQuery = { where: { id } };
if (typeof query.where !== 'string') {
}
return this.source.queryOne(Todo, query);
}
/**
* Create a Todo
*/
@Post('/')
async save(todo: Todo): Promise {
return this.source.create(Todo, todo);
}
/**
* Update a Todo
*/
@Put('/:id')
async update(todo: Todo): Promise {
return this.source.update(Todo, todo);
}
/**
* Delete a Todo
*/
@Delete('/:id')
async remove(id: string): Promise {
const query: ModelQuery = { where: { id } };
if (typeof query.where !== 'string') {
}
if (await this.source.deleteByQuery(Todo, query) !== 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 web feature to be selected.Terminal: Starting the App
`bash
npm start
``