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 6d2295ff
authored
May 12, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(model/attributes): attribute.field support for includes
1 parent
fde91e4c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
84 additions
and
7 deletions
lib/dialects/abstract/query-generator.js
lib/model.js
test/model/attributes.test.js
lib/dialects/abstract/query-generator.js
View file @
6d2295f
...
...
@@ -639,6 +639,8 @@ module.exports = (function() {
if
(
attr
[
0
]
instanceof
Utils
.
fn
||
attr
[
0
]
instanceof
Utils
.
col
)
{
attr
[
0
]
=
attr
[
0
].
toString
(
self
)
addTable
=
false
}
else
{
attr
[
0
]
=
this
.
quoteIdentifier
(
attr
[
0
])
}
attr
=
[
attr
[
0
],
this
.
quoteIdentifier
(
attr
[
1
])].
join
(
' as '
)
}
else
{
...
...
@@ -648,7 +650,6 @@ module.exports = (function() {
if
(
options
.
include
&&
attr
.
indexOf
(
'.'
)
===
-
1
&&
addTable
)
{
attr
=
mainTableAs
+
'.'
+
attr
}
return
attr
}.
bind
(
this
))
...
...
@@ -687,7 +688,17 @@ module.exports = (function() {
// includeIgnoreAttributes is used by aggregate functions
if
(
options
.
includeIgnoreAttributes
!==
false
)
{
attributes
=
include
.
attributes
.
map
(
function
(
attr
)
{
return
self
.
quoteIdentifier
(
as
)
+
"."
+
self
.
quoteIdentifier
(
attr
)
+
" AS "
+
self
.
quoteIdentifier
(
as
+
"."
+
attr
)
var
attrAs
=
attr
;
if
(
Array
.
isArray
(
attr
)
&&
attr
.
length
==
2
)
{
attr
=
attr
.
map
(
function
(
$attr
)
{
return
$attr
.
_isSequelizeMethod
?
$attr
.
toString
(
self
)
:
$attr
;
})
attrAs
=
attr
[
1
];
attr
=
attr
[
0
];
}
return
self
.
quoteIdentifier
(
as
)
+
"."
+
self
.
quoteIdentifier
(
attr
)
+
" AS "
+
self
.
quoteIdentifier
(
as
+
"."
+
attrAs
);
})
if
(
include
.
subQuery
&&
subQuery
)
{
...
...
lib/model.js
View file @
6d2295f
...
...
@@ -1695,6 +1695,8 @@ module.exports = (function() {
include
.
attributes
=
Object
.
keys
(
include
.
model
.
attributes
)
}
include
=
mapFieldNames
(
include
,
include
.
model
);
// pseudo include just needed the attribute logic, return
if
(
include
.
_pseudo
)
{
return
include
...
...
test/model/attributes.test.js
View file @
6d2295f
...
...
@@ -2,6 +2,7 @@
/* jshint expr: true */
var
chai
=
require
(
'chai'
)
,
Sequelize
=
require
(
'../../index'
)
,
Promise
=
Sequelize
.
Promise
,
expect
=
chai
.
expect
,
Support
=
require
(
__dirname
+
'/../support'
)
,
DataTypes
=
require
(
__dirname
+
"/../../lib/data-types"
)
...
...
@@ -25,19 +26,59 @@ describe(Support.getTestDialectTeaser("Model"), function () {
},
{
tableName
:
'users'
,
timestamps
:
false
})
})
;
return
queryInterface
.
createTable
(
'users
'
,
{
this
.
Task
=
this
.
sequelize
.
define
(
'task
'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
autoIncrement
:
true
,
field
:
'taskId'
},
full_name
:
{
type
:
DataTypes
.
STRING
title
:
{
type
:
DataTypes
.
STRING
,
field
:
'name'
}
},
{
tableName
:
'tasks'
,
timestamps
:
false
});
this
.
User
.
hasMany
(
this
.
Task
,
{
foreignKey
:
'user_id'
});
this
.
Task
.
belongsTo
(
this
.
User
,
{
foreignKey
:
'user_id'
});
return
Promise
.
all
([
queryInterface
.
createTable
(
'users'
,
{
id
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
},
full_name
:
{
type
:
DataTypes
.
STRING
}
}),
queryInterface
.
createTable
(
'tasks'
,
{
taskId
:
{
type
:
DataTypes
.
INTEGER
,
allowNull
:
false
,
primaryKey
:
true
,
autoIncrement
:
true
},
user_id
:
{
type
:
DataTypes
.
INTEGER
},
name
:
{
type
:
DataTypes
.
STRING
}
})
])
});
it
(
'should create, fetch and update with alternative field names from a simple model'
,
function
()
{
...
...
@@ -63,6 +104,29 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
});
it
(
'should work with attributes and where on includes'
,
function
()
{
var
self
=
this
;
return
this
.
User
.
create
({
name
:
'Foobar'
}).
then
(
function
(
user
)
{
return
user
.
createTask
({
title
:
'DoDat'
});
}).
then
(
function
()
{
return
self
.
User
.
findAll
({
include
:
[
{
model
:
self
.
Task
,
where
:
{
title
:
'DoDat'
}}
]
});
}).
then
(
function
(
users
)
{
users
.
forEach
(
function
(
user
)
{
expect
(
user
.
get
(
'name'
)).
to
.
be
.
ok
;
expect
(
user
.
get
(
'tasks'
)[
0
].
get
(
'title'
)).
to
.
equal
(
'DoDat'
);
});
});
});
it
(
'should work with a simple where'
,
function
()
{
var
self
=
this
;
...
...
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