简体   繁体   English

我如何在Bookshelfjs和knex中编写此代码

[英]How do I write this in Bookshelfjs and knex

I'm making a simple session store from bookshelfjs , but I need to set the timestamp column correctly. 我正在从一个简单的会话存储bookshelfjs ,但我需要正确设置时间戳列。 I'm trying to write the column's ttl with now() + ttl interval of 1ms, but I can't recall how to get it done, or how to trigger raw content for that column: 我试图用now()+ ttl间隔为1ms来写列的ttl,但是我不记得如何完成它或如何触发该列的原始内容:

var Session = Models.Bookshelf.Model.extend({
  tableName: 'sessions',
  idAttribute: 'id'
},{
  get: Promise.method(function(sid){
    console.log("Fetching SID = ", sid);
    return new this({id: sid}).fetch();
  }),
  set: Promise.method(function(sid, session, ttl){
    console.log("Setting SID = ", sid);
    console.log("Setting SID session = ", session );
    console.log("Setting SID ttl = ", ttl);
    new this({id: sid}).save({session: session, expiry: "(now() + " + ttl + " * interval '1 ms')" });
  }),
  destroy: Promise.method(function(sid){
    this.destroy({id: sid});
  })
});

The original Query from koa-pg-session is: 来自koa-pg-session的原始查询是:

UPDATE %I.%I SET session = $1, expiry = (now() + $2 * interval '1 ms') WHERE id = $3;

I'm sure it's with Knex's raw query formatter, but I can't find the correct part in the documentation for setting a the column's value. 我确定它与Knex的原始查询格式化程序一起使用,但是我在文档中找不到用于设置列值的正确部分。 I don't want to write the full query in a string, I want to know how to do it for just the single column 'expiry'. 我不想在字符串中写完整的查询,我想知道如何仅对单列“ expiry”执行此操作。

我还没有测试过,请告诉我它是否有效(当然,您首先需要使用knex):

new this({id: sid}).save({session: session, expiry: knex.raw("(now() + " + ttl + " * interval '1 ms')") });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM