zappyzep/backend/src/data/dataSource.ts

61 lines
1.5 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 { MysqlConnectionOptions } from "typeorm/driver/mysql/MysqlConnectionOptions.js";
2023-07-01 12:17:45 +00:00
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
type DbOpts = Pick<MysqlConnectionOptions, "host" | "port" | "username" | "password" | "database">;
const dbOpts: DbOpts =
env.NODE_ENV === "production"
? {
host: env.DB_HOST,
port: env.DB_PORT,
username: env.DB_USER,
password: env.DB_PASSWORD,
database: env.DB_DATABASE,
}
: {
host: "mysql",
port: 3306,
username: "zeppelin",
password: env.DEVELOPMENT_MYSQL_PASSWORD,
database: "zeppelin",
};
2023-07-01 12:17:45 +00:00
export const dataSource = new DataSource({
type: "mysql",
...dbOpts,
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
});