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
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
18 deletions
lib/model.js
test/model/attributes.test.js
lib/model.js
View file @
54a194a
...
...
@@ -1151,8 +1151,8 @@ module.exports = (function() {
,
createdAtAttr
=
this
.
_timestampAttributes
.
createdAt
,
errors
=
[]
,
daoPromises
=
[]
,
daos
=
records
.
map
(
function
(
v
)
{
return
self
.
build
(
v
,
{
,
daos
=
records
.
map
(
function
(
v
alues
)
{
return
self
.
build
(
v
alues
,
{
isNewRecord
:
true
})
})
...
...
@@ -1224,6 +1224,18 @@ module.exports = (function() {
// Validation or hooks failed
return
self
.
sequelize
.
Promise
.
reject
(
errors
)
}
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
return
self
.
QueryInterface
.
bulkInsert
(
self
.
getTableName
(),
records
,
options
,
self
).
then
(
runAfterCreate
)
}
else
{
...
...
test/model/attributes.test.js
View file @
54a194a
...
...
@@ -14,17 +14,18 @@ chai.config.includeStack = true
describe
(
Support
.
getTestDialectTeaser
(
"Model"
),
function
()
{
describe
(
'attributes'
,
function
()
{
describe
(
'field'
,
function
()
{
it
(
'should create and fetch with alternative field names from a simple model'
,
function
()
{
var
queryInterface
=
this
.
sequelize
.
getQueryInterface
()
,
User
=
this
.
sequelize
.
define
(
'user'
,
{
name
:
{
type
:
DataTypes
.
STRING
,
field
:
'full_name'
}
},
{
tableName
:
'users'
,
timestamps
:
false
})
beforeEach
(
function
()
{
var
queryInterface
=
this
.
sequelize
.
getQueryInterface
();
this
.
User
=
this
.
sequelize
.
define
(
'user'
,
{
name
:
{
type
:
DataTypes
.
STRING
,
field
:
'full_name'
}
},
{
tableName
:
'users'
,
timestamps
:
false
})
return
queryInterface
.
createTable
(
'users'
,
{
id
:
{
...
...
@@ -36,18 +37,48 @@ describe(Support.getTestDialectTeaser("Model"), function () {
full_name
:
{
type
:
DataTypes
.
STRING
}
});
});
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
()
{
return
User
.
create
({
name
:
'Foobar'
return
self
.
User
.
find
({
limit
:
1
});
}).
then
(
function
(
user
)
{
expect
(
user
.
get
(
'name'
)).
to
.
equal
(
'Foobar'
);
return
user
.
updateAttributes
({
name
:
'Barfoo'
});
}).
then
(
function
()
{
return
User
.
find
({
return
self
.
User
.
find
({
limit
:
1
});
}).
then
(
function
(
user
)
{
expect
(
user
.
get
(
'name'
)).
to
.
equal
(
'Foobar'
);
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