Does db.all() always return an array of objects?

Question

Does db.all() always return an array of objects?

Answer

As a matter of fact, yes, no matter how the query is being set, the return will always be an array of objects. for example:

db.all("SELECT * FROM Elements WHERE Atomic_number > 20",(err, row)=>{
  console.log(row);
})

in that case, we could get a long array of all the elements which electrons are greater than 20, and it may look like so:

[ { Name: 'Actinium',
   Symbol:  'Ac',
   Mass_number: 227,
   Atomic_number: 85,
    ...
  },
  {...},
  {...},
  {...},
  {...},
  {...},
   ...]

Now, we could think if we adjusted it to just ask for names we would get an array of strings, but no, it will still be an object with one key/value pair, ie.:

db.all("SELECT Name FROM Elements WHERE Atomic_number < 20",(err, row)=>{
  console.log(row);
})

which would look like so:

[ { Name: 'Aluminum'},
  { Name: 'Argon'},
  { Name: 'Beryllium'},
  { Name: 'Boron'},
  {...},
  {...},
  {...},
  {...},
  {...},
  {...}, 
   ...]

So db.all() from SQLite3 will always return an array of objects, unless it does not find anything, in that case we will just receive back a wonderful undefined.

1 Like