feat(link-modules,modules-sdk, utils, types, products) - Remote Link and Link modules (#4695)
What:
- Definition of all Modules links
- `link-modules` package to manage the creation of all pre-defined link or custom ones
```typescript
import { initialize as iniInventory } from "@medusajs/inventory";
import { initialize as iniProduct } from "@medusajs/product";
import {
initialize as iniLinks,
runMigrations as migrateLinks
} from "@medusajs/link-modules";
await Promise.all([iniInventory(), iniProduct()]);
await migrateLinks(); // create tables based on previous loaded modules
await iniLinks(); // load link based on previous loaded modules
await iniLinks(undefined, [
{
serviceName: "product_custom_translation_service_link",
isLink: true,
databaseConfig: {
tableName: "product_transalations",
},
alias: [
{
name: "translations",
},
],
primaryKeys: ["id", "product_id", "translation_id"],
relationships: [
{
serviceName: Modules.PRODUCT,
primaryKey: "id",
foreignKey: "product_id",
alias: "product",
},
{
serviceName: "custom_translation_service",
primaryKey: "id",
foreignKey: "translation_id",
alias: "transalation",
deleteCascade: true,
},
],
extends: [
{
serviceName: Modules.PRODUCT,
relationship: {
serviceName: "product_custom_translation_service_link",
primaryKey: "product_id",
foreignKey: "id",
alias: "translations",
isList: true,
},
},
{
serviceName: "custom_translation_service",
relationship: {
serviceName: "product_custom_translation_service_link",
primaryKey: "product_id",
foreignKey: "id",
alias: "product_link",
},
},
],
},
]); // custom links
```
Remote Link
```typescript
import { RemoteLink, Modules } from "@medusajs/modules-sdk";
// [...] initialize modules and links
const remoteLink = new RemoteLink();
// upsert the relationship
await remoteLink.create({ // one (object) or many (array)
[Modules.PRODUCT]: {
variant_id: "var_abc",
},
[Modules.INVENTORY]: {
inventory_item_id: "iitem_abc",
},
data: { // optional additional fields
required_quantity: 5
}
});
// dismiss (doesn't cascade)
await remoteLink.dismiss({ // one (object) or many (array)
[Modules.PRODUCT]: {
variant_id: "var_abc",
},
[Modules.INVENTORY]: {
inventory_item_id: "iitem_abc",
},
});
// delete
await remoteLink.delete({
// every key is a module
[Modules.PRODUCT]: {
// every key is a linkable field
variant_id: "var_abc", // single or multiple values
},
});
// restore
await remoteLink.restore({
// every key is a module
[Modules.PRODUCT]: {
// every key is a linkable field
variant_id: "var_abc", // single or multiple values
},
});
```
Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com>
This commit is contained in:
co-authored by
Riqwan Thamir
parent
bc4c9e0d32
commit
4d16acf5f0
@@ -0,0 +1,2 @@
|
||||
export { MikroOrmBaseRepository as BaseRepository } from "@medusajs/utils"
|
||||
export { getLinkRepository } from "./link"
|
||||
@@ -0,0 +1,104 @@
|
||||
import { Context, FindOptions, ModuleJoinerConfig } from "@medusajs/types"
|
||||
import {
|
||||
EntitySchema,
|
||||
LoadStrategy,
|
||||
FilterQuery as MikroFilterQuery,
|
||||
FindOptions as MikroOptions,
|
||||
} from "@mikro-orm/core"
|
||||
|
||||
import {
|
||||
MikroOrmAbstractBaseRepository,
|
||||
generateEntityId,
|
||||
} 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
|
||||
readonly joinerConfig_: ModuleJoinerConfig
|
||||
|
||||
constructor({
|
||||
manager,
|
||||
joinerConfig,
|
||||
}: {
|
||||
manager: SqlEntityManager
|
||||
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) {
|
||||
filter[key] = {
|
||||
$in: Array.isArray(data[key]) ? data[key] : [data[key]],
|
||||
}
|
||||
}
|
||||
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
await manager.nativeDelete(this.model_, data, {})
|
||||
}
|
||||
|
||||
async create(data: object[], context: Context = {}): Promise<object[]> {
|
||||
const manager = this.getActiveManager<SqlEntityManager>(context)
|
||||
|
||||
const links = data.map((link: any) => {
|
||||
link.id = generateEntityId(
|
||||
link.id,
|
||||
this.joinerConfig_.databaseConfig?.idPrefix ?? "link"
|
||||
)
|
||||
|
||||
return manager.create(this.model_, link)
|
||||
})
|
||||
|
||||
await manager.upsertMany(this.model_, links)
|
||||
|
||||
return links
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user