Object Relational Mapper
Object ... We're working with objects (as in database rows)
Relational ... Specifically handling relational data
Mapper ... Mapping objects together for you
- Describe the features of an ORM
- Define common ORM terms
- Describe common use cases for an ORM
- Describe what ORM code is doing
ORMs generally include:
-
Easy querying without writing SQL (a.k.a. Query Building)
.find()
,.where()
,.orderBy()
-
Extending functionality with hooks or events
- Validations, attribute manipulations, computed properties, limited scopes
-
Relationship handling without JOINs
- Mapping relationships in model definitions
- Easily loading relationships through Query Building
Model ... Single row or record
Collection ... Group of models
Query Building ... Chaining methods together to generate a SQL query
Eager Loading ... Loading relationships when querying
- You are using relational data in your database
- You would like to abstract your SQL query creation
- You need to create SQL queries on-the-fly
- You are too lazy to write SQL
- When would an ORM not be useful?
- One-to-one
- One-to-many
- Many-to-many
- One items have singular names
- Many items have plural names
- Tables are always named plural
// Get a book with specific ISBN-13 number, including its summary
new Book({'ISBN-13': '9780440180296'})
.fetch({
withRelated: ['summary']
})
.then(function(book) {
// 'Slaughterhouse Five'
book.get('title')
// Access to properties on "Summary" model
book.related('summary')
})
In this code:
- Fetching a book by ISBN-13 number
- Eager Loading the summary data
- Accessing properties on book model
- Accessing related summary model
// Get a book with specific ISBN-13 number, including its pages
new Book({'ISBN-13': '9780440180296'})
.fetch({
withRelated: ['pages']
}).then(function(book) {
// Collection of page models
book.related('pages')
// Only retrieve odd pages
book.related('pages').filter(function(page) {
return page.get('number') % 1
})
})
In this code:
- Fetching a book by ISBN-13 number
- Eager Loading the pages
- Accessing related pages Collection
- ??
// Get a book with specific ISBN-13 number, including its pages
new Book({'ISBN-13': '9780440180296'})
.fetch({
withRelated: ['pages']
}).then(function(book) {
// Collection of page models
book.related('pages')
// Only retrieve odd pages
book.related('pages').filter(function(page) {
return page.get('number') % 1
})
})
In this code:
- Fetching a book by ISBN-13 number
- Eager Loading the pages
- Accessing related pages Collection
- Filtering to only odd pages
- What is the following code doing?
Post.where({ id: 12345 }).fetch({
withRelated: ['blog', 'author', 'comments']
}).then(function(post) {
post.related('author').get('name')
post.related('blog').get('title')
post.related('comments').length
})
// Get author info along with their books
Author.where({name: 'George Costanza'})
.fetch({
withRelated: ['books']
}).then(function(author) {
author.related('books').where({ year: 2017 })
})
In this code:
- Fetching an author by name
- ??
- ??
// Get author info along with their books
Author.where({name: 'George Costanza'})
.fetch({
withRelated: ['books']
}).then(function(author) {
author.related('books').where({ year: 2017 })
})
In this code:
- Fetching an author by name
- Eager Loading their books
- ??
// Get author info along with their books
Author.where({name: 'George Costanza'})
.fetch({
withRelated: ['books']
}).then(function(author) {
author.related('books').where({ year: 2017 })
})
In this code:
- Fetching an author by name
- Eager Loading their books
- Filtering their books down to releases this year
- What kind of relationship does the following code setup?
let Classroom = bookshelf.Model.extend({
tableName: 'classrooms',
students: function() {
return this.hasMany(Student);
}
});
let Student = bookshelf.Model.extend({
tableName: 'students',
classroom: function() {
return this.belongsTo(Classroom);
}
});
- What does ORM stand for?
- What are some common ORM features?
- What is Eager Loading?
And finally ...
- What is wrong with this code?
let Folders = bookshelf.Model.extend({
tableName: 'folder',
files: function() {
return this.hasMany(File);
}
});
let File = bookshelf.Model.extend({
tableName: 'files',
folders: function() {
this.belongsTo(Folder);
}
});
- ??
- ??
- ??
- ??
- What is wrong with this code?
let Folders = bookshelf.Model.extend({
tableName: 'folder',
files: function() {
return this.hasMany(File);
}
});
let File = bookshelf.Model.extend({
tableName: 'files',
folders: function() {
this.belongsTo(Folder);
}
});
Folders
model should be singular- ??
- ??
- ??
- What is wrong with this code?
let Folders = bookshelf.Model.extend({
tableName: 'folder',
files: function() {
return this.hasMany(File);
}
});
let File = bookshelf.Model.extend({
tableName: 'files',
folders: function() {
this.belongsTo(Folder);
}
});
Folders
model should be singularFolder
model should have plural table name- ??
- ??
- What is wrong with this code?
let Folders = bookshelf.Model.extend({
tableName: 'folder',
files: function() {
return this.hasMany(File);
}
});
let File = bookshelf.Model.extend({
tableName: 'files',
folders: function() {
this.belongsTo(Folder);
}
});
Folders
model should be singularFolder
model should have plural table nameFile
model should havefolder
property (singular)- ??
- What is wrong with this code?
let Folders = bookshelf.Model.extend({
tableName: 'folder',
files: function() {
return this.hasMany(File);
}
});
let File = bookshelf.Model.extend({
tableName: 'files',
folders: function() {
this.belongsTo(Folder);
}
});
Folders
model should be singularFolder
model should have plural table nameFile
model should havefolder
property (singular)return
missing fromFile
model
- Describe the features of an ORM
- Define common ORM terms
- Describe common use cases for an ORM
- Describe what ORM code is doing