我们在 node.js 应用程序中使用 typeorm,似乎在启动时加载实体会增加大量时间。或者 db 模式相当小/简单,但是加载应用程序似乎需要 2 分钟以上。如果我传递一个空数组(没有实体),那么应用加载速度会明显加快。
export const entities = [
Account,
Bundle,
Category,
Customer,
Device,
Event,
EventMember,
GoldrushMasterEvent,
Order,
Organization,
OrganizationGroup,
Province,
Role,
Ticket,
TicketPackage,
SalesReportView,
User,
SystemConfig,
TicketDetailsView,
EventMemberSalesView,
Invoice,
InvoiceItem,
InvoiceItemCode,
EventSalesView,
OrderDetailsView
];
export interface DatabaseConnectionOptions {
connectionName?: string;
username: string;
password: string;
host: string;
port: number;
database: string;
maxPoolSize: number;
}
/**
* Uses type orm to connect to the RDS instance
*
* @param options
*/
const connectDatabase = async (options: DatabaseConnectionOptions): Promise<void> => {
const dbConfig: ConnectionOptions = {
name: options.connectionName,
type: 'postgres',
schema: 'public',
entities: entities,
synchronize: false,
logging: process.env.DB_LOGGING === 'true',
logger: 'advanced-console',
username: options.username,
password: options.password,
host: options.host,
port: options.port,
database: options.database,
extra: {
max: options.maxPoolSize
},
// log anything that takes more than a second
maxQueryExecutionTime: 1000
};
await createConnection(dbConfig);
};