You're just going to have to create your TypeORM entities just like you always did. Then there are 2 @ decorators that
are specific to entity-routes :
@EntityRoute which is a class decorator, must be placed at the top of it so that your entities can be exposed
through an EntityRouter.@Groups which is a property decorator, must be placed at the top of every properties you want to expose through
your routes, more details on it laterHere is a simple example.
# 1 export class AbstractEntity { 3 @ PrimaryGeneratedColumn ( ) 7 @ EntityRoute ( { path : "/users" , operations : [ "create" , "update" , "details" , "list" , "delete" ] } ) 9 export class User extends AbstractEntity { 14 @ Subresource ( ( ) => Article ) 15 @ OneToMany ( ( ) => Article , ( article ) => article . author ) 21 export class Article extends AbstractEntity { 31 @ ManyToOne ( ( ) => User , ( user ) => user . articles ) It will automatically generate those routes :
Verb Path Name POST /users user_create POST /users/mapping user_create_mapping PUT /users/:id(\d+) user_update PUT /users/:id(\d+)/mapping user_update_mapping GET /users/:id(\d+) user_details GET /users/:id(\d+)/mapping user_details_mapping GET /users user_list GET /users/mapping user_list_mapping DELETE /users/:id(\d+) user_delete POST /users/:UserId(\d+)/articles user_articles_create GET /users/:UserId(\d+)/articles user_articles_list DELETE /users/:UserId(\d+)/articles/:id(\d+) user_articles_delete
Register your Entity Routes
# Koa Express
# 1 import { AddressInfo } from "net" ; 2 import { makeKoaEntityRouters } from "@entity-routes/koa" ; 3 import * as Koa from "koa" ; 4 import * as bodyParser from "koa-bodyparser" ; 5 import { Connection } from "typeorm" ; 6 export async function setupKoaApp ( connection : Connection ) { 7 const entities = connection . entityMetadatas . map ( ( meta ) => meta . target ) as Function [ ] ; 8 const bridgeRouters = await makeKoaEntityRouters ( { connection , entities , options } ) ; 10 const app = new Koa ( ) ; 11 app . use ( bodyParser ( ) ) ; 14 bridgeRouters . forEach ( ( router ) => app . use ( router . instance . routes ( ) ) ) ; 16 const server = app . listen ( ) ; / random port 17 const baseURL = ` http://127.0.0.1: ${ ( server . address ( ) as AddressInfo ) . port } ` ; 19 return { baseURL , server } ; # 1 import { AddressInfo } from "net" ; 2 import { makeExpressEntityRouters } from "@entity-routes/express" ; 3 import * as bodyParser from "body-parser" ; 4 import * as express from "express" ; 5 import { Connection } from "typeorm" ; 6 export async function setupExpressApp ( connection : Connection ) { 7 const entities = connection . entityMetadatas . map ( ( meta ) => meta . target ) as Function [ ] ; 8 const bridgeRouters = await makeExpressEntityRouters ( { connection , entities , options } ) ; 10 const app = express ( ) ; 11 app . use ( bodyParser . json ( ) ) ; 12 app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; 15 bridgeRouters . forEach ( ( router ) => app . use ( router . instance ) ) ; 17 const server = app . listen ( ) ; / random port 18 const baseURL = ` http://127.0.0.1: ${ ( server . address ( ) as AddressInfo ) . port } ` ; 20 return { baseURL , server } ; That's it. There is no final step. Your routes are ready.
While browsing the documentation, if you feel like an example is missing, feel free to check the
tests
folder to see the actual usage of the feature you
were reading about.