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 54a194a0
authored
May 07, 2014
by
Mick Hansen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(model/attribute): support explicit field names in bulkCreate
1 parent
665fadc2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
53 additions
and
9 deletions
lib/model.js
test/model/attributes.test.js
lib/model.js
View file @
54a194a
...
@@ -1151,8 +1151,8 @@ module.exports = (function() {
...
@@ -1151,8 +1151,8 @@ module.exports = (function() {
,
createdAtAttr
=
this
.
_timestampAttributes
.
createdAt
,
createdAtAttr
=
this
.
_timestampAttributes
.
createdAt
,
errors
=
[]
,
errors
=
[]
,
daoPromises
=
[]
,
daoPromises
=
[]
,
daos
=
records
.
map
(
function
(
v
)
{
,
daos
=
records
.
map
(
function
(
v
alues
)
{
return
self
.
build
(
v
,
{
return
self
.
build
(
v
alues
,
{
isNewRecord
:
true
isNewRecord
:
true
})
})
})
})
...
@@ -1224,6 +1224,18 @@ module.exports = (function() {
...
@@ -1224,6 +1224,18 @@ module.exports = (function() {
// Validation or hooks failed
// Validation or hooks failed
return
self
.
sequelize
.
Promise
.
reject
(
errors
)
return
self
.
sequelize
.
Promise
.
reject
(
errors
)
}
else
if
(
records
.
length
)
{
}
else
if
(
records
.
length
)
{
// Map field names
records
.
forEach
(
function
(
values
)
{
for
(
var
attr
in
values
)
{
if
(
values
.
hasOwnProperty
(
attr
))
{
if
(
self
.
rawAttributes
[
attr
].
field
)
{
values
[
self
.
rawAttributes
[
attr
].
field
]
=
values
[
attr
];
delete
values
[
attr
];
}
}
}
});
// Insert all records at once
// Insert all records at once
return
self
.
QueryInterface
.
bulkInsert
(
self
.
getTableName
(),
records
,
options
,
self
).
then
(
runAfterCreate
)
return
self
.
QueryInterface
.
bulkInsert
(
self
.
getTableName
(),
records
,
options
,
self
).
then
(
runAfterCreate
)
}
else
{
}
else
{
...
...
test/model/attributes.test.js
View file @
54a194a
...
@@ -14,9 +14,10 @@ chai.config.includeStack = true
...
@@ -14,9 +14,10 @@ chai.config.includeStack = true
describe
(
Support
.
getTestDialectTeaser
(
"Model"
),
function
()
{
describe
(
Support
.
getTestDialectTeaser
(
"Model"
),
function
()
{
describe
(
'attributes'
,
function
()
{
describe
(
'attributes'
,
function
()
{
describe
(
'field'
,
function
()
{
describe
(
'field'
,
function
()
{
it
(
'should create and fetch with alternative field names from a simple model'
,
function
()
{
beforeEach
(
function
()
{
var
queryInterface
=
this
.
sequelize
.
getQueryInterface
()
var
queryInterface
=
this
.
sequelize
.
getQueryInterface
();
,
User
=
this
.
sequelize
.
define
(
'user'
,
{
this
.
User
=
this
.
sequelize
.
define
(
'user'
,
{
name
:
{
name
:
{
type
:
DataTypes
.
STRING
,
type
:
DataTypes
.
STRING
,
field
:
'full_name'
field
:
'full_name'
...
@@ -36,18 +37,48 @@ describe(Support.getTestDialectTeaser("Model"), function () {
...
@@ -36,18 +37,48 @@ describe(Support.getTestDialectTeaser("Model"), function () {
full_name
:
{
full_name
:
{
type
:
DataTypes
.
STRING
type
:
DataTypes
.
STRING
}
}
}).
then
(
function
()
{
return
User
.
create
({
name
:
'Foobar'
});
});
});
it
(
'should create, fetch and update with alternative field names from a simple model'
,
function
()
{
var
self
=
this
;
return
this
.
User
.
create
({
name
:
'Foobar'
}).
then
(
function
()
{
}).
then
(
function
()
{
return
User
.
find
({
return
self
.
User
.
find
({
limit
:
1
limit
:
1
});
});
}).
then
(
function
(
user
)
{
}).
then
(
function
(
user
)
{
expect
(
user
.
get
(
'name'
)).
to
.
equal
(
'Foobar'
);
expect
(
user
.
get
(
'name'
)).
to
.
equal
(
'Foobar'
);
return
user
.
updateAttributes
({
name
:
'Barfoo'
});
}).
then
(
function
()
{
return
self
.
User
.
find
({
limit
:
1
});
});
}).
then
(
function
(
user
)
{
expect
(
user
.
get
(
'name'
)).
to
.
equal
(
'Barfoo'
);
})
})
});
it
(
'should work with bulkCreate and findAll'
,
function
()
{
var
self
=
this
;
return
this
.
User
.
bulkCreate
([{
name
:
'Abc'
,
},
{
name
:
'Bcd'
},
{
name
:
'Cde'
}]).
then
(
function
()
{
return
self
.
User
.
findAll
();
}).
then
(
function
(
users
)
{
users
.
forEach
(
function
(
user
)
{
expect
([
'Abc'
,
'Bcd'
,
'Cde'
].
indexOf
(
user
.
get
(
'name'
))
!==
-
1
).
to
.
be
.
true
});
});
});
})
})
})
})
})
})
\ No newline at end of file
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