zappyzep/backend/src/data/dataSource.ts

46 lines
1.2 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");
const entities = path.relative(process.cwd(), path.resolve(backendDir, "dist/data/entities/*.js"));
const migrations = path.relative(process.cwd(), path.resolve(backendDir, "dist/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",
host: env.DB_HOST || "mysql",
port: env.DB_PORT || 3306,
username: env.DB_USER || "zeppelin",
password: env.DB_PASSWORD || env.DEVELOPMENT_MYSQL_PASSWORD,
database: env.DB_DATABASE || "zeppelin",
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
});