AngeloC
AngeloC

Reputation: 3533

How to create a nodejs module to which I can pass an argument?

I'd like to create a data access object where I can pass a database name, say:

var Dao = require('./dao')

mydb = new Dao('sample_database');
mydb.append('hello')

any idea how to do this? thanks

Upvotes: 1

Views: 63

Answers (1)

Sumit Jha
Sumit Jha

Reputation: 1691

I have created minimal solution from what I understand.

doa.js

let Dao = function(dbName) {
    console.log(dbName);
}

Dao.prototype.append = function (text) {
  console.log("append " + text)
}
module.exports = Dao;´

main.js

let Dao = require('./dao');
let daoInstance = new Dao('testName');
daoInstance.append("apple");

Let me know if it helps.

Upvotes: 3

Related Questions