Nodes connecting to db only once

I have a db.js file that has this line at the top for connecting to a database. I call this file to start requests from other js files:

var sqlite3 = require('sqlite3').verbose();

var db = new sqlite3.Database('./mydatabase');

db.serialize(function() {

db.each("SELECT rowid AS id, info FROM lorem", function(err, row) {

  console.log(row.id + ": " + row.info);

});


db.close();

If I require () this file above 4 times in different files, does this mean that the sqlite database will be initialized many times?

I want to initialize it only for the first time.

Is it really ineffective? Is there a more efficient way?

+4
source share
1 answer

Official documentation: https://github.com/mapbox/node-sqlite3/wiki/Caching

Sqlite3 , require('sqlite3').cached, .. new sqlite3.cached.Database(file), , file, , . : https://github.com/mapbox/node-sqlite3/blob/master/lib/sqlite3.js

. - , . , , , :

//module1.js
module.exports = function(db){
    db.serialize(...)
    //dostuff
}

//start.js
var sqlite3 = require('sqlite3').verbose();
var module1 = require('./module1.js');
var db = new sqlite3.Database('./mydatabase');

module1(db);
+2

Source: https://habr.com/ru/post/1538682/


All Articles