This repository has been archived by the owner on Dec 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from C2FO/naked-filter-include
adding eager loading on dataset and model
- Loading branch information
Showing
8 changed files
with
369 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,6 @@ after_script: | |
language: node_js | ||
node_js: | ||
- "4" | ||
- "5" | ||
- "5.6" | ||
- "5.12" | ||
- "stable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,64 @@ exports.AssociationPlugin = comb.define(null, { | |
return this.associate(this.ONE_TO_MANY, name, options, filter); | ||
}, | ||
|
||
/* Allows eager loading of an association. This does an extra SQL query for the association. | ||
* It will load any association singular or plural. | ||
* | ||
* @example | ||
* | ||
* Person.eager('company').one() | ||
* { id: 1, | ||
* name: 'Obi-Wan', | ||
* company: { | ||
* id: 1, | ||
* name: 'Jedi council' | ||
* } | ||
* } | ||
* | ||
* Person.eager(['emails', 'phones', 'company']).limit(2).all() | ||
* [{ id: 1, | ||
* name: 'Obi-Wan', | ||
* emails: ['[email protected]', '[email protected]'], | ||
* phones: ['911', '888-991-0991'], | ||
* company: { | ||
* id: 1, | ||
* name: 'Jedi council' | ||
* } | ||
* }, | ||
* { id: 2, | ||
* name: 'Luke', | ||
* emails: ['[email protected]', '[email protected]'], | ||
* phones: ['911', '888-991-0992'], | ||
* company: { | ||
* id: 1, | ||
* name: 'Jedi council' | ||
* } | ||
* }] | ||
* | ||
*/ | ||
eager: function(associations) { | ||
var model = new this(), | ||
includes = [], | ||
associationsObj = {}; | ||
|
||
if (Array.isArray(associations)) { | ||
includes = includes.concat(associations); | ||
} else if(associations) { | ||
includes.push(associations); | ||
} | ||
|
||
includes.forEach(function(association) { | ||
associationsObj[association] = function(parent) { | ||
if (!parent[association]) { | ||
throw new Error("Association of " + association + " not found"); | ||
} | ||
return parent[association]; | ||
}; | ||
}); | ||
|
||
return model.dataset.eager(associationsObj); | ||
}, | ||
|
||
/** | ||
* Creates a MANY_TO_ONE association. | ||
* See {@link patio.plugins.AssociationPlugin.oneToMany}. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,6 @@ | |
"patio": "./bin/patio" | ||
}, | ||
"engines": { | ||
"node": ">=4.0.0" | ||
"node": ">=4.0.0 <6.0.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
"use strict"; | ||
|
||
var it = require('it'), | ||
assert = require('assert'), | ||
helper = require("../data/oneToOne.helper.js"), | ||
patio = require("../../lib"), | ||
comb = require("comb"); | ||
|
||
var gender = ["M", "F"]; | ||
|
||
it.describe("patio.Model static eager method", function (it) { | ||
var Works, Employee, DB; | ||
it.beforeAll(function () { | ||
Works = patio.addModel("works"); | ||
Works.manyToOne("employee", {fetchType: Works.fetchType.LAZY}); | ||
Employee = patio.addModel("employee"); | ||
Employee.oneToMany("works", {fetchType: Employee.fetchType.LAZY}); | ||
DB = null; | ||
return helper.createSchemaAndSync(true).chain(function(db){ | ||
DB = db; | ||
}); | ||
}); | ||
|
||
|
||
it.should("have associations", function () { | ||
assert.deepEqual(Employee.associations, ["works"]); | ||
assert.deepEqual(Works.associations, ["employee"]); | ||
var emp = new Employee(); | ||
var work = new Works(); | ||
assert.deepEqual(emp.associations, ["works"]); | ||
assert.deepEqual(work.associations, ["employee"]); | ||
}); | ||
|
||
it.describe("load associations", function (it) { | ||
|
||
it.beforeEach(function () { | ||
return comb | ||
.when([ | ||
Employee.remove(), | ||
Works.remove() | ||
]) | ||
.chain(function () { | ||
return new Employee({ | ||
lastName: "last" + 1, | ||
firstName: "first" + 1, | ||
midInitial: "m", | ||
gender: gender[1 % 2], | ||
street: "Street " + 1, | ||
city: "City " + 1, | ||
works: [{ | ||
companyName: "Google", | ||
salary: 100000 | ||
},{ | ||
companyName: "Alphabet", | ||
salary: 100000 | ||
}] | ||
}).save(); | ||
}).chain(function () { | ||
return new Employee({ | ||
lastName: "Skywalker", | ||
firstName: "Luke", | ||
midInitial: "m", | ||
gender: gender[1 % 2], | ||
street: "Street " + 1, | ||
city: "City " + 1, | ||
works: { | ||
companyName: "C2FO", | ||
salary: 200000 | ||
} | ||
}).save(); | ||
}); | ||
|
||
}); | ||
|
||
it.should("when querying", function () { | ||
return comb | ||
.when([Employee.eager('works').one(), Works.eager('employee').one()]) | ||
.chain(function (res) { | ||
var emp = res[0], work = res[1]; | ||
var empWorks = emp.works, worksEmp = work.employee; | ||
assert(emp.works[0].id, work.id); | ||
assert(work.employee.id, emp.id); | ||
}); | ||
}); | ||
|
||
it.should("when querying with filtering", function () { | ||
return Employee.eager('works').filter({lastName: "Skywalker"}).one() | ||
.chain(function (emp) { | ||
assert(emp.id, emp.works[0].employeeId); | ||
}); | ||
}); | ||
|
||
it.should("and load other eager queries", function () { | ||
return Employee.eager('works').eager({ | ||
who: function(emp) { | ||
return Employee.findById(emp.id); | ||
} | ||
}).one().chain(function (emp) { | ||
assert(emp.id, emp.works[0].employeeId); | ||
assert(emp.id, emp.who.id); | ||
}).chain(function() { | ||
// run same queries back to back | ||
// make sure eager is not being cached across model instances | ||
return Employee.eager('works').eager({ | ||
you: function(emp) { | ||
return Employee.findById(emp.id); | ||
} | ||
}).one() | ||
.chain(function (emp) { | ||
assert(emp.id, emp.works[0].employeeId); | ||
assert.isUndefined(emp.who); | ||
assert(emp.id, emp.you.id); | ||
}); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
it.describe("dataset loading", function (it) { | ||
|
||
it.beforeEach(function () { | ||
return comb | ||
.when([ | ||
Employee.remove(), | ||
Works.remove() | ||
]) | ||
.chain(function () { | ||
return new Employee({ | ||
lastName: "last" + 1, | ||
firstName: "first" + 1, | ||
midInitial: "m", | ||
gender: gender[1 % 2], | ||
street: "Street " + 1, | ||
city: "City " + 1, | ||
works: [{ | ||
companyName: "Google", | ||
salary: 100000 | ||
},{ | ||
companyName: "Alphabet", | ||
salary: 100000 | ||
}] | ||
}).save(); | ||
}).chain(function () { | ||
return new Employee({ | ||
lastName: "Skywalker", | ||
firstName: "Luke", | ||
midInitial: "m", | ||
gender: gender[1 % 2], | ||
street: "Street " + 1, | ||
city: "City " + 1, | ||
works: { | ||
companyName: "C2FO", | ||
salary: 200000 | ||
} | ||
}).save(); | ||
}); | ||
|
||
}); | ||
|
||
it.should("and load other eager queries", function () { | ||
return DB.from('employee').filter({lastName: 'Skywalker'}) | ||
.eager({ | ||
who: function(emp) { | ||
return DB.from('works').filter({employeeId: emp.id}).one(); | ||
} | ||
}).one() | ||
.chain(function (emp) { | ||
assert(emp.lastName, 'Skywalker'); | ||
assert(emp.who.companyName, 'C2FO'); | ||
assert(emp.id, emp.who.employeeId); | ||
}); | ||
}); | ||
|
||
}); | ||
|
||
it.afterAll(function () { | ||
return helper.dropModels(); | ||
}); | ||
}); |
Oops, something went wrong.