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 484bc574
authored
Sep 11, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor(model/find): find now relies on findAll for the heavy lifting
1 parent
c5ef4740
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
79 deletions
lib/model.js
test/dao-factory/find.test.js
lib/model.js
View file @
484bc57
...
@@ -729,94 +729,31 @@ module.exports = (function() {
...
@@ -729,94 +729,31 @@ module.exports = (function() {
*
*
* @see {Model#findAll} for an explanation of options and queryOptions
* @see {Model#findAll} for an explanation of options and queryOptions
* @return {Promise<Instance>}
* @return {Promise<Instance>}
* @alias find
*/
*/
Model
.
prototype
.
find
=
function
(
options
,
queryOptions
)
{
Model
.
prototype
.
findOne
=
function
(
param
,
queryOptions
)
{
var
hasJoin
=
false
;
// For sanity findOne will never return if no options are passed
if
([
null
,
undefined
].
indexOf
(
param
)
!==
-
1
)
{
// no options defined?
// return an emitter which emits null
if
([
null
,
undefined
].
indexOf
(
options
)
!==
-
1
)
{
return
Promise
.
resolve
(
null
);
return
Promise
.
resolve
(
null
);
}
}
var
where
var
options
=
{};
,
primaryKeys
=
this
.
primaryKeys
if
(
typeof
param
===
'number'
||
typeof
param
===
'string'
)
{
,
keys
=
Object
.
keys
(
primaryKeys
)
options
.
where
=
{};
,
keysLength
=
keys
.
length
options
.
where
[
this
.
primaryKeyAttribute
]
=
param
;
,
tableNames
=
{
};
}
else
{
options
=
optClone
(
param
);
tableNames
[
this
.
getTableName
()]
=
true
;
// options is not a hash but an id
if
(
typeof
options
===
'number'
)
{
var
oldOption
=
options
;
options
=
{
where
:
{}
};
if
(
keysLength
===
1
)
{
options
.
where
[
keys
[
0
]]
=
oldOption
;
}
else
{
options
.
where
.
id
=
oldOption
;
}
}
else
if
(
Utils
.
_
.
size
(
primaryKeys
)
&&
Utils
.
argsArePrimaryKeys
(
arguments
,
primaryKeys
))
{
where
=
{};
Utils
.
_
.
each
(
arguments
,
function
(
arg
,
i
)
{
var
key
=
keys
[
i
];
where
[
key
]
=
arg
;
});
options
=
{
where
:
where
};
}
else
if
(
typeof
options
===
'string'
&&
parseInt
(
options
,
10
).
toString
()
===
options
)
{
var
parsedId
=
parseInt
(
options
,
10
);
if
(
!
Utils
.
_
.
isFinite
(
parsedId
))
{
throw
new
Error
(
'Invalid argument to find(). Must be an id or an options object.'
);
}
options
=
{
where
:
parsedId
};
}
else
if
(
typeof
options
===
'object'
)
{
options
=
Utils
.
_
.
clone
(
options
,
function
(
thing
)
{
if
(
Buffer
.
isBuffer
(
thing
))
{
return
thing
;
}
return
undefined
;
});
if
(
options
.
hasOwnProperty
(
'include'
)
&&
options
.
include
)
{
hasJoin
=
true
;
validateIncludedElements
.
call
(
this
,
options
,
tableNames
);
}
// whereCollection is used for non-primary key updates
this
.
options
.
whereCollection
=
options
.
where
||
null
;
}
else
if
(
typeof
options
===
'string'
)
{
where
=
{};
if
(
this
.
primaryKeyCount
===
1
)
{
where
[
primaryKeys
[
keys
[
0
]]]
=
options
;
options
=
where
;
}
else
if
(
this
.
primaryKeyCount
<
1
)
{
// Revert to default behavior which is {where: [int]}
options
=
{
where
:
parseInt
(
Number
(
options
)
||
0
,
0
)};
}
}
if
(
options
.
attributes
===
undefined
)
{
options
.
attributes
=
Object
.
keys
(
this
.
tableAttributes
);
}
}
if
(
options
.
limit
===
undefined
&&
!
(
options
.
where
&&
options
.
where
[
this
.
primaryKeyAttribute
]))
{
if
(
options
.
limit
===
undefined
&&
!
(
options
.
where
&&
options
.
where
[
this
.
primaryKeyAttribute
]))
{
options
.
limit
=
1
;
options
.
limit
=
1
;
}
}
mapFieldNames
.
call
(
this
,
options
,
this
);
return
this
.
findAll
(
options
,
Utils
.
_
.
defaults
({
options
=
paranoidClause
.
call
(
this
,
options
);
plain
:
true
},
queryOptions
||
{}));
return
this
.
QueryInterface
.
select
(
this
,
this
.
getTableName
(),
options
,
Utils
.
_
.
defaults
({
plain
:
true
,
type
:
QueryTypes
.
SELECT
,
hasJoin
:
hasJoin
,
tableNames
:
Object
.
keys
(
tableNames
)
},
queryOptions
,
{
transaction
:
(
options
||
{}).
transaction
}));
};
};
Model
.
prototype
.
find
=
Model
.
prototype
.
findOne
;
/**
/**
* Run an aggregation method on the specified field
* Run an aggregation method on the specified field
...
...
test/dao-factory/find.test.js
View file @
484bc57
...
@@ -40,8 +40,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -40,8 +40,12 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
User
.
sync
({
force
:
true
}).
success
(
function
()
{
User
.
sync
({
force
:
true
}).
success
(
function
()
{
sequelize
.
transaction
().
then
(
function
(
t
)
{
sequelize
.
transaction
().
then
(
function
(
t
)
{
User
.
create
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
()
{
User
.
create
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
()
{
User
.
find
({
username
:
'foo'
}).
success
(
function
(
user1
)
{
User
.
find
({
User
.
find
({
username
:
'foo'
},
{
transaction
:
t
}).
success
(
function
(
user2
)
{
where
:
{
username
:
'foo'
}
}).
success
(
function
(
user1
)
{
User
.
find
({
where
:
{
username
:
'foo'
},
},
{
transaction
:
t
}).
success
(
function
(
user2
)
{
expect
(
user1
).
to
.
be
.
null
expect
(
user1
).
to
.
be
.
null
expect
(
user2
).
to
.
not
.
be
.
null
expect
(
user2
).
to
.
not
.
be
.
null
...
...
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