Joe Karlsson
progress. tech. cats. coffee. art. food. photog. mpls.
Basically, OO doesn't talk with SQL
Object-relational Mapping (ORM) is a way of mapping between objects and relational databases.
OOP
Validation
No need to write SQL
Cache Objects
Lazy/Eager Loading
Lazy Updating
-- Creating a Tasks table w/o an ORM
CREATE TABLE IF NOT EXISTS tasks (
id serial PRIMARY KEY NOT NULL,
title character varying(125) NOT NULL,
description character varying(125) NOT NULL,
deadline DATE NOT NULL,
);
-- Querying w/o an ORM
SELECT * FROM tasks;
-- Inserting a new tasks
-- into the Tasks table w/o an ORM
INSERT INTO tasks (
default,
'pay bills',
'very important',
10/10/16
);
// Define mapping between a
// model and a table w/ an ORM
var Task = sequelize.define('tasks', {
title: Sequelize.STRING,
description: Sequelize.TEXT,
deadline: Sequelize.DATE
});
// Querying with an ORM
Task.findAll();
// Create a new task w/ an ORM
Task.create({
title: 'pay bills',
description: 'very important task',
deadline: new Date('2016-10-10T14:30:00')
})
.then(function(task){
...
});
Can add complexity to code
Sequelize + Express
By Joe Karlsson
Introduction to Object Relational Mapping