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 023bab43
authored
Aug 25, 2014
by
Jan Aagaard Meier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Findorcreate with transaction working
1 parent
2302ec49
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
20 deletions
lib/model.js
test/dao-factory/create.test.js
test/error.test.js
lib/model.js
View file @
023bab4
...
@@ -1123,7 +1123,6 @@ module.exports = (function() {
...
@@ -1123,7 +1123,6 @@ module.exports = (function() {
queryOptions
.
exception
=
'WHEN unique_violation THEN RETURN QUERY SELECT * FROM <%= table %> WHERE 1 <> 1;'
;
queryOptions
.
exception
=
'WHEN unique_violation THEN RETURN QUERY SELECT * FROM <%= table %> WHERE 1 <> 1;'
;
return
self
.
create
(
values
,
queryOptions
).
bind
(
this
).
then
(
function
(
instance
)
{
return
self
.
create
(
values
,
queryOptions
).
bind
(
this
).
then
(
function
(
instance
)
{
console
.
log
(
this
);
if
(
instance
[
self
.
primaryKeyAttribute
]
===
null
)
{
if
(
instance
[
self
.
primaryKeyAttribute
]
===
null
)
{
// If the query returned an empty result for the primary key, we know that this was actually a unique constraint violation
// If the query returned an empty result for the primary key, we know that this was actually a unique constraint violation
throw
new
self
.
sequelize
.
UniqueConstraintError
();
throw
new
self
.
sequelize
.
UniqueConstraintError
();
...
@@ -1133,7 +1132,7 @@ module.exports = (function() {
...
@@ -1133,7 +1132,7 @@ module.exports = (function() {
}).
catch
(
self
.
sequelize
.
UniqueConstraintError
,
function
()
{
}).
catch
(
self
.
sequelize
.
UniqueConstraintError
,
function
()
{
// Someone must have created a matching instance inside the same transaction since we last did a find. Let's find it!
// Someone must have created a matching instance inside the same transaction since we last did a find. Let's find it!
return
self
.
find
(
options
,
{
return
self
.
find
(
options
,
{
transaction
:
this
.
transaction
,
transaction
:
internalTransaction
?
null
:
this
.
transaction
,
}).
then
(
function
(
instance
)
{
}).
then
(
function
(
instance
)
{
return
[
instance
,
false
];
return
[
instance
,
false
];
});
});
...
...
test/dao-factory/create.test.js
View file @
023bab4
...
@@ -33,25 +33,25 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -33,25 +33,25 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
});
});
});
});
describe
.
only
(
'findOrCreate'
,
function
()
{
describe
(
'findOrCreate'
,
function
()
{
it
(
"supports transactions"
,
function
(
done
)
{
it
(
"supports transactions"
,
function
(
done
)
{
var
self
=
this
;
var
self
=
this
;
this
.
sequelize
.
transaction
().
then
(
function
(
t
)
{
this
.
sequelize
.
transaction
().
then
(
function
(
t
)
{
self
.
User
.
findOrCreate
({
where
:
{
username
:
'Username'
},
defaults
:
{
data
:
'some data'
}},
{
transaction
:
t
}).
then
(
function
()
{
self
.
User
.
findOrCreate
({
where
:
{
username
:
'Username'
},
defaults
:
{
data
:
'some data'
}},
{
transaction
:
t
}).
then
(
function
()
{
//
self.User.count().success(function(count) {
self
.
User
.
count
().
success
(
function
(
count
)
{
//
expect(count).to.equal(0)
expect
(
count
).
to
.
equal
(
0
)
t
.
commit
().
success
(
function
()
{
t
.
commit
().
success
(
function
()
{
self
.
User
.
count
().
success
(
function
(
count
)
{
self
.
User
.
count
().
success
(
function
(
count
)
{
expect
(
count
).
to
.
equal
(
1
)
expect
(
count
).
to
.
equal
(
1
)
done
()
done
()
//
})
})
})
})
})
})
})
})
})
})
})
})
it
.
skip
(
"returns instance if already existent. Single find field."
,
function
(
done
)
{
it
(
"returns instance if already existent. Single find field."
,
function
(
done
)
{
var
self
=
this
,
var
self
=
this
,
data
=
{
data
=
{
username
:
'Username'
username
:
'Username'
...
@@ -69,7 +69,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -69,7 +69,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
it
.
skip
(
"Returns instance if already existent. Multiple find fields."
,
function
(
done
)
{
it
(
"Returns instance if already existent. Multiple find fields."
,
function
(
done
)
{
var
self
=
this
,
var
self
=
this
,
data
=
{
data
=
{
username
:
'Username'
,
username
:
'Username'
,
...
@@ -87,7 +87,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -87,7 +87,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
it
.
skip
(
"creates new instance with default value."
,
function
(
done
)
{
it
(
"creates new instance with default value."
,
function
(
done
)
{
var
data
=
{
var
data
=
{
username
:
'Username'
username
:
'Username'
},
},
...
@@ -103,7 +103,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -103,7 +103,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
})
})
it
.
skip
(
"supports .or() (only using default values)"
,
function
(
done
)
{
it
(
"supports .or() (only using default values)"
,
function
(
done
)
{
this
.
User
.
findOrCreate
({
this
.
User
.
findOrCreate
({
where
:
Sequelize
.
or
({
username
:
'Fooobzz'
},
{
secretValue
:
'Yolo'
}),
where
:
Sequelize
.
or
({
username
:
'Fooobzz'
},
{
secretValue
:
'Yolo'
}),
defaults
:
{
username
:
'Fooobzz'
,
secretValue
:
'Yolo'
}
defaults
:
{
username
:
'Fooobzz'
,
secretValue
:
'Yolo'
}
...
@@ -132,15 +132,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -132,15 +132,14 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect
(
first
[
0
].
id
).
to
.
equal
(
second
[
0
].
id
);
expect
(
first
[
0
].
id
).
to
.
equal
(
second
[
0
].
id
);
return
transaction
.
rollback
();
return
transaction
.
commit
();
}
}
);
);
});
});
});
});
it
(
'works without a transaction'
,
function
()
{
// Creating two concurrent transactions and selecting / inserting from the same table throws sqlite off
var
self
=
this
;
(
dialect
!==
'sqlite'
?
it
:
it
.
skip
)(
'works without a transaction'
,
function
()
{
if
(
dialect
!==
'sqlite'
)
{
// Creating two concurrent transactions and selecting / inserting from the same table throws sqlite off
return
Promise
.
join
(
return
Promise
.
join
(
this
.
User
.
findOrCreate
({
where
:
{
uniqueName
:
'winner'
}}),
this
.
User
.
findOrCreate
({
where
:
{
uniqueName
:
'winner'
}}),
this
.
User
.
findOrCreate
({
where
:
{
uniqueName
:
'winner'
}}),
this
.
User
.
findOrCreate
({
where
:
{
uniqueName
:
'winner'
}}),
...
@@ -153,7 +152,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
...
@@ -153,7 +152,6 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
expect
(
first
[
0
].
id
).
to
.
equal
(
second
[
0
].
id
);
expect
(
first
[
0
].
id
).
to
.
equal
(
second
[
0
].
id
);
}
}
);
);
}
});
});
});
});
});
});
...
...
test/error.test.js
View file @
023bab4
...
@@ -74,12 +74,9 @@ describe(Support.getTestDialectTeaser("Sequelize Errors"), function () {
...
@@ -74,12 +74,9 @@ describe(Support.getTestDialectTeaser("Sequelize Errors"), function () {
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
this
.
sequelize
.
sync
({
force
:
true
}).
bind
(
this
).
then
(
function
()
{
return
User
.
create
({
first_name
:
'jan'
,
last_name
:
'meier'
});
return
User
.
create
({
first_name
:
'jan'
,
last_name
:
'meier'
});
}).
then
(
function
()
{
}).
then
(
function
()
{
return
Promise
.
join
(
return
User
.
create
({
first_name
:
'jan'
,
last_name
:
'meier'
}).
catch
(
this
.
sequelize
.
UniqueConstraintError
,
spy
);
User
.
create
({
first_name
:
'jan'
,
last_name
:
'meier'
}).
catch
(
this
.
sequelize
.
UniqueConstraintError
,
spy
),
}).
then
(
function
()
{
function
()
{
expect
(
spy
).
to
.
have
.
been
.
calledOnce
;
expect
(
spy
).
to
.
have
.
been
.
calledOnce
;
}
);
});
});
});
});
});
});
...
...
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