-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BaseModel): add updateOrCreateMany to avoid creating duplicate rows
- Loading branch information
1 parent
45ea50e
commit 7c24bdc
Showing
3 changed files
with
252 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2131,6 +2131,216 @@ test.group('Base Model | fetch', (group) => { | |
const usersList = await db.query().from('users') | ||
assert.lengthOf(usersList, 1) | ||
}) | ||
|
||
test('persist records to db when find call returns zero rows', async (assert) => { | ||
class User extends BaseModel { | ||
@column({ primary: true }) | ||
public id: number | ||
|
||
@column() | ||
public username: string | ||
|
||
@column() | ||
public email: string | ||
|
||
@column() | ||
public points: number | ||
} | ||
|
||
const users = await User.updateOrCreateMany( | ||
'username', | ||
[ | ||
{ | ||
username: 'virk', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
username: 'nikk', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
username: 'romain', | ||
email: '[email protected]', | ||
}, | ||
], | ||
) | ||
|
||
assert.lengthOf(users, 3) | ||
assert.isTrue(users[0].$persisted) | ||
assert.equal(users[0].username, 'virk') | ||
assert.equal(users[0].email, '[email protected]') | ||
|
||
assert.isTrue(users[1].$persisted) | ||
assert.equal(users[1].username, 'nikk') | ||
assert.equal(users[1].email, '[email protected]') | ||
|
||
assert.isTrue(users[2].$persisted) | ||
assert.equal(users[2].username, 'romain') | ||
assert.equal(users[2].email, '[email protected]') | ||
|
||
const usersList = await db.query().from('users') | ||
assert.lengthOf(usersList, 3) | ||
}) | ||
|
||
test('update records and avoiding duplicates', async (assert) => { | ||
class User extends BaseModel { | ||
@column({ primary: true }) | ||
public id: number | ||
|
||
@column() | ||
public username: string | ||
|
||
@column() | ||
public email: string | ||
|
||
@column() | ||
public points: number | ||
} | ||
|
||
await db.insertQuery().table('users').insert({ | ||
username: 'virk', | ||
email: '[email protected]', | ||
points: 10, | ||
}) | ||
|
||
const users = await User.updateOrCreateMany( | ||
'username', | ||
[ | ||
{ | ||
username: 'virk', | ||
email: '[email protected]', | ||
points: 4, | ||
}, | ||
{ | ||
username: 'nikk', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
username: 'romain', | ||
email: '[email protected]', | ||
}, | ||
], | ||
) | ||
|
||
assert.lengthOf(users, 3) | ||
assert.isTrue(users[0].$persisted) | ||
assert.equal(users[0].username, 'virk') | ||
assert.equal(users[0].email, '[email protected]') | ||
assert.equal(users[0].points, 4) | ||
|
||
assert.isTrue(users[1].$persisted) | ||
assert.equal(users[1].username, 'nikk') | ||
assert.equal(users[1].email, '[email protected]') | ||
assert.isUndefined(users[1].points) | ||
|
||
assert.isTrue(users[2].$persisted) | ||
assert.equal(users[2].username, 'romain') | ||
assert.equal(users[2].email, '[email protected]') | ||
assert.isUndefined(users[2].points) | ||
|
||
const usersList = await db.query().from('users') | ||
assert.lengthOf(usersList, 3) | ||
}) | ||
|
||
test('wrap create calls inside a transaction using updateOrCreateMany', async (assert) => { | ||
class User extends BaseModel { | ||
@column({ primary: true }) | ||
public id: number | ||
|
||
@column() | ||
public username: string | ||
|
||
@column() | ||
public email: string | ||
|
||
@column() | ||
public points: number | ||
} | ||
|
||
await db.insertQuery().table('users').insert({ | ||
username: 'virk', | ||
email: '[email protected]', | ||
points: 10, | ||
}) | ||
|
||
const trx = await db.transaction() | ||
|
||
await User.updateOrCreateMany( | ||
'username', | ||
[ | ||
{ | ||
username: 'virk', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
username: 'nikk', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
username: 'romain', | ||
email: '[email protected]', | ||
}, | ||
], | ||
{ | ||
client: trx, | ||
}, | ||
) | ||
|
||
await trx.rollback() | ||
const usersList = await db.query().from('users') | ||
assert.lengthOf(usersList, 1) | ||
}) | ||
|
||
test('wrap update calls inside a transaction using updateOrCreateMany', async (assert) => { | ||
class User extends BaseModel { | ||
@column({ primary: true }) | ||
public id: number | ||
|
||
@column() | ||
public username: string | ||
|
||
@column() | ||
public email: string | ||
|
||
@column() | ||
public points: number | ||
} | ||
|
||
await db.insertQuery().table('users').insert({ | ||
username: 'virk', | ||
email: '[email protected]', | ||
points: 10, | ||
}) | ||
|
||
const trx = await db.transaction() | ||
|
||
await User.updateOrCreateMany( | ||
'username', | ||
[ | ||
{ | ||
username: 'virk', | ||
email: '[email protected]', | ||
points: 4, | ||
}, | ||
{ | ||
username: 'nikk', | ||
email: '[email protected]', | ||
}, | ||
{ | ||
username: 'romain', | ||
email: '[email protected]', | ||
}, | ||
], | ||
{ | ||
client: trx, | ||
}, | ||
) | ||
|
||
await trx.rollback() | ||
const usersList = await db.query().from('users') | ||
assert.lengthOf(usersList, 1) | ||
assert.equal(usersList[0].points, 10) | ||
}) | ||
}) | ||
|
||
test.group('Base Model | hooks', (group) => { | ||
|