Lokijs:插入具有唯一密钥违规的文档

Lokijs: Inserting document with unique key violation

我是 运行 最新的,截至 5.3.2016,从 Lokijs.org 和 NW.js v0.12.3-win-x64 缩小 Lokijs。我有一个文档已经保存在 Lokijs 中:

"collections":[{"name":"admins","data":[{"username":"erik","meta":{"revision":1,"created":1459028934981,"version":0,"updated":1462333795190},"$loki":1}],"idIndex":[1],"binaryIndices":{},"constraints":null,"uniqueNames":["username"],"transforms":{},"objType":"admins","dirty":true,"cachedIndex":null,"cachedBinaryIndex":null,"cachedData":null,"transactional":false,"cloneObjects":false,"cloneMethod":"parse-stringify","asyncListeners":false,"disableChangesApi":true,"autoupdate":false,"ttl":{"age":null,"ttlInterval":null,"daemon":null},"maxId":2,"DynamicViews":[],"events":{"insert":[null],"update":[null],"pre-insert":[],"pre-update":[],"close":[],"flushbuffer":[],"error":[null],"delete":[null],"warning":[null]},"changes":[],"username":{"name":"username","regExp":{}}}.

我试图在尝试插入重复键值时生成错误。我在此集合中的 'username' 键上添加了唯一约束,并验证了 collection.uniqueNames 数组包含 'username'.

当我 运行 下面的代码时,正如预期的那样,没有其他文档被插入到 collection.data 数组中并且数据库被保存。但是,不会生成任何错误。另外,当我console.log插入方法后的文档对象有运行时,它变为:

Object {username: "erik", meta: Object, $loki: 2}.

当我将键值更改为其他值时,唯一文档将被插入并正确保存。

尝试插入包含违反唯一约束的键的文档时,如何生成错误?谢谢。

insertDocument: function(objParameters) {

    var collection = objParameters.insert.collection;
    collection.ensureUniqueIndex('username');
    var document = {username: ''};
    document.username = 'erik';

    collection.on('error', function(err) {
        return console.log(err);
    });

    collection.insert(document);

    return thisModule.$body.triggerHandler('app.database.save');
}

编辑:loki.db 测试克隆

{"filename":"loki.db","collections":[{"name":"test","data":[{"name":"erik","meta":{"revision":0,"created":1462493328062,"version":0},"$loki":1}],"idIndex":[1],"binaryIndices":{},"constraints":null,"uniqueNames":["name"],"transforms":{},"objType":"test","dirty":true,"cachedIndex":null,"cachedBinaryIndex":null,"cachedData":null,"transactional":false,"cloneObjects":true,"cloneMethod":"parse-stringify","asyncListeners":false,"disableChangesApi":true,"autoupdate":false,"ttl":{"age":null,"ttlInterval":null,"daemon":null},"maxId":2,"DynamicViews":[],"events":{"insert":[null],"update":[null],"pre-insert":[],"pre-update":[],"close":[],"flushbuffer":[],"error":[null],"delete":[null],"warning":[null]},"changes":[]}],"databaseVersion":1.1,"engineVersion":1.1,"autosave":false,"autosaveInterval":5000,"autosaveHandle":null,"options":{},"persistenceMethod":"fs","persistenceAdapter":null,"verbose":false,"events":{"init":[null],"loaded":[],"flushChanges":[],"close":[],"changes":[],"warning":[]},"ENV":"NODEJS"}

用于测试克隆的代码:

var loki = require('lokijs-1.3.min.js');
var db = new loki();
var collection = db.addCollection('test', {
    clone: true,
    unique: 'name'
});

collection.on('error', function(error) {
    return console.log(error);
});

collection.insert({ name: 'erik'});
collection.insert({ name: 'erik'});

db.saveDatabase();

如果不使用clone: true则需要调用coll.update(document)强制重新计算索引,这会触发错误。