gpt4 book ai didi

mysql - 使用带有 Breeze Angular mySql Node Express 堆栈的命名约定的异常

转载 作者:行者123 更新时间:2023-11-28 23:57:55 24 4
gpt4 key购买 nike

按照 todo-angular 示例,我能够通过 Breeze/Angular 客户端从 mySql 数据库成功连接和查询数据。我关闭了 db 表和 GUI,但仍然没问题。当我尝试使用命名约定时,问题就开始了。 (我无法控制我必须连接到的数据库,我真的不想在我的客户端中使用 Uppercase_Underscored_Words!)

我收到以下异常:

/Users/Sherri/Sites/awdb-web/node_modules/breeze-sequelize/node_modules/breeze-client/breeze.debug.js:1852
throw new Error("Unable to locate a registered object by the name: " + k
^
Error: Unable to locate a registered object by the name: NamingConvention.underscoreCamelCase
at Object.__config._fetchObject (/Users/Sherri/Sites/awdb-web/node_modules/breeze-sequelize/node_modules/breeze-client/breeze.debug.js:1852:13)
at MetadataStore.proto.importMetadata (/Users/Sherri/Sites/awdb-web/node_modules/breeze-sequelize/node_modules/breeze-client/breeze.debug.js:6517:40)
at new module.exports.MetadataMapper (/Users/Sherri/Sites/awdb-web/node_modules/breeze-sequelize/MetadataMapper.js:19:8)
at SequelizeManager.importMetadata (/Users/Sherri/Sites/awdb-web/node_modules/breeze-sequelize/SequelizeManager.js:46:24)
at createSequelizeManager (/Users/Sherri/Sites/awdb-web/server/routes.js:114:8)
at /Users/Sherri/Sites/awdb-web/server/routes.js:23:27

当我从 metadata.json 文件中取出 "namingConvention": "camelCase" 行时,错误消失了,但是当然,数据库属性无法正确转换。

这是我用来设置实体管理器的相关代码:(编辑:我很确定我的问题出在服务器端,但与此代码无关)

var namingConvention = new UnderscoreCamelCaseConvention();
namingConvention.setAsDefault();

breeze.core.config.initializeAdapterInstance("uriBuilder", "json");

var serviceName = 'breeze/awdb';
var manager = new breeze.EntityManager(serviceName);


// Take any server property name and make it camelCase for the client to use.
// also, save it so that we can convert from the client back to the server's name
function UnderscoreCamelCaseConvention() {
var serverNames = {
netPoints: 'netPoints',
netPointsSpent: 'netPointsSpent'
}; // every translated server name

return new breeze.NamingConvention({
name: 'underscoreCamelCase',
clientPropertyNameToServer: clientPropertyNameToServer,
serverPropertyNameToClient: serverPropertyNameToClient
});

function clientPropertyNameToServer(clientPropertyName) {
return serverNames[clientPropertyName];
}

function serverPropertyNameToClient(serverPropertyName) {
var clientName = _.camelCase(serverPropertyName);
serverNames[clientName] = serverPropertyName;
return clientName;
}
}

这是我的 metadata.json 文件的片段:

{
"metadataVersion": "1.0.5",
"namingConvention": "underscoreCamelCase",
"localQueryComparisonOptions": "caseInsensitiveSQL",
"dataServices": [
{
"serviceName": "breeze/awdb/",
"hasServerMetadata": true,
"jsonResultsAdapter": "webApi_default",
"useJsonp": false
}
],
"structuralTypes": [
{
"shortName": "person",
"namespace": "AWdb.Models",
"autoGeneratedKeyType": "Identity",
"defaultResourceName": "people",
"dataProperties": [
{
"name": "Person_ID",
"dataType": "Int32",
"isNullable": false,
"defaultValue": 0,
"isPartOfKey": true,
"validators": [
{
"name": "required"
},
{
"min": -2147483648,
"max": 2147483647,
"name": "int32"
}
]
},
{
"name": "Household_ID",
"dataType": "Int32",
"validators": [
{
"min": -2147483648,
"max": 2147483647,
"name": "int32"
}
]
},
....
]
}
],
"resourceEntityTypeMap": {"people": "person:#AWdb.Models"}
}

编辑:这是获取元数据的 routes.js 文件中的代码。

  var fs = require('fs');
var breezeSequelize = require('breeze-sequelize');

var SequelizeManager = breezeSequelize.SequelizeManager;
var SequelizeQuery = breezeSequelize.SequelizeQuery;
var SequelizeSaveHandler = breezeSequelize.SequelizeSaveHandler;

var breeze = breezeSequelize.breeze;
var EntityQuery = breeze.EntityQuery;

var dbConfig = {
host: 'localhost',
user: 'xx',
password: 'xx',
dbName: 'xx'
};

var _sequelizeManager = createSequelizeManager();

// _sequelizeManager.sync(true).then(seed).then(function(){
// console.log('db init successful');
// });

exports.init = init;

function init(app) {
app.get('/breeze/awdb/Metadata', function (req, res, next) {
try {
var metadata = readMetadata();
res.send(metadata);
} catch(e){
next(e);
}
});

function createSequelizeManager() {
var metadata = readMetadata();
var sm = new SequelizeManager(dbConfig);
sm.importMetadata(metadata);

return sm;
}

function readMetadata() {
var filename = "server/AWdbMetadata.json";
if (!fs.existsSync(filename)) {
filename = "AWdbMetadata.json";

if (!fs.existsSync(filename)) {
throw new Error("Unable to locate file: " + filename);
}
}
var metadata = fs.readFileSync(filename, 'utf8');
return JSON.parse(metadata);
}

有什么想法吗?当我在 node.js 服务器上使用 metadata.json 文件而不是 .net Entity Framework 时,我是否应该能够使用自定义命名约定?

最佳答案

如果我没有看错,那么我认为您的问题是服务器上的元数据。如果我理解正确,您的表名和列名遵循 Uppercase_Underscored_Word 模式。服务器上的 Breeze/Sequelize 堆栈目前没有转换名称的能力,因此您必须完全按照它们在 DB 模式中使用实体和属性的名称。否则,Breeze to Sequelize 转换将失败。您仍然可以在客户端上使用命名约定将带下划线的服务器名称转换为您希望它们在客户端上的任何名称。

因此,您需要两个元数据文件。一个用于 Breeze/Sequelize 堆栈使用的服务器,它使用与数据库中完全相同的名称,然后一个单独的元数据文件用于客户端,您可以在其中进行翻译。

关于mysql - 使用带有 Breeze Angular mySql Node Express 堆栈的命名约定的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31015520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com