Skip to content

Commit

Permalink
feat(api): dynamically load entities using webpack require.context
Browse files Browse the repository at this point in the history
don't need to manually adding all entities for TypeOrmModule config
  • Loading branch information
xmlking committed Jan 2, 2019
1 parent 5e5e255 commit 57dfd96
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 51 deletions.
7 changes: 7 additions & 0 deletions PLAYBOOK-NEST.md
Expand Up @@ -107,6 +107,13 @@ nest g class notification/notification.entity app/notifications --no-spec --dry-
nest g controller subscription app/notifications --dry-run
nest g service subscription app/notifications --dry-run
nest g class subscription/subscription.entity app/notifications --no-spec --dry-run


You could also use `ng g` if you `npm i -D @nestjs/schematics`
ng g @nestjs/schematics:module game --path app --source-root apps/api/src -d
ng g @nestjs/schematics:controller match --path app/game --source-root apps/api/src -d
ng g @nestjs/schematics:service match --path app/game --source-root apps/api/src -d
ng g @nestjs/schematics:class match/match.entity --path app/game --source-root apps/api/src --spec -d
```
### Ref
Expand Down
18 changes: 14 additions & 4 deletions apps/api/src/app/core/core.module.ts
Expand Up @@ -8,9 +8,19 @@ import { RequestContextMiddleware } from './context';
import { ConfigService } from '../config';
import { ConnectionOptions } from 'typeorm';
import { environment as env } from '@env-api/environment';
import { User } from '../auth/user.entity';
import { Notification } from '../notifications/notification/notification.entity';
import { Subscription } from '../notifications/subscription/subscription.entity';
import { isClass } from '@ngx-starter-kit/utils';

function requireAllClasses(rc) {
return rc
.keys()
.filter(filePath => !filePath.includes('base'))
.flatMap(key => Object.values(rc(key)))
.filter(isClass);
}

const requireContext = (require as any).context('../..', true, /\.entity.ts/);
// const requireContext = (require as any).context('../..', true, /^\.\/.*\/.*\/(?!(base|audit-base)).*\.entity.ts$/);
const entities = requireAllClasses(requireContext);

@Module({
imports: [
Expand All @@ -21,7 +31,7 @@ import { Subscription } from '../notifications/subscription/subscription.entity'
useFactory: async (config: ConfigService) =>
({
...env.database,
entities: [User, Notification, Subscription],
entities,
} as ConnectionOptions),
inject: [ConfigService],
}),
Expand Down
1 change: 0 additions & 1 deletion apps/api/src/app/core/index.ts
@@ -1,5 +1,4 @@
export * from './core.module';
export * from './services/base-remote.service';
export * from './entities/audit-base.entity';
export * from './crud/crud.service';
export * from './crud/crud.controller';
4 changes: 2 additions & 2 deletions apps/api/src/app/user/profile/profile.entity.ts
@@ -1,6 +1,6 @@
import { Column, Entity, JoinColumn, OneToOne } from 'typeorm';
import { AuditBase } from '../../core';
import { Image } from './Image.entity';
import { AuditBase } from '../../core/entities/audit-base.entity';
import { Image } from './image.entity';

@Entity('profile')
export class Profile extends AuditBase {
Expand Down
9 changes: 5 additions & 4 deletions libs/utils/src/index.ts
@@ -1,4 +1,5 @@
export { StateDef } from './lib/StateDef';
export { waitUntil } from './lib/utils';
export { DeepPartial } from './lib/DeepPartial';
export { ObjectLiteral } from './lib/ObjectLiteral';
export { StateDef } from './lib/state-def';
export { waitUntil } from './lib/wait-until';
export { DeepPartial } from './lib/deep-partial';
export { ObjectLiteral } from './lib/object-literal';
export * from './lib/require-multi';
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions libs/utils/src/lib/require-multi.ts
@@ -0,0 +1,15 @@
export function isClass(obj) {
return !!obj.prototype && !!obj.prototype.constructor.name;
}

export function requireAll(rc) {
return rc.keys().map(rc);
}

export function requireAllClasses(rc) {
return rc
.keys()
// .filter(filePath => !filePath.includes('base'))
.flatMap(key => Object.values(rc(key)))
.filter(isClass);
}
File renamed without changes.
File renamed without changes.
134 changes: 98 additions & 36 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -135,8 +135,8 @@
"@nestjs/swagger": "^2.5.1",
"@nestjs/typeorm": "^5.2.2",
"@nestjs/websockets": "^5.5.0",
"@ngx-formly/core": "^5.0.0-beta.20",
"@ngx-formly/material": "^5.0.0-beta.20",
"@ngx-formly/core": "^5.0.0-beta.21",
"@ngx-formly/material": "^5.0.0-beta.21",
"@ngx-lite/in-viewport": "^0.1.3",
"@ngx-lite/input-star-rating": "^0.2.5",
"@ngx-lite/input-tag": "^0.2.8",
Expand Down Expand Up @@ -175,7 +175,7 @@
"ngx-filepond": "^4.1.0",
"ngx-page-scroll": "^5.0.0",
"ngx-perfect-scrollbar": "^7.1.0",
"nodemailer": "^4.7.0",
"nodemailer": "^5.0.0",
"passport": "^0.4.0",
"passport-jwt": "^4.0.0",
"pg": "^7.7.0",
Expand All @@ -198,6 +198,7 @@
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"@compodoc/compodoc": "^1.1.7",
"@nestjs/schematics": "^5.11.0",
"@nestjs/testing": "^5.5.0",
"@ngx-formly/schematics": "5.0.0-beta.5",
"@ngxs/schematics": "0.0.1-alpha.5",
Expand Down Expand Up @@ -226,7 +227,7 @@
"husky": "^1.3.0",
"jest": "^23.6.0",
"jest-preset-angular": "6.0.2",
"kubernetes-client": "^6.6.0",
"kubernetes-client": "^6.7.0",
"lint-staged": "^8.1.0",
"lite-server": "^2.4.0",
"loaders.css": "^0.1.2",
Expand Down

0 comments on commit 57dfd96

Please sign in to comment.