Strongloop How to config mysql timezone?

橙三吉。 提交于 2019-12-12 02:23:18

问题


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 ?


回答1:


Only solution exist is by modifying the core file. edit /var/www/yourproject/node_modules/loopback-connector-mysql/lib/mysql.js

change

function dateToMysql(val) {
  return val.getUTCFullYear() + '-' +
    fillZeros(val.getUTCMonth() + 1) + '-' +
    fillZeros(val.getUTCDate()) + ' ' +
    fillZeros(val.getUTCHours()) + ':' +
    fillZeros(val.getUTCMinutes()) + ':' +
    fillZeros(val.getUTCSeconds());

  function fillZeros(v) {
    return v < 10 ? '0' + v : v;
  }
}

To

***function dateToMysql(val) {
  return val.getFullYear() + '-' +
    fillZeros(val.getMonth() + 1) + '-' +
    fillZeros(val.getDate()) + ' ' +
    fillZeros(val.getHours()) + ':' +
    fillZeros(val.getMinutes()) + ':' +
    fillZeros(val.getSeconds());

  function fillZeros(v) {
    return v < 10 ? '0' + v : v;
  }
}***

It will save your hours Don't forgot to vote ;-)




回答2:


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.



来源:https://stackoverflow.com/questions/30003166/strongloop-how-to-config-mysql-timezone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!