Javascript ES6数据模型类
我试图使用ES6类在我正在构建的API中构造数据模型(来自MySQL数据库)。我不喜欢使用ORM/ODM库,因为这将是一个非常基本、简单的API。但是,我正在努力弄清楚如何定义这些模型 我的数据实体是(这些只是一些简化的示例): 顾客 数据模型 私有方法 请求 我希望能够做到:Javascript ES6数据模型类,javascript,api,model-view-controller,ecmascript-6,models,Javascript,Api,Model View Controller,Ecmascript 6,Models,我试图使用ES6类在我正在构建的API中构造数据模型(来自MySQL数据库)。我不喜欢使用ORM/ODM库,因为这将是一个非常基本、简单的API。但是,我正在努力弄清楚如何定义这些模型 我的数据实体是(这些只是一些简化的示例): 顾客 数据模型 私有方法 请求 我希望能够做到: findById:提供单个customer.id,返回该特定客户的数据,即SELECT*FROM customers,其中id=? findByGroupId:提供group.id,返回属于该组的所有客户(对象数组中)
- findById:提供单个customer.id,返回该特定客户的数据,即
SELECT*FROM customers,其中id=?
- findByGroupId:提供group.id,返回属于该组的所有客户(对象数组中)的数据,即
SELECT*FROM customers,其中groupId=?
findById(1)代码>:
findByGroupId(2)代码>:
团体
数据模型
请求
我希望能够做到:
- findById:提供单个group.id,返回该特定组的数据,即
SELECT*FROM-groups,其中id=?
响应有效载荷
对于每个组对象,我希望返回JSON,如下所示:
findById(2)代码>:
要求:
- 必须使用ES6类
- 每个模型都有自己的文件(例如,
customer.js
)要导出
问题:
我的主要问题是:
在哪里使用私有方法(例如\u getState()
)定义数据结构,包括需要数据转换的字段
是否应在类的范围内定义findById
、findByGroupId
等?或者,应该使用单独的方法(与类在同一文件中)来实例化对象吗
我应该如何处理一个对象是另一个对象的子对象的情况,例如,将属于组对象的客户对象作为组的findById
中的对象数组返回
将连接到数据库的SQL查询应该在哪里定义?在getById
,getByGroupId
,等等
更新!!
这就是我想到的-(如果有人能够评论和评论,那就太棒了):
客户模型
群模型
客户控制器(实例化客户模型的位置)
这似乎工作得很好,我已经能够使用类
、承诺
和让
使它更为ES6友好
所以,我想了解一下我的方法。另外,我是否在此上下文中正确使用了导出
和必需
功能?这里是另一种方法
使用私有方法(例如_getState())在哪里定义数据结构,包括需要数据转换的字段
您应该在扩展顶层模型的模型类中定义这些字段和关系。例如:
class Group extends Model {
attributes() {
return {
id: {
type: 'integer',
primary: true
},
title: {
type: 'string'
}
};
}
relationships() {
return {
'Customer': {
type: 'hasMany',
foreignKey: 'groupId'
}
};
}
}
findById、findByGroupId等是否应在类的范围内定义?或者,是否应该使用单独的方法(与类位于同一文件中)来实例化对象?
在模型示例中使用findByAttribute(attr)
,而不是使用许多函数:
static findByAttribute(attr) {
return new Promise((resolve, reject) => {
var query = this._convertObjectToQueriesArray(attr);
query = query.join(" and ");
let records = `SELECT * from ${this.getResourceName()} where ${query}`;
var result = this.run(records);
// Note: Only support 'equals' and 'and' operator
if (!result) {
reject('Could not found records');
} else {
var data = [];
result.forEach(function(record) {
data.push(new this(record));
});
resolve(data);
}
});
}
/**
* Convert Object of key value to sql filters
*
* @param {Object} Ex: {id:1, name: "John"}
* @return {Array of String} ['id=1', 'name=John']
*/
static _convertObjectToQueriesArray(attrs) {
var queryArray = [];
for (var key in attrs) {
queryArray.push(key + " = " + attrs[key]);
}
return queryArray;
}
/**
* Returns table name or resource name.
*
* @return {String}
*/
static getResourceName() {
if (this.resourceName) return this.resourceName();
if (this.constructor.name == "Model") {
throw new Error("Model is not initialized");
}
return this.constructor.name.toLowerCase();
}
我应该如何处理一个对象是另一个对象的子对象的情况,例如,将属于组对象的客户对象作为组的findById中的对象数组返回?
对于关系,您应该有findRelations、getRelatedRecords等方法
var customer1 = new Customer({ id: 1, groupId: 3});
customer1.getRelatedRecords('Group');
class Model {
...
getRelatedRecords(reln) {
var targetRelationship = this.relationships()[reln];
if (!targetRelationship) {
throw new Error("No relationship found.");
}
var primaryKey = this._getPrimaryKey();
var relatedObject = eval(reln);
var attr = {};
if (targetRelationship.type == "hasOne") {
console.log(this.values);
attr[relatedObject.prototype._getPrimaryKey()] = this.values[targetRelationship.foreignKey];
} else if (targetRelationship.type == "hasMany") {
attr[targetRelationship.foreignKey] = this.values[this._getPrimaryKey()];
}
relatedObject.findByAttribute(attr).then(function(records) {
// this.values[reln] = records;
});
}
...
}
将连接到数据库的SQL查询应该在哪里定义?在getById、getByGroupId等中?
这是一个棘手的问题,但因为您希望解决方案简单,所以将查询放在find方法中。理想的情况是拥有自己的QueryBuilder类
检查下面的完整代码解决方案功能不全,但你明白了。我还在模型中添加了引擎变量,您可以使用它来增强抓取机制。所有其他设计理念都是你想象得到的:)
完整代码:
var配置={
引擎:“db'//Ex:rest,db
};
类模型{
构造函数(值){
这个值=值;
this.engine=config.engine;
}
toObj(){
变量数据={};
for(此.values中的var键){
if(此.values[key]instanceof Model){
data[key]=this.values[key].toObj();
}else if(此.values[key]数组实例){
data[key]=this.values[key].map(x=>x.toObj());
}否则{
数据[键]=此.值[键];
}
}
返回数据;
}
静态findByAttribute(attr){
返回新承诺((解决、拒绝)=>{
var query=this.\u convertObjectToQueriesArray(attr);
query=query.join(“and”);
let records=`SELECT*from${this.getResourceName()},其中${query}`;
var结果=此.run(记录);
//注意:仅支持“等于”和“与”运算符
如果(!结果){
拒绝(“找不到记录”);
}否则{
var数据=[];
结果.forEach(函数(记录){
数据推送(新此(记录));
});
解析(数据);
}
});
}
getRelatedRecords(reln){
var targetRelationship=this.relations()[reln];
if(!targetRelationship){
抛出新错误(“未找到关系”);
}
var primaryKey=this.\u getPrimaryKey();
var relatedObject=eval(reln);
var attr={};
if(targetRelationship.type==“hasOne”){
console.log(this.values);
attr[relatedObject.prototype.\u getPrimaryKey()]=this.values[targetRelationship.foreignKey];
}else if(targetRelationship.type==“hasMany”){
attr[targetRelationship.foreignKey]=this.values[this._getPrimaryKey()];
}
relatedObject.FindByaAttribute(attr).then(函数(记录){
[{
"id" : 1,
"name" : "John Doe",
"groupId" : 2,
"status" : "active",
"state" : "good"
},
{
"id" : 4,
"name" : "Pete Smith",
"groupId" : 2,
"status" : "suspended",
"state" : "bad"
}]
id
title
{
"id" : 2,
"title" : "This is Group 2",
"customers" : [{
"id" : 1,
"name" : "John Doe",
"groupId" : 2,
"status" : "active",
"state" : "good"
},
{
"id" : 4,
"name" : "Pete Smith",
"groupId" : 2,
"status" : "suspended",
"state" : "bad"
}]
}
'use strict';
class Cust {
constructor (custData) {
this.id = custData.id;
this.name = custData.name;
this.groupId = custData.groupId;
this.status = custData.status;
this.state = this._getState(custData.status);
}
_getState(status) {
let state = (status == 'active' ? 'good' : 'bad');
return state;
}
}
exports.findById = ((id) => {
return new Promise ((resolve, reject) => {
let custData = `do the MySQL query here`;
let cust = new Cust (custData);
let Group = require(appDir + process.env.PATH_API + process.env.PATH_MODELS + 'group');
Group.findById(cust.groupId).then(
(group) => {
cust.group = group;
resolve (cust)
},
(err) => {
resolve (cust);
}
);
});
});
'use strict';
class Group {
constructor (groupData) {
this.id = groupData.id;
this.title = groupData.title;
}
}
exports.findById = ((id) => {
return new Promise ((resolve, reject) => {
let groupData = `do the MySQL query here`;
if (id != 2){
reject('group - no go');
};
let group = new Group (groupData);
resolve (group);
});
});
'use strict';
var Cust = require(appDir + process.env.PATH_API + process.env.PATH_MODELS + 'cust');
class CustController {
constructor () {
}
getCust (req, res) {
Cust.findById(req.params.id).then(
(cust) => {
res(cust);
},
(err) => {
res(err);
}
)
}
}
module.exports = CustController;
class Group extends Model {
attributes() {
return {
id: {
type: 'integer',
primary: true
},
title: {
type: 'string'
}
};
}
relationships() {
return {
'Customer': {
type: 'hasMany',
foreignKey: 'groupId'
}
};
}
}
static findByAttribute(attr) {
return new Promise((resolve, reject) => {
var query = this._convertObjectToQueriesArray(attr);
query = query.join(" and ");
let records = `SELECT * from ${this.getResourceName()} where ${query}`;
var result = this.run(records);
// Note: Only support 'equals' and 'and' operator
if (!result) {
reject('Could not found records');
} else {
var data = [];
result.forEach(function(record) {
data.push(new this(record));
});
resolve(data);
}
});
}
/**
* Convert Object of key value to sql filters
*
* @param {Object} Ex: {id:1, name: "John"}
* @return {Array of String} ['id=1', 'name=John']
*/
static _convertObjectToQueriesArray(attrs) {
var queryArray = [];
for (var key in attrs) {
queryArray.push(key + " = " + attrs[key]);
}
return queryArray;
}
/**
* Returns table name or resource name.
*
* @return {String}
*/
static getResourceName() {
if (this.resourceName) return this.resourceName();
if (this.constructor.name == "Model") {
throw new Error("Model is not initialized");
}
return this.constructor.name.toLowerCase();
}
var customer1 = new Customer({ id: 1, groupId: 3});
customer1.getRelatedRecords('Group');
class Model {
...
getRelatedRecords(reln) {
var targetRelationship = this.relationships()[reln];
if (!targetRelationship) {
throw new Error("No relationship found.");
}
var primaryKey = this._getPrimaryKey();
var relatedObject = eval(reln);
var attr = {};
if (targetRelationship.type == "hasOne") {
console.log(this.values);
attr[relatedObject.prototype._getPrimaryKey()] = this.values[targetRelationship.foreignKey];
} else if (targetRelationship.type == "hasMany") {
attr[targetRelationship.foreignKey] = this.values[this._getPrimaryKey()];
}
relatedObject.findByAttribute(attr).then(function(records) {
// this.values[reln] = records;
});
}
...
}