我刚开始在Node.js中使用Sequelize并且发现文档确实缺乏。我有一个'db'模块,我通过Sequelize
连接到数据库,这将从./config.json
相对于项目根目录的应用程序范围的配置文件中读取配置。这是一个嵌套配置,极不可能以Sequelize想要CLI的配置文件的方式构建。
现在我正在尝试使用迁移,文档引用了“配置文件”。我知道我可以设置该配置文件的路径,但是我放入了什么?它没有在任何地方记录(我见过)。
答案 0 :(得分:6)
您可以在配置文件中添加更多内容:
var sequelize = new Sequelize(config.database.dbName, config.database.master.user, config.database.master.password, {
dialect: config.database.protocol,
port: config.database.port,
host: config.database.master.host,
/* You could setup replication as well
replication: {
read: [
{
host: config.database.master.host,
username: config.database.master.host,
password: config.database.master.password
},
{
host: config.database.master.host,
username: config.database.master.host,
password: config.database.master.password
}
],
write: {
host: config.database.master.host,
username: config.database.master.host,
password: config.database.master.password
}
*/
},
pool: {
maxConnections: config.database.pool.maxConnections,
maxIdleTime: config.database.pool.maxIdleTime
},
logging: false,
define: {
underscored: false,
freezeTableName: false,
syncOnAssociation: true,
charset: 'utf8',
collate: 'utf8_general_ci',
classMethods: {method1: function() {}},
instanceMethods: {method2: function() {}},
timestamps: true
schema: "prefix"
}
}),
答案 1 :(得分:2)
我认为documentation for the constructor描述了配置中所有可用的选项。截至2018年1月12日(Sequelize v4;当前版本)。*
options.host String optional default: 'localhost' The host of the relational database.
options.port Integer optional default: The port of the relational database.
options.username String optional default: null The username which is used to authenticate against the database.
options.password String optional default: null The password which is used to authenticate against the database.
options.database String optional default: null The name of the database
options.dialect String optional The dialect of the database you are connecting to. One of mysql, postgres, sqlite and mssql.
options.dialectModulePath String optional default: null If specified, load the dialect library from this path. For example, if you want to use pg.js instead of pg when connecting to a pg database, you should specify 'pg.js' here
options.dialectOptions Object optional An object of additional options, which are passed directly to the connection library
options.storage String optional Only used by sqlite. Defaults to ':memory:'
options.protocol String optional default: 'tcp' The protocol of the relational database.
options.define Object optional default: {} Default options for model definitions. See sequelize.define for options
options.query Object optional default: {} Default options for sequelize.query
options.set Object optional default: {} Default options for sequelize.set
options.sync Object optional default: {} Default options for sequelize.sync
options.timezone String optional default: '+00:00' The timezone used when converting a date from the database into a JavaScript date. The timezone is also used to SET TIMEZONE when connecting to the server, to ensure that the result of NOW, CURRENT_TIMESTAMP and other time related functions have in the right timezone. For best cross platform performance use the format +/-HH:MM. Will also accept string versions of timezones used by moment.js (e.g. 'America/Los_Angeles'); this is useful to capture daylight savings time changes.
options.logging Function optional default: console.log A function that gets executed every time Sequelize would log something.
options.benchmark Boolean optional default: false Pass query execution time in milliseconds as second argument to logging function (options.logging).
options.omitNull Boolean optional default: false A flag that defines if null values should be passed to SQL queries or not.
options.native Boolean optional default: false A flag that defines if native library shall be used or not. Currently only has an effect for postgres
options.replication Boolean optional default: false Use read / write replication. To enable replication, pass an object, with two properties, read and write. Write should be an object (a single server for handling writes), and read an array of object (several servers to handle reads). Each read/write server can have the following properties: host, port, username, password, database
options.pool Object optional sequelize connection pool configuration
options.pool.max Integer optional default: 5 Maximum number of connection in pool
options.pool.min Integer optional default: 0 Minimum number of connection in pool
options.pool.idle Integer optional default: 10000 The maximum time, in milliseconds, that a connection can be idle before being released. Use with combination of evict for proper working, for more details read https://github.com/coopernurse/node-pool/issues/178#issuecomment-327110870
options.pool.acquire Integer optional default: 10000 The maximum time, in milliseconds, that pool will try to get connection before throwing error
options.pool.evict Integer optional default: 10000 The time interval, in milliseconds, for evicting stale connections. Set it to 0 to disable this feature.
options.pool.handleDisconnects Boolean optional default: true Controls if pool should handle connection disconnect automatically without throwing errors
options.pool.validate Function optional A function that validates a connection. Called with client. The default function checks that client is an object, and that its state is not disconnected
options.quoteIdentifiers Boolean optional default: true Set to false to make table names and attributes case-insensitive on Postgres and skip double quoting of them. WARNING: Setting this to false may expose vulnerabilities and is not recommended!
options.transactionType String optional default: 'DEFERRED' Set the default transaction type. See Sequelize.Transaction.TYPES for possible options. Sqlite only.
options.isolationLevel String optional Set the default transaction isolation level. See Sequelize.Transaction.ISOLATION_LEVELS for possible options.
options.retry Object optional Set of flags that control when a query is automatically retried.
options.retry.match Array optional Only retry a query if the error matches one of these strings.
options.retry.max Integer optional How many times a failing query is automatically retried. Set to 0 to disable retrying on SQL_BUSY error.
options.typeValidation Boolean optional default: false Run built in type validators on insert and update, e.g. validate that arguments passed to integer fields are integer-like.
options.operatorsAliases Object | Boolean optional default: true String based operator alias, default value is true which will enable all operators alias. Pass object to limit set of aliased operators or false to disable completely.
*请注意,其他答案中提到了许多类似pool
和logging
的属性。 @siyang's answer直接描述了define
属性,但documentation also covers that (see the options
property)
顺便说一句,我试图解决issue of "Resource request timed out" while using Sequelize来进行一些PostgreSQL更新,这就是为什么我共享了这些文档链接的原因。如果您遇到此问题,请调整pool
属性可能会对您有所帮助。
答案 2 :(得分:1)
我读了代码来弄明白。它是扁平结构。我只是以与我自己的配置不同的格式重建配置。
var config = require('./config');
module.exports = {
database: config.database.name,
username: config.database.user,
password: config.database.pass,
dialect: 'postgres',
dialectModulePath: 'pg.js',
host: config.database.host,
port: config.database.port,
pool: config.database.pool
};