#Service Factory Question
1 messages · Page 1 of 1 (latest)
can you share your code for better context
// 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
Please which version of Medusa are you using ?
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?🫡
Yes please
I will replicate the code you have given and give feedback by the end of today
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
thx @gleaming socket ! it's very useful!