I am in process of writing nodejs app. It is based on expressjs. I am confused on doing inheritance in nodejs modules. What i am trying to do is create a model base class, let's say my_model.js.
module.exports = function my_model(){
my_model.fromID = function(){
//do query here
}
}
Now i want to use those methods in my_model in my other model class. let's say user_model.js
How do i inherit my_model in user_model?
in base_model:
function BaseModel() { /* ... */ }
BaseModel.prototype.fromID = function () { /* ... */ };
module.exports = BaseModel;
in user_model:
var BaseModel = require('relative/or/absolute/path/to/base_model');
function UserModel() {
UserModel.super_.apply(this, arguments);
}
UserModel.super_ = BaseModel;
UserModel.prototype = Object.create(BaseModel.prototype, {
constructor: {
value: UserModel,
enumerable: false
}
});
UserModel.prototype.yourFunction = function () { /* ... */ };
module.exports = UserModel;
Instead of using Object.create() directly, you can also use util.inherits, so your user_model becomes:
var BaseModel = require('relative/or/absolute/path/to/base_model'),
util = require('util');
function UserModel() {
BaseModel.apply(this, arguments);
}
util.inherits(UserModel, BaseModel);
UserModel.prototype.yourFunction = function () { /* ... */ };
module.exports = UserModel;
With ES6 the usage of util.inherits() is discouraged in favor of ES6 class and extends
const EventEmitter = require('events');
class MyStream extends EventEmitter {
constructor() {
super();
}
write(data) {
this.emit('data', data);
}
}
const stream = new MyStream();
stream.on('data', (data) => {
console.log(`Received data: "${data}"`);
});
stream.write('With ES6');
Using utility.inherits can also help you decouple the child from the parent.
Instead of calling the parent explicitly, you can use super_ to call the parent.
var BaseModel = require('relative/or/absolute/path/to/base_model'),
util = require('util');
function UserModel() {
this.super_.apply(this, arguments);
}
util.inherits(UserModel, BaseModel);
utility.inherits source:
var inherits = function (ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false
}
});
};
Related
I have 2 tests which each one them pass but when i run them together the second test fails.
I have this global var (CURR_GUID) which i want to clear between the tests but i am not sure how to do that.
Since it is not cleared between the tests , its value is still "1111111" and not null.
Any idea?
Class W
"use strict";
var CURR_GUID = null;
class W {
constructor() {
console.log("W ctor")
}
async getDataFromDb() {
if (CURR_GUID) {
return CURR_GUID;
} else {
this.db = require("./Connection").getDb();
console.log("W getDataFromDb")
let res = null;
res = await this.db.one("SELECT guid FROM table", ["NONE"]);
return res.guid;
}
}
async saveGuid(guid) {
this.db = require("./Connection").getDb();
await this.db.none("update table set guid=$1", [guid]);
CURR_GUID = guid;
console.log("currr is" + CURR_GUID);
}
}
module.exports = W;
Class X
"use strict";
const wDao = new (require("../db/W"))();
class X {
async getGuid() {
console.log("X getGuid")
let guid = await wDao.getDataFromDb();
console.log("got guid " + guid);
await wDao.saveGuid(guid);
return guid;
}
}
module.exports = X;
Test.spec.js
const chai = require("chai");
const expect = chai.expect;
const X = require("../src/service/X");
const Connection = require("../src/db/Connection");
const sinon = require("sinon");
describe("component_test", function () {
afterEach(function () {
this.dbStub.restore();
});
it("X_component_test", async function () {
var db = {
one: async () => {
return {
guid: '1111111',
created_on: "123"
}
},
none: async () => {
}
};
this.dbStub = sinon.stub(Connection, "getDb").returns(db);
var dao = new X();
var guid = await dao.getGuid();
expect(guid).to.equal('1111111');
});
it("X_component_test_2", async function () {
var db = {
one: async () => {
return {
guid: '222222',
created_on: "123"
}
},
none: async () => {
}
};
this.dbStub = sinon.stub(Connection, "getDb").returns(db);
var dao = new X();
var guid = await dao.getGuid();
expect(guid).to.equal('222222');
});
});
While it should be possible to achieve the desired result by deleting W and X modules from require.cache in afterEach hook, it might be better to modify the source code a bit so that it can be tested without cache manipulations.
Class W
Turn CURR_GUID variable into an instance property.
"use strict";
class W {
constructor() {
console.log("W ctor");
this.CURR_GUID = null;
}
async getDataFromDb() {
if (this.CURR_GUID) {
return this.CURR_GUID;
} else {
this.db = require("./Connection").getDb();
console.log("W getDataFromDb")
let res = null;
res = await this.db.one("SELECT guid FROM table", ["NONE"]);
return res.guid;
}
}
async saveGuid(guid) {
this.db = require("./Connection").getDb();
await this.db.none("update table set guid=$1", [guid]);
this.CURR_GUID = guid;
console.log("currr is" + this.CURR_GUID);
}
}
module.exports = W;
Class X
Turn wDao from variable into an instance property
"use strict";
const WDao = require("../db/W");
class X {
constructor() {
this.wDao = new WDao();
}
async getGuid() {
console.log("X getGuid")
let guid = await this.wDao.getDataFromDb();
console.log("got guid " + guid);
await this.wDao.saveGuid(guid);
return guid;
}
}
module.exports = X;
In this way every test should work with fresh instances of W and X that do not depend on the closure state.
I have a feathersjs service created using the feathers generate service command. I want to override its definition of create method.
This is my service class
/* eslint-disable no-unused-vars */
// Initializes the `userGroup` service on path `/usergroup`
const createService = require('feathers-sequelize');
const createModel = require('../../models/user-group.model');
const hooks = require('./user-group.hooks');
const filters = require('./user-group.filters');
const async = require('async');
module.exports = function () {
const app = this;
const Model = createModel(app);
const paginate = app.get('paginate');
const options = {
name: 'usergroup',
Model,
paginate,
create: fnCreateGroup // documentation says this allows you to create your own create method
};
function fnCreateGroup(data, params) {
let json = {
done: false
};
let permissionIds = Object.keys(data.permissionList), inIds = [];
for (let i in permissionIds) {
if (data.permissionList[permissionIds[i]]) {
inIds.push(parseInt(permissionIds[i]));
}
}
if (inIds.length === 0) {
json.cause = 'You must provide the permission List';
return Promise.resolve(json);
}
async.parallel([
function (cb) {
Model.create({
groupName: data.groupName
}).then(function (ug) {
cb(null, ug);
}, function (err) {
cb(err, null);
});
},
function (cb) {
app.models.permissions.findAll({
where: {
id: {
$in: inIds
}
}
}).then(function (plist) {
cb(null, plist);
});
}
]
, function (err, results) {
if (typeof err !== 'undefined' && err !== null) {
json.err = err;
return Promise.resolve(json);
} else {
let permissions = results[1], group = results[0];
for (let i = 0; i < permissions.length; i++) {
group.addPermission(permissions[i]);
}
json.done = true;
json.id = group.id;
return Promise.resolve(json);
}
});
}
// Initialize our service with any options it requires
app.use('/usergroup', createService(options));
// Get our initialized service so that we can register hooks and filters
let service = app.service('usergroup');
service.hooks(hooks);
if (service.filter) {
service.filter(filters);
}
};
The test on create method shows the result of original method and not mine.
What can I do?
How to override and extend existing services is documented in the database adapter common API. You can use hooks by setting hook.result or extend the existing ES6 class:
const Service = require( 'feathers-sequelize').Service;
class MyService extends Service {
create(data, params) {
data.created_at = new Date();
return super.create(data, params);
}
}
app.use('/todos', new MyService({
paginate: {
default: 2,
max: 4
}
}));
my code that works..
var express=require("express"),
app=express();
class bar {
constructor()
{
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
app.get("/liste/:ext",function(req,res){
var ext=req.params.ext;
res.setHeader('Content-Type', 'application/json');
if(ext==1)
{
var newbar=new bar();
newbar.name("alex").surname("broox").age(32).get(function(result){
res.json({data:result})
})
}
if(ext==2)
{
var newbar=new bar();
newbar.name("alex2").get(function(result){
res.json({data:result})
})
}
})
app.listen(4000,function(log){
console.log("listening")
})
but..following code does not work.. with require that class from other file..
test.js
module.exports = {
class bar {
constructor()
{
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
};
app.js file.. with require that class
var express=require("express"),
app=express();
require("./test")
app.get("/liste/:ext",function(req,res){
var ext=req.params.ext;
res.setHeader('Content-Type', 'application/json');
if(ext==1)
{
var newbar=new bar();
newbar.name("alex").surname("broox").age(32).get(function(result){
res.json({data:result})
})
}
if(ext==2)
{
newbar.name("alex2").get(function(result){
res.json({data:result})
})
}
})
app.listen(4000,function(log){
console.log("listening")
})
but why does not it work...please help me..above code that works but this code that does not work..
When exporting classes in node you need to define the class first, then export the class using module.exports followed by the name of the class you wish to export.
// test.js
class Bar {
constructor() {
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
module.exports = Bar
From there you can just require the file and grab the class as such.
var Bar = require('./test');
var bar = new Bar();
test.js isn't valid syntax - you shouldn't have your entire file wrapped in braces like that. module.exports is just a variable which you set; if you want to export bar, set it to bar:
class bar {
...
}
module.exports = bar;
Additionally, you need to assign the result of your require call in app.js.
var bar = require("./test");
(On a slightly pedantic note - it's more idiomatic to captialize your class names!)
hello again #DominicValenciana
test.js
class bar {
constructor()
{
this.user_name=null;
this.user_surname=null;
this.user_age=null;
}
name(user_name) {
this.user_name=user_name;
return this;
}
surname(user_surname) {
this.user_surname=user_surname;
return this;
}
age(user_age) {
this.user_age=user_age;
return this;
}
get(callback) {
var list ={};
list.uname=this.user_name;
list.usurname=this.user_surname;
list.uage=this.user_age;
callback(list);
}
}
module.exports = bar;
app.js
var express=require("express"),
app=express();
app.get("/liste/:ext",function(req,res){
var ext=req.params.ext;
res.setHeader('Content-Type', 'application/json');
if(ext==1)
{
var bar=new require('./test');
bar.name("alex").surname("broox").age(32).get(function(result){
res.json({data:result})
})
}
if(ext==2)
{
var bar=new require('./test');
bar.name("alex2").get(function(result){
res.json({data:result})
})
}
})
app.listen(4000,function(log){
console.log("listening")
})
as error : TypeError: bar.name is not a function
I couldnt write function worked automatically (constructor) when file required in node.js..
this is so...file.js
module.exports = {
index: function () {
//code here
}
};
app.js
var file=require("./file");
res.send(file.index());
what I want...
module.exports = {
main :__constructor()
{
this.name="blabla";
},
index: function () {
//code here
this.name // will be used this place name variable
}
};
Use .prototype() to do this. Hope it helps!
const App = function(name) {
this.name = name
}
App.prototype.main = function() {
return this.name
}
b = new App('myName')
console.log(b.main())
//You can do module.exports = App and invoke it in another file
okey..but
module.exports = {
app :function (name)
{
this.name="blabla";
},
app.prototype.index: function () {
}
};
this does not work!!!
/var/www/public/nodeapi/app/v1/blog/index.js:19
app.prototype.index: function () {
const App = function(name) {
this.name = name
}
App.prototype.main = function() {
return this.name
}
b = new App('myName')
console.log(b.main())
//You can do module.exports = App and invoke it in another file
Something like this?
// file.js
class MyClass {
constructor() {
this.name = 'blabla';
}
index() {
console.log('name', this.name);
}
}
module.exports = new MyClass();
class blog {
constructor()
{
this.name="fookey";
}
index(callback)
{
var data={};
data.test=this.name;
callback(data);
}
}
module.exports=new blog();
TypeError: Cannot read property 'name' of undefined
why does not work?
I have a Node.js module which return memory used:
//meminfo.js
module.exports = myObj;
myObj = (function() {
var pub = {};
pub.getmem = function() {
meminfo.list(function(memused) {
return memused;
});
};
return pub;
})();
If I want to retrieve memused by this:
//main.js
var meminfo = require('./meminfo');
console.log(myObj.getmem());
Then could you give me some suggestions about how to return 'memused' from this in meminfo.js:
meminfo.list(function(memused) {
return memused;
});