bootstrap.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { NestFactory } from '@nestjs/core';
  2. import { Type } from '../../shared/shared-types';
  3. import { getConfig, setConfig, VendureConfig } from './config/vendure-config';
  4. import { VendureEntity } from './entity/base/base.entity';
  5. import { registerCustomEntityFields } from './entity/custom-entity-fields';
  6. /**
  7. * Bootstrap the Vendure server.
  8. */
  9. export async function bootstrap(userConfig: Partial<VendureConfig>) {
  10. if (userConfig) {
  11. setConfig(userConfig);
  12. }
  13. // Entities *must* be loaded after the user config is set in order for the
  14. // base VendureEntity to be correctly configured with the primary key type
  15. // specified in the EntityIdStrategy.
  16. // tslint:disable-next-line:whitespace
  17. const { coreEntitiesMap } = await import('./entity/entities');
  18. setConfig({
  19. dbConnectionOptions: {
  20. entities: Object.values(coreEntitiesMap) as Array<Type<VendureEntity>>,
  21. },
  22. });
  23. const config = getConfig();
  24. registerCustomEntityFields(config);
  25. // The AppModule *must* be loaded only after the entities have been set in the
  26. // config, so that they are available when the AppModule decorator is evaluated.
  27. // tslint:disable-next-line:whitespace
  28. const appModule = await import('./app.module');
  29. const app = await NestFactory.create(appModule.AppModule, { cors: config.cors });
  30. await app.listen(config.port);
  31. }