2

I am running the latest, as of 5.3.2016, minified Lokijs from Lokijs.org and NW.js v0.12.3-win-x64. I have a document already saved in 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":{}}}.

I am trying to generate an error when I attempt to insert a duplicate key value. I have added a unique constraint on the 'username' key in this collection and have verified the collection.uniqueNames array contains 'username'.

When I run the code below, as expected, no additional documents are inserted into the collection.data array and the database is saved. However, no errors are generated. Also, when I console.log the document object after the insert method has run, it changes to:

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

When I change the key value to something else, the unique document is then inserted and saved properly.

How do I go about generating an error when attempting to insert a document containing key(s) that violate unique constraints? Thank you.

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');
}

EDIT: loki.db to test clone

{"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"}

Code to test clone:

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();
eschlosser
  • 23
  • 5

1 Answers1

0

If you don't use clone: true then you need to call coll.update(document) to force index recomputation, which will trigger the error.

Joe Minichino
  • 2,793
  • 20
  • 20
  • Thanks for your reply Joe. I do get an error when I insert a blank document then update that document. However, I seem to be missing something when I try to use 'clone: true' instead. I receive the same results as previously described. I have edited my post to include the new db and test code. What am I missing? – eschlosser May 06 '16 at 00:33
  • @eschlosser sorry I worded things in a confusing way. The clone option just makes it apparent that the object you modify is not the one in the datastore. update() is where the recomputation takes place. However it should definitely throw an error on insert, maybe it's worth opening an issue on github. – Joe Minichino May 06 '16 at 14:48