chore: abstract the modules repository (#6035)
**What** Reduce the work effort to create repositories when building new modules by abstracting the most common cases into the base class default implementation returned by a factory - [x] Migrate all modules Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com>
This commit is contained in:
co-authored by
Riqwan Thamir
parent
ef5024980d
commit
b6ac768698
@@ -1,77 +1,22 @@
|
||||
import { Context, FindOptions, ModuleJoinerConfig } from "@medusajs/types"
|
||||
import {
|
||||
EntitySchema,
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
import { Context, ModuleJoinerConfig } from "@medusajs/types"
|
||||
import { EntitySchema } from "@mikro-orm/core"
|
||||
|
||||
import {
|
||||
MikroOrmAbstractBaseRepository,
|
||||
generateEntityId,
|
||||
mikroOrmBaseRepositoryFactory,
|
||||
} from "@medusajs/utils"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export function getLinkRepository(model: EntitySchema) {
|
||||
return class LinkRepository extends MikroOrmAbstractBaseRepository<object> {
|
||||
readonly manager_: SqlEntityManager
|
||||
readonly model_: EntitySchema
|
||||
return class LinkRepository extends mikroOrmBaseRepositoryFactory(model) {
|
||||
readonly joinerConfig_: ModuleJoinerConfig
|
||||
|
||||
constructor({
|
||||
manager,
|
||||
joinerConfig,
|
||||
}: {
|
||||
manager: SqlEntityManager
|
||||
joinerConfig: ModuleJoinerConfig
|
||||
}) {
|
||||
constructor({ joinerConfig }: { joinerConfig: ModuleJoinerConfig }) {
|
||||
// @ts-ignore
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
this.model_ = model
|
||||
this.joinerConfig_ = joinerConfig
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: FindOptions<unknown> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<unknown[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
this.model_,
|
||||
findOptions_.where as MikroFilterQuery<unknown>,
|
||||
findOptions_.options as MikroOptions<any>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: FindOptions<object> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[object[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
this.model_,
|
||||
findOptions_.where as MikroFilterQuery<unknown>,
|
||||
findOptions_.options as MikroOptions<any>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(data: any, context: Context = {}): Promise<void> {
|
||||
const filter = {}
|
||||
for (const key in data) {
|
||||
@@ -81,7 +26,7 @@ export function getLinkRepository(model: EntitySchema) {
|
||||
}
|
||||
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(this.model_, data, {})
|
||||
await manager.nativeDelete(model, data, {})
|
||||
}
|
||||
|
||||
async create(data: object[], context: Context = {}): Promise<object[]> {
|
||||
@@ -93,10 +38,10 @@ export function getLinkRepository(model: EntitySchema) {
|
||||
this.joinerConfig_.databaseConfig?.idPrefix ?? "link"
|
||||
)
|
||||
|
||||
return manager.create(this.model_, link)
|
||||
return manager.create(model, link)
|
||||
})
|
||||
|
||||
await manager.upsertMany(this.model_, links)
|
||||
await manager.upsertMany(model, links)
|
||||
|
||||
return links
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user