Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit 0af0f7fb
authored
Mar 04, 2013
by
Mick Hansen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #472 from sdepold/features/eager_loading_to_json
Features/eager loading to json
2 parents
427a2576
ef1ab8cb
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
56 additions
and
20 deletions
docs/data.json
docs/files/lib_dialects_abstract_query.js.html
docs/files/lib_dialects_sqlite_query-interface.js.html
lib/dao.js
lib/dialects/abstract/query.js
spec-jasmine/dao.spec.js
spec/dao.spec.js
spec/sequelize.spec.js
docs/data.json
View file @
0af0f7f
...
...
@@ -311,13 +311,13 @@
},
{
"file"
:
"lib/dialects/abstract/query.js"
,
"line"
:
3
45
,
"line"
:
3
50
,
"description"
:
"The function takes the result of the query execution and groups
\n
the associated data by the callee.
\n\n
Example:
\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\n
Result:
\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"
},
{
"file"
:
"lib/dialects/abstract/query.js"
,
"line"
:
4
05
,
"line"
:
4
10
,
"description"
:
"This function will prepare the result of select queries with joins."
,
"params"
:
[
{
...
...
@@ -542,11 +542,11 @@
},
{
"message"
:
"Missing item type
\n
The function takes the result of the query execution and groups
\n
the associated data by the callee.
\n\n
Example:
\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\n
Result:
\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:3
45
"
"line"
:
" lib/dialects/abstract/query.js:3
50
"
},
{
"message"
:
"Missing item type
\n
This function will prepare the result of select queries with joins."
,
"line"
:
" lib/dialects/abstract/query.js:4
05
"
"line"
:
" lib/dialects/abstract/query.js:4
10
"
},
{
"message"
:
"Missing item type
\n
Search for an instance."
,
...
...
docs/files/lib_dialects_abstract_query.js.html
View file @
0af0f7f
...
...
@@ -410,9 +410,14 @@ module.exports = (function() {
dao[accessor] = isEmpty ? null : daoInstance
} else {
dao[accessor] = dao[accessor] || []
if (! isEmpty)
if (!isEmpty) {
dao[accessor].push(daoInstance)
}
}
//
add the accessor to the eagerly loaded associations array
dao.__eagerlyLoadedAssociations = Utils._.uniq(dao.__eagerlyLoadedAssociations.concat([accessor]))
})
}
...
...
docs/files/lib_dialects_sqlite_query-interface.js.html
View file @
0af0f7f
...
...
@@ -90,7 +90,7 @@
<div
class=
"file"
>
<pre
class=
"code prettyprint linenums"
>
Utils = require(
"
..
/
..
/
utils
"
)
var
Utils = require(
"
..
/
..
/
utils
"
)
/
**
Returns an object that treats SQLite
'
s inabilities to do certain queries.
...
...
lib/dao.js
View file @
0af0f7f
...
...
@@ -5,10 +5,12 @@ var Utils = require("./utils")
module
.
exports
=
(
function
()
{
var
DAO
=
function
(
values
,
options
,
isNewRecord
)
{
var
self
=
this
;
this
.
__options
=
options
;
this
.
hasPrimaryKeys
=
options
.
hasPrimaryKeys
;
this
.
selectedValues
=
values
;
var
self
=
this
this
.
__options
=
options
this
.
hasPrimaryKeys
=
options
.
hasPrimaryKeys
this
.
selectedValues
=
values
this
.
__eagerlyLoadedAssociations
=
[]
initAttributes
.
call
(
this
,
values
,
isNewRecord
)
...
...
@@ -51,7 +53,7 @@ module.exports = (function() {
var
result
=
{}
,
self
=
this
this
.
attributes
.
forEach
(
function
(
attr
)
{
this
.
attributes
.
concat
(
this
.
__eagerlyLoadedAssociations
).
forEach
(
function
(
attr
)
{
result
[
attr
]
=
self
[
attr
]
})
...
...
lib/dialects/abstract/query.js
View file @
0af0f7f
...
...
@@ -318,9 +318,14 @@ module.exports = (function() {
dao
[
accessor
]
=
isEmpty
?
null
:
daoInstance
}
else
{
dao
[
accessor
]
=
dao
[
accessor
]
||
[]
if
(
!
isEmpty
)
if
(
!
isEmpty
)
{
dao
[
accessor
].
push
(
daoInstance
)
}
}
// add the accessor to the eagerly loaded associations array
dao
.
__eagerlyLoadedAssociations
=
Utils
.
_
.
uniq
(
dao
.
__eagerlyLoadedAssociations
.
concat
([
accessor
]))
})
}
...
...
spec-jasmine/dao.spec.js
View file @
0af0f7f
...
...
@@ -334,7 +334,6 @@ describe('DAO', function() {
})
})
})
})
})
})
spec/dao.spec.js
View file @
0af0f7f
...
...
@@ -274,12 +274,14 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
username
:
Helpers
.
Sequelize
.
STRING
,
age
:
Helpers
.
Sequelize
.
INTEGER
,
isAdmin
:
Helpers
.
Sequelize
.
BOOLEAN
},
{
timestamps
:
false
,
logging
:
true
})
},
{
timestamps
:
false
})
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
()
{
...
...
@@ -296,6 +298,30 @@ describe(Helpers.getTestDialectTeaser("DAO"), function() {
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
})
})
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
()
{
...
...
spec/sequelize.spec.js
View file @
0af0f7f
...
...
@@ -2,7 +2,6 @@ if(typeof require === 'function') {
const
buster
=
require
(
"buster"
)
,
Helpers
=
require
(
'./buster-helpers'
)
,
dialect
=
Helpers
.
getTestDialect
()
}
var
qq
=
function
(
str
)
{
...
...
@@ -137,7 +136,7 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
it
(
'destructs dot separated attributes when doing a raw query'
,
function
(
done
)
{
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
)
{
expect
(
result
).
toEqual
([
{
foo
:
{
bar
:
{
baz
:
1
}
}
}
])
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment