typeorm#Logger TypeScript Examples
The following examples show how to use
typeorm#Logger.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeOrmLogger.ts From typescript-clean-architecture with MIT License | 5 votes |
export class TypeOrmLogger implements Logger {
public log(level: 'log' | 'info' | 'warn', message: string): void {
NestLogger.log(message, TypeOrmLogger.name);
}
public logMigration(message: string): void {
NestLogger.log(message, TypeOrmLogger.name);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public logQuery(query: string, parameters?: any[]): void {
let message: string = `Query: ${query} `;
if (parameters) {
message = `${message} Parameters: ${JSON.stringify(parameters)}`;
}
NestLogger.log(message, TypeOrmLogger.name);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public logQueryError(error: string, query: string, parameters?: any[]): void {
let message: string = `Query: ${query} `;
if (parameters) {
message = `${message} Parameters: ${JSON.stringify(parameters)}`;
}
NestLogger.error(message, error, TypeOrmLogger.name);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public logQuerySlow(time: number, query: string, parameters?: any[]): void {
let message: string = `Query: ${query} Time: ${time}`;
if (parameters) {
message = `${message} Parameters: ${JSON.stringify(parameters)}`;
}
NestLogger.log(message, TypeOrmLogger.name);
}
public logSchemaBuild(message: string): void {
NestLogger.log(message, TypeOrmLogger.name);
}
public static new(): TypeOrmLogger {
return new TypeOrmLogger();
}
}
Example #2
Source File: connect-database.ts From Cromwell with MIT License | 4 votes |
connectDatabase = async ({ ormConfigOverride, development }: {
ormConfigOverride?: Partial<Writeable<ConnectionOptions & MysqlConnectionOptions>>;
development?: boolean;
}) => {
const cmsConfig = await readCMSConfig();
const tempDBPath = normalizePath(resolve(getServerTempDir(), 'db.sqlite3'));
const defaultOrmConfig: ConnectionOptions = {
type: "sqlite",
database: tempDBPath,
entityPrefix: 'crw_',
migrationsTableName: "crw_migrations",
cli: {
migrationsDir: "migrations/sqlite"
},
logging: ["error", "warn", "info", "migration"],
logger: {
logQuery: () => null,
logQueryError: logger.error,
logQuerySlow: logger.warn,
logSchemaBuild: logger.log,
logMigration: logger.info,
log: (level, message) => {
if (level === 'info') logger.info(message);
if (level === 'warn') logger.warn(message);
if (level === 'log') logger.log(message);
},
} as Logger,
}
let hasDatabasePath = false;
const serverDir = getServerDir();
if (cmsConfig?.orm?.database) hasDatabasePath = true;
let ormconfig: DeepWriteable<ConnectionOptions> = Object.assign({}, defaultOrmConfig, ormConfigOverride, cmsConfig.orm)
if (!ormconfig.cli) ormconfig.cli = {};
ormconfig.cli.migrationsDir = getMigrationsDirName(ormconfig.type);
if (!ormconfig || !ormconfig.type) throw new Error('Invalid ormconfig');
setStoreItem('dbInfo', {
dbType: ormconfig.type
});
// Adjust unset options for different DBs
const adjustedOptions: Partial<DeepWriteable<ConnectionOptions & MysqlConnectionOptions>> = {};
adjustedOptions.migrationsRun = true;
adjustedOptions.synchronize = false;
if (ormconfig.type === 'sqlite') {
if (development) {
adjustedOptions.synchronize = true;
adjustedOptions.migrationsRun = false;
}
if (!hasDatabasePath) {
if (!await fs.pathExists(defaultOrmConfig.database) && serverDir) {
// Server probably was launched at the first time and has no DB created
// Use mocked DB
const mockedDBPath = resolve(serverDir, 'db.sqlite3');
if (await fs.pathExists(mockedDBPath)) {
await fs.copy(mockedDBPath, tempDBPath);
}
}
}
}
if (ormconfig.type === 'mysql' || ormconfig.type === 'mariadb') {
adjustedOptions.timezone = '+0';
}
if (ormconfig.type === 'postgres') {
adjustedOptions.timezone = '+0';
}
ormconfig = Object.assign(adjustedOptions, ormconfig);
const pluginExports = await collectPlugins({ updateCache: true });
const connectionOptions: ConnectionOptions = {
...(ormconfig as ConnectionOptions),
entities: [
...ORMEntities,
...(pluginExports.entities ?? [] as any),
...(ormconfig?.entities ?? [])
],
migrations: [
...(pluginExports.migrations ?? [] as any),
(serverDir && ormconfig?.cli?.migrationsDir) ?
normalizePath(resolve(serverDir, ormconfig.cli.migrationsDir)) + '/*.js' : '',
].filter(it => it && it !== ''),
};
if (connectionOptions) await createConnection(connectionOptions);
}