问题描述
我是Strongloop的常客.我的datasources.json配置如下:
I'm a greenhand for strongloop. My datasources.json config is as follows:
"platformDB": {
"host": "localhost",
"port": 3306,
"database": "way",
"username": "root",
"password": "root",
"name": "platformDB",
"connector": "mysql"
},
UserAccount是我的一个模型,如下所示:
and UserAccount is my one model as follows:
"name": "UserAccount",
"plural": "user",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"id": {
"type": "number",
"required": false
},
"accountName": {
"type": "string",
"required": false
},
"roleName": {
"type": "string",
"required": false
},
"accessToken": {
"type": "string",
"required": false
},
"loginTime": {
"type": "date",
"required": false,
"mysql": {
"dataType": "datetime"
}
}
当我调用UserAccount.create和UserAccount.findById函数时,它们的结果正常.但是当我通过客户端工具连接mysql数据库时,我发现'loginTime'的值为utc时间.这个结果与其他系统组件不协调,总之,我需要本地时间.所以我跟踪了loopback-connector-mysql源代码,我在数据源中找到了timezone属性,如下所示:
when i call UserAccount.create and UserAccount.findById function, their results are ok. but when i connect mysql database by client tool, i find 'loginTime' value is utc time. this result didn't coordinate with other system component , in a word , i need local time.so i trace loopback-connector-mysql source code, i find timezone property in datasource as follows:
var options = {
host: s.host || s.hostname || 'localhost',
port: s.port || 3306,
user: s.username || s.user,
password: s.password,
timezone: s.timezone,
socketPath: s.socketPath,
charset: s.collation.toUpperCase(), // Correct by docs despite seeming odd.
supportBigNumbers: s.supportBigNumbers,
connectionLimit: s.connectionLimit
};
所以我在datasources.json中配置了"timezone":"utc8",UserAccount.findById函数结果与客户端工具相当,但是UserAccount.create函数结果仍然是utc时间.这是为什么?
so i config "timezone":"utc8" in my datasources.json, UserAccount.findById function result is eqauls to client tools, but UserAccount.create function result is still utc time. it's why ?
推荐答案
您还可以通过将时区条目添加到数据源JSON配置中来设置运行API的服务器的时区:
You can also set the timezone of the server that the API is running on by adding a timezone entry to the datasource JSON config:
datasources.json:
datasources.json:
{
"mysqlDS": {
"name": "mysqlDS",
"connector": "mysql",
"host": "localhost",
"port": 3306,
"database": "dbname",
"username": "root",
"password": "",
"timezone": "UTC"
}
}
然后可以使用env vars在datasources.local.js中的生产环境上覆盖哪些内容:
Which can then be overridden on production envs in a datasources.local.js by using env vars:
datasources.local.js:
datasources.local.js:
module.exports = {
"mysqlDS": {
host : process.env.APP_MYSQL_HOST,
port : process.env.APP_MYSQL_PORT,
database: process.env.APP_MYSQL_DB,
username: process.env.APP_MYSQL_USER,
password: process.env.APP_MYSQL_PW,
timezone: process.env.APP_MYSQL_TZ
}
}
也可能无法检入datasources.json并将其作为所有环境的部署/构建配置的一部分.
Could also just not check in datasources.json and make it part of the deploy/build configuration for all envs.
这篇关于Strongloop如何配置mysql时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!