strongloop mysql,Strongloop如何配置mysql时区?

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"

},

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"

}

}

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

};

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 ?

解决方案

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:

{

"mysqlDS": {

"name": "mysqlDS",

"connector": "mysql",

"host": "localhost",

"port": 3306,

"database": "dbname",

"username": "root",

"password": "",

"timezone": "UTC"

}

}

Which can then be overridden on production envs in a datasources.local.js by using env vars:

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

}

}

Could also just not check in datasources.json and make it part of the deploy/build configuration for all envs.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值