SQLite Database update on file change

20/06/18 06:59:54

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

16/04/18 07:35:25

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!

01/04/18 14:46:39

Shorter code and faster results!

It sure makes it easy and awesome in every way.


I just learned about better-sqlite3

26/03/18 10:40:41
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

25/03/18 12:21:01
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