Skip to content

Commit 5831b1e

Browse files
Merge pull request #1 from binarygeotech/binarygeotech-patch-1
Binarygeotech patch 1
2 parents 05485c3 + 6cafe17 commit 5831b1e

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

README.md

+67-2
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,76 @@ export class TestSecuredController {
162162
}
163163
}
164164
```
165+
You can now add the `entities` option to your app root module
166+
167+
```typescript
168+
//app.module.ts
169+
import { Module } from '@nestjs/common';
170+
import { OAuth2Module } from '@switchit/nestjs-oauth2-server';
171+
172+
@Module({
173+
imports: [
174+
OAuth2Module.forRoot({
175+
userLoader: new UserLoader(),
176+
userValidator: new UserValidator(),
177+
entities: [
178+
ClientEntity,
179+
AccessTokenEntity,
180+
],
181+
}),
182+
],
183+
})
184+
export class AppModule {}
185+
```
186+
187+
Also for async
188+
```typescript
189+
@Module({
190+
imports: [
191+
OAuth2Module.forRootAsync({
192+
useFactory: () => ({
193+
userLoader: new UserLoader(),
194+
userValidator: new UserValidator(),
195+
entities: [
196+
ClientEntity,
197+
AccessTokenEntity,
198+
],
199+
})
200+
}),
201+
],
202+
})
203+
```
204+
205+
## Using custom entities
206+
207+
**OPTIONAL**: You can also use custom `entities` either by extending from the on that comes with the module or create a new entity that conforms with the module's entities.
208+
e.g
209+
```typescript
210+
...
211+
import { ClientEntity as OClientEntity } from '@switchit/nestjs-oauth2-server/dist/domain/client.entity';
212+
213+
@Entity('...')
214+
@Unique(['client_id'])
215+
export class ClientEntity extends OClientEntity {
216+
...
217+
}
218+
```
219+
220+
```typescript
221+
...
222+
import { ClientEntity } from './client.entity'; // Use your client entity here
223+
import { AccessTokenEntity as OAccessTokenEntity } from '@switchit/nestjs-oauth2-server/dist/domain/access-token.entity';
224+
225+
@Entity('...')
226+
export class AccessTokenEntity extends OAccessTokenEntity {
227+
...
228+
}
229+
```
165230

166231
## Adding the entities to your TypeORM configuration
167232

168233
**IMPORTANT**: The module comes with entities you have to add the configuration `node_modules/@switchit/**/*.entity.js`
169-
to let typeorm scan your entities or add them to the `entitie` configuration variable in TypeORM.
234+
to let typeorm scan your entities or add them to the `entities` configuration variable in TypeORM.
170235

171236
## Using the global validation pipes
172237

@@ -184,4 +249,4 @@ async function bootstrap() {
184249
await app.listen(3000);
185250
}
186251
bootstrap();
187-
```
252+
```

lib/app/oauth2-core.module.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,17 @@ export class Oauth2CoreModule implements OnModuleInit {
8787
},
8888
inject: [OAUTH2_SERVER_OPTIONS],
8989
};
90+
91+
const typeOrmEntities = options.entities || [
92+
ClientEntity,
93+
AccessTokenEntity,
94+
];
9095

9196
return {
9297
module: Oauth2CoreModule,
9398
imports: [
9499
CqrsModule,
95-
TypeOrmModule.forFeature([
96-
ClientEntity,
97-
AccessTokenEntity,
98-
]),
100+
TypeOrmModule.forFeature(typeOrmEntities),
99101
],
100102
controllers: [
101103
Oauth2Controller
@@ -142,15 +144,17 @@ export class Oauth2CoreModule implements OnModuleInit {
142144
inject: [OAUTH2_SERVER_OPTIONS],
143145
};
144146

147+
const typeOrmEntities = options.entities || [
148+
ClientEntity,
149+
AccessTokenEntity,
150+
];
151+
145152
return {
146153
module: Oauth2CoreModule,
147154
imports: [
148155
...(options.imports || []),
149156
CqrsModule,
150-
TypeOrmModule.forFeature([
151-
ClientEntity,
152-
AccessTokenEntity,
153-
]),
157+
TypeOrmModule.forFeature(typeOrmEntities),
154158
],
155159
providers: [
156160
...providers,

lib/domain/access-token.entity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ export class AccessTokenEntity {
4545
@Column('timestamp', {name: 'created_on', nullable: false, default: () => 'now()'})
4646
createdAt: Date;
4747

48-
@Column({name: 'created_from', type: 'jsonb', nullable: true})
48+
@Column({name: 'created_from', type: 'json', nullable: true})
4949
createdFrom: OAuth2Request;
5050
}

0 commit comments

Comments
 (0)