Skip to content

Commit c22e78b

Browse files
committed
feat(api): dynamically load entities using webpack require.context
don't need to manually adding all entities for TypeOrmModule config
1 parent 53edd1d commit c22e78b

File tree

13 files changed

+146
-51
lines changed

13 files changed

+146
-51
lines changed

PLAYBOOK-NEST.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ nest g class notification/notification.entity app/notifications --no-spec --dry-
107107
nest g controller subscription app/notifications --dry-run
108108
nest g service subscription app/notifications --dry-run
109109
nest g class subscription/subscription.entity app/notifications --no-spec --dry-run
110+
111+
112+
You could also use `ng g` if you `npm i -D @nestjs/schematics`
113+
ng g @nestjs/schematics:module game --path app --source-root apps/api/src -d
114+
ng g @nestjs/schematics:controller match --path app/game --source-root apps/api/src -d
115+
ng g @nestjs/schematics:service match --path app/game --source-root apps/api/src -d
116+
ng g @nestjs/schematics:class match/match.entity --path app/game --source-root apps/api/src --spec -d
110117
```
111118
112119
### Ref

apps/api/src/app/core/core.module.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@ import { RequestContextMiddleware } from './context';
88
import { ConfigService } from '../config';
99
import { ConnectionOptions } from 'typeorm';
1010
import { environment as env } from '@env-api/environment';
11-
import { User } from '../auth/user.entity';
12-
import { Notification } from '../notifications/notification/notification.entity';
13-
import { Subscription } from '../notifications/subscription/subscription.entity';
11+
import { isClass } from '@ngx-starter-kit/utils';
12+
13+
function requireAllClasses(rc) {
14+
return rc
15+
.keys()
16+
.filter(filePath => !filePath.includes('base'))
17+
.flatMap(key => Object.values(rc(key)))
18+
.filter(isClass);
19+
}
20+
21+
const requireContext = (require as any).context('../..', true, /\.entity.ts/);
22+
// const requireContext = (require as any).context('../..', true, /^\.\/.*\/.*\/(?!(base|audit-base)).*\.entity.ts$/);
23+
const entities = requireAllClasses(requireContext);
1424

1525
@Module({
1626
imports: [
@@ -21,7 +31,7 @@ import { Subscription } from '../notifications/subscription/subscription.entity'
2131
useFactory: async (config: ConfigService) =>
2232
({
2333
...env.database,
24-
entities: [User, Notification, Subscription],
34+
entities,
2535
} as ConnectionOptions),
2636
inject: [ConfigService],
2737
}),

apps/api/src/app/core/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
export * from './core.module';
22
export * from './services/base-remote.service';
3-
export * from './entities/audit-base.entity';
43
export * from './crud/crud.service';
54
export * from './crud/crud.controller';
File renamed without changes.

apps/api/src/app/user/profile/profile.entity.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Column, Entity, JoinColumn, OneToOne } from 'typeorm';
2-
import { AuditBase } from '../../core';
3-
import { Image } from './Image.entity';
2+
import { AuditBase } from '../../core/entities/audit-base.entity';
3+
import { Image } from './image.entity';
44

55
@Entity('profile')
66
export class Profile extends AuditBase {

libs/utils/src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
export { StateDef } from './lib/StateDef';
2-
export { waitUntil } from './lib/utils';
3-
export { DeepPartial } from './lib/DeepPartial';
4-
export { ObjectLiteral } from './lib/ObjectLiteral';
1+
export { StateDef } from './lib/state-def';
2+
export { waitUntil } from './lib/wait-until';
3+
export { DeepPartial } from './lib/deep-partial';
4+
export { ObjectLiteral } from './lib/object-literal';
5+
export * from './lib/require-multi';
File renamed without changes.
File renamed without changes.

libs/utils/src/lib/require-multi.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export function isClass(obj) {
2+
return !!obj.prototype && !!obj.prototype.constructor.name;
3+
}
4+
5+
export function requireAll(rc) {
6+
return rc.keys().map(rc);
7+
}
8+
9+
export function requireAllClasses(rc) {
10+
return rc
11+
.keys()
12+
// .filter(filePath => !filePath.includes('base'))
13+
.flatMap(key => Object.values(rc(key)))
14+
.filter(isClass);
15+
}
File renamed without changes.

0 commit comments

Comments
 (0)