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:
Carlos R. L. Rodrigues
2023-08-30 11:31:32 -03:00
committed by GitHub
parent bc4c9e0d32
commit 4d16acf5f0
97 changed files with 3540 additions and 424 deletions

View File

@@ -1,6 +1,12 @@
import { Context, DAL, RepositoryTransformOptions } from "@medusajs/types"
import {
Context,
DAL,
FilterQuery,
RepositoryTransformOptions,
} from "@medusajs/types"
import { isString } from "../../common"
import { MedusaContext } from "../../decorators"
import { buildQuery, InjectTransactionManager } from "../../modules-sdk"
import { InjectTransactionManager, buildQuery } from "../../modules-sdk"
import {
getSoftDeletedCascadedEntitiesIdsMappedBy,
transactionWrapper,
@@ -68,11 +74,21 @@ export abstract class MikroOrmAbstractBaseRepository<T = any>
@InjectTransactionManager()
async softDelete(
ids: string[],
idsOrFilter: string[] | FilterQuery,
@MedusaContext()
{ transactionManager: manager }: Context = {}
): Promise<[T[], Record<string, unknown[]>]> {
const entities = await this.find({ where: { id: { $in: ids } } as any })
const isArray = Array.isArray(idsOrFilter)
const filter =
isArray || isString(idsOrFilter)
? {
id: {
$in: isArray ? idsOrFilter : [idsOrFilter],
},
}
: idsOrFilter
const entities = await this.find({ where: filter as any })
const date = new Date()
await mikroOrmUpdateDeletedAtRecursively(manager, entities, date)
@@ -86,22 +102,34 @@ export abstract class MikroOrmAbstractBaseRepository<T = any>
@InjectTransactionManager()
async restore(
ids: string[],
idsOrFilter: string[] | FilterQuery,
@MedusaContext()
{ transactionManager: manager }: Context = {}
): Promise<T[]> {
const query = buildQuery(
{ id: { $in: ids } },
{
withDeleted: true,
}
)
): Promise<[T[], Record<string, unknown[]>]> {
const isArray = Array.isArray(idsOrFilter)
const filter =
isArray || isString(idsOrFilter)
? {
id: {
$in: isArray ? idsOrFilter : [idsOrFilter],
},
}
: idsOrFilter
const query = buildQuery(filter, {
withDeleted: true,
})
const entities = await this.find(query)
await mikroOrmUpdateDeletedAtRecursively(manager, entities, null)
return entities
const softDeletedEntitiesMap = getSoftDeletedCascadedEntitiesIdsMappedBy({
entities,
restored: true,
})
return [entities, softDeletedEntitiesMap]
}
}

View File

@@ -54,7 +54,10 @@ export abstract class AbstractBaseRepository<T = any>
context?: Context
): Promise<[T[], Record<string, unknown[]>]>
abstract restore(ids: string[], context?: Context): Promise<T[]>
abstract restore(
ids: string[],
context?: Context
): Promise<[T[], Record<string, unknown[]>]>
abstract getFreshManager<TManager = unknown>(): TManager

View File

@@ -47,10 +47,12 @@ export function getSoftDeletedCascadedEntitiesIdsMappedBy({
entities,
deletedEntitiesMap,
getEntityName,
restored,
}: {
entities: any[]
deletedEntitiesMap?: Map<string, any[]>
getEntityName?: (entity: any) => string
restored?: boolean
}): Record<string, any[]> {
deletedEntitiesMap ??= new Map<string, any[]>()
getEntityName ??= (entity) => entity.constructor.name
@@ -61,7 +63,7 @@ export function getSoftDeletedCascadedEntitiesIdsMappedBy({
.get(entityName)
?.some((e) => e.id === entity.id)
if (!entity.deleted_at || shouldSkip) {
if ((restored ? !!entity.deleted_at : !entity.deleted_at) || shouldSkip) {
continue
}

View File

@@ -3,15 +3,19 @@ import { loadDatabaseConfig } from "../load-module-database-config"
describe("loadDatabaseConfig", function () {
afterEach(() => {
delete process.env.POSTGRES_URL
delete process.env.MEDUSA_POSTGRES_URL
delete process.env.PRODUCT_POSTGRES_URL
})
it("should return the local configuration using the environment variable", function () {
process.env.POSTGRES_URL = "postgres://localhost:5432/medusa"
it("should return the local configuration using the environment variable respecting their precedence", function () {
process.env.MEDUSA_POSTGRES_URL = "postgres://localhost:5432/medusa"
process.env.PRODUCT_POSTGRES_URL = "postgres://localhost:5432/product"
process.env.POSTGRES_URL = "postgres://localhost:5432/share_db"
let config = loadDatabaseConfig("product")
expect(config).toEqual({
clientUrl: process.env.POSTGRES_URL,
clientUrl: process.env.PRODUCT_POSTGRES_URL,
driverOptions: {
connection: {
ssl: false,
@@ -21,12 +25,25 @@ describe("loadDatabaseConfig", function () {
schema: "",
})
delete process.env.POSTGRES_URL
process.env.PRODUCT_POSTGRES_URL = "postgres://localhost:5432/medusa"
delete process.env.PRODUCT_POSTGRES_URL
config = loadDatabaseConfig("product")
expect(config).toEqual({
clientUrl: process.env.PRODUCT_POSTGRES_URL,
clientUrl: process.env.MEDUSA_POSTGRES_URL,
driverOptions: {
connection: {
ssl: false,
},
},
debug: false,
schema: "",
})
delete process.env.MEDUSA_POSTGRES_URL
config = loadDatabaseConfig("product")
expect(config).toEqual({
clientUrl: process.env.POSTGRES_URL,
driverOptions: {
connection: {
ssl: false,
@@ -127,7 +144,7 @@ describe("loadDatabaseConfig", function () {
}
expect(error.message).toEqual(
"No database clientUrl provided. Please provide the clientUrl through the PRODUCT_POSTGRES_URL or POSTGRES_URL environment variable or the options object in the initialize function."
"No database clientUrl provided. Please provide the clientUrl through the [MODULE]_POSTGRES_URL, MEDUSA_POSTGRES_URL or POSTGRES_URL environment variable or the options object in the initialize function."
)
})
})

View File

@@ -1,9 +1,11 @@
import { MedusaError } from "../common"
import { ModulesSdkTypes } from "@medusajs/types"
import { MedusaError } from "../common"
function getEnv(key: string, moduleName: string): string {
const value =
process.env[`${moduleName.toUpperCase()}_${key}`] ?? process.env[`${key}`]
process.env[`${moduleName.toUpperCase()}_${key}`] ??
process.env[`MEDUSA_${key}`] ??
process.env[`${key}`]
return value ?? ""
}
@@ -39,6 +41,16 @@ function getDefaultDriverOptions(clientUrl: string) {
: {}
}
function getDatabaseUrl(
config: ModulesSdkTypes.ModuleServiceInitializeOptions
): string {
const { clientUrl, host, port, user, password, database } = config.database!
if (clientUrl) {
return clientUrl
}
return `postgres://${user}:${password}@${host}:${port}/${database}`
}
/**
* Load the config for the database connection. The options can be retrieved
* e.g through PRODUCT_* (e.g PRODUCT_POSTGRES_URL) or * (e.g POSTGRES_URL) environment variables or the options object.
@@ -49,11 +61,14 @@ export function loadDatabaseConfig(
moduleName: string,
options?: ModulesSdkTypes.ModuleServiceInitializeOptions,
silent: boolean = false
): ModulesSdkTypes.ModuleServiceInitializeOptions["database"] {
): Pick<
ModulesSdkTypes.ModuleServiceInitializeOptions["database"],
"clientUrl" | "schema" | "driverOptions" | "debug"
> {
const clientUrl = getEnv("POSTGRES_URL", moduleName)
const database = {
clientUrl: getEnv("POSTGRES_URL", moduleName),
clientUrl,
schema: getEnv("POSTGRES_SCHEMA", moduleName) ?? "public",
driverOptions: JSON.parse(
getEnv("POSTGRES_DRIVER_OPTIONS", moduleName) ||
@@ -63,7 +78,7 @@ export function loadDatabaseConfig(
}
if (isModuleServiceInitializeOptions(options)) {
database.clientUrl = options.database!.clientUrl ?? database.clientUrl
database.clientUrl = getDatabaseUrl(options)
database.schema = options.database!.schema ?? database.schema
database.driverOptions =
options.database!.driverOptions ??
@@ -74,7 +89,7 @@ export function loadDatabaseConfig(
if (!database.clientUrl && !silent) {
throw new MedusaError(
MedusaError.Types.INVALID_ARGUMENT,
"No database clientUrl provided. Please provide the clientUrl through the PRODUCT_POSTGRES_URL or POSTGRES_URL environment variable or the options object in the initialize function."
"No database clientUrl provided. Please provide the clientUrl through the [MODULE]_POSTGRES_URL, MEDUSA_POSTGRES_URL or POSTGRES_URL environment variable or the options object in the initialize function."
)
}

View File

@@ -51,7 +51,6 @@ export async function mikroOrmConnectionLoader({
const shouldSwallowError = !!(
options as ModulesSdkTypes.ModuleServiceInitializeOptions
)?.database?.connection
dbConfig = {
...loadDatabaseConfig(
"product",
@@ -97,7 +96,7 @@ async function loadShared({ container, entities }) {
)
if (!sharedConnection) {
throw new Error(
"The module is setup to use a shared resources but no shared connection is present. A new connection will be created"
"The module is setup to use a shared resources but no shared connection is present."
)
}