Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "5.37.0",
"version": "5.38.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
13 changes: 13 additions & 0 deletions src/database/drivers/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,19 @@ export abstract class Driver<Client = any, QB = any> {
return data
}

/**
* Find a value in database or create a new one if it doesn't exist.
*/
public async findOrCreate<T = any>(data: Partial<T>): Promise<T> {
const hasValue = await this.find()

if (hasValue) {
return hasValue
}

return this.create(data)
}

/**
* Return a single model instance or, if no results are found,
* execute the given closure.
Expand Down
13 changes: 13 additions & 0 deletions src/database/drivers/FakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,19 @@ export class FakeDriver {
return data
}

/**
* Find a value in database or create a new one if it doesn't exist.
*/
public static async findOrCreate(data: Partial<any>): Promise<any> {
const hasValue = await this.find()

if (hasValue) {
return hasValue
}

return this.create(data)
}

/**
* Find a value in database or execute closure.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/models/BaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,23 @@ export class BaseModel {
return query.findOrFail()
}

/**
* Find a value in database or create a new one if it doesn't exist.
*/
public static async findOrCreate<T extends typeof BaseModel>(
this: T,
where: Partial<InstanceType<T>>,
data: Partial<InstanceType<T>>
): Promise<InstanceType<T>> {
const query = this.query()

if (where) {
query.where(where)
}

return query.findOrCreate(data)
}

/**
* Return a single data or, if no results are found,
* execute the given closure.
Expand Down
13 changes: 13 additions & 0 deletions src/models/builders/ModelQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,19 @@ export class ModelQueryBuilder<
return data
}

/**
* Find a value in database or create a new one if it doesn't exist.
*/
public async findOrCreate(data: Partial<M> = {}) {
const hasValue = await this.find()

if (hasValue) {
return hasValue
}

return this.create(data)
}

/**
* Return a single data or, if no results are found,
* execute the given closure.
Expand Down