ES6 Classes for Data Models

24,616

Here is another approach,

Where would I define the data structure, including fields that require data transformation, using the private methods (e.g. _getState())

You should define those fields, relationship in your model class extending the top model. Example:

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

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

Should the findById, findByGroupId, etc by defined within the scope of the class? Or, should these by separate methods (in the same file as the class), that would instantiate the object?

Instead of having many functions use findByAttribute(attr) in Model Example:

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

How should I deal with the case where one object is a child of the other, e.g. returning the Customer objects that belongs to a Group object as an array of objects in the Group's findById?

In case of relationships, you should have methods like 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;
    });
   }
 ...
}

Where should the SQL queries that will connect to the DB be defined? In the getById, getByGroupId, etc?

This one is tricky, but since you want your solution to be simple put the queries inside your find methods. Ideal scenario will be to have their own QueryBuilder Class.

Check the following full code the solution is not fully functional but you get the idea. I've also added engine variable in the model which you can use to enhance fetching mechanism. All other design ideas are upto your imagination :)

FULL CODE:

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

Share:
24,616

Related videos on Youtube

go4cas
Author by

go4cas

Updated on January 04, 2020

Comments

  • go4cas
    go4cas over 4 years

    I'm trying to use ES6 Classes to construct data models (from a MySQL database) in an API that I'm building. I prefer not using an ORM/ODM library, as this will be a very basic, simple API. But, I'm struggling to get my head around how to define these models.

    My data entities are (these are just some simplified examples):

    CUSTOMER

    Data Model

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

    Private Methods

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

    Requests

    I want to be able to do:

    • findById: Providing a single customer.id, return the data for that specific customer, i.e. SELECT * FROM customers WHERE id = ?

    • findByGroupId: Providing a group.id, return the data for all the customers (in an array of objects), belonging to that group, i.e. SELECT * FROM customers WHERE groupId = ?

    Response Payloads

    For each customer object, I want to return JSON like this:

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

    GROUP

    Data Model

    id
    title
    

    Requests

    I want to be able to do:

    • findById: Providing a single group.id, return the data for that specific group, i.e. SELECT * FROM groups WHERE id = ?

    Response Payloads

    For each group object, I want to return JSON like this:

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


    Requirements:

    • Must use ES6 Classes
    • Each model in its own file (e.g. customer.js) to be exported


    Questions:

    My main questions are:

    1. Where would I define the data structure, including fields that require data transformation, using the private methods (e.g. _getState())
    2. Should the findById, findByGroupId, etc by defined within the scope of the class? Or, should these by separate methods (in the same file as the class), that would instantiate the object?
    3. How should I deal with the case where one object is a child of the other, e.g. returning the Customer objects that belongs to a Group object as an array of objects in the Group's findById?
    4. Where should the SQL queries that will connect to the DB be defined? In the getById, getByGroupId, etc?

    UPDATE!!

    This is what I came up with - (would be awesome if someone could review, and comment):

    CUSTOMER Model

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

    GROUP Model

    '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 (where the Customer model is instantiated)

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

    This seems to be working well, and I've been able to use Class, Promise and let to make it more ES6 friendly.

    So, I'd like to get some input on my approach. Also, am I using the export and required features correctly in this context?

    • pid
      pid over 8 years
      what are you using? node.js? what's the architecture? client-server? has it to run remotely or in a browser or is it just a script you run from bash (if it's even on linux)?
    • go4cas
      go4cas over 8 years
      This is a hapi.js API server. The idea is to have a number of data entities, each with its own routes, models and controllers. It will mainly be used for GET requests. And, as it will be a fairly small set of entities and end-points, I would prefer not to use a full fledged ORM.
    • code_monk
      code_monk over 7 years
      that looks pretty good! i see a few areas of minor concern. you should post your question Code Review, though. That is the appropriate forum for a question like this. Here, you'll probably get flagged