Skip to content

Commit

Permalink
refactor: rename firstOrSave to firstOrCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 13, 2019
1 parent d5a2cba commit 90e6e02
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion adonis-typings/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ declare module '@ioc:Adonis/Lucid/Model' {
/**
* Returns the first row or save it to the database
*/
firstOrSave<T extends ModelConstructorContract> (
firstOrCreate<T extends ModelConstructorContract> (
this: T,
search: any,
savePayload?: any,
Expand Down
2 changes: 1 addition & 1 deletion src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export class BaseModel implements ModelContract {
/**
* Find model instance using a key/value pair
*/
public static async firstOrSave<T extends ModelConstructorContract> (
public static async firstOrCreate<T extends ModelConstructorContract> (
this: T,
search: any,
savePayload?: any,
Expand Down
18 changes: 9 additions & 9 deletions test/orm/base-model-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ test.group('Model options | Model.findMany', (group) => {
})
})

test.group('Model options | Model.firstOrSave', (group) => {
test.group('Model options | Model.firstOrCreate', (group) => {
group.before(async () => {
db = getDb()
BaseModel = getBaseModel(ormAdapter(db))
Expand Down Expand Up @@ -465,7 +465,7 @@ test.group('Model options | Model.firstOrSave', (group) => {

await db.insertQuery().table('users').insert({ username: 'virk' })

const user = await User.firstOrSave({ username: 'virk' }, undefined, { connection: 'secondary' })
const user = await User.firstOrCreate({ username: 'virk' }, undefined, { connection: 'secondary' })
const total = await db.from('users').count('*', 'total')

assert.equal(total[0].total, 1)
Expand All @@ -486,7 +486,7 @@ test.group('Model options | Model.firstOrSave', (group) => {

await db.insertQuery().table('users').insert({ username: 'virk' })

const user = await User.firstOrSave({ username: 'nikk' }, undefined, { connection: 'secondary' })
const user = await User.firstOrCreate({ username: 'nikk' }, undefined, { connection: 'secondary' })
const total = await db.from('users').count('*', 'total')

assert.equal(total[0].total, 2)
Expand All @@ -508,7 +508,7 @@ test.group('Model options | Model.firstOrSave', (group) => {
await db.insertQuery().table('users').insert({ username: 'virk' })
const profiler = new Profiler({})

const user = await User.firstOrSave({ username: 'virk' }, undefined, { profiler })
const user = await User.firstOrCreate({ username: 'virk' }, undefined, { profiler })
const total = await db.from('users').count('*', 'total')

assert.equal(total[0].total, 1)
Expand All @@ -530,7 +530,7 @@ test.group('Model options | Model.firstOrSave', (group) => {
await db.insertQuery().table('users').insert({ username: 'virk' })
const profiler = new Profiler({})

const user = await User.firstOrSave({ username: 'nikk' }, undefined, { profiler })
const user = await User.firstOrCreate({ username: 'nikk' }, undefined, { profiler })
const total = await db.from('users').count('*', 'total')

assert.equal(total[0].total, 2)
Expand All @@ -551,7 +551,7 @@ test.group('Model options | Model.firstOrSave', (group) => {
await db.insertQuery().table('users').insert({ username: 'virk' })
const client = db.connection('secondary')

const user = await User.firstOrSave({ username: 'virk' }, undefined, { client })
const user = await User.firstOrCreate({ username: 'virk' }, undefined, { client })
const total = await db.from('users').count('*', 'total')

assert.equal(total[0].total, 1)
Expand All @@ -573,7 +573,7 @@ test.group('Model options | Model.firstOrSave', (group) => {
await db.insertQuery().table('users').insert({ username: 'virk' })
const client = db.connection('secondary')

const user = await User.firstOrSave({ username: 'nikk' }, undefined, { client })
const user = await User.firstOrCreate({ username: 'nikk' }, undefined, { client })
const total = await db.from('users').count('*', 'total')

assert.equal(total[0].total, 2)
Expand All @@ -595,7 +595,7 @@ test.group('Model options | Model.firstOrSave', (group) => {
await db.insertQuery().table('users').insert({ username: 'virk' })
const client = await db.connection('secondary').transaction()

const user = await User.firstOrSave({ username: 'virk' }, undefined, { client })
const user = await User.firstOrCreate({ username: 'virk' }, undefined, { client })
await client.commit()

const total = await db.from('users').count('*', 'total')
Expand All @@ -618,7 +618,7 @@ test.group('Model options | Model.firstOrSave', (group) => {

const client = await db.connection('secondary').transaction()

const user = await User.firstOrSave({ username: 'virk' }, undefined, { client })
const user = await User.firstOrCreate({ username: 'virk' }, undefined, { client })
await client.rollback()

const total = await db.from('users').count('*', 'total')
Expand Down
4 changes: 2 additions & 2 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1569,7 +1569,7 @@ test.group('Base Model | fetch', (group) => {
}

await db.insertQuery().table('users').insert({ username: 'virk' })
const user = await User.firstOrSave({ userName: 'virk' })
const user = await User.firstOrCreate({ userName: 'virk' })

const totalUsers = await db.query().from('users').count('*', 'total')

Expand All @@ -1592,7 +1592,7 @@ test.group('Base Model | fetch', (group) => {
}

await db.insertQuery().table('users').insert({ username: 'virk' })
const user = await User.firstOrSave({ userName: 'nikk' }, { email: '[email protected]' })
const user = await User.firstOrCreate({ userName: 'nikk' }, { email: '[email protected]' })

const totalUsers = await db.query().from('users').count('*', 'total')

Expand Down

0 comments on commit 90e6e02

Please sign in to comment.