Skip to content

Commit 75baade

Browse files
fix: support string type primary keys
Beta
2 parents 2a6a8dc + 5a9dcb3 commit 75baade

File tree

13 files changed

+13002
-38
lines changed

13 files changed

+13002
-38
lines changed

README.md

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ Installation: `yarn add @admin-bro/typeorm`
77
## Usage
88

99
The plugin can be registered using standard `AdminBro.registerAdapter` method.
10-
10+
o
1111
```typescript
12-
import { Database, Resource } from "@admin-bro/typeorm";
12+
import { Database, Resource } from '@admin-bro/typeorm'
1313
import AdminBro from 'admin-bro'
1414

1515
AdminBro.registerAdapter({ Database, Resource });
1616

1717
// optional: if you use class-validator you have to inject this to resource.
1818
import { validate } from 'class-validator'
19-
Resource.validate = validate;
19+
Resource.validate = validate
2020
```
2121

2222
## Example
@@ -28,16 +28,16 @@ import {
2828
createConnection,
2929
ManyToOne,
3030
RelationId
31-
} from 'typeorm';
32-
import * as express from 'express';
33-
import { Database, Resource } from '@admin-bro/typeorm';
31+
} from 'typeorm'
32+
import * as express from 'express'
33+
import { Database, Resource } from '@admin-bro/typeorm'
3434
import { validate } from 'class-validator'
3535

36-
import AdminBro from 'admin-bro';
36+
import AdminBro from 'admin-bro'
3737
import * as AdminBroExpress from '@admin-bro/express'
3838

39-
Resource.validate = validate;
40-
AdminBro.registerAdapter({ Database, Resource });
39+
Resource.validate = validate
40+
AdminBro.registerAdapter({ Database, Resource })
4141

4242
@Entity()
4343
export class Person extends BaseEntity
@@ -67,26 +67,86 @@ export class Person extends BaseEntity
6767

6868
( async () =>
6969
{
70-
const connection = await createConnection({/* ... */});
70+
const connection = await createConnection({/* ... */})
7171

7272
// Applying connection to model
73-
Person.useConnection(connection);
73+
Person.useConnection(connection)
7474

7575
const adminBro = new AdminBro({
7676
// databases: [connection],
7777
resources: [
7878
{ resource: Person, options: { parent: { name: 'foobar' } } }
7979
],
8080
rootPath: '/admin',
81-
});
81+
})
8282

83-
const app = express();
84-
const router = AdminBroExpress.buildRouter(adminBro);
85-
app.use(adminBro.options.rootPath, router);
86-
app.listen(3000);
87-
})();
83+
const app = express()
84+
const router = AdminBroExpress.buildRouter(adminBro)
85+
app.use(adminBro.options.rootPath, router)
86+
app.listen(3000)
87+
})()
8888
```
8989

9090
## ManyToOne
9191

9292
Admin supports ManyToOne relationship but you also have to define @RealationId as stated in the example above.
93+
94+
## Contribution
95+
96+
### Running the example app
97+
98+
If you want to set this up locally this is the suggested process:
99+
100+
1. fork the repo
101+
2. Install dependencies
102+
103+
```
104+
yarn install
105+
```
106+
107+
3. register this package as a (linked package)[https://classic.yarnpkg.com/en/docs/cli/link/]
108+
109+
```
110+
yarn link
111+
```
112+
113+
4. Setup example app
114+
115+
Install all dependencies and use previously linked version of `@admin-bro/typeorm`.
116+
117+
```
118+
cd example-app
119+
yarn install
120+
yarn link @admin-bro/typeorm
121+
```
122+
123+
Optionally you might want to link your local version of `admin-bro` package
124+
125+
5. Make sure you have all the envs set (which are defined in `example-app/ormconfig.js`)
126+
127+
6. Build the package in watch mode
128+
129+
(in the root folder)
130+
131+
```
132+
yarn watch
133+
```
134+
135+
6. run the app in the dev mode
136+
137+
```
138+
cd example-app
139+
yarn start:dev
140+
```
141+
142+
### Pull request
143+
144+
Before you make a PR make sure all tests pass and your code wont causes linter errors.
145+
You can do this by running:
146+
147+
```
148+
yarn lint
149+
yarn test
150+
```
151+
152+
or with proper envs: `POSTGRES_USER=yourtestuser POSTGRES_DATABASE="database_test" yarn test`

example-app/src/entity/Car.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, RelationId, ManyToOne } from 'typeorm'
22
import { User } from './User'
3+
import { Seller } from './Seller'
34

45
@Entity()
56
export class Car extends BaseEntity {
@@ -12,7 +13,13 @@ export class Car extends BaseEntity {
1213
@ManyToOne((type) => User, (user) => user.cars)
1314
owner: User;
1415

16+
@ManyToOne((type) => Seller, (seller) => seller.cars)
17+
seller: User;
18+
1519
// in order be able to fetch resources in admin-bro - we have to have id available
1620
@RelationId((car: Car) => car.owner)
1721
ownerId: number;
22+
23+
@RelationId((car: Car) => car.seller)
24+
sellerId: string;
1825
}

example-app/src/entity/Seller.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm'
2+
import { Car } from './Car'
3+
4+
export enum UserRoles {
5+
DESIGNER = 'designer',
6+
CLIENT = 'client'
7+
}
8+
9+
@Entity()
10+
export class Seller extends BaseEntity {
11+
@PrimaryGeneratedColumn('uuid')
12+
id: string;
13+
14+
@Column()
15+
name: string;
16+
17+
@OneToMany((type) => Car, (car) => car.seller)
18+
cars: Array<Car>
19+
}

example-app/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { buildRouter } from '@admin-bro/express'
66
import * as TypeormAdapter from '@admin-bro/typeorm'
77
import { User } from './entity/User'
88
import { Car } from './entity/Car'
9+
import { Seller } from './entity/Seller'
910

1011
AdminBro.registerAdapter(TypeormAdapter)
1112

@@ -24,7 +25,7 @@ const run = async () => {
2425
},
2526
},
2627
},
27-
}, Car],
28+
}, Car, Seller],
2829
})
2930
const router = buildRouter(admin)
3031

0 commit comments

Comments
 (0)