更多信息参考 API Reference for the Sequelize constructor。如果你要从多个进程连接到数据库,则必须为每个进程创建一个示例,但是每个实例都应设定一个最大连接池大小,这样才能保证总的最大连接数大小。例如,如果你希望最大连接池大小为 90,同时拥有 3 个进程,那么每个进程的 Sequelize 示例的最大连接池大小应该是 30。
测试连接
你可以使用 .authenticate() 方法测试连接是否正常
1 2 3 4 5 6 7 8
sequelize .authenticate() .then(() => { console.log('Connection has been established successfully.'); }) .catch(err => { console.error('Unable to connect to the database:', err); });
const sequelize = new Sequelize(connectionURI, { define: { // The `timestamps` field specify whether ornot the `createdAt` and `updatedAt` fields will be created. // This was true by default, but now is false by default timestamps:false } });
// Here `timestamps` will be false, so the `createdAt` and `updatedAt` fields will not be created. const Foo = sequelize.define('foo', { /* ... */ });
// Here `timestamps` is directly set to true, so the `createdAt` and `updatedAt` fields will be created. const Bar = sequelize.define('bar', { /* ... */ }, { timestamps:true });
// Note: using `force: true` will drop the tableif it already exists User.sync({ force: true }).then(() => { // Now the `users` tablein the database corresponds to the model definition returnUser.create({ firstName: 'John', lastName: 'Hancock' }); });