Git Product home page Git Product logo

Comments (4)

sinkhaha avatar sinkhaha commented on August 25, 2024

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

from egg.

Seey215 avatar Seey215 commented on August 25, 2024

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

from egg.

floralatin avatar floralatin commented on August 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);
  });

from egg.

floralatin avatar floralatin commented on August 25, 2024

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

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

from egg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.