SQLite Database update on file change

in 2 years

Here is how i use SQLite to grab new data on file change before someone calls it(from the routes).

I use this on express js you can set it up on app.js.

All the database will be stored into one variable and you can use javascript commands to manage/filter/split whatever you want as long as you don't want to access the db for adding or do any other stuff with it.

var Database = require('better-sqlite3'),
    db = new Database('./data/db.db'),
    database = [];
function data(){
   database = db.prepare('SELECT * FROM items').all;
}
data();
fs.watch('data.db',(s)=>{
  console.log(`There was a ${s} in the database`);
  data();
});

Just to be safe it's always nice to have these options on

db = new Database('data.db',{readonly:true,fileMustExist: true})

{readonly: true} blocks SQLite commands from executing like `insert`

{fileMustExist: true} throws error if you mess up with the location of the database instead of creating a new one

How to use universal-analytics with express

in 2 years

Just place this inside the app.js

Before you call the routes middleware


var ua = require('universal-analytics');
app.use(function(req,res,next){
ua('GoogleAnaliticsUsedIdGoesHere').pageview({
dp: req.url,
dh: req.headers.host,
uip: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
ua: req.headers['user-agent'],
dr: req.headers.referrer || req.headers.referer,
de: req.headers['accept-encoding'],
ul: req.headers['accept-language']
}).send();
next();
});

better-sqlite3 is truly better!

in 2 years

Shorter code and faster results!

It sure makes it easy and awesome in every way.


I just learned about better-sqlite3

in 2 years
It seems faster comparing it with the one i use right now(sqlite3)

So i will update the website again and share my experience!

Go check better-sqlite3 on npm

I updated the website

in 2 years
I created this blog today,
I don't realy know if i am going to use it but i wanted to check sqlite (how it works on nodejs and compare it with other databases).
Seems prety easy!
previous