thenativeweb / node-eventstore

EventStore Implementation in node.js
http://eventstore.js.org/
MIT License
539 stars 117 forks source link

When using tingodb, no more events are published after restarting app #154

Open gingobaum opened 4 years ago

gingobaum commented 4 years ago

Hi,

I wanted to use eventstore with tingodb, since it's the only why for using eventstore with the local filesystem.

When I start my app for the first time, everything works fine. But when I start it again, it does not commit and publish the events. It seems there is an error when committing.

Error: duplicate key error index

The options I pass to eventstore are:

{
    "type": "tingodb",
    "dbPath": "./data/store",
    "eventsCollectionName": "events",
    "snapshotsCollectionName": "snapshots",
    "transactionsCollectionName": "transactions"
}

When I delete the .tingo file and start again, it's all fine.

Any clues what is going wrong?

adrai commented 4 years ago

Is it reproducable in a sample repo?

gingobaum commented 4 years ago

Here is one.

Using:

Running it for the first time works as expected. A second time ends up in mentioned error.

const eventStore = require('eventstore');

const customPublisher = function (event) {
    console.log('customPublisher:', event);
};

const options = {
    type: 'tingodb',
    dbPath: './data',
    eventsCollectionName: 'events',
    snapshotsCollectionName: 'snapshots',
    transactionsCollectionName: 'transactions'
};

const es = eventStore(options);
es.useEventPublisher(function (event) {
    customPublisher(event);
});

const add = function (event) {
    es.getEventStream(event.id, function (err, stream) {
        if(err) {
            console.error(err);
            return;
        }
        stream.addEvent(event);
        stream.commit( function (err) {
            if(err) {
                console.log(err);
                return;
            }
            console.log('committed event: ', event);
        });
    });
};

es.init( function (err) {
    if(err) {
        console.log(err);
        return;
    }
    const events = [];
    const numbers = 2;
    for(let i=0; i<numbers; i++) {
        events.push({
            id: String(i),
            name: 'event ' + i
        });
    }
    events.forEach(add);
});

And I just noticed, that in the case of running the script, increasing the number of events and running it again, will cause an error for the number of events saved before. Every additional will be added as usual.

It's all the same, if I dynamicly generate the ids of my events.

adrai commented 4 years ago

Can you try with v1.15.2 ?

gingobaum commented 4 years ago

It seems to work as expected with v1.15.2. Thank you very much.