Skip to content

Commit

Permalink
Merge branch 'Dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
Arteha committed Dec 12, 2019
2 parents 5547cd3 + 48c66de commit be9db06
Showing 1 changed file with 36 additions and 10 deletions.
46 changes: 36 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import {
BaseEntity,
Entity, PrimaryGeneratedColumn, Column,
createConnection,
ManyToOne,
ManyToOne, OneToMany,
RelationId
} from "typeorm";
import * as express from "express";
import { Database, Resource } from "admin-bro-typeorm";
import { Database, Resource, UseAsTitle, UseForSearch } from "admin-bro-typeorm";
import { validate } from 'class-validator'

import AdminBro from "admin-bro";
Expand All @@ -39,7 +39,31 @@ import * as AdminBroExpress from "admin-bro-expressjs"
Resource.validate = validate;
AdminBro.registerAdapter({ Database, Resource });

@Entity()

@Entity({name: "Organizations"})
export class Organization extends BaseEntity
{
@PrimaryGeneratedColumn()
public id: number;

@UseForSearch()
@Column({type: 'varchar', unique: true})
public govRegCode: string;

@Column({type: 'varchar', unique: true})
public name: string;

@OneToMany(type => Person, person => person.organization)
public employees?: Array<Person>;

@UseAsTitle()
public toString(): string
{
return `${this.firstName} ${this.lastName}`;
}
}

@Entity({name: "Persons"})
export class Person extends BaseEntity
{
@PrimaryGeneratedColumn()
Expand All @@ -51,17 +75,17 @@ export class Person extends BaseEntity
@Column({type: 'varchar'})
public lastName: string;

@ManyToOne(type => CarDealer, carDealer => carDealer.cars)
organization: Organization;
@ManyToOne(type => Organization, org => org.employees)
public organization?: Organization;

// in order be able to fetch resources in admin-bro - we have to have id available
@RelationId((person: Person) => person.organization)
organizationId: number;
public organizationId: number | null;

// For fancy clickable relation links:
@UseAsTitle()
public toString(): string
{
return `${firstName} ${lastName}`;
return `${this.firstName} ${this.lastName}`;
}
}

Expand All @@ -70,11 +94,13 @@ export class Person extends BaseEntity
const connection = await createConnection({/* ... */});

// Applying connection to model
Organization.useConnection(connection);
Person.useConnection(connection);

const adminBro = new AdminBro({
// databases: [connection],
resources: [
{ resource: Organization, options: { parent: { name: "foobar" } } },
{ resource: Person, options: { parent: { name: "foobar" } } }
],
rootPath: '/admin',
Expand All @@ -89,8 +115,8 @@ export class Person extends BaseEntity

## ManyToOne

Admin supports ManyToOne relationship but you also have to define @RealationId as stated in the example above.
Admin supports ManyToOne relationship but you also have to define @RelationId 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.
Typescript developers who want to use admin-bro of version `~1.3.0` - don't do this - use `^1.4.0` instead.

0 comments on commit be9db06

Please sign in to comment.