有没有办法在 loki 中指定复合唯一索引?我试过类似的东西:
db.addCollection('contents', {unique: ['id', 'wsID']});
但这似乎产生了 2 个不同的唯一索引..这是我想要使其独一无二的两者的组合。
非常感谢您的任何建议。
有没有办法在 loki 中指定复合唯一索引?我试过类似的东西:
db.addCollection('contents', {unique: ['id', 'wsID']});
但这似乎产生了 2 个不同的唯一索引..这是我想要使其独一无二的两者的组合。
非常感谢您的任何建议。
我通过制作代理键做了这样的事情:
db.addCollection('contents', {unique: 'mySurrogateKey'});
向集合中添加记录时,您可以创建自定义属性并将其设置为简单的串联:
record.mySurrogateKey = `${record.id}${record.wsID}`
collection.insert(record)
相关: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);