zappyzep/backend/src/data/dataSource.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-07-01 12:17:45 +00:00
import moment from "moment-timezone";
import path from "path";
import { DataSource } from "typeorm";
import { env } from "../env";
import { backendDir } from "../paths";
2021-09-11 19:06:51 +03:00
moment.tz.setDefault("UTC");
2022-06-26 15:02:34 +03:00
const entities = path.relative(process.cwd(), path.resolve(backendDir, "dist/backend/src/data/entities/*.js"));
const migrations = path.relative(process.cwd(), path.resolve(backendDir, "dist/backend/src/migrations/*.js"));
2018-12-14 06:47:58 +02:00
2023-07-01 12:17:45 +00:00
export const dataSource = new DataSource({
type: "mysql",
2022-06-26 15:02:34 +03:00
host: env.DB_HOST,
port: env.DB_PORT,
username: env.DB_USER,
password: env.DB_PASSWORD,
database: env.DB_DATABASE,
2021-09-11 19:06:51 +03:00
charset: "utf8mb4",
supportBigNumbers: true,
bigNumberStrings: true,
dateStrings: true,
synchronize: false,
connectTimeout: 2000,
logging: ["error", "warn"],
2021-09-12 21:47:03 +03:00
// Entities
2018-12-14 06:47:58 +02:00
entities: [entities],
// Pool options
extra: {
typeCast(field, next) {
2021-09-11 19:06:51 +03:00
if (field.type === "DATETIME") {
const val = field.string();
2021-09-11 19:06:51 +03:00
return val != null ? moment.utc(val).format("YYYY-MM-DD HH:mm:ss") : null;
}
return next();
2021-09-11 19:06:51 +03:00
},
},
// Migrations
2018-12-14 06:47:58 +02:00
migrations: [migrations],
2023-07-01 12:17:45 +00:00
});