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 186a2ba8
authored
Oct 14, 2013
by
Sascha Depold
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'findOrInitialize' of
https://github.com/durango/sequelize
into dur…
…ango-findOrInitialize
2 parents
39fda2ce
56c35300
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
2 deletions
lib/dao-factory.js
test/dao-factory.test.js
lib/dao-factory.js
View file @
186a2ba
...
...
@@ -359,8 +359,8 @@ module.exports = (function() {
// whereCollection is used for non-primary key updates
this
.
options
.
whereCollection
=
optcpy
.
where
||
null
;
return
this
.
QueryInterface
.
select
(
this
,
[
this
.
getTableName
(),
joinTableName
],
optcpy
,
Utils
.
_
.
defaults
({
type
:
'SELECT'
return
this
.
QueryInterface
.
select
(
this
,
[
this
.
getTableName
(),
joinTableName
],
optcpy
,
Utils
.
_
.
defaults
({
type
:
'SELECT'
},
queryOptions
))
}
...
...
@@ -531,6 +531,39 @@ module.exports = (function() {
return
this
.
build
(
values
).
save
(
fields
)
}
DAOFactory
.
prototype
.
findOrInitialize
=
DAOFactory
.
prototype
.
findOrBuild
=
function
(
params
,
defaults
)
{
var
self
=
this
,
defaultKeys
=
Object
.
keys
(
defaults
||
{})
,
defaultLength
=
defaultKeys
.
length
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
find
({
where
:
params
}).
success
(
function
(
instance
)
{
if
(
instance
===
null
)
{
var
i
=
0
for
(
i
=
0
;
i
<
defaultLength
;
i
++
)
{
params
[
defaultKeys
[
i
]]
=
defaults
[
defaultKeys
[
i
]]
}
var
build
=
self
.
build
(
params
)
build
.
hookValidate
({
skip
:
Object
.
keys
(
params
)}).
success
(
function
(
instance
)
{
emitter
.
emit
(
'success'
,
build
,
true
)
})
.
error
(
function
(
error
)
{
emitter
.
emit
(
'error'
,
error
)
})
}
else
{
emitter
.
emit
(
'success'
,
instance
,
false
)
}
}).
error
(
function
(
error
)
{
emitter
.
emit
(
'error'
,
error
)
})
}).
run
()
}
DAOFactory
.
prototype
.
findOrCreate
=
function
(
params
,
defaults
)
{
var
self
=
this
;
...
...
test/dao-factory.test.js
View file @
186a2ba
...
...
@@ -328,6 +328,61 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
describe
(
'findOrInitialize'
,
function
()
{
describe
(
'returns an instance if it already exists'
,
function
()
{
it
(
'with a single find field'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'Username'
}).
success
(
function
(
user
)
{
self
.
User
.
findOrInitialize
({
username
:
user
.
username
}).
success
(
function
(
_user
,
initialized
)
{
expect
(
_user
.
id
).
to
.
equal
(
user
.
id
)
expect
(
_user
.
username
).
to
.
equal
(
'Username'
)
expect
(
initialized
).
to
.
be
.
false
done
()
})
})
})
it
(
'with multiple find fields'
,
function
(
done
)
{
var
self
=
this
this
.
User
.
create
({
username
:
'Username'
,
data
:
'data'
}).
success
(
function
(
user
)
{
self
.
User
.
findOrInitialize
({
username
:
user
.
username
,
data
:
user
.
data
}).
success
(
function
(
_user
,
initialized
)
{
expect
(
_user
.
id
).
to
.
equal
(
user
.
id
)
expect
(
_user
.
username
).
to
.
equal
(
'Username'
)
expect
(
_user
.
data
).
to
.
equal
(
'data'
)
expect
(
initialized
).
to
.
be
.
false
done
()
})
})
})
it
(
'builds a new instance with default value.'
,
function
(
done
)
{
var
data
=
{
username
:
'Username'
},
default_values
=
{
data
:
'ThisIsData'
}
this
.
User
.
findOrInitialize
(
data
,
default_values
).
success
(
function
(
user
,
initialized
)
{
expect
(
user
.
id
).
to
.
be
.
null
expect
(
user
.
username
).
to
.
equal
(
'Username'
)
expect
(
user
.
data
).
to
.
equal
(
'ThisIsData'
)
expect
(
initialized
).
to
.
be
.
true
expect
(
user
.
isNewRecord
).
to
.
be
.
true
expect
(
user
.
isDirty
).
to
.
be
.
true
done
()
})
})
})
})
describe
(
'findOrCreate'
,
function
()
{
it
(
"Returns instance if already existent. Single find field."
,
function
(
done
)
{
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