diff --git a/.env.example b/.env.example index 1402762..1aa9f66 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,3 @@ -DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=auth diff --git a/.gitignore b/.gitignore index b3ab1ae..b35170d 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ node_modules/ build/ tmp/ -temp/ \ No newline at end of file +temp/ +yarn.lock +ormconfig.json \ No newline at end of file diff --git a/README.md b/README.md index 0fdaaf8..5882520 100644 --- a/README.md +++ b/README.md @@ -3,5 +3,5 @@ Steps to run this project: 1. Run `npm i` command -2. Setup database settings inside `data-source.ts` file +2. Setup database settings inside `database.ts` file 3. Run `npm start` command diff --git a/main.ts b/main.ts index d430d7a..4fbe4d9 100644 --- a/main.ts +++ b/main.ts @@ -1,21 +1,7 @@ import express from 'express'; import logger from 'morgan' -import path from 'path' + const app = express(); -import fs from 'fs' -import dotenv from 'dotenv' - -// 检测运行目录下是否有 .env 文件,如果没有,则创建 -const envPath = path.resolve(__dirname, '.env') -if (!fs.existsSync(envPath)) { - fs.writeFileSync(envPath, '') - // log - console.log('create .env file') -} - -// 加载 .env 文件 -dotenv.config() -console.log(process.env) import indexRouter from './src/routes/index' import usersRouter from './src/routes/users' diff --git a/package.json b/package.json index 07470a5..fc6cd4f 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "dependencies": { "express": "^4.18.2", "morgan": "^1.10.0", - "mysql": "^2.14.1", + "mysql2": "^3.6.2", "path": "^0.12.7", "reflect-metadata": "^0.1.13", "typeorm": "0.3.17" @@ -19,7 +19,7 @@ "typescript": "4.5.2" }, "scripts": { - "start": "ts-node src/config/index.ts", + "start": "ts-node main.ts", "typeorm": "typeorm-ts-node-commonjs" } -} \ No newline at end of file +} diff --git a/src/config/data-source.ts b/src/config/data-source.ts deleted file mode 100644 index 6cc19ba..0000000 --- a/src/config/data-source.ts +++ /dev/null @@ -1,17 +0,0 @@ -import "reflect-metadata" -import { DataSource } from "typeorm" -import { User } from "../entity/User" - -export const AppDataSource = new DataSource({ - type: "mysql", - host: "localhost", - port: 3306, - username: "test", - password: "test", - database: "test", - synchronize: true, - logging: false, - entities: [User], - migrations: [], - subscribers: [], -}) diff --git a/src/config/database.ts b/src/config/database.ts new file mode 100644 index 0000000..ce4e0b7 --- /dev/null +++ b/src/config/database.ts @@ -0,0 +1,38 @@ +import "reflect-metadata" +import { DataSource } from "typeorm" +// import { User } from "../entity/User"/ + +import "./env" + +const options = { + type: "mysql", + host: process.env.DB_HOST, + port: Number(process.env.DB_PORT), + username: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + synchronize: true, + logging: false, + // entities: [User], + entities: ["../entity/*.ts"], + migrations: ["../migration/*.ts"], + subscribers: [], +} + +console.log(options) + + +export const AppDataSource = new DataSource({ + type: "mysql", + host: process.env.DB_HOST, + port: Number(process.env.DB_PORT), + username: process.env.DB_USERNAME, + password: process.env.DB_PASSWORD, + database: process.env.DB_DATABASE, + synchronize: true, + logging: false, + // entities: [User], + entities: ["../entity/*.ts"], + migrations: ["../migration/*.ts"], + subscribers: [], +}) diff --git a/src/config/env.ts b/src/config/env.ts new file mode 100644 index 0000000..bd80d65 --- /dev/null +++ b/src/config/env.ts @@ -0,0 +1,26 @@ +import fs from 'fs' +import dotenv from 'dotenv' + +let is_loaded = false + +if (!is_loaded) { + // 检测运行目录下是否有 .env 文件,如果没有,则创建 + const envPath = process.cwd() + + console.log(envPath) + + if (!fs.existsSync(envPath)) { + // 如果没有,则创建 + if (!fs.existsSync(envPath)) { + console.log('create .env file at ' + envPath) + fs.writeFileSync(envPath, '') + } + } + + // 加载 .env 文件 + dotenv.config({ + path: envPath + '/.env' + }) + + is_loaded = true +} diff --git a/src/config/index.ts b/src/config/index.ts index 52fbd22..7c765e5 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -1,4 +1,4 @@ -import { AppDataSource } from "./data-source" +import { AppDataSource } from "./database" import { User } from "../entity/User" AppDataSource.initialize().then(async () => {