Reputation: 51
Is there a way to specify a compound unique index in loki ? I tried something like:
db.addCollection('contents', {unique: ['id', 'wsID']});
but this appears to make 2 different unique indexes.. it's the combination of the two that I'd like to make unique.
Many thanks for any suggestions.
Upvotes: 3
Views: 1187
Reputation: 2208
Related: https://github.com/techfort/LokiJS/issues/450
What I really use
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);
Upvotes: 0
Reputation: 289
I did something like this by making a surrogate key:
db.addCollection('contents', {unique: 'mySurrogateKey'});
When adding records to the collection, you can create a custom property and set it to be a simple concatenation:
record.mySurrogateKey = `${record.id}${record.wsID}`
collection.insert(record)
Upvotes: 1