Search code examples
javascriptnode.jsdatabasenedb

TypeError: Cannot read property 'setAutocompactionInterval' of undefined


My database has several duplicates (same _id), even after restarting and using autoload: true. This is strange, but I decided to solve it by using the setAutocompactionInterval as is suggested in the guide in the repository, and the result was simply TypeError: Cannot read property 'setAutocompactionInterval' of undefined.

const Db = require('nedb-promise')
    , curry = new Db({
        filename: 'curry'
        , autoload: true
        , onload: (e) => e && console.err(e)
    })

curry.persistence.setAutocompactionInterval(3600000)
// TypeError: Cannot read property 'setAutocompactionInterval' of undefined
Db.persistence.setAutocompactionInterval(3600000)
// TypeError: Cannot read property 'setAutocompactionInterval' of undefined
Db.curry.persistence.setAutocompactionInterval(3600000)
// TypeError: Cannot read property 'persistence' of undefined

I don't know what causes this error. I think I will make a Github issue on nedb-promise, but is this because of a misuse? Am I misunderstanding the way it should work? Nobody seems to have this error, according to my Google searches.


Solution

  • This might help, datastore loosely follows nedb's implementation. It is not an exact representation, that's instead of going through all these lines of codes:

    const Db = require('nedb-promise')
        , curry = new Db({
            filename: 'curry'
            , autoload: true
            , onload: (e) => e && console.err(e)
        })
    

    If you want to control the underlying datastore, you can create it as regular using the original nedb library, and then create a wrapped version:

    const nedb = require('nedb')
    const nedbP = require('nedb-promise')
    
    const ds = nedb(...)
    const db = nedbP.fromInstance(ds)
    
    const Datastore = require('nedb')
    const nedbPromise = require('nedb-promise')
    
    const store = Datastore({autoload: true, filename: '...'})
    const db = nedbPromise.fromInstance(store)
    await db.insert(...)
    store.persistence.compactDatafile()
    

    I hope this helps.