Bộ điều hợp sails-mssqlserver là trình bao bọc của ứng dụng khách Microsoft SQL Server chính thức cho Node.js có sẵn here tuy nhiên, phụ thuộc của nó không nằm trong bản phát hành mới nhất.
Tùy chọn 1: Theo tài liệu chính thức này của gói MsSQL, bạn có thể bật nhiều bản ghi trong truy vấn bằng lệnh request.multiple = true.
Để bật nhiều truy vấn/bản ghi trong bộ điều hợp sails-mssqlserver, giải pháp khắc phục sự cố là mở sails-mssqlserver/lib/adapter.js và chỉnh sửa hàm truy vấn thô. Thêm request.multiple = true bên dưới var request = new mssql.Request (mssqlConnect). Như được trình bày trong ví dụ bên dưới.
// Raw Query Interface
query: function (connection, collection, query, data, cb) {
if (_.isFunction(data)) {
if (debugging) {
console.log('Data is function. A cb was passed back')
}
cb = data
data = null
}
adapter.connectConnection(connection, function __FIND__ (err, uniqId) {
if (err) {
console.error('Error inside query __FIND__', err)
return cb(err)
}
uniqId = uniqId || false
var mssqlConnect
if (!uniqId) {
mssqlConnect = connections[connection].mssqlConnection
} else {
mssqlConnect = connections[connection].mssqlConnection[uniqId]
}
var request = new mssql.Request(mssqlConnect)
// Add it here
request.multiple = true
request.query(query, function (err, recordset) {
if (err) return cb(err)
if (connections[connection] && !connections[connection].persistent) {
mssqlConnect && mssqlConnect.close()
}
cb(null, recordset)
})
})
},
Bây giờ bản ghi trả lại phải chứa nhiều kết quả.
Phương án 2: Một lựa chọn bền vững hơn đối với trường hợp sử dụng khi chạy một thủ tục lưu trữ mà trả về nhiều recordsets, là sử dụng phiên bản mới nhất của khách hàng chính thức của Microsoft SQL Server cho Node.js. Thông tin về chạy thủ tục lưu trữ có sẵn here
Đầu tiên cài đặt các gói mới nhất:
npm install mssql --save
Trong mã của bạn nơi bạn muốn chạy các thủ tục lưu trữ thêm một kết nối đến cơ sở dữ liệu MSSQL:
// require the mssql package
const sql = require('mssql')
// make a connection, you can use the values you have already stored in your adapter
const pool = new sql.ConnectionPool({
user: sails.config.connections.<yourMsSQLConnection>.user,
password: sails.config.connections.<yourMsSQLConnection>.password,
server: sails.config.connections.<yourMsSQLConnection>.server,
database: sails.config.connections.<yourMsSQLConnection>.database
})
// connect the pool and test for error
pool.connect(err => {
// ...
})
// run the stored procedure using request
const request = new sql.Request()
request.execute('procedure_name', (err, result) => {
// ... error checks
console.log(result.recordsets.length) // count of recordsets returned by the procedure
console.log(result.recordsets[0].length) // count of rows contained in first recordset
console.log(result.recordset) // first recordset from result.recordsets
console.log(result.returnValue) // procedure return value
console.log(result.output) // key/value collection of output values
console.log(result.rowsAffected) // array of numbers, each number represents the number of rows affected by executed statemens
// ...
})
// you can close the pool using
pool.close()
Trong trường hợp, nơi bộ điều hợp cơ sở dữ liệu sails- * không bao gồm tất cả các chức năng mà bạn yêu cầu. Tôi tìm thấy nó tốt nhất để tạo ra một dịch vụ buồm mà kết thúc tốt đẹp các chức năng bổ sung. Nó là một giải pháp thực sự sạch sẽ.
Bạn có thể sao chép cấu hình bộ điều hợp và lời gọi phương thức mô hình không? –