Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Create wrong name for ModuleService methods #10004

Closed
FarNys opened this issue Nov 10, 2024 · 4 comments
Closed

[Bug]: Create wrong name for ModuleService methods #10004

FarNys opened this issue Nov 10, 2024 · 4 comments

Comments

@FarNys
Copy link

FarNys commented Nov 10, 2024

Package.json file

{
  "name": "medusa-starter-default",
  "version": "0.0.1",
  "description": "A starter for Medusa projects.",
  "author": "Medusa (https://medusajs.com)",
  "license": "MIT",
  "keywords": [
    "sqlite",
    "postgres",
    "typescript",
    "ecommerce",
    "headless",
    "medusa"
  ],
  "scripts": {
    "build": "medusa build",
    "seed": "medusa exec ./src/scripts/seed.ts",
    "start": "medusa start",
    "dev": "medusa develop",
    "test:integration:http": "TEST_TYPE=integration:http NODE_OPTIONS=--experimental-vm-modules jest --silent=false --runInBand --forceExit",
    "test:integration:modules": "TEST_TYPE=integration:modules NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit",
    "test:unit": "TEST_TYPE=unit NODE_OPTIONS=--experimental-vm-modules jest --silent --runInBand --forceExit"
  },
  "dependencies": {
    "@medusajs/admin-sdk": "latest",
    "@medusajs/cli": "latest",
    "@medusajs/framework": "latest",
    "@medusajs/icons": "^2.0.1",
    "@medusajs/medusa": "latest",
    "@mikro-orm/core": "5.9.7",
    "@mikro-orm/knex": "5.9.7",
    "@mikro-orm/migrations": "5.9.7",
    "@mikro-orm/postgresql": "5.9.7",
    "awilix": "^8.0.1",
    "pg": "^8.13.0"
  },
  "devDependencies": {
    "@medusajs/test-utils": "latest",
    "@mikro-orm/cli": "5.9.7",
    "@swc/core": "1.5.7",
    "@swc/jest": "^0.2.36",
    "@types/jest": "^29.5.13",
    "@types/node": "^20.0.0",
    "@types/react": "^18.3.2",
    "@types/react-dom": "^18.2.25",
    "jest": "^29.7.0",
    "prop-types": "^15.8.1",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "ts-node": "^10.9.2",
    "typescript": "^5.6.2",
    "vite": "^5.2.11"
  },
  "engines": {
    "node": ">=20"
  }
}

Node.js version

v22.9.0

Database and its version

14.4

Operating system name and version

Win10

Browser name

What happended?

I create a brand module step by step from medusa instructions and it works well, but when i try to create a Module named Media to save address of uploaded images for later use, it throws type error on all default medusa generated function like (TypeError: mediaModuleService.listAndCountMedias is not a function, TypeError: mediaModuleService.createMedias is not a function).

after some struggle i find the problem, something related to generate naming of functions by medusa is wrong, medusa tries to create collective nouns for medusa services methods, but at least for media that doesn't work!

how to fix this?
just drop "s" from end of the name of auto generated methods and it works.

for example:
what medusa create: await mediaModuleService.listAndCountMedias()
what works : await mediaModuleService.listAndCountMedia()

(default generated methods worked for brand)

Codes to reproduce:

modules/media/models/media.ts

import { model } from "@medusajs/framework/utils";

export const Media = model.define("media", {
  id: model.id().primaryKey(),
  name: model.text().searchable(),
  url: model.text(),
});

modules/media/service.ts

import { MedusaService } from "@medusajs/framework/utils";
import { Media } from "./models/media";

class MediaModuleService extends MedusaService({
  Media,
}) {}

export default MediaModuleService;

modules/media/index.ts

import { Module } from "@medusajs/framework/utils";
import MediaModuleService from "./service";

export const MEDIA_MODULE = "mediaModuleService";

export default Module(MEDIA_MODULE, {
  service: MediaModuleService,
});

medusa-config.ts

import { loadEnv, defineConfig } from "@medusajs/framework/utils";

loadEnv(process.env.NODE_ENV || "development", process.cwd());

module.exports = defineConfig({
  projectConfig: {
    databaseUrl: process.env.DATABASE_URL,
    http: {
      storeCors: process.env.STORE_CORS!,
      adminCors: process.env.ADMIN_CORS!,
      authCors: process.env.AUTH_CORS!,
      jwtSecret: process.env.JWT_SECRET || "supersecret",
      cookieSecret: process.env.COOKIE_SECRET || "supersecret",
    },
  },
  modules: [
    {
      resolve: "./src/modules/brand",
    },
    {
      resolve: "./src/modules/media",
    },
  ],
});

Expected behavior

it must create correct types or names for ModuleService methods.

Actual behavior

it throws type error when you try to use ModuleService methods.

Link to reproduction repo

https://github.com/FarNys/medusa2_backend

@olivermrbl
Copy link
Contributor

olivermrbl commented Nov 10, 2024

We use pluralize to infer the singular and plural forms of the data models when creating the service methods. The plural form of media is media, so this is expected behavior. Or rather, the noun media does not have a plural form, as per their source code.

@FarNys
Copy link
Author

FarNys commented Nov 10, 2024

Thank you for response.

You are right. but typing system generate wrong suggestion for methods.

medua_type_error

Copy link
Contributor

We will look into fixing this!

@thetutlage
Copy link
Contributor

Fixed by #10011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants