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>
113 lines
2.7 KiB
TypeScript
113 lines
2.7 KiB
TypeScript
import {
|
|
Logger,
|
|
MedusaContainer,
|
|
MODULE_RESOURCE_TYPE,
|
|
MODULE_SCOPE,
|
|
ModulesSdkTypes,
|
|
} from "@medusajs/types"
|
|
import { PostgreSqlDriver, SqlEntityManager } from "@mikro-orm/postgresql"
|
|
import { asValue } from "awilix"
|
|
import { ContainerRegistrationKeys, MedusaError } from "../../common"
|
|
import { mikroOrmCreateConnection } from "../../dal"
|
|
import { loadDatabaseConfig } from "../load-module-database-config"
|
|
|
|
export async function mikroOrmConnectionLoader({
|
|
container,
|
|
options,
|
|
moduleDeclaration,
|
|
entities,
|
|
}: {
|
|
entities: any[]
|
|
container: MedusaContainer
|
|
options?:
|
|
| ModulesSdkTypes.ModuleServiceInitializeOptions
|
|
| ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
|
moduleDeclaration?: ModulesSdkTypes.InternalModuleDeclaration
|
|
logger?: Logger
|
|
}) {
|
|
let manager = (
|
|
options as ModulesSdkTypes.ModuleServiceInitializeCustomDataLayerOptions
|
|
)?.manager
|
|
|
|
// Custom manager provided
|
|
if (manager) {
|
|
container.register({
|
|
manager: asValue(manager),
|
|
})
|
|
return
|
|
}
|
|
|
|
if (
|
|
moduleDeclaration?.scope === MODULE_SCOPE.INTERNAL &&
|
|
moduleDeclaration.resources === MODULE_RESOURCE_TYPE.SHARED
|
|
) {
|
|
return await loadShared({ container, entities })
|
|
}
|
|
|
|
/**
|
|
* Reuse an existing connection if it is passed in the options
|
|
*/
|
|
let dbConfig
|
|
const shouldSwallowError = !!(
|
|
options as ModulesSdkTypes.ModuleServiceInitializeOptions
|
|
)?.database?.connection
|
|
dbConfig = {
|
|
...loadDatabaseConfig(
|
|
"product",
|
|
(options ?? {}) as ModulesSdkTypes.ModuleServiceInitializeOptions,
|
|
shouldSwallowError
|
|
),
|
|
connection: (options as ModulesSdkTypes.ModuleServiceInitializeOptions)
|
|
?.database?.connection,
|
|
}
|
|
|
|
manager ??= await loadDefault({
|
|
database: dbConfig,
|
|
entities,
|
|
})
|
|
|
|
container.register({
|
|
manager: asValue(manager),
|
|
})
|
|
}
|
|
|
|
async function loadDefault({
|
|
database,
|
|
entities,
|
|
}): Promise<SqlEntityManager<PostgreSqlDriver>> {
|
|
if (!database) {
|
|
throw new MedusaError(
|
|
MedusaError.Types.INVALID_ARGUMENT,
|
|
`Database config is not present at module config "options.database"`
|
|
)
|
|
}
|
|
|
|
const orm = await mikroOrmCreateConnection(database, entities)
|
|
|
|
return orm.em.fork()
|
|
}
|
|
|
|
async function loadShared({ container, entities }) {
|
|
const sharedConnection = container.resolve(
|
|
ContainerRegistrationKeys.PG_CONNECTION,
|
|
{
|
|
allowUnregistered: true,
|
|
}
|
|
)
|
|
if (!sharedConnection) {
|
|
throw new Error(
|
|
"The module is setup to use a shared resources but no shared connection is present."
|
|
)
|
|
}
|
|
|
|
const manager = await loadDefault({
|
|
entities,
|
|
database: {
|
|
connection: sharedConnection,
|
|
},
|
|
})
|
|
container.register({
|
|
manager: asValue(manager),
|
|
})
|
|
}
|