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,86 +1,16 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { AuthProvider } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
import { Context } from "@medusajs/types"
|
||||
|
||||
export class AuthProviderRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
export class AuthProviderRepository extends DALUtils.mikroOrmBaseRepositoryFactory<
|
||||
AuthProvider,
|
||||
{
|
||||
create: RepositoryTypes.CreateAuthProviderDTO
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<AuthProvider> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<AuthProvider[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
AuthProvider,
|
||||
findOptions_.where as MikroFilterQuery<AuthProvider>,
|
||||
findOptions_.options as MikroOptions<AuthProvider>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<AuthProvider> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[AuthProvider[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
AuthProvider,
|
||||
findOptions_.where as MikroFilterQuery<AuthProvider>,
|
||||
findOptions_.options as MikroOptions<AuthProvider>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(AuthProvider, { provider: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
async create(
|
||||
data: RepositoryTypes.CreateAuthProviderDTO[],
|
||||
context: Context = {}
|
||||
): Promise<AuthProvider[]> {
|
||||
const manager: SqlEntityManager =
|
||||
this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const authProviders = data.map((authProviderData) => {
|
||||
return manager.create(AuthProvider, authProviderData)
|
||||
})
|
||||
|
||||
manager.persist(authProviders)
|
||||
|
||||
return authProviders
|
||||
}
|
||||
|
||||
>(AuthProvider, "provider") {
|
||||
async update(
|
||||
data: RepositoryTypes.UpdateAuthProviderDTO[],
|
||||
context: Context = {}
|
||||
|
||||
@@ -1,77 +1,17 @@
|
||||
import { Context, DAL } from "@medusajs/types"
|
||||
import { Context } from "@medusajs/types"
|
||||
import { DALUtils } from "@medusajs/utils"
|
||||
import {
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
LoadStrategy,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import { AuthUser } from "@models"
|
||||
import { RepositoryTypes } from "@types"
|
||||
import { SqlEntityManager } from "@mikro-orm/postgresql"
|
||||
|
||||
export class AuthUserRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
protected readonly manager_: SqlEntityManager
|
||||
|
||||
constructor({ manager }: { manager: SqlEntityManager }) {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line prefer-rest-params
|
||||
super(...arguments)
|
||||
this.manager_ = manager
|
||||
}
|
||||
|
||||
async find(
|
||||
findOptions: DAL.FindOptions<AuthUser> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<AuthUser[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.find(
|
||||
AuthUser,
|
||||
findOptions_.where as MikroFilterQuery<AuthUser>,
|
||||
findOptions_.options as MikroOptions<AuthUser>
|
||||
)
|
||||
}
|
||||
|
||||
async findAndCount(
|
||||
findOptions: DAL.FindOptions<AuthUser> = { where: {} },
|
||||
context: Context = {}
|
||||
): Promise<[AuthUser[], number]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const findOptions_ = { ...findOptions }
|
||||
findOptions_.options ??= {}
|
||||
|
||||
Object.assign(findOptions_.options, {
|
||||
strategy: LoadStrategy.SELECT_IN,
|
||||
})
|
||||
|
||||
return await manager.findAndCount(
|
||||
AuthUser,
|
||||
findOptions_.where as MikroFilterQuery<AuthUser>,
|
||||
findOptions_.options as MikroOptions<AuthUser>
|
||||
)
|
||||
}
|
||||
|
||||
async delete(ids: string[], context: Context = {}): Promise<void> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(AuthUser, { id: { $in: ids } }, {})
|
||||
}
|
||||
|
||||
export class AuthUserRepository extends DALUtils.mikroOrmBaseRepositoryFactory(
|
||||
AuthUser
|
||||
) {
|
||||
async create(
|
||||
data: RepositoryTypes.CreateAuthUserDTO[],
|
||||
context: Context = {}
|
||||
): Promise<AuthUser[]> {
|
||||
const manager: SqlEntityManager =
|
||||
this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const toCreate = data.map((authUser) => {
|
||||
const authUserClone = { ...authUser } as any
|
||||
|
||||
@@ -80,13 +20,7 @@ export class AuthUserRepository extends DALUtils.MikroOrmBaseRepository {
|
||||
return authUserClone
|
||||
})
|
||||
|
||||
const authUsers = toCreate.map((authUserData) => {
|
||||
return manager.create(AuthUser, authUserData)
|
||||
})
|
||||
|
||||
manager.persist(authUsers)
|
||||
|
||||
return authUsers
|
||||
return await super.create(toCreate, context)
|
||||
}
|
||||
|
||||
async update(
|
||||
|
||||
Reference in New Issue
Block a user