bajankristof / nedb-promises

A dead-simple promise wrapper for nedb.
MIT License
298 stars 45 forks source link

[BUG] ensureIndex duplicating #50

Closed alexgaya closed 2 years ago

alexgaya commented 3 years ago

The function ensureIndex is duplicating the code in the .db file but the docs say: " No need to remove any ensureIndex though, if it is called on a database that already has the index, nothing happens."

Before app inits I have the db.ensureIndex({ fieldName: 'name', unique: true }) and the line of code written in the .db file is being duplicated.

I've tried the same with '@seald-io/nedb' and its not being duplicated.

bajankristof commented 2 years ago

Hi!

Thanks for the bug report!

I did some digging around and to me it looks like this is indeed a bug with @seald-io/nedb or nedb itself.

The index is being duplicated when you start from a new process and run the ensureIndex function on an existing datastore file.

I verified this by doing the following:

  1. I entered a new node interactive shell and ran the following commands:
    Welcome to Node.js v12.22.6.
    Type ".help" for more information.
    > const Datastore = require('.')
    undefined
    > const datastore = Datastore.create("test.db")
    undefined
    > datastore.ensureIndex({ fieldName: 'test', unique: true })
    Promise { <pending> }
    > datastore.ensureIndex({ fieldName: 'test', unique: true })
    Promise { undefined }
    > 
    (To exit, press ^C again or ^D or type .exit)

    After exiting the test.db datastore file contained the following:

    {"$$indexCreated":{"fieldName":"test","unique":true}}
  2. I ran the same commands again in a new node interactive shell and exited, after which the test.db file contained the duplicated index:
    {"$$indexCreated":{"fieldName":"test","unique":true}}
    {"$$indexCreated":{"fieldName":"test","unique":true}}
  3. Finally I deleted the datastore file and did all of this again using @seald-io/nedb (instead of require('.') - require('@seald-io/nedb')) and I got the same result as I did using nedb-promises.

I am not sure whether this is intentional or not (maybe compacting the datastore would get rid of the duplicate index entries) so I suggest opening a discussion about this in @seald-io/nedb.

tex0l commented 2 years ago

Hi, this might be related to something I'm actively working on: https://github.com/seald/nedb/issues/9

If you can make a repro case on @seald-io/nedb, I'd be interested.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.