【发布时间】:2021-05-20 00:16:48
【问题描述】:
我被困了好几天,试图将 mysql 作为非默认数据存储添加到我的sails 项目中。如果有人知道解决方案。请
似乎 config/datastores.js 中的附加数据存储未注册,或者显示的错误不正确。
“帆”:“^1.4.0”, “帆-mysql”:“^1.0.1”
我不断收到此错误:
错误:钩子 (
orm) 加载失败!错误:无法拆除 ORM 钩子。错误详细信息:错误:数据存储标识无效。没有数据 商店以该身份存在。
config/datastores.js
module.exports.datastores = {
default: {
adapter: 'sails-disk',
},
mysqlDB: {
adapter: 'sails-mysql',
url: `mysql://${process.env.TC_DB_USERNAME}:${process.env.TC_DB_PASSWORD}@${process.env.TC_DB_HOST}:3306/Auth`,
},
};
api/models/Account.js
module.exports = {
datastore: 'mysqlDB',
schema: false,
};
作为一种解决方法,我使用标准的“mysql”包在助手内部进行连接。这工作得很好,但是资源很重,我很想获得标准的sails方法(Account.sendNativeQuery())来让模型工作。 如果我不能让它工作,也许有人可以告诉我如何使这个连接全局化,所以我只需要连接一次,而不是每次调用助手?
helpers/new-account.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host: process.env.TC_DB_HOST,
user: process.env.TC_DB_USERNAME,
password: process.env.TC_DB_PASSWORD,
database: 'Auth',
});
connection.connect();
module.exports = {
friendlyName: 'Creates new account',
description: 'returns the id of the new usr or throws error',
inputs: {
username: {
description: 'The new account name.',
type: 'string',
required: true,
minLength: 4,
maxLength: 64,
},
salt: {
required: true,
type: 'ref',
description: 'generated salt.',
},
verifier: {
required: true,
type: 'ref',
description: 'generated verifier.',
},
email: {
required: true,
type: 'string',
isEmail: true,
description: 'Email linked to account.',
},
},
exits: {
success: {
description: 'All done.',
},
},
fn: async function ({ username, salt, verifier, email }, exits) {
connection.query(
'INSERT INTO account SET ?',
{ username, salt, verifier, email },
function (err, result) {
if (err) {
throw new Error(`MYSQL ERROR: ${err}`);
} else {
console.log(`new user with id ${result.insertId} created.`);
exits.success(result.insertId);
}
}
);
connection.end();
},
};
谢谢
【问题讨论】: