我刚刚开始使用 Node.js 中的 Sequelize,发现文档确实缺乏。我有一个“db”模块,在其中通过
Sequelize
连接到数据库,并且它从相对于我的项目根目录的 ./config.json
处的应用程序范围配置文件读取配置。这是一个嵌套配置,极不可能以 Sequelize 需要 CLI 配置文件的方式构建。
现在我正在尝试使用迁移,并且文档引用了“配置文件”。我知道我可以设置该配置文件的路径,但是我到底要在其中放入什么?它没有在任何地方记录(我见过)。
您可以将更多内容放入配置文件中:
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"
}
}),
我阅读了代码来弄清楚。这是一个扁平结构。我只是以与我自己的配置不同的格式重建配置。
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
};
我认为构造函数的文档描述了配置上的所有可用选项。截至 2022 年 8 月 17 日(Sequelize v6;当前版本)。*
options.host string
optional
default: 'localhost'
The host of the relational database.
options.port number
optional
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, db2, mariadb and mssql.
options.dialectModule string
optional
default: null
If specified, use this dialect library. For example, if you want to use pg.js instead of pg when connecting to a pg database, you should specify 'require("pg.js")' here
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 '/path/to/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 Model.init.
options.query object
optional
default: {}
Default options for sequelize.query
options.schema string
optional
default: null
A schema to use
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.clientMinMessages string | boolean
optional
default: 'warning'
(Deprecated) The PostgreSQL client_min_messages session parameter. Set to false to not override the database's default.
options.standardConformingStrings boolean
optional
default: true
The PostgreSQL standard_conforming_strings session parameter. Set to false to not set the option. WARNING: Setting this to false may expose vulnerabilities and is not recommended!
options.logging Function
optional
default: console.log
A function that gets executed every time Sequelize would log something. Function may receive multiple parameters but only first one is printed by console.log. To print all values use (...msg) => console.log(msg)
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 as values to CREATE/UPDATE 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 number
optional
default: 5
Maximum number of connection in pool
options.pool.min number
optional
default: 0
Minimum number of connection in pool
options.pool.idle number
optional
default: 10000
The maximum time, in milliseconds, that a connection can be idle before being released.
options.pool.acquire number
optional
default: 60000
The maximum time, in milliseconds, that pool will try to get connection before throwing error
options.pool.evict number
optional
default: 1000
The time interval, in milliseconds, after which sequelize-pool will remove idle connections.
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.pool.maxUses number
optional
default: Infinity
The number of times a connection can be used before discarding it for a replacement, used for eventual cluster rebalancing.
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. Accepts all options for retry-as-promised.
options.retry.match Array
optional
Only retry a query if the error matches one of these strings.
options.retry.max number
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, and select with where clause, e.g. validate that arguments passed to integer fields are integer-like.
options.operatorsAliases object
optional
String based operator alias. Pass object to limit set of aliased operators.
options.hooks object
optional
An object of global hook functions that are called before and after certain lifecycle events. Global hooks will run after any model-specific hooks defined for the same event (See Sequelize.Model.init() for a list). Additionally, beforeConnect(), afterConnect(), beforeDisconnect(), and afterDisconnect() hooks may be defined here.
options.minifyAliases boolean
optional
default: false
A flag that defines if aliases should be minified (mostly useful to avoid Postgres alias character limit of 64)
options.logQueryParameters boolean
optional
default: false
A flag that defines if show bind parameters in log.
*请注意,其他答案中提到了许多这些属性,例如
pool
和 logging
。 @siyang 的回答直接描述了 define
属性,但文档也涵盖了该属性(请参阅 options
属性)(TODO:插入静态方法 init 的文档链接)
顺便说一下,我在使用 Sequelize 进行一些 PostgreSQL 更新时试图解决“资源请求超时”问题,这就是我分享这些文档链接的原因;如果您遇到此问题,调整 pool
属性可能会对您有所帮助。
编辑
我后来注意到缺少一些属性,这些选项与配置数据库/用户名/密码结合起来,创建一个更大的sequelize.config,TODO:确认下面的代码仍然相关;在 github/sequelize 当前代码中找到它;其中包括一些上面未描述的值,包括ssl
和
defaultTimezone
。
this.config = {
database: config.database,
username: config.username,
password: config.password,
host: config.host || this.options.host,
port: config.port || this.options.port,
pool: this.options.pool,
protocol: this.options.protocol,
native: this.options.native,
ssl: this.options.ssl,
replication: this.options.replication,
dialectModulePath: this.options.dialectModulePath,
keepDefaultTimezone: this.options.keepDefaultTimezone,
dialectOptions: this.options.dialectOptions
};