admin-bro-typeorm
This is an inofficial admin-bro adapter which integrates TypeORM into admin-bro.
Installation: npm install admin-bro-typeorm
Usage
The plugin can be registered using standard AdminBro.registerAdapter
method.
import { Database, Resource } from "admin-bro-typeorm";
import AdminBro from 'admin-bro'
AdminBro.registerAdapter({ Database, Resource });
import { validate } from 'class-validator'
Resource.validate = validate;
Example
import {
BaseEntity,
Entity, PrimaryGeneratedColumn, Column,
createConnection,
ManyToOne,
RelationId
} from "typeorm";
import * as express from "express";
import { Database, Resource } from "admin-bro-typeorm";
import { validate } from 'class-validator'
import AdminBro from "admin-bro";
import * as AdminBroExpress from "admin-bro-expressjs"
Resource.validate = validate;
AdminBro.registerAdapter({ Database, Resource });
@Entity()
export class Person extends BaseEntity
{
@PrimaryGeneratedColumn()
public id: number;
@Column({type: 'varchar'})
public firstName: string;
@Column({type: 'varchar'})
public lastName: string;
@ManyToOne(type => CarDealer, carDealer => carDealer.cars)
organization: Organization;
@RelationId((person: Person) => person.organization)
organizationId: number;
public toString(): string
{
return `${firstName} ${lastName}`;
}
}
( async () =>
{
const connection = await createConnection({});
Person.useConnection(connection);
const adminBro = new AdminBro({
resources: [
{ resource: Person, options: { parent: { name: "foobar" } } }
],
rootPath: '/admin',
});
const app = express();
const router = AdminBroExpress.buildRouter(adminBro);
app.use(adminBro.options.rootPath, router);
app.listen(3000);
})();
ManyToOne
Admin supports ManyToOne relationship but you also have to define @RealationId as stated in the example above.
Warning
Typescript developers who want to use admin-bro of version ~1.3.0
- don't do this - use ~1.4.0
instead.