Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add UpdateorCreate #484

Closed
SnapnetDev opened this issue Aug 21, 2019 · 4 comments
Closed

Add UpdateorCreate #484

SnapnetDev opened this issue Aug 21, 2019 · 4 comments
Assignees

Comments

@SnapnetDev
Copy link

Prerequisites

Why this feature is required (specific use-cases will be appreciated)?

This feature is required when you want to run a lucid query to update or create item in the database

Have you tried any other work arounds?

Yes , i have added a new function that enables be achieve this in

static async updateOrCreate (whereClause, payload, trx) {
if (!payload) {
payload = whereClause
}

/**
 * Find a row using where clause and update if exist
 */
const row = await this.query().where(whereClause).first()
if (row) {
  const update= this.query().where(whereClause).update(payload);
  return update;
}

/**
 * Otherwise create one
 */
return this.create(payload, trx)

}

Are you willing to work on it with little guidance?

Yes

@adailsonm
Copy link

Great relevance, congratulations on the suggestion.

@MZanggl
Copy link
Contributor

MZanggl commented Sep 8, 2019

Meanwhile, I have made this available through a model trait. https://github.com/MZanggl/adonis-lucid-update-or-create

@thetutlage thetutlage self-assigned this Oct 7, 2019
thetutlage added a commit that referenced this issue Oct 7, 2019
@triage-new-issues triage-new-issues bot removed the triage label Oct 7, 2019
thetutlage added a commit that referenced this issue Jan 12, 2020
@syntaxlexx
Copy link

syntaxlexx commented Apr 6, 2020

Hi everyone. When is the new release incorporating this PR being released? Just being curious

@RomainLanz
Copy link
Member

RomainLanz commented Apr 11, 2020

It's already released in the new version of AdonisJS (aka, the Preview of V5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants