javascript - 数据模型的 ES6 类

标签 javascript api model-view-controller ecmascript-6 models

我正在尝试使用 ES6 类在我正在构建的 API 中构建数据模型(来自 MySQL 数据库)。我不喜欢使用 ORM/ODM 库,因为这将是一个非常基本、简单的 API。但是,我正在努力思考如何定义这些模型。

我的数据实体是(这些只是一些简化的例子):

客户

数据模型

id
name
groupId
status (enum of: active, suspended, closed)

私有(private)方法

_getState(status) {
    var state = (status  == 'active' ? 'good' : 'bad');
    return state;
}

请求

我希望能够做到:

  • findById:提供单个 customer.id,返回该特定客户的数据,即 SELECT * FROM customers WHERE id = ?

  • findByGroupId:提供 group.id,返回属于该组的所有客户(在对象数组中)的数据,即 SELECT * FROM customers WHERE groupId = ?

响应负载

对于每个客户对象,我想像这样返回 JSON:

findById(1);:

[{
    "id" : 1,
    "name" : "John Doe",
    "groupId" : 2,
    "status" : "active",
    "state" : "good"
}]

findByGroupId(2);:

[{
    "id" : 1,
    "name" : "John Doe",
    "groupId" : 2,
    "status" : "active",
    "state" : "good"
},
{
    "id" : 4,
    "name" : "Pete Smith",
    "groupId" : 2,
    "status" : "suspended",
    "state" : "bad"
}]

集团

数据模型

id
title

请求

我希望能够做到:

  • findById:提供单个 group.id,返回该特定组的数据,即 SELECT * FROM groups WHERE id = ?

响应负载

对于每个组对象,我想像这样返回 JSON:

findById(2);:

{
    "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"
    }]
}


要求:

  • 必须使用 ES6 类
  • 每个模型在其自己的文件(例如 customer.js)中导出


问题:

我的主要问题是:

  1. 我在哪里定义数据结构,包括需要数据转换的字段,使用私有(private)方法(例如 _getState())
  2. findByIdfindByGroupId 等是否应该在类的范围内定义?或者,它们是否应该通过单独的方法(在与类相同的文件中)来实例化对象?
  3. 我应该如何处理一个对象是另一个对象的子对象的情况,例如返回属于 Group 对象的 Customer 对象作为 GroupfindById 中的对象数组?
  4. 应该在哪里定义将连接到数据库的 SQL 查询?在getById,getByGroupId等?

更新!!

这就是我想出的 - (如果有人可以评论和评论那就太棒了):

客户模型

'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);
  });
});

CUSTOMER Controller(实例化 Customer 模型的地方)

'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;

这似乎运作良好,我已经能够使用 ClassPromiselet 使其对 ES6 更加友好.

所以,我想就我的方法征求一些意见。此外,我是否在此上下文中正确使用了 exportrequired 功能?

最佳答案

这是另一种方法,

我应该在哪里定义数据结构,包括需要数据转换的字段,使用私有(private)方法(例如 _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();
}

我应该如何处理一个对象是另一个对象的子对象的情况,例如将属于 Group 对象的 Customer 对象作为 Group 的 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 等?

这很棘手,但由于您希望解决方案简单,所以请将查询放在查找方法中。理想的场景是拥有自己的 QueryBuilder 类。

检查以下完整代码,解决方案功能不全,但您明白了。我还在模型中添加了引擎变量,您可以使用它来增强获取机制。所有其他设计想法都取决于您的想象力:)

完整代码:

var config = {
  engine: 'db' // Ex: rest, db
};
class Model {

  constructor(values) {
    this.values = values;
    this.engine = config.engine;
  }

  toObj() {
    var data = {};
    for (var key in this.values) {
      if (this.values[key] instanceof Model) {
        data[key] = this.values[key].toObj();
      } else if (this.values[key] instanceof Array) {
        data[key] = this.values[key].map(x => x.toObj());
      } else {
        data[key] = this.values[key];
      }
    }
    return data;
  }

  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);
      }
    });
  }

  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;
    });
  }

  /**
   * Test function to show what queries are being ran.
   */
  static run(query) {
    console.log(query);
    return [];
  }

  _getPrimaryKey() {
    for (var key in this.attributes()) {
      if (this.attributes()[key].primary) {
        return key;
      }
    }
  }

  /**
   * 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();
  }
}

class Customer extends Model {
  attributes() {
    return {
      id: {
        type: 'integer',
        primary: true
      },
      name: {
        type: 'string'
      },
      groupId: {
        type: 'integer'
      },
      status: {
        type: 'string'
      },
      state: {
        type: 'string'
      }
    };
  }

  relationships() {
    return {
      'Group': {
        type: 'hasOne',
        foreignKey: 'groupId'
      }
    };
  }
}

class Group extends Model {
  attributes() {
    return {
      id: {
        type: 'integer',
        primary: true
      },
      title: {
        type: 'string'
      }
    };
  }

  relationships() {
    return {
      'Customer': {
        type: 'hasMany',
        foreignKey: 'groupId'
      }
    };
  }
}

var cust = new Customer({
  id: 1,
  groupId: 3
});
cust.getRelatedRecords('Group');

var group = new Group({
  id: 3,
  title: "Awesome Group"
});
group.getRelatedRecords('Customer');

var groupData = new Group({
  "id": 2,
  "title": "This is Group 2",
  "customers": [new Customer({
      "id": 1,
      "name": "John Doe",
      "groupId": 2,
      "status": "active",
      "state": "good"
    }),
    new Customer({
      "id": 4,
      "name": "Pete Smith",
      "groupId": 2,
      "status": "suspended",
      "state": "bad"
    })
  ]
});

console.log(groupData.toObj());

关于javascript - 数据模型的 ES6 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35110505/

相关文章:

javascript - 为 Node 6 转译所需的最少一组 babel 插件是什么

javascript - 带参数的 React(组件)onClick 方法

php - 限制来自 Magento SOAP 查询的结果集

java - 从 Path 中捕获元素

javascript - 使用 Elasticsearch 的 Ajax 查询格式

javascript - D3 条形图未捕获类型错误

Android - 当您使用较新 API 的方法时,如何在没有 2 个已编译 .apk 文件的情况下支持较旧的 API?

api - 用于检索字符信息的 IMDB API

php - Singleton 和 Registry 设计模式有什么区别

php - Codeigniter 模型只是实用程序类?