|
1
|
import { Context, ModuleJoinerConfig } from "@medusajs/framework/types" |
|
2
|
import { EntitySchema } from "@medusajs/framework/mikro-orm/core" |
|
3
|
|
|
4
|
import { |
|
5
|
generateEntityId, |
|
6
|
mikroOrmBaseRepositoryFactory, |
|
7
|
} from "@medusajs/framework/utils" |
|
8
|
import { SqlEntityManager } from "@medusajs/framework/mikro-orm/postgresql" |
|
9
|
|
|
10
|
export function getLinkRepository(model: EntitySchema) { |
|
11
|
return class LinkRepository extends mikroOrmBaseRepositoryFactory(model) { |
|
12
|
readonly joinerConfig_: ModuleJoinerConfig |
|
13
|
|
|
14
|
constructor({ joinerConfig }: { joinerConfig: ModuleJoinerConfig }) { |
|
15
|
// @ts-ignore |
|
16
|
super(...arguments) |
|
17
|
this.joinerConfig_ = joinerConfig |
|
18
|
} |
|
19
|
|
|
20
|
async delete( |
|
21
|
data: any, |
|
22
|
context: Context = {} |
|
23
|
): Promise<string[] | Record<string, any>[]> { |
|
24
|
const filter = {} |
|
25
|
for (const key in data) { |
|
26
|
filter[key] = { |
|
27
|
$in: Array.isArray(data[key]) ? data[key] : [data[key]], |
|
28
|
} |
|
29
|
} |
|
30
|
|
|
31
|
return await super.delete(filter, context) |
|
32
|
} |
|
33
|
|
|
34
|
async create(data: object[], context: Context = {}): Promise<object[]> { |
|
35
|
const manager = this.getActiveManager<SqlEntityManager>(context) |
|
36
|
|
|
37
|
const links = data.map((link: any) => { |
|
38
|
link.id = generateEntityId( |
|
39
|
link.id, |
|
40
|
this.joinerConfig_.databaseConfig?.idPrefix ?? "link" |
|
41
|
) |
|
42
|
link.deleted_at = null |
|
43
|
return manager.create(model, link) |
|
44
|
}) |
|
45
|
|
|
46
|
await manager.upsertMany(model, links) |
|
47
|
|
|
48
|
return links |
|
49
|
} |
|
50
|
} |
|
51
|
} |