Skip to content

Commit

Permalink
Merge pull request #119 from AthennaIO/develop
Browse files Browse the repository at this point in the history
fix(type): adjust types and exist method of fake
  • Loading branch information
jlenon7 authored Dec 29, 2023
2 parents 40ce18e + cb2d186 commit b1d3d75
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 43 deletions.
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": "4.14.2",
"version": "4.14.3",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
20 changes: 8 additions & 12 deletions src/database/builders/QueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,33 +179,29 @@ export class QueryBuilder<T = any, Driver extends DriverImpl = any> {
/**
* Create a value in database.
*/
public async create(data?: Partial<T> | ModelColumns<T>): Promise<T> {
return this.driver.create(data as Partial<T>)
public async create(data?: Partial<T>): Promise<T> {
return this.driver.create(data)
}

/**
* Create many values in database.
*/
public async createMany(
data?: Partial<T>[] | ModelColumns<T>[]
): Promise<T[]> {
return this.driver.createMany(data as Partial<T>[])
public async createMany(data?: Partial<T>[]): Promise<T[]> {
return this.driver.createMany(data)
}

/**
* Create data or update if already exists.
*/
public async createOrUpdate(
data?: Partial<T> | ModelColumns<T>
): Promise<T | T[]> {
return this.driver.createOrUpdate(data as Partial<T>)
public async createOrUpdate(data?: Partial<T>): Promise<T | T[]> {
return this.driver.createOrUpdate(data)
}

/**
* Update data in database.
*/
public async update(data: Partial<T> | ModelColumns<T>): Promise<T | T[]> {
return this.driver.update(data as Partial<T>)
public async update(data: Partial<T>): Promise<T | T[]> {
return this.driver.update(data)
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/database/drivers/FakeDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,12 @@ export class FakeDriver {
}

/**
* Find a value in database.
* Find a value in database and return as boolean.
*/
public static async exists(): Promise<boolean> {
return true
const data = await this.find()

return !!data
}

/**
Expand Down
36 changes: 18 additions & 18 deletions src/models/BaseModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import equal from 'fast-deep-equal'
import { Database } from '#src/facades/Database'
import type { ModelColumns, ModelRelations } from '#src/types'
import type { ModelRelations } from '#src/types'
import { faker, type Faker } from '@faker-js/faker'
import { ModelSchema } from '#src/models/schemas/ModelSchema'
import { Collection, Is, Json, String } from '@athenna/common'
Expand Down Expand Up @@ -156,7 +156,7 @@ export class BaseModel {
*/
public static async find<T extends typeof BaseModel>(
this: T,
where?: Partial<ModelColumns<T>>
where?: Partial<InstanceType<T>>
): Promise<InstanceType<T>> {
const query = this.query()

Expand All @@ -172,7 +172,7 @@ export class BaseModel {
*/
public static async exists<T extends typeof BaseModel>(
this: T,
where?: Partial<ModelColumns<T>>
where?: Partial<InstanceType<T>>
): Promise<boolean> {
const query = this.query()

Expand All @@ -188,7 +188,7 @@ export class BaseModel {
*/
public static async findOrFail<T extends typeof BaseModel>(
this: T,
where?: Partial<ModelColumns<T>>
where?: Partial<InstanceType<T>>
): Promise<InstanceType<T>> {
const query = this.query()

Expand All @@ -205,7 +205,7 @@ export class BaseModel {
*/
public static async findOr<T extends typeof BaseModel>(
this: T,
where: Partial<ModelColumns<T>>,
where: Partial<InstanceType<T>>,
closure: () => any | Promise<any>
): Promise<InstanceType<T> | any> {
const query = this.query()
Expand All @@ -222,7 +222,7 @@ export class BaseModel {
*/
public static async findMany<T extends typeof BaseModel>(
this: T,
where?: Partial<ModelColumns<T>>
where?: Partial<InstanceType<T>>
): Promise<InstanceType<T>[]> {
const query = this.query()

Expand All @@ -239,7 +239,7 @@ export class BaseModel {
*/
public static async collection<T extends typeof BaseModel>(
this: T,
where?: Partial<ModelColumns<T>>
where?: Partial<InstanceType<T>>
): Promise<Collection<InstanceType<T>>> {
const query = this.query()

Expand All @@ -255,61 +255,61 @@ export class BaseModel {
*/
public static async create<T extends typeof BaseModel>(
this: T,
data: Partial<ModelColumns<T>> = {}
data: Partial<InstanceType<T>> = {}
): Promise<InstanceType<T>> {
return this.query().create(data as any)
return this.query().create(data)
}

/**
* Create many values in database.
*/
public static async createMany<T extends typeof BaseModel>(
this: T,
data: Partial<ModelColumns<T>>[]
data: Partial<InstanceType<T>>[]
): Promise<InstanceType<T>[]> {
return this.query().createMany(data as any[])
return this.query().createMany(data)
}

/**
* Create or update a value in database.
*/
public static async createOrUpdate<T extends typeof BaseModel>(
this: T,
where: Partial<ModelColumns<T>>,
data: Partial<ModelColumns<T>>
where: Partial<InstanceType<T>>,
data: Partial<InstanceType<T>>
): Promise<InstanceType<T> | InstanceType<T>[]> {
const query = this.query()

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

return query.createOrUpdate(data as any)
return query.createOrUpdate(data)
}

/**
* Update a value in database.
*/
public static async update<T extends typeof BaseModel>(
this: T,
where: Partial<ModelColumns<T>>,
data: Partial<ModelColumns<T>>
where: Partial<InstanceType<T>>,
data: Partial<InstanceType<T>>
): Promise<InstanceType<T> | InstanceType<T>[]> {
const query = this.query()

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

return query.update(data as any)
return query.update(data)
}

/**
* Delete or soft delete a value in database.
*/
public static async delete<T extends typeof BaseModel>(
this: T,
where: Partial<ModelColumns<T>>,
where: Partial<InstanceType<T>>,
force = false
): Promise<void> {
const query = this.query()
Expand Down
10 changes: 5 additions & 5 deletions src/models/builders/ModelQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class ModelQueryBuilder<
/**
* Create a value in database.
*/
public async create(data: ModelColumns<M> = {} as any) {
public async create(data: Partial<M> = {}) {
const created = await this.createMany([data])

return created[0]
Expand All @@ -218,8 +218,8 @@ export class ModelQueryBuilder<
/**
* Create many values in database.
*/
public async createMany(data: ModelColumns<M>[]) {
data = (await Promise.all(
public async createMany(data: Partial<M>[]) {
data = await Promise.all(
data.map(async d => {
const date = new Date()
const createdAt = this.schema.getCreatedAtColumn()
Expand Down Expand Up @@ -249,7 +249,7 @@ export class ModelQueryBuilder<

return parsed
})
)) as any[]
)

const created = await super.createMany(data)

Expand All @@ -268,7 +268,7 @@ export class ModelQueryBuilder<
return this.where(pk, hasValue[pk as any]).update(data)
}

return this.create(data as any)
return this.create(data)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/drivers/MySqlDriverTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ export default class MySqlDriverTest {

@Test()
public async shouldBeAbleToValidateThatDataExistsUsingDriver({ assert }: Context) {
const data = { _id: '1', name: 'Charles Babbage' }
const data = { id: '1', name: 'Charles Babbage' }
await this.driver.table('users').create(data)

const result = await this.driver.table('users').exists()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/drivers/PostgresDriverTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ export default class PostgresDriverTest {

@Test()
public async shouldBeAbleToValidateThatDataExistsUsingDriver({ assert }: Context) {
const data = { _id: '1', name: 'Charles Babbage' }
const data = { id: '1', name: 'Charles Babbage' }
await this.driver.table('users').create(data)

const result = await this.driver.table('users').exists()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/models/BaseModelTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export default class BaseModelTest {
Mock.when(Database.driver, 'find').resolve(undefined)
Mock.when(Database.driver, 'createMany').resolve([{ id: '1' }])

const data = await User.create({ id: '1' })
const data = await User.create()

assert.deepEqual(data.id, '1')
}
Expand Down

0 comments on commit b1d3d75

Please sign in to comment.