Skip to content

Instantly share code, notes, and snippets.

@ambrizals
Created February 16, 2022 14:05
Show Gist options
  • Save ambrizals/85aac9d1ca3e69425b9834f259059f0c to your computer and use it in GitHub Desktop.
Save ambrizals/85aac9d1ca3e69425b9834f259059f0c to your computer and use it in GitHub Desktop.
hehehe pusing cuk
import {
ExtractModelRelations,
LucidModel,
ModelObject,
ModelQueryBuilderContract,
RelationQueryBuilderContract,
} from '@ioc:Adonis/Lucid/Orm'
import PaginationHelper from 'App/Helpers/PaginationHelpers'
import { ResponseContract } from '@ioc:Adonis/Core/Response'
// import Profile from 'App/Models/Profile'
export default abstract class Repository<T extends LucidModel> {
public model: T
public query: ModelQueryBuilderContract<T>
public pagination: PaginationHelper
public _qs?: Record<string, any> | undefined
public get qs(): Record<string, any> | undefined {
return this._qs
}
public set qs(value: Record<string, any> | undefined) {
this._qs = value
}
public async findOrFail(id: number) {
return this.model.findOrFail(id)
}
public findByOrFail(key: string, value: any) {
return this.model.findByOrFail(key, value)
}
/**
* Preload relationship row
*
* @param relation
* @returns
*/
public preload(
relation: ExtractModelRelations<InstanceType<T>>,
callback?: (callback: RelationQueryBuilderContract<T, any>) => void
) {
this.query.preload(relation, callback)
return this
}
/**
* Must override this method filter to use
*
*/
public filter() {
throw 'Not implemented'
}
/**
* sort query row result
*
* @param field
* @param direction
* @returns
*/
public orderBy(field: string, direction: 'asc' | 'desc') {
this.query.orderBy(field, direction)
return this
}
/**
*
* Fetch data from query
*
* @returns Promise<ModelObject[]>
*/
public async fetch(response?: ResponseContract): Promise<ModelObject[]> {
return await this.pagination.fetch(this.query, response)
}
public select(fields: string[]) {
this.query.select(fields)
return this
}
}
// TODO: This code is work as expected
// interface BaseRepository {
// findOrFail<T extends Repository<LucidModel>>(
// this: new (...args: any[]) => T,
// id: number
// ): Promise<LucidRow>
// }
// class CobaGituRepository extends Repository<typeof Profile> implements BaseRepository {
// public static async findOrFail<T extends Repository<typeof Profile>>(
// this: new (...args: any[]) => T,
// id: number
// ) {
// let instance = new this()
// return instance.findOrFail(id)
// }
// }
// const coba = async () => {
// const coba = await CobaGituRepository.findOrFail(1)
// }
// TODO: This code is work as expected, but requiring very long generic types
// class CobaDoloRepository extends Repository<typeof Profile> {}
// const coba = async () => {
// const data = await CobaDoloRepository.findOrFail<typeof Profile, Repository<typeof Profile>>(1)
// console.log(data.fullName)
// }
// TODO: This code is work as expected, but to achive iam need to create an object
// class YaakRepository extends AbstractBaseRepository<typeof Profile> {
// public model = Profile
// }
// const coba = async () => {
// const repository = new YaakRepository()
// const data = await repository.findOrFail(1)
// }
// TODO: This code is actualy work but return types is not iam expected
// Example 1
// class YaakRepository extends AbstractBaseRepository<typeof Profile> {
// public model = Profile
// }
// const coba = async () => {
// const data = await YaakRepository.findOrFail(1)
// }
// Example 2
// class CobaBaseRepository<Model extends LucidModel> extends AbstractBaseRepository<Model> {
// public static async findOrFail<Model extends LucidModel, T extends CobaBaseRepository<Model>>(
// this: new (...args: any[]) => T,
// id: number
// ) {
// let instance = new this()
// return instance.model.findOrFail(id)
// }
// }
// class GiniRepository extends CobaBaseRepository<typeof Profile> {
// public model = Profile
// }
// const coba = async () => {
// const data = await GiniRepository.findOrFail(1)
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment