Skip to content

Commit

Permalink
feat(type): add relation type
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Jan 3, 2024
1 parent 0aebe5d commit 65be1bf
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 37 deletions.
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.19.0",
"version": "4.20.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
36 changes: 18 additions & 18 deletions src/database/drivers/MongoDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set the columns that should be selected on query raw.
*/
public selectRaw() {
public selectRaw(): this {
throw new NotImplementedMethodException(this.selectRaw.name, 'mongo')
}

Expand All @@ -752,7 +752,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
* Different from `table()` method, this method
* doesn't change the driver table.
*/
public from() {
public from(): this {
throw new NotImplementedMethodException(this.from.name, 'mongo')
}

Expand All @@ -761,7 +761,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
* Different from `table()` method, this method
* doesn't change the driver table.
*/
public fromRaw() {
public fromRaw(): this {
throw new NotImplementedMethodException(this.selectRaw.name, 'mongo')
}

Expand Down Expand Up @@ -868,7 +868,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set a join raw statement in your query.
*/
public joinRaw() {
public joinRaw(): this {
throw new NotImplementedMethodException(this.joinRaw.name, 'mongo')
}

Expand All @@ -889,7 +889,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set a group by raw statement in your query.
*/
public groupByRaw() {
public groupByRaw(): this {
throw new NotImplementedMethodException(this.groupByRaw.name, 'mongo')
}

Expand All @@ -907,21 +907,21 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set a having raw statement in your query.
*/
public havingRaw() {
public havingRaw(): this {
throw new NotImplementedMethodException(this.havingRaw.name, 'mongo')
}

/**
* Set a having exists statement in your query.
*/
public havingExists() {
public havingExists(): this {
throw new NotImplementedMethodException(this.havingExists.name, 'mongo')
}

/**
* Set a having not exists statement in your query.
*/
public havingNotExists() {
public havingNotExists(): this {
throw new NotImplementedMethodException(this.havingNotExists.name, 'mongo')
}

Expand Down Expand Up @@ -981,21 +981,21 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set an or having raw statement in your query.
*/
public orHavingRaw() {
public orHavingRaw(): this {
throw new NotImplementedMethodException(this.orHavingRaw.name, 'mongo')
}

/**
* Set an or having exists statement in your query.
*/
public orHavingExists() {
public orHavingExists(): this {
throw new NotImplementedMethodException(this.orHavingExists.name, 'mongo')
}

/**
* Set an or having not exists statement in your query.
*/
public orHavingNotExists() {
public orHavingNotExists(): this {
throw new NotImplementedMethodException(
this.orHavingNotExists.name,
'mongo'
Expand Down Expand Up @@ -1091,21 +1091,21 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set a where raw statement in your query.
*/
public whereRaw() {
public whereRaw(): this {
throw new NotImplementedMethodException(this.whereRaw.name, 'mongo')
}

/**
* Set a where exists statement in your query.
*/
public whereExists() {
public whereExists(): this {
throw new NotImplementedMethodException(this.whereExists.name, 'mongo')
}

/**
* Set a where not exists statement in your query.
*/
public whereNotExists() {
public whereNotExists(): this {
throw new NotImplementedMethodException(this.whereNotExists.name, 'mongo')
}

Expand Down Expand Up @@ -1221,21 +1221,21 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set a or where raw statement in your query.
*/
public orWhereRaw() {
public orWhereRaw(): this {
throw new NotImplementedMethodException(this.orWhereRaw.name, 'mongo')
}

/**
* Set an or where exists statement in your query.
*/
public orWhereExists() {
public orWhereExists(): this {
throw new NotImplementedMethodException(this.orWhereExists.name, 'mongo')
}

/**
* Set an or where not exists statement in your query.
*/
public orWhereNotExists() {
public orWhereNotExists(): this {
throw new NotImplementedMethodException(this.orWhereNotExists.name, 'mongo')
}

Expand Down Expand Up @@ -1323,7 +1323,7 @@ export class MongoDriver extends Driver<Connection, Collection> {
/**
* Set an order by raw statement in your query.
*/
public orderByRaw() {
public orderByRaw(): this {
throw new NotImplementedMethodException(this.orderByRaw.name, 'mongo')
}

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from '#src/types/Operations'
export * from '#src/types/Connections'
export * from '#src/types/ConnectionOptions'
export * from '#src/types/columns/ColumnType'
export * from '#src/types/relations/Relation'
export * from '#src/types/columns/ModelColumns'
export * from '#src/types/columns/ColumnOptions'

Expand Down
12 changes: 12 additions & 0 deletions src/types/relations/Relation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* @athenna/database
*
* (c) João Lenon <lenon@athenna.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import type { BaseModel } from '#src/models/BaseModel'

export type Relation<M extends BaseModel | BaseModel[]> = M
8 changes: 5 additions & 3 deletions tests/fixtures/models/e2e/Course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
* file that was distributed with this source code.
*/

import type { Relation } from '#src/types'
import { BaseModel } from '#src/models/BaseModel'
import { Column } from '#src/models/annotations/Column'
import { type Student } from '#tests/fixtures/models/e2e/Student'
import { Student } from '#tests/fixtures/models/e2e/Student'
import { BelongsToMany } from '#src/models/annotations/BelongsToMany'
import { StudentsCourses } from '#tests/fixtures/models/e2e/StudentsCourses'

export class Course extends BaseModel {
public static connection() {
Expand All @@ -23,6 +25,6 @@ export class Course extends BaseModel {
@Column()
public name: string

@BelongsToMany('Student', 'StudentsCourses')
public students: Student[]
@BelongsToMany(() => Student, () => StudentsCourses)
public students: Relation<Student[]>
}
7 changes: 4 additions & 3 deletions tests/fixtures/models/e2e/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* file that was distributed with this source code.
*/

import type { Relation } from '#src/types'
import { BaseModel } from '#src/models/BaseModel'
import { User } from '#tests/fixtures/models/e2e/User'
import { Column } from '#src/models/annotations/Column'
import { type User } from '#tests/fixtures/models/e2e/User'
import { BelongsTo } from '#src/models/annotations/BelongsTo'

export class Product extends BaseModel {
Expand All @@ -23,8 +24,8 @@ export class Product extends BaseModel {
@Column()
public userId: number

@BelongsTo('User')
public user: User
@BelongsTo(() => User)
public user: Relation<User>

@Column({ isCreateDate: true })
public createdAt: Date
Expand Down
7 changes: 4 additions & 3 deletions tests/fixtures/models/e2e/Profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* file that was distributed with this source code.
*/

import type { Relation } from '#src/types'
import { BaseModel } from '#src/models/BaseModel'
import { User } from '#tests/fixtures/models/e2e/User'
import { Column } from '#src/models/annotations/Column'
import { type User } from '#tests/fixtures/models/e2e/User'
import { BelongsTo } from '#src/models/annotations/BelongsTo'

export class Profile extends BaseModel {
Expand All @@ -23,8 +24,8 @@ export class Profile extends BaseModel {
@Column()
public userId: number

@BelongsTo('User')
public user: User
@BelongsTo(() => User)
public user: Relation<User>

@Column({ isCreateDate: true })
public createdAt: Date
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/models/e2e/Student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import type { Relation } from '#src/types'
import { BaseModel } from '#src/models/BaseModel'
import { Column } from '#src/models/annotations/Column'
import { Course } from '#tests/fixtures/models/e2e/Course'
Expand All @@ -25,5 +26,5 @@ export class Student extends BaseModel {
public name: string

@BelongsToMany(() => Course, () => StudentsCourses)
public courses: Course[]
public courses: Relation<Course[]>
}
13 changes: 7 additions & 6 deletions tests/fixtures/models/e2e/StudentsCourses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
* file that was distributed with this source code.
*/

import type { Relation } from '#src/types'
import { BaseModel } from '#src/models/BaseModel'
import { Column } from '#src/models/annotations/Column'
import { Course } from '#tests/fixtures/models/e2e/Course'
import { Student } from '#tests/fixtures/models/e2e/Student'
import { BelongsTo } from '#src/models/annotations/BelongsTo'
import { type Course } from '#tests/fixtures/models/e2e/Course'
import { type Student } from '#tests/fixtures/models/e2e/Student'

export class StudentsCourses extends BaseModel {
public static connection() {
Expand All @@ -24,12 +25,12 @@ export class StudentsCourses extends BaseModel {
@Column()
public courseId: number

@BelongsTo('Course')
public course: Course
@BelongsTo(() => Course)
public course: Relation<Course>

@Column()
public studentId: number

@BelongsTo('Student')
public student: Student
@BelongsTo(() => Student)
public student: Relation<Student>
}
5 changes: 3 additions & 2 deletions tests/fixtures/models/e2e/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file that was distributed with this source code.
*/

import type { Relation } from '#src/types'
import { BaseModel } from '#src/models/BaseModel'
import { Column } from '#src/models/annotations/Column'
import { HasOne } from '#src/models/annotations/HasOne'
Expand All @@ -26,10 +27,10 @@ export class User extends BaseModel {
public name: string

@HasOne(() => Profile)
public profile: Profile
public profile: Relation<Profile>

@HasMany(() => Product)
public products: Product[]
public products: Relation<Product[]>

@Column({ isCreateDate: true })
public createdAt: Date
Expand Down

0 comments on commit 65be1bf

Please sign in to comment.