Introducing mysql-orm-promise

December 31, 2015

  • database
  • Javascript
  • node
  • module
  • plugin
  • mysql
  • nodejs
  • operations
  • orm
  • promise
  • query

mysql-orm-promise

A node.js based ORM to communicate with your MySQL database.

After trying several npm packages for interacting with MySQL, I decided to create a cleaner and robust version of my own. I have come across packages which follow the callback as well as the promise based implementations, but query chaining was something which felt missing from those packages. I needed something which would be a shameless mockery of the mongoosejs package(a pure model implementation on schema basis, and allow me to chain my queries in an easy and cleaner way), but for MySQL.

Enter mysql-orm-promise

Using this package can be as easy as eating a pie(smirks). So let’s begin.

Install
npm install mysql-orm-promise
Initialization
var MysqlORM = require('mysql-orm-promise')
MysqlORM.connection.connect({
    host     : '<hostname>',
    user     : '<username>',
    password : '<password>',
    database : '<database_name>'
})
A simple query - retrieving all records
var UserModel = MysqlORM.model('user')
UserModel.findAll().exec().then(function(usersData){
    // usersData is the array of results
    // The attributes of the records can be accessed as `usersData[0].attributes`
    // In order to get the `name` attribute of the user, you will have to execute the following code
    console.log(usersData[0].attributes.name)
})
Query Chaining
UserModel.where({"name": "Jack"})
.skip(5)
.limit(10)
.orderBy('last_name')
.exec()
.then(function(usersData){
    // handle the result data here
})

Moving Forward

mysql-orm-promise currently only allows you execute read operations on your database. Write operations are still a work in progress. You can check out the links below for future updates


LOCATION

Mumbai, Maharashtra, India

AROUND THE WEB

Copyright © 2021