一个应用程序中的不同变量名称大小写约定
Different variable name case convention in one application
这真是一个微不足道的问题。我只是想知道如何以 "professional" 的方式处理这个问题。
我正在努力遵守变量命名约定。对于 NodeJs,我正在做驼峰式。对于数据库,我正在使用 PostgreSQL 并使用 underscore_casing.
现在从PostgreSQL查询数据时出现问题。我将得到一个具有以下格式的用户对象,
{user_id: 1, account_type : "Admin"}
我可以将此对象直接传递给服务器端渲染,并且必须使用下划线来访问 account_type。当然,我可以使用 属性 userId
和 accountType
手动创建一个新用户 JSON 对象,但这是不必要的工作。
是否可以遵循两种语言的变量命名约定并避免在某些文件中使用混合变量名大小写?保持井井有条的好方法是什么?
我也曾为此苦苦挣扎,我得出的结论是,除非重写来自数据库的对象,否则真的没有办法避免这种丑陋。幸运的是,这在 Javascript:
中并不难
const fromDBtoJS = (obj) => {
// declare a variable to hold the result
const result = {};
// iterate over the keys on the object
Object.keys(obj).forEach((key) => {
// adjust the key
const newKey = key.replace(/_[a-z]/g, (x) => x[1].toUpperCase());
// add the value from the old object with the new key
result[newKey] = obj[key];
});
// return the result
return result;
};
这是一个JSFiddle. The "replace" code above was found here
如果您想在应用程序中对模型使用 类,您可以将此代码合并到构造函数或数据库加载方法中,以便 more-or-less 自动处理。
有两种解决此问题的好方法。最简单的一个 - 不做任何转换,使用准确的数据库名称。第二个是自动 camel-case 列。
无论哪种方式,您都应该始终遵循所有 PostgreSQL 声明的下划线符号,因为它会让您选择在以后需要时在您的应用程序中激活 camel-casing。永远不要在数据库中使用camel-case,否则你以后会很痛苦。
如果您想两全其美,请遵循所有 PostgreSQL 声明的下划线符号,并在读取数据时转换为 camel-case。
下面是如何使用 pg-promise, copied from event receive 示例正确执行此操作的示例:
// Example below shows the fastest way to camelize column names:
var options = {
receive: function (data, result, e) {
camelizeColumns(data);
}
};
function camelizeColumns(data) {
var template = data[0];
for (var prop in template) {
var camel = pgp.utils.camelize(prop);
if (!(camel in template)) {
for (var i = 0; i < data.length; i++) {
var d = data[i];
d[camel] = d[prop];
delete d[prop];
}
}
}
}
这真是一个微不足道的问题。我只是想知道如何以 "professional" 的方式处理这个问题。
我正在努力遵守变量命名约定。对于 NodeJs,我正在做驼峰式。对于数据库,我正在使用 PostgreSQL 并使用 underscore_casing.
现在从PostgreSQL查询数据时出现问题。我将得到一个具有以下格式的用户对象,
{user_id: 1, account_type : "Admin"}
我可以将此对象直接传递给服务器端渲染,并且必须使用下划线来访问 account_type。当然,我可以使用 属性 userId
和 accountType
手动创建一个新用户 JSON 对象,但这是不必要的工作。
是否可以遵循两种语言的变量命名约定并避免在某些文件中使用混合变量名大小写?保持井井有条的好方法是什么?
我也曾为此苦苦挣扎,我得出的结论是,除非重写来自数据库的对象,否则真的没有办法避免这种丑陋。幸运的是,这在 Javascript:
中并不难const fromDBtoJS = (obj) => {
// declare a variable to hold the result
const result = {};
// iterate over the keys on the object
Object.keys(obj).forEach((key) => {
// adjust the key
const newKey = key.replace(/_[a-z]/g, (x) => x[1].toUpperCase());
// add the value from the old object with the new key
result[newKey] = obj[key];
});
// return the result
return result;
};
这是一个JSFiddle. The "replace" code above was found here
如果您想在应用程序中对模型使用 类,您可以将此代码合并到构造函数或数据库加载方法中,以便 more-or-less 自动处理。
有两种解决此问题的好方法。最简单的一个 - 不做任何转换,使用准确的数据库名称。第二个是自动 camel-case 列。
无论哪种方式,您都应该始终遵循所有 PostgreSQL 声明的下划线符号,因为它会让您选择在以后需要时在您的应用程序中激活 camel-casing。永远不要在数据库中使用camel-case,否则你以后会很痛苦。
如果您想两全其美,请遵循所有 PostgreSQL 声明的下划线符号,并在读取数据时转换为 camel-case。
下面是如何使用 pg-promise, copied from event receive 示例正确执行此操作的示例:
// Example below shows the fastest way to camelize column names:
var options = {
receive: function (data, result, e) {
camelizeColumns(data);
}
};
function camelizeColumns(data) {
var template = data[0];
for (var prop in template) {
var camel = pgp.utils.camelize(prop);
if (!(camel in template)) {
for (var i = 0; i < data.length; i++) {
var d = data[i];
d[camel] = d[prop];
delete d[prop];
}
}
}
}