【问题标题】:lokijs - is there a way to make a compound unique index in loki?lokijs - 有没有办法在 loki 中制作复合唯一索引?
【发布时间】:2016-11-15 18:30:58
【问题描述】:

有没有办法在 loki 中指定复合唯一索引?我试过类似的东西:

db.addCollection('contents', {unique: ['id', 'wsID']});

但这似乎产生了 2 个不同的唯一索引.. 这是我想要唯一的两者的组合。

非常感谢您的任何建议。

【问题讨论】:

    标签: lokijs


    【解决方案1】:

    我通过制作代理键做了类似的事情:

    db.addCollection('contents', {unique: 'mySurrogateKey'});
    

    向集合中添加记录时,您可以创建自定义属性并将其设置为简单的串联:

    record.mySurrogateKey = `${record.id}${record.wsID}`
    collection.insert(record)
    

    【讨论】:

      【解决方案2】:

      相关:https://github.com/techfort/LokiJS/issues/450

      我真正使用的东西

      import SparkMD5 from "spark-md5";
      import stringify from "fast-json-stable-stringify";
      
      public getTemplateId(t: IDbTemplate) {
          const {front, back, css, js} = t;
          return SparkMD5.hash(stringify({front, back, css, js}));
      }
      
      const tHook = (t: IDbTemplate) => {
          t.key = this.getTemplateId(t);
      };
      
      this.template.on("pre-insert", tHook);
      this.template.on("pre-update", tHook);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-23
        • 2012-10-31
        • 1970-01-01
        • 2021-02-21
        • 2010-11-12
        • 1970-01-01
        • 1970-01-01
        • 2014-05-01
        相关资源
        最近更新 更多