#Service Factory Question

1 messages · Page 1 of 1 (latest)

carmine bluff
#

Hello, I have created a custom model and added a link to the customer module. When I added the relation: [customer] in the service factory, there is throwing the error Entity '' does not have property 'customer'. Does anything i have missing?

gleaming socket
#

can you share your code for better context

carmine bluff
#
// links/customer-custom_customer.ts
import { defineLink } from "@medusajs/framework/utils"
import CustomCustomerModule from "../modules/custom_customer"
import CustomerModule from "@medusajs/medusa/customer"

export default defineLink(
  CustomerModule.linkable.customer,
  CustomCustomerModule.linkable.customCustomer
)
// modules/custom_customer/server.ts
import { MedusaService } from "@medusajs/framework/utils";
import CustomCustomer from "./models/custom_customer";

class CustomCustomerService extends MedusaService({
  CustomCustomer,
}) {}

export default CustomCustomerService;
// modules/custom_customer/custom_customer.ts
import { model } from "@medusajs/framework/utils";

const CustomCustomer = model.define("custom_customer", {
  id: model.id().primaryKey(),
  verified_at: model.dateTime().nullable(),
  verification_token: model.text().nullable(),
});

export default CustomCustomer;
const customCustomerService: CustomCustomerService =
      container.resolve(CUSTOM_CUSTOMER_MODULE);

    const customCustomer =
      await customCustomerService.listAndCountCustomCustomers(
        {
          verification_token: verificationToken,
          is_verified_email: false,
        },
        {
          relations: ["customer"],
        }
      );

hello, thank ur reply and this is my coding. When I get the customCustomer data, there is an error in the relations: ["customer"]. I am thinking about whether it's wrong

gleaming socket
#

Please which version of Medusa are you using ?

carmine bluff
#

Medusa 2.2

#

hello, may I know if u are trying to use mikroORM in ur project? I am facing a problem which is that I don't know how to write a complex query by using their service factory and the await query.graph()

for example, there is a customer and verify token table for verifying the customer account. I didn't find a solution to write a query to check that the customer is active and verify token is active,

Did u have any idea?🫡

gleaming socket
#

Yes please
I will replicate the code you have given and give feedback by the end of today

gleaming socket
# carmine bluff ``` // links/customer-custom_customer.ts import { defineLink } from "@medusajs/f...

You are getting the error because the the customer model and the custom_customer are related through a module link. Therefore you must use query.graph. For example

  const result = await query.graph({
    entity: "customer",
    fields: ["*", "custom_customer.*"],
  });

The limitation using query.graph is that it does not allow for complex filtering and queries and mostly you have to do them manually after querying from the database.

#

However, there is a workaround where you can access the knexjs instance connected to your database
This can be achieved by

  const knex = req.scope.resolve("__pg_connection__")

Then you can run your complex queries that you want. For example

  knex.table('customers').innerJoin("link_table_defined","customer_id","custom_customer_id").innerJoin("custom_customer","customer.id","")

Please lemme know if you have other questions as well

carmine bluff
#

thx @gleaming socket ! it's very useful!