Val
Val

Reputation: 1350

How to automate sequelize request statements programmatically?

I use sequelize as the Object Relational Mapper to connect to PostgreSQL database. The following statement works great, yet each request I am having to manually write.

global.db.dataBaseTable.build().instanceMethod(successcb, data, errcb);

Is there a way build this statement using an array filled with commands to create multiple statements using loops? The following is an example of the code I used, yet the compiler kicks back with errors.

var ary_db_table = ["aTable", "bTable", "cTable"]

for(var i = 0; i<=1; i++){
     global.db.ary_db_table[i].build().instanceMethod(successcb, data, errcb)
}

Upvotes: 0

Views: 44

Answers (1)

piotrbienias
piotrbienias

Reputation: 7401

Your condition inside the for is wrong, it should be i < ary_db_table.length

for(var i = 0; i < ary_db_table.length; i++){
    global.db[ary_db_table[i]].build().instanceMethod(successcb, data, errcb);
}

Or you could use forEach method

ary_db_table.forEach(function(dbTable){
    global.db[dbTable].build().instanceMethod(successcb, data, errcb);
});

Anyway, what is the purpose of doing such an operation? Why do you use global here?

Upvotes: 1

Related Questions