Method 1:
create a module that will have only one instance from the class it contains on all modules,
lets call it level-driver.js:
const level = require('level-party'); // used for local lightweight db (google's leveldb) with multi process connection
class LevelDriver
{
constructor(dbPath)
{
// connect to the local leveldb using json schema
this.db = level(dbPath);
}
...
}
// singleton instance holder
let instance = null;
function levelDriver(dbPath)
{
if(instance == null)
{
// create an instance of LevelDriver
instance = new LevelDriver(dbPath);
}
return instance;
}
// export only the instance (not the object) so it will be a singleton
// to all requireing modules (no matter the name or path)
module.exports = levelDriver;
then use it like this:
const levelDriver = require('./level-driver')('./users/users-db'); // manages leveldb connection and actions
Method 2:
const level = require('level-party'); // used for local lightweight db (google's leveldb) with multi process connection
class LevelDriver
{
constructor(dbPath)
{
// connect to the local leveldb using json schema
this.db = level(dbPath);
}
...
}
// export only the instance (not the object) so it will be a singleton
// to all requireing modules (no matter the name or path)
module.exports = (dbPath) => { return new LevelDriver(dbPath) }
then use it like this:
const levelDriver = require('./level-driver')('./users/users-db'); // manages leveldb connection and actions
newthat you need to avoid!new, so you can do one of these:let foo = new Object({ name: "fooInstance", type: "someObject" });or:let foo = { name: "fooInstance", type: "someObject" };orlet foo = new Foo("fooInstance", "someObject");and it will all be the same all will return true to:foo instanceof Object(beside obviously the last example all others will return false onfoo instanceof Foo) and they all execute the Object constructor, and return a reference to that created instance