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

@@ -0,0 +1,27 @@
export const InventoryModule = {
__definition: {
key: "inventoryService",
registrationName: "inventoryService",
defaultPackage: false,
label: "InventoryService",
isRequired: false,
canOverride: true,
isQueryable: true,
dependencies: [],
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
serviceName: "inventoryService",
primaryKeys: ["id"],
linkableKeys: [
"inventory_item_id",
"inventory_level_id",
"reservation_item_id",
],
},
softDelete: jest.fn(() => {}),
}

View File

@@ -0,0 +1,71 @@
export const InventoryStockLocationLink = {
__definition: {
key: "inventoryStockLocationLink",
registrationName: "inventoryStockLocationLink",
defaultPackage: "",
label: "inventoryStockLocationLink",
canOverride: true,
isRequired: false,
isQueryable: true,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
serviceName: "inventoryStockLocationLink",
isLink: true,
alias: [
{
name: "inventory_level_stock_location",
},
{
name: "inventory_level_stock_locations",
},
],
primaryKeys: ["inventory_level_id", "stock_location_id"],
relationships: [
{
serviceName: "inventoryService",
primaryKey: "id",
foreignKey: "inventory_level_id",
alias: "inventory_level",
args: {},
},
{
serviceName: "stockLocationService",
primaryKey: "id",
foreignKey: "stock_location_id",
alias: "stock_location",
},
],
extends: [
{
serviceName: "inventoryService",
relationship: {
serviceName: "inventoryStockLocationLink",
primaryKey: "inventory_level_id",
foreignKey: "id",
alias: "inventory_location_items",
},
},
{
serviceName: "stockLocationService",
relationship: {
serviceName: "inventoryStockLocationLink",
primaryKey: "stock_location_id",
foreignKey: "id",
alias: "inventory_location_items",
},
},
],
},
create: jest.fn(
async (
primaryKeyOrBulkData: string | string[] | [string | string[], string][],
foreignKeyData?: string
) => {}
),
softDelete: jest.fn(() => {}),
}

View File

@@ -0,0 +1,77 @@
export const ProductInventoryLinkModule = {
__definition: {
key: "productVariantInventoryInventoryItemLink",
registrationName: "productVariantInventoryInventoryItemLink",
defaultPackage: "",
label: "productVariantInventoryInventoryItemLink",
canOverride: true,
isRequired: false,
isQueryable: true,
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
serviceName: "productVariantInventoryInventoryItemLink",
isLink: true,
databaseConfig: {
tableName: "product_variant_inventory_item",
},
alias: [
{
name: "product_variant_inventory_item",
},
{
name: "product_variant_inventory_items",
},
],
primaryKeys: ["variant_id", "inventory_item_id"],
relationships: [
{
serviceName: "productService",
primaryKey: "id",
foreignKey: "variant_id",
alias: "variant",
args: {},
deleteCascade: true,
},
{
serviceName: "inventoryService",
primaryKey: "id",
foreignKey: "inventory_item_id",
alias: "inventory",
deleteCascade: true,
},
],
extends: [
{
serviceName: "productService",
relationship: {
serviceName: "productVariantInventoryInventoryItemLink",
primaryKey: "variant_id",
foreignKey: "id",
alias: "inventory_items",
isList: true,
},
},
{
serviceName: "inventoryService",
relationship: {
serviceName: "productVariantInventoryInventoryItemLink",
primaryKey: "inventory_item_id",
foreignKey: "id",
alias: "variant_link",
},
},
],
},
create: jest.fn(
async (
primaryKeyOrBulkData: string | string[] | [string | string[], string][],
foreignKeyData?: string
) => {}
),
softDelete: jest.fn(() => {}),
}

View File

@@ -0,0 +1,24 @@
export const ProductModule = {
__definition: {
key: "productService",
registrationName: "productModuleService",
defaultPackage: false,
label: "ProductModuleService",
isRequired: false,
canOverride: true,
isQueryable: true,
dependencies: ["eventBusModuleService"],
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
serviceName: "productService",
primaryKeys: ["id", "handle"],
linkableKeys: ["product_id", "variant_id"],
alias: [],
},
softDelete: jest.fn(() => {}),
}

View File

@@ -0,0 +1,24 @@
export const StockLocationModule = {
__definition: {
key: "stockLocationService",
registrationName: "stockLocationService",
defaultPackage: false,
label: "StockLocationService",
isRequired: false,
canOverride: true,
isQueryable: true,
dependencies: ["eventBusService"],
defaultModuleDeclaration: {
scope: "internal",
resources: "shared",
},
},
__joinerConfig: {
serviceName: "stockLocationService",
primaryKeys: ["id"],
linkableKeys: ["stock_location_id"],
alias: [],
},
softDelete: jest.fn(() => {}),
}