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 5d27f33c
authored
Jul 24, 2012
by
Edgar Veiga
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add getOrCreate method + proper testing.
1 parent
d4ed9d39
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
80 additions
and
0 deletions
lib/dao-factory.js
spec/dao-factory.spec.js
lib/dao-factory.js
View file @
5d27f33
...
...
@@ -194,6 +194,34 @@ module.exports = (function() {
return
this
.
build
(
values
).
save
(
fields
)
}
DAOFactory
.
prototype
.
getOrCreate
=
function
(
params
,
defaults
)
{
var
self
=
this
;
return
new
Utils
.
CustomEventEmitter
(
function
(
emitter
)
{
self
.
find
({
where
:
params
}).
success
(
function
(
instance
)
{
if
(
instance
===
null
)
{
for
(
var
attrname
in
defaults
)
{
params
[
attrname
]
=
defaults
[
attrname
];
}
self
.
create
(
params
)
.
success
(
function
(
instance
)
{
emitter
.
emit
(
'success'
,
instance
)
})
.
error
(
function
(
erro
)
{
emitter
.
emit
(
'error'
,
error
)
});
}
else
{
emitter
.
emit
(
'success'
,
instance
)
}
}).
error
(
function
(
error
)
{
emitter
.
emit
(
'error'
,
error
)
});
}).
run
()
}
DAOFactory
.
prototype
.
__defineGetter__
(
'primaryKeys'
,
function
()
{
var
result
=
{}
Utils
.
_
.
each
(
this
.
attributes
,
function
(
dataTypeString
,
attributeName
)
{
...
...
spec/dao-factory.spec.js
View file @
5d27f33
...
...
@@ -28,6 +28,58 @@ dialects.forEach(function(dialect) {
})
})
describe
(
'getOrCreate'
,
function
()
{
it
(
"Returns instace if already existent. Single find field."
,
function
(
done
)
{
var
self
=
this
,
data
=
{
username
:
'Username'
};
this
.
User
.
create
(
data
).
success
(
function
(
user
)
{
self
.
User
.
getOrCreate
({
username
:
user
.
username
}).
success
(
function
(
_user
)
{
expect
(
_user
.
id
).
toEqual
(
user
.
id
)
expect
(
_user
.
username
).
toEqual
(
'Username'
)
done
()
})
})
})
it
(
"Returns instace if already existent. Multiple find fields."
,
function
(
done
)
{
var
self
=
this
,
data
=
{
username
:
'Username'
,
data
:
'ThisIsData'
};
this
.
User
.
create
(
data
).
success
(
function
(
user
)
{
self
.
User
.
getOrCreate
(
data
).
success
(
function
(
_user
)
{
expect
(
_user
.
id
).
toEqual
(
user
.
id
)
expect
(
_user
.
username
).
toEqual
(
'Username'
)
expect
(
_user
.
data
).
toEqual
(
'ThisIsData'
)
done
()
})
})
})
it
(
"Creates new instance with default value."
,
function
(
done
)
{
var
self
=
this
,
data
=
{
username
:
'Username'
},
default_values
=
{
data
:
'ThisIsData'
};
this
.
User
.
getOrCreate
(
data
,
default_values
).
success
(
function
(
user
)
{
expect
(
user
.
username
).
toEqual
(
'Username'
)
expect
(
user
.
data
).
toEqual
(
'ThisIsData'
)
done
()
})
})
})
describe
(
'create'
,
function
()
{
it
(
'should only store the values passed in the witelist'
,
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