これで、完全に機能するソリューションができました。非常に動的なので、ここの例で解決策を見つけることができなかったので共有したいと思います.
これは、 app.jsによって呼び出されるdb-functions.jsファイルです。
var express = require("express");
var tds = require("tedious");
var Request = require("tedious").Request;
var Types = require("tedious").TYPES;
var ConnectionPool = require("tedious-connection-pool");
var poolConfig = {
min: 10,
log: true,
};
var config = {
userName: "User",
password: "password",
server: "192.168.1.123",
options: {
requestTimeout: 30 * 1000,
instanceName: "InstanceOnServer",
//instanceName: '\SQLServer', // Name of SQL Instance
rowCollectionOnRequestCompletion: true,
database: "PlantDB",
encrypt: true,
debug: {
data: true, // lots of info generated in console when true
payload: false,
token: false,
packet: true, // was on true
log: true,
},
},
};
var pool = new ConnectionPool(poolConfig, config); //multiple connections
pool.acquire(function (err, connection) {
if (err) {
console.log(err);
} else {
console.log("connected");
}
});
//var conn = new tds.Connection(config);
pool.on("error", function (err) {
console.log(err);
});
var conn = new tds.Connection(Config);
function requestDone(rowCount, more) {
console.log(rowCount + "rows------");
console.log(column);
}
function infoError(info) {
console.log(info.number + " : " + info.message);
}
function debug(message) {
console.log(message);
}
var exec = function (sql, callback) {
// use this function to execute SELECT statements and Stored Procedures
var request = new Request(sql, function (err, rowCount) {
if (err) {
console.log("Statement failed: " + err);
} else {
console.log(rowCount + " rows");
}
callback(err, obj);
});
var obj = []; //holds the data generated by the SQL statement when applicable
request.on("row", function (columns) {
//identify individual rows
var values = {}; //insert a value
columns.forEach(function (column) {
//identify the column
if (column.isNull) {
/*adds a key to the value so that each item in
the object is broken into "Column: Value" pairs*/
values[column.metadata.colName] = null;
} else {
values[column.metadata.colName] = column.value;
}
});
obj.push(values);
});
conn.execSql(request);
//console.log('obj: ' + obj);
return obj;
function columnMetadata(columnsMetadata) {
columnsMetadata.forEach(function (column) {
//console.log(column);
});
}
};
var storedProcedures = {
//This is an actual stored procedure that exists in my
purchasing_list:
//Purchases Database which is used to identify this object
{
fullName: "Purchases.dbo.purchasing_list",
//fullname of stored procedure
variable: "purchaseList",
//the name of my "Table Type" Variable created in SQL
Columns: [
{ name: "ItemNo", type: Types.Int },
{ name: "ProductName", type: Types.NVarChar },
{ name: "ProductDescription", type: Types.NVarChar },
{ name: "UnitOfMeasure", type: Types.NVarChar },
{ name: "Qty", type: Types.Int },
{ name: "Department", type: Types.NVarChar },
{ name: "RequestedBy", type: Types.NVarChar },
],
},
another_stored_procedure: {
fullName: "Purchases.dbo.another_stored_procedure",
variable: "purchaseList",
Columns: [
{ name: "ItemNo", type: Types.Int },
{ name: "ProductName", type: Types.NVarChar },
{ name: "ProductDescription", type: Types.NVarChar },
{ name: "UnitOfMeasure", type: Types.NVarChar },
{ name: "Qty", type: Types.Int },
{ name: "Department", type: Types.NVarChar },
{ name: "RequestedBy", type: Types.NVarChar },
],
},
};
var callProc = function (procedureName, Rows, callback) {
var usefulData = storedProcedures[procedureName];
var message = "";
var table = {
columns: usefulData["Columns"],
rows: Rows,
//insert an array of arrays with values defined by storedProcedure
};
var request = new Request(usefulData["fullName"], function (err, rowCount) {
if (err) {
console.log("Statement failed: " + err);
message = "There was a problem submitting your request!";
} else {
console.log(rowCount + "No errors in TVP");
//this will likely return an undefined rowCount since this function
//is generally used to insert and update
message = "Success";
}
callback(err, message);
});
request.on("doneProc", function (rowCount, more, returnStatus, rows) {
//console.log('Row Count' + rowCount);
//console.log('More? ' + more);
//console.log('Return Status: ' + returnStatus);
//console.log('Rows:' + rows);
});
request.addParameter(usefulData["variable"], Types.TVP, table);
//Add a table type set of parameters to the request to complete the
//transaction
conn.callProcedure(request);
};
var insert = function (sql, callback) {
var request = new Request(sql, function (err, rowCount) {
if (err) {
console.log("Statement failed: " + err);
callback = err;
} else {
console.log("Insert Statement succeeded");
callback = "Success";
}
});
conn.execSql(request);
};
var update = function (sql, callback) {
var insert = "";
var request = new Request(sql, function (err, rowCount) {
if (err) {
insert = "Statement failed: " + err;
} else {
insert = "Insert Statement succeeded";
}
callback(err, insert);
});
conn.execSql(request);
};
exports.conn = conn;
exports.exec = exec;
exports.callProc = callProc;
exports.insert = insert;
exports.update = update;
App.jsファイル内で、次のようにデータベースをロードしました。
var db = require('./public/javascripts/db-functions');
複数の行を挿入または更新できるcallProcedureは、次のように実行できます。
app.route("/update_purchasing").post(function (req, res) {
var Rows = req.body.Rows;
db.callProc("create_purchasing_list", Rows, function (err, success) {
if (err) {
res.json("Error creating purchasing list: " + err);
} else {
res.json("Successfully updated your purchase list");
}
});
});
行は、ストアド プロシージャで選択された列に基づいて、次のようになります。
var Rows = [
[0101, "hair gel", "makes your hair stick up", "vat", 1, "house", "Bob"],
[0102, "carbon", "makes light boats", "roll", 1, "Layup", "Rob"],
];
EXEC my lookを使用してストアド プロシージャを実行する方法の例は、次のようになります。
app.get("/start", function (req, res) {
var sess = req.session;
db.exec(
"dbo.populate_shipping_schedule" + "parameter1",
function (err, schedule) {
if (!err) {
sess.schedule = schedule;
res.render("start", { title: "Start" });
} else {
console.log("This is an Error: " + err);
}
}
);
});