log*_*ins 5 postgresql node.js bookshelf.js knex.js
我在我的应用程序中使用Knex查询构建器和Postgres.我想在UTC时间中使用ISO8016格式的数据在我的数据库中添加created_at和updated_at字段.我想让我的数据看起来像这样:
2017-04-20T16:33:56.774Z
Run Code Online (Sandbox Code Playgroud)
在我的Knex迁移中,我尝试使用.timestamps()方法创建两者created_at并updated_at手动使用.timestamp()
方法并自己命名.
当我种子我的数据库,并设置created_at和updated_at等于moment().utc().toISOString(),但它在我的数据库,因为这存储:
2017-04-20 11:20:00.851-05
Run Code Online (Sandbox Code Playgroud)
代码和数据库之间存在一些改变数据的东西,我不知道它是Knex,Postgres节点库还是Postgres本身.
Postgres 以内部格式存储时间戳,当您读取它时,它会以您请求的格式显示它。
knex_test=# update accounts set created_at = '2017-04-20T16:33:56.774Z'; UPDATE 47
knex_test=# select created_at from accounts where id = 3;
created_at
----------------------------
2017-04-20 19:33:56.774+03
(1 row)
knex_test=# \d accounts
Table "public.accounts"
Column | Type | Modifiers
------------+--------------------------+-------------------------------------------------------------
id | bigint | not null default nextval('test_table_one_id_seq'::regclass)
last_name | character varying(255) |
email | character varying(255) |
logins | integer | default 1
about | text |
created_at | timestamp with time zone |
updated_at | timestamp with time zone |
phone | character varying(255) |
Indexes:
"test_table_one_pkey" PRIMARY KEY, btree (id)
"test_table_one_email_unique" UNIQUE CONSTRAINT, btree (email)
"test_table_one_logins_index" btree (logins)
knex_test=#
Run Code Online (Sandbox Code Playgroud)
您可以更改 postgres 返回连接时间戳的时区
knex_test=# SET timezone = 'UTC';
SET
knex_test=# select created_at from accounts where id = 3;
created_at
----------------------------
2017-04-20 16:33:56.774+00
(1 row)
knex_test=#
Run Code Online (Sandbox Code Playgroud)
这是如何使用 knex 完成的https://github.com/tgriesser/knex/issues/97
var knex = Knex.initialize({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test',
},
pool: {
afterCreate: function(connection, callback) {
connection.query('SET timezone = timezone;', function(err) {
callback(err, connection);
});
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2151 次 |
| 最近记录: |