Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么在没有app上下文的文件中 获取config中的配置? #5327

Open
floralatin opened this issue Jun 14, 2024 · 4 comments
Open

Comments

@floralatin
Copy link

floralatin commented Jun 14, 2024

在此输入你需要反馈的 Bug 具体信息(Bug in Detail):

1、在APP启动之前:初始化一个 数据库,但需要获取Config中的配置
2、const config = require("../config/config.default");
3、发现必须传入APPInfo。
4、觉得不太理解,为什么在设计 config 的时候 app 不能作为一个 require 选项,而非是一个参数传入。

可复现问题的仓库地址(Reproduction Repo)

const { Sequelize } = require('sequelize');
const config = require("../config/config.default");  **//问题在这, 我怎么获取里面的信息呢,现在 config 是一个function**
const sequelize = new Sequelize(config.database, config.username, config.password, {
  host: config.host,
  dialect: config.dialect,
});

(async () => {
  try {
    await sequelize.authenticate();
    console.log('Connection has been established successfully.');
    
    await sequelize.sync({ force: true });
    console.log('Database synchronized successfully.');

    process.exit(0);
  } catch (error) {
    console.error('Unable to connect to the database:', error);
    process.exit(1);
  }
})();

Node 版本号:

22.0

Eggjs 版本号:

3.24.1

相关插件名称与版本号(PlugIn and Name):

"egg": "^3.24.1", "egg-bin": "^6.10.0", "egg-ci": "^2.2.0", "egg-cors": "^3.0.1", "egg-jsonp": "^2.0.0", "egg-mock": "^5.12.0", "egg-mysql": "^4.0.0", "egg-onerror": "^2.2.0", "egg-passport": "^2.1.1", "egg-redis": "^2.6.0", "egg-scripts": "^3.0.1", "egg-sequelize": "^6.0.0", "egg-session": "^3.3.0", "egg-socket.io": "^4.1.6", "egg-validate": "^2.0.2",

操作平台与版本号(Platform and Version):

mac 15

@sinkhaha
Copy link
Contributor

可以单独抽出一份更简单的跟egg无关的配置,单独导入这份配置,config.default那边用到这份配置也是单独导入这份配置后去使用。要用sequelize也可以使用egg-sequelize插件

@Seey215
Copy link

Seey215 commented Jun 20, 2024

https://www.eggjs.org/zh-CN/basics/app-start
可以看下app.js中的“生命周期函数”能否解决你的问题。

@floralatin
Copy link
Author

floralatin commented Jun 25, 2024

我发现了解决办法:


// init_db.js

const mock = require('egg-mock');
const path = require("path");

// 初始化 app 对象
const app = mock.app({
  baseDir: path.dirname(__dirname),  // 项目的根目录
});

// 等待 app 准备好
app.ready()
  .then(async () => {
    // 获取 sequelize 实例
    console.log('---------Database initialized start---------');
    await  app.model.sync({  force: true });
    // 同步所有模型到数据库
    console.log('---------Database initialized successfully---------');
    process.exit(0);
  })
  .catch(err => {
    console.error('Failed to initialize database', err);
    process.exit(1);
  });

@floralatin
Copy link
Author

可以单独抽出一份更简单的跟egg无关的配置,单独导入这份配置,config.default那边用到这份配置也是单独导入这份配置后去使用。要用sequelize也可以使用egg-sequelize插件

您没有明白我的意思,即使按照您的方式连接上数据录了,但是app还没有启动 这时就没有办法使用egg-sequelize 初始化数据库

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants