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

时间:2016-05-04 04:04:00

标签: lokijs

我正在运行最新的,截至2016年5月5日,从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":{}}}

我尝试插入重复键值时尝试生成错误。我在'用户名'上添加了一个唯一约束。密钥在此集合中并验证了collection.uniqueNames数组包含'用户名'。

当我按照预期运行下面的代码时,没有其他文档插入到collection.data数组中并保存数据库。但是,不会生成错误。此外,当我在insert方法运行后控制文档对象时,它会更改为:

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();

1 个答案:

答案 0 :(得分:0)

如果您不使用 node.name: any-name node.master: false node.data: false tribe: t1: cluster.name: mycluster1 discovery.zen.ping.unicast.hosts: ["host1"] discovery.zen.ping.multicast.enabled: false t2: cluster.name: mycluster2 discovery.zen.ping.unicast.hosts: ["host2"] discovery.zen.ping.multicast.enabled: false ,则需要调用clone: true来强制重新计算索引,这将触发错误。