entity-routes logo
Docs

Quick start#

Make your entities#

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 later

Here is a simple example.

#
typescript
1export class AbstractEntity {
2 @Groups("all")
3 @PrimaryGeneratedColumn()
4 id: number;
5}
7@EntityRoute({ path: "/users", operations: ["create", "update", "details", "list", "delete"] })
8@Entity()
9export class User extends AbstractEntity {
10 @Groups("basic")
11 @Column()
12 name: string;
14 @Subresource(() => Article)
15 @OneToMany(() => Article, (article) => article.author)
16 articles: Article[];
19@EntityRoute()
20@Entity()
21export class Article extends AbstractEntity {
22 @Groups("basic")
23 @Column()
24 title: string;
26 @Groups("basic")
27 @Column()
28 content: string;
30 @Groups("basic")
31 @ManyToOne(() => User, (user) => user.articles)
32 author: User;

Minimize

It will automatically generate those routes :

VerbPathName
POST/usersuser_create
POST/users/mappinguser_create_mapping
PUT/users/:id(\d+)user_update
PUT/users/:id(\d+)/mappinguser_update_mapping
GET/users/:id(\d+)user_details
GET/users/:id(\d+)/mappinguser_details_mapping
GET/usersuser_list
GET/users/mappinguser_list_mapping
DELETE/users/:id(\d+)user_delete
POST/users/:UserId(\d+)/articlesuser_articles_create
GET/users/:UserId(\d+)/articlesuser_articles_list
DELETE/users/:UserId(\d+)/articles/:id(\d+)user_articles_delete

Register your Entity Routes#

#
typescript
1import { AddressInfo } from "net";
2import { makeKoaEntityRouters } from "@entity-routes/koa";
3import * as Koa from "koa";
4import * as bodyParser from "koa-bodyparser";
5import { Connection } from "typeorm";
6export 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());
13 // Register all routes on koa server
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 };

Final step#

That's it. There is no final step. Your routes are ready.

Prev
Installation
Next
Introduction