Code Monkey home page Code Monkey logo

Comments (5)

 avatar commented on July 21, 2024

As with all asynchronous functions - you simply put your code which needs to run into the callback - something like this

db.loadDatabase(function (err) {
if (err)
console.log(err);
else {
// database is loaded - do your thing here...
}
});

It's crucial that you design your applications around this 'cascading' or 'waterfall' model - that each operation's success triggers the next (and that a failure causes everything to die in a manageable way).

from nedb.

szwacz avatar szwacz commented on July 21, 2024

Yes, of course I'm aware of this solution :)

But that's not the problem. The problem is that when my application want to get some data before database was loaded (and it does) I will have to implement some "deferred operations queue" or "stop the world until loaded" functionalities, when delegating this task to database engine make much more sense to me.

And by the way current behaviour (returning empty list without any warning) is actually kind of bug.

It would be very neat if database engine could simply accept all incoming requests, but execute them after "fully loaded" (what is perfectly ok, because database queries are asynchronous anyway).

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

Hello,

I'm still not quite sure about the need for that. It is not especially hard to do but it will take some time, so I want to be sure it's useful. Could you explain a situation where your app wants data before database was loaded ?

If I were to do it, I think it would be in the form of an "autoload" where the database is loaded as soon as it is created but that comes with the drawback that a query executed during the load will take longer to execute.

from nedb.

szwacz avatar szwacz commented on July 21, 2024

When all data accessors are ready to work just after instantiation (are not asynchroneous) you don't have to implement init state and init view in application, you can just jump to important stuff :).
So it's just a matter of convenience and ease of development. Of course you can live without it.

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

This is now done (as of v0.7.14).

Summary of the relevant part in the docs: if you use a persistent database, no operation will be executed unless you either call loadDatabase manually or create the datastore using the autoload option. In the latter case, loadDatabase will automatically be called and any command issued before it finished will be buffered and be executed when load is finished.

For now the default for autoload is false since it could produce unexpected behavior for people who actually want to control when db is loaded. The default will be true in the future.

from nedb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.