-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: upsert should set createdAt by default while it not set (#277)
Co-authored-by: JimmyDaddy <[email protected]>
- Loading branch information
1 parent
5302657
commit 75c2a50
Showing
6 changed files
with
151 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -853,6 +853,16 @@ describe('=> Basic', () => { | |
assert.equal(await post.upsert(), 0); | ||
}); | ||
|
||
it('bone.upsert should set createdAt default', async () => { | ||
await (new Post({ title: 'New Post', isPrivate: 0 }).upsert()); | ||
const post = await Post.findOne('title = ?', 'New Post'); | ||
assert(post.createdAt); | ||
const date = new Date(2017, 11, 12); | ||
await (new Post({ title: 'post 1', isPrivate: 0, createdAt: date }).upsert()); | ||
const post1 = await Post.findOne('title = ?', 'post 1'); | ||
assert.deepEqual(post1.createdAt, date); | ||
}); | ||
|
||
it('bone.upsert() should return affectedRows', async function() { | ||
const post = new Post({ title: 'New Post', isPrivate: 0 }); | ||
// INSERT ... UPDATE returns 1 if the INSERT branch were chosen | ||
|
@@ -892,6 +902,16 @@ describe('=> Basic', () => { | |
assert.equal(count, 1); | ||
}); | ||
|
||
it('Bone.upsert should set createdAt default', async () => { | ||
await Post.upsert({ title: 'New Post', isPrivate: 0 }); | ||
const post = await Post.findOne('title = ?', 'New Post'); | ||
assert(post.createdAt); | ||
const date = new Date(2017, 11, 12); | ||
await Post.upsert({ title: 'post 1', isPrivate: 0, createdAt: date }); | ||
const post1 = await Post.findOne('title = ?', 'post 1'); | ||
assert.deepEqual(post1.createdAt, date); | ||
}); | ||
|
||
it('Bone.upsert() should return affectedRows', async function() { | ||
const tag = await Tag.create({ name: 'Sekiro', type: 1 }); | ||
// INSERT ... UPDATE returns 1 if the INSERT branch were chosen | ||
|
@@ -1156,6 +1176,57 @@ describe('=> Basic', () => { | |
assert.equal(p2.title, 'Leah1'); | ||
}); | ||
|
||
it('Bone.bulkCreate() updateOnDuplicate with createdAt default or set', async () => { | ||
await User.bulkCreate([ | ||
{ nickname: 'TYRAEL', email: '[email protected]', status: 1 }, | ||
{ nickname: 'LEAH', email: '[email protected]', status: 1 }, | ||
], { | ||
updateOnDuplicate: true, | ||
}); | ||
|
||
assert.equal(await User.count(), 2); | ||
let p1 = await User.findOne({ email: '[email protected]' }); | ||
assert.equal(p1.nickname, 'TYRAEL'); | ||
const p1CreatedAt = p1.createdAt; | ||
assert(p1CreatedAt); | ||
let p2 = await User.findOne({ email: '[email protected]' }); | ||
assert.equal(p2.nickname, 'LEAH'); | ||
const p2CreatedAt = p2.createdAt; | ||
assert(p2CreatedAt); | ||
|
||
await User.bulkCreate([ | ||
{ nickname: 'TYRAEL1', email: '[email protected]', status: 1 }, | ||
{ nickname: 'LEAH1', email: '[email protected]', status: 1 }, | ||
], { | ||
updateOnDuplicate: [ 'nickname', 'status' ] | ||
}); | ||
|
||
let p1Updated1 = await User.findOne({ email: '[email protected]' }); | ||
assert.equal(p1Updated1.nickname, 'TYRAEL1'); | ||
const p1Updated1CreatedAt = p1Updated1.createdAt; | ||
assert.deepEqual(p1Updated1CreatedAt, p1CreatedAt); | ||
let p2Updated1 = await User.findOne({ email: '[email protected]' }); | ||
assert.equal(p2Updated1.nickname, 'LEAH1'); | ||
const p2Updated1CreatedAt = p2Updated1.createdAt; | ||
assert.deepEqual(p2Updated1CreatedAt, p2CreatedAt); | ||
|
||
await User.bulkCreate([ | ||
{ nickname: 'TYRAEL2', email: '[email protected]', status: 1 }, | ||
{ nickname: 'LEAH2', email: '[email protected]', status: 1 }, | ||
], { | ||
updateOnDuplicate: [ 'nickname', 'status', 'createdAt' ] | ||
}); | ||
|
||
let p1Updated2 = await User.findOne({ email: '[email protected]' }); | ||
assert.equal(p1Updated2.nickname, 'TYRAEL2'); | ||
const p1Updated2CreatedAt = p1Updated2.createdAt; | ||
assert.notDeepEqual(p1Updated2CreatedAt, p1CreatedAt); | ||
let p2Updated2 = await User.findOne({ email: '[email protected]' }); | ||
assert.equal(p2Updated2.nickname, 'LEAH2'); | ||
const p2Updated2CreatedAt = p2Updated2.createdAt; | ||
assert.notDeepEqual(p2Updated2CreatedAt, p2CreatedAt); | ||
}); | ||
|
||
it('Bone.bulkCreate() should work with updateOnDuplicate keys', async () => { | ||
await User.bulkCreate([ | ||
{ nickname: 'Tyrael', email: '[email protected]', status: 1 }, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters