Skip to content

Instantly share code, notes, and snippets.

@enko
Created March 20, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enko/26eff8ca7e7528a708d384a6eb39f597 to your computer and use it in GitHub Desktop.
Save enko/26eff8ca7e7528a708d384a6eb39f597 to your computer and use it in GitHub Desktop.
Basic API example
import { Connection } from "typeorm";
export abstract class BaseApiController<T> {
protected classType: new () => T;
public static connection: Connection;
constructor(classType: new () => T) {
this.classType = classType;
}
public async find(): Promise<T[]> {
return await BaseApiController.connection.manager.find(this.classType);
}
public async save(entity: T): Promise<T> {
console.dir(entity);
return await BaseApiController.connection.manager.save(entity);
}
}
import {
BaseApiController,
} from '../controllers';
import {
User,
} from '../entity';
import {
JsonController,
Get,
Post,
Body,
} from 'routing-controllers';
@JsonController('/user')
export class UserController extends BaseApiController<User> {
constructor() {
super(User)
}
@Get()
public async find(): Promise<User[]> {
return super.find();
}
@Post()
public async save(
@Body() user: User,
): Promise<User> {
return super.save(user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment