ProtonType


A simple framework made with TypeScript.

The Protontype aims make APIs REST development easy.

Instalation

npm install protontype --save

Models

Uses TypeORM by default database manipulation. But any framework can be used.

@Entity()
export class TasksModel {
    @PrimaryGeneratedColumn()
    id: number;
    @Column({ nullable: true })
    title: string;
    @Column()
    done: boolean;
    @Column()
    userId: number;
}

Middlewares

Supports middlewares implementation

export class TasksMiddleware extends BaseMiddleware {
    @Middleware()
    printTaskTitle(params: MiddlewareFunctionParams) {
        cosole.log(params.req.body.title);
        params.next();
    }
}

Router

CRUD basic routes already implemented in CrudRouter

 @RouterClass({
    baseUrl: "/tasks",
    model: TasksModel,
    middlewares: [new TasksMiddleware()]
})
export class TasksRouter extends CrudRouter {
    /*
    GET / - Lists all records
    POST / - Creates a records
    GET /:id - Queries a records
    PUT /:id - Updates a records
    DELETE /:id - Removes a records
    */

    //New custom routes ....
}

Or can implements custom routes

 @RouterClass({
    baseUrl: "/tasks",
    model: TasksModel,
    middlewares: [new TasksMiddleware()]
})
export class TasksRouter extends BaseRouter {
    @Route({
        endpoint: '/test/msg',
        method: Method.GET,
        middlewares: [new OtherMiddleware()]
    })
    routeTest(params: RouterFunctionParams) {
        console.log("Hello!");
    }
}

Database manipulation

let tasksRepository = TypeORMDB.getBD().getRepository(TasksModel);
let tasks = await tasksRepository.find();

Starting application

new ProtonApplication()
    .addRouterAs(TasksRouter)
    .addMiddlewareAs(SomeoneGlobalMiddleware)
    .start();

Examples

Development version

npm install protontype@dev --save