.driver
此数据存储的通用、无状态、低级驱动程序(如果适配器支持)。
datastore.driver;
此属性并非所有数据库适配器都保证存在。如果数据存储的基础适配器不支持标准化驱动程序接口,则
driver
将不存在。
假设您正在构建自己的结构化数据可视化工具(例如 phpMyAdmin)。您可能希望动态连接到任意数量的不同数据库。
// Get the generic, stateless driver for our database (e.g. MySQL).
var Driver = sails.getDatastore().driver;
// Create our own dynamic connection manager (e.g. connection pool)
var manager = (
await Driver.createManager({ connectionString: req.param('connectionUrl') })
).manager;
var db;
try {
db = (
await Driver.getConnection({ manager: managerReport.manager })
).connection;
} catch (err) {
await Driver.destroyManager({ manager: managerReport.manager });
throw err;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Do some stuff here...
// e.g.
// await Driver.sendNativeQuery({
// connection: db,
// nativeQuery: '...'
// });
// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Finally, before we continue, tear down the dynamic connection manager.
// (this also takes care of releasing the active connection we acquired above)
await Driver.destroyManager({ manager: managerReport.manager });
return res.ok();