WebReflection / dblite

sqlite for node.js without gyp problems
MIT License
209 stars 34 forks source link

on close events don't work as expected. #52

Closed SSANSH closed 6 years ago

SSANSH commented 6 years ago

Hi,

I am happy to use this great package, however I would like to make a process which create db, import data inside and then send data to a remote host. My issue is when I call close methods with query, I never recevied on close events as describe on specification

db.on('close', function (code) {
  // by default, it logs "bye bye"
  // invoked once the database has been closed
  // and every statement in the queue executed
  // the code is the exit code returned via SQLite3
  // usually 0 if everything was OK
  console.log('safe to get out of here ^_^_');
});

and so I never can launch my third step of my process.

plese see eveidence bellow

var dblite = require('dblite'),
db = dblite('./db.sqlite');
db.on('close', function (code) {
  // by default, it logs "bye bye"
  // invoked once the database has been closed
  // and every statement in the queue executed
  // the code is the exit code returned via SQLite3
  // usually 0 if everything was OK
  console.log('safe to get out of here ^_^_', code);
});
db.close();

outpus is : safe to get out of here ^^ 0

This is OK.

however

var dblite = require('dblite'),
db = dblite('./db.sqlite');
db.on('close', function (code) {
  // by default, it logs "bye bye"
  // invoked once the database has been closed
  // and every statement in the queue executed
  // the code is the exit code returned via SQLite3
  // usually 0 if everything was OK
  console.log('safe to get out of here ^_^_', code);
});
db.query('CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)');
db.close();

output is empty, event is never emit, this is not OK.

Can you please tell me if I am doing something wrong?

WebReflection commented 6 years ago

Being the query asynchronous, how about this?

var dblite = require('dblite'),
db = dblite('./db.sqlite');
db.on('close', function (code) {
  // by default, it logs "bye bye"
  // invoked once the database has been closed
  // and every statement in the queue executed
  // the code is the exit code returned via SQLite3
  // usually 0 if everything was OK
  console.log('safe to get out of here ^_^_', code);
});
db.query(
  'CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY, value TEXT)',
  function () {
    db.close();
  }
);
SSANSH commented 6 years ago

With following code, event is already not provided, do you try our your side? I have no console log on my output.

WebReflection commented 6 years ago

the whole test is based on closing so I don't really need to, this is also an old project so if people where unable to close the db this bug would've been still open.

Are you sure there are no other issues?

miellaby commented 6 years ago

looks like https://github.com/WebReflection/dblite/issues/32 , do you have the right package?

WebReflection commented 6 years ago

Hey @SSANSH please give us some answer ASAP or there's no point in keeping this opened since there's no action to take as it is.

Thanks.

SSANSH commented 6 years ago

Hi, Yes I have the right package without error of my side. This is the latest package published on npmjs "dblite": "^0.9.0". I don't see other issue, you can test the issue by executing the code provided by me. Please tell me if you need more.

Thanks.

SSANSH commented 6 years ago

@miellaby, @WebReflection, problem seems to come from unref method which is called before sending onclose event. By chaning the code like this, event onclose is correctly sent.

diff --git a/src/dblite.js b/src/dblite.js
index ce7d0c7..335af4e 100644
--- a/src/dblite.js
+++ b/src/dblite.js
@@ -357,7 +357,6 @@ function dblite() {
     // node 0.6 has not unref
     if (program.unref) {
       program.on('close', close);
-      program.unref();
     } else {
       IS_NODE_06 = true;
       program.stdout.on('close', close);
@@ -374,6 +373,7 @@ function dblite() {
   function close(code) {
     if (self.listeners('close').length) {
       self.emit('close', code);
+      program.unref();
     } else {
       log('bye bye');
     }
@@ -909,4 +909,4 @@ db.query('CREATE TABLE test (key INTEGER PRIMARY KEY, value TEXT)') && undefined
 db.query('INSERT INTO test VALUES(null, "asd")') && undefined;
 db.query('SELECT * FROM test') && undefined;
 // db.close();
-*/
\ No newline at end of file
+*/
WebReflection commented 6 years ago

hey @SSANSH , why didn't you file directly a PR ? Thanks for the finding though

WebReflection commented 6 years ago

anyway, v0.10 is out and it should fix this.

SSANSH commented 6 years ago

ok thanks you for your quick fix, in future I will do a PR. Thanks.