Basic usage#
An entity for entity-router is nothing more than a TypeORM entity.
There are 3 steps to make and use an EntityRouter.
1. Creating your entities#
Every entities used by entity-routes should have an id property exposed through any
route scope, so that they properly implement the
GenericEntity.
So a minimal implementation should look at least like this :
And then you would make all your entities extends from this one,
just like any other class inheritance with Typescript. You
can learn more about TypeORM table inheritance here.
2. Creating an EntityRouter#
The @EntityRoute decorator creates an EntityRouter
on the decorated entity.
The name distinction is to avoid any conflict between the service (EntityRouter)
and the decorator (EntityRoute).
Example :
It takes two arguments, both optional. Complete definition here.
The first argument (EntityRouteArgs) defines your routes.
- a string
path, which will be the root path for all routes on that entity. Path should start with a "/". - an array of operations, which will enable matching CRUD actions. For example,
adding
createoperation will lead to having aPOST:/usersroute in the example above.
The second argument (EntityRouteConfig) defines the options passed to every routes on that entity.
If you don't wan't to write all basic operations everytime you just need to import & use CRUD_OPERATIONS. It looks like this.
3. Usage#
After you have decorated every entities for which you want to create an EntityRouter and exposed their properties (with the @Groups decorator), you must register your routes on your node application.
Maker#
Any maker should wrap the makeEntityRouters, so
the arguments to pass (MakeEntityRouters) should remain the basically the same :
- connection: the TypeORM Connection used
- entities: an array of every entities that you decorated with
@EntityRouteand for which you want itsEntityRouterto be used - options: EntityRouteOptions, an object of global options that will be passed down
to all
EntityRouter(defaults)
There are 2 built-in makers: makeKoaEntityRouters &
makeExpressEntityRouters.
If your Node framework doesn't support classic middleware syntax (Express-like with (req, res, next) => void or
Koa-like with (ctx, next) => void), you can make your own maker with your own
custom ContextAdapter. TODO link
Route options#
Here are the options available :
There are multiple stages where you can set an EntityRouteOptions.
- You can set global options for your
entity-routesby passing them tomakeEntityRouters(or an implentation of it such asmakeExpressEntityRouters) options key. - You can then override the global options by passing a custom options object to the
@EntityRoutedecorator as 2nd argument. - If you directly use a component's (such as
RouteController,Readeror any other) method you can also pass just the options that are needed locally
Metadata#
You can retrieve the RouteMetadata registered by an
@EntityRoute decorator by using the getRouteMetadata
function.