From 38da37870a731f9f6b0af90ea314d8cf42478e27 Mon Sep 17 00:00:00 2001 From: FishExplosion <1arteha1@gmail.com> Date: Thu, 12 Dec 2019 15:26:31 +0100 Subject: [PATCH] README++ --- README.md | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 20044ed0..666496a6 100644 --- a/README.md +++ b/README.md @@ -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"; @@ -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; + + @UseAsTitle() + public toString(): string + { + return `${this.firstName} ${this.lastName}`; + } +} + +@Entity({name: "Persons"}) export class Person extends BaseEntity { @PrimaryGeneratedColumn() @@ -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}`; } } @@ -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', @@ -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. \ No newline at end of file +Typescript developers who want to use admin-bro of version `~1.3.0` - don't do this - use `^1.4.0` instead. \ No newline at end of file