不要怂,就是干,撸起袖子干!

Commit 0af0f7fb by Mick Hansen

Merge pull request #472 from sdepold/features/eager_loading_to_json

Features/eager loading to json
2 parents 427a2576 ef1ab8cb
...@@ -311,13 +311,13 @@ ...@@ -311,13 +311,13 @@
}, },
{ {
"file": "lib/dialects/abstract/query.js", "file": "lib/dialects/abstract/query.js",
"line": 345, "line": 350,
"description": "The function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]", "description": "The function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]",
"class": "QueryInterface" "class": "QueryInterface"
}, },
{ {
"file": "lib/dialects/abstract/query.js", "file": "lib/dialects/abstract/query.js",
"line": 405, "line": 410,
"description": "This function will prepare the result of select queries with joins.", "description": "This function will prepare the result of select queries with joins.",
"params": [ "params": [
{ {
...@@ -542,11 +542,11 @@ ...@@ -542,11 +542,11 @@
}, },
{ {
"message": "Missing item type\nThe function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]", "message": "Missing item type\nThe function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]",
"line": " lib/dialects/abstract/query.js:345" "line": " lib/dialects/abstract/query.js:350"
}, },
{ {
"message": "Missing item type\nThis function will prepare the result of select queries with joins.", "message": "Missing item type\nThis function will prepare the result of select queries with joins.",
"line": " lib/dialects/abstract/query.js:405" "line": " lib/dialects/abstract/query.js:410"
}, },
{ {
"message": "Missing item type\nSearch for an instance.", "message": "Missing item type\nSearch for an instance.",
......
...@@ -410,9 +410,14 @@ module.exports = (function() { ...@@ -410,9 +410,14 @@ module.exports = (function() {
dao[accessor] = isEmpty ? null : daoInstance dao[accessor] = isEmpty ? null : daoInstance
} else { } else {
dao[accessor] = dao[accessor] || [] dao[accessor] = dao[accessor] || []
if (! isEmpty)
if (!isEmpty) {
dao[accessor].push(daoInstance) dao[accessor].push(daoInstance)
}
} }
// add the accessor to the eagerly loaded associations array
dao.__eagerlyLoadedAssociations = Utils._.uniq(dao.__eagerlyLoadedAssociations.concat([accessor]))
}) })
} }
......
...@@ -90,7 +90,7 @@ ...@@ -90,7 +90,7 @@
<div class="file"> <div class="file">
<pre class="code prettyprint linenums"> <pre class="code prettyprint linenums">
Utils = require(&quot;..&#x2F;..&#x2F;utils&quot;) var Utils = require(&quot;..&#x2F;..&#x2F;utils&quot;)
&#x2F;** &#x2F;**
Returns an object that treats SQLite&#x27;s inabilities to do certain queries. Returns an object that treats SQLite&#x27;s inabilities to do certain queries.
......
...@@ -5,10 +5,12 @@ var Utils = require("./utils") ...@@ -5,10 +5,12 @@ var Utils = require("./utils")
module.exports = (function() { module.exports = (function() {
var DAO = function(values, options, isNewRecord) { var DAO = function(values, options, isNewRecord) {
var self = this; var self = this
this.__options = options;
this.hasPrimaryKeys = options.hasPrimaryKeys; this.__options = options
this.selectedValues = values; this.hasPrimaryKeys = options.hasPrimaryKeys
this.selectedValues = values
this.__eagerlyLoadedAssociations = []
initAttributes.call(this, values, isNewRecord) initAttributes.call(this, values, isNewRecord)
...@@ -51,7 +53,7 @@ module.exports = (function() { ...@@ -51,7 +53,7 @@ module.exports = (function() {
var result = {} var result = {}
, self = this , self = this
this.attributes.forEach(function(attr) { this.attributes.concat(this.__eagerlyLoadedAssociations).forEach(function(attr) {
result[attr] = self[attr] result[attr] = self[attr]
}) })
......
...@@ -318,9 +318,14 @@ module.exports = (function() { ...@@ -318,9 +318,14 @@ module.exports = (function() {
dao[accessor] = isEmpty ? null : daoInstance dao[accessor] = isEmpty ? null : daoInstance
} else { } else {
dao[accessor] = dao[accessor] || [] dao[accessor] = dao[accessor] || []
if (! isEmpty)
if (!isEmpty) {
dao[accessor].push(daoInstance) dao[accessor].push(daoInstance)
}
} }
// add the accessor to the eagerly loaded associations array
dao.__eagerlyLoadedAssociations = Utils._.uniq(dao.__eagerlyLoadedAssociations.concat([accessor]))
}) })
} }
......
...@@ -334,7 +334,6 @@ describe('DAO', function() { ...@@ -334,7 +334,6 @@ describe('DAO', function() {
}) })
}) })
}) })
}) })
}) })
}) })
...@@ -274,12 +274,14 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() { ...@@ -274,12 +274,14 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
username: Helpers.Sequelize.STRING, username: Helpers.Sequelize.STRING,
age: Helpers.Sequelize.INTEGER, age: Helpers.Sequelize.INTEGER,
isAdmin: Helpers.Sequelize.BOOLEAN isAdmin: Helpers.Sequelize.BOOLEAN
}, { }, { timestamps: false })
timestamps: false,
logging: true this.Project = this.sequelize.define('NiceProject', { title: Helpers.Sequelize.STRING }, { timestamps: false })
})
this.User.hasMany(this.Project, { as: 'Projects' })
this.Project.belongsTo(this.User, { as: 'LovelyUser' })
this.User.sync({ force: true }).success(done) this.sequelize.sync({ force: true }).success(done)
}) })
it('returns an object containing all values', function() { it('returns an object containing all values', function() {
...@@ -296,6 +298,30 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() { ...@@ -296,6 +298,30 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
var user = this.User.build({ username: 'test.user', age: 99, isAdmin: true }) var user = this.User.build({ username: 'test.user', age: 99, isAdmin: true })
expect(JSON.parse(JSON.stringify(user))).toEqual({ username: 'test.user', age: 99, isAdmin: true, id: null }) expect(JSON.parse(JSON.stringify(user))).toEqual({ username: 'test.user', age: 99, isAdmin: true, id: null })
}) })
it('includes the eagerly loaded associations', function(done) {
this.User.create({ username: 'fnord', age: 1, isAdmin: true }).success(function(user) {
this.Project.create({ title: 'fnord' }).success(function(project) {
user.setProjects([ project ]).success(function() {
this.User.findAll({include: [ { model: this.Project, as: 'Projects' } ]}).success(function(users) {
var _user = users[0]
expect(_user.projects).toBeDefined()
expect(JSON.parse(JSON.stringify(_user)).projects).toBeDefined()
this.Project.findAll({include: [ { model: this.User, as: 'LovelyUser' } ]}).success(function(projects) {
var _project = projects[0]
expect(_project.lovelyUser).toBeDefined()
expect(JSON.parse(JSON.stringify(_project)).lovelyUser).toBeDefined()
done()
})
}.bind(this))
}.bind(this))
}.bind(this))
}.bind(this))
})
}) })
describe('findAll', function findAll() { describe('findAll', function findAll() {
......
...@@ -2,7 +2,6 @@ if(typeof require === 'function') { ...@@ -2,7 +2,6 @@ if(typeof require === 'function') {
const buster = require("buster") const buster = require("buster")
, Helpers = require('./buster-helpers') , Helpers = require('./buster-helpers')
, dialect = Helpers.getTestDialect() , dialect = Helpers.getTestDialect()
} }
var qq = function(str) { var qq = function(str) {
...@@ -137,7 +136,7 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() { ...@@ -137,7 +136,7 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
it('destructs dot separated attributes when doing a raw query', function(done) { it('destructs dot separated attributes when doing a raw query', function(done) {
var tickChar = (dialect === 'postgres') ? '"' : '`' var tickChar = (dialect === 'postgres') ? '"' : '`'
, sql = "select 1 as " + Utils.addTicks('foo.bar.baz', tickChar) , sql = "select 1 as " + Helpers.Sequelize.Utils.addTicks('foo.bar.baz', tickChar)
this.sequelize.query(sql, null, { raw: true }).success(function(result) { this.sequelize.query(sql, null, { raw: true }).success(function(result) {
expect(result).toEqual([ { foo: { bar: { baz: 1 } } } ]) expect(result).toEqual([ { foo: { bar: { baz: 1 } } } ])
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!