eastern-cyan
eastern-cyan4w ago

Single join does not work with nestjsx/crud

Hello, Nestjsx crud request builder generates join query like "join[0]" or "join[1]" for multiple joins, but nextjsx-crud is not able to process that. It's only able to process "join" only. and it can accept multiple joins. Did anyone bumbed into this?
17 Replies
kapa.ai
kapa.ai4w ago
Hi @Onder. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hello, It seems like you're encountering an issue with the nestjsx/crud data provider where it does not properly handle multiple joins in the format "join[0]", "join[1]", etc. Instead, it only processes a single "join" parameter that can accept multiple joins. This issue has been noted in the past, specifically in the context of the @refinedev/nestjsx-crud adapter not supporting join (populate) for getOne. The original post from a closed GitHub issue mentions the necessity to populate or join related fields of the main resource, which is not achievable with the current adapter provided by @refinedev/nestjsx-crud. Here is the relevant part from the original post:
It is often necessary to populate or join related fields of the main resource in edit pages. Unfortunately, this cannot be achieved with the current adapter provided by @refinedev/nestjsx-crud. (@refinedev/strapi-v4 can do it though) (source (https://github.com/refinedev/refine/issues/5228))
While this does not provide a direct solution to your problem, it does confirm that the issue with handling joins in nestjsx/crud has been recognized. For a more detailed solution or workaround, you might want to check the latest updates or discussions in the refine GitHub repository or consider contributing a fix if you have a potential solution. You can also refer to the Contributing Guide (https://refine.dev/docs/guides-concepts/contributing) if you are interested in contributing. If you need further assistance, you might want to open a new issue on the refine GitHub repository to get more visibility and support from the community. I hope this helps! If you have any more questions, feel free to ask. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@238969575289389057> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyan
eastern-cyan4w ago
@kapa.ai it doesn't work with any method. because backend accepts join keywrod only not join[num]. i wonder if there any other way to support join outside of nextjsx-crud-request package
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- It appears that the nestjsx/crud backend only accepts the "join" keyword and not "join[num]", which is causing issues with multiple joins. Unfortunately, the current @refinedev/nestjsx-crud adapter does not support this functionality out-of-the-box. However, there are a few potential workarounds you could consider: 1. Custom Data Provider: You can create a custom data provider that handles the join logic according to your backend's requirements. This way, you can format the join parameters in a way that your backend can process. 2. Alternative Adapters: If feasible, you might want to explore other data provider adapters that support join functionality. For instance, the @refinedev/strapi-v4 adapter is mentioned to support join (populate) functionality. 3. Modify Backend: If you have control over the backend, you could modify it to accept the "join[num]" format or handle multiple joins in a way that aligns with the nestjsx/crud request builder. Here is a link to the relevant GitHub issue where this problem was discussed: GitHub Issue #5228 (https://github.com/refinedev/refine/issues/5228). For more detailed guidance on creating a custom data provider, you can refer to the refine documentation: Data Provider (https://refine.dev/docs/core/providers/data-provider/). If you need further assistance or have more specific requirements, consider opening a new issue on the refine GitHub repository to get more visibility and support from the community. I hope this helps! If you have any more questions, feel free to ask. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@238969575289389057> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyan
eastern-cyan4w ago
@kapa.ai i already swizzled the data provider. can you please let me know how to generate querystring with proper join clause?
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Certainly! Since you've already swizzled the data provider, you can customize it to generate the query string with the proper join clause. Below is an example of how you can modify the data provider to handle the join parameters correctly. Step-by-Step Guide to Customize the Data Provider 1. Run the Swizzle Command: If you haven't already, run the swizzle command to customize the data provider.
npm run refine swizzle

npm run refine swizzle

2. Select the Data Provider: Choose @refinedev/nestjsx-crud from the list of available data providers. 3. Edit the Data Provider: Open the generated data provider file (e.g., /nestjsx-crud-data-provider/index.ts) and modify the query string generation logic to handle the join parameters correctly. Example Code to Handle Join Parameters Here's an example of how you can modify the data provider to generate the query string with the proper join clause:
import { DataProvider } from "@refinedev/core";

const generateJoin = (joins?: string[]) => {
if (joins && joins.length > 0) {
return joins.map((join, index) => `join=${join}`).join("&");
}
return "";
};

const dataProvider: DataProvider = {
getList: async ({ resource, pagination, filters, sorters, joins }) => {
const { current = 1, pageSize = 10 } = pagination ?? {};
const params: string[] = [];

params.push(`_start=${(current - 1) * pageSize}`);
params.push(`_end=${current * pageSize}`);

if (filters) {
// Add filter logic here
}

if (sorters) {
// Add sorter logic here
}

if (joins) {
params.push(generateJoin(joins));
}

const query = params.join("&");
const response = await fetch(`${url}/${resource}?${query}`);
const data = await response.json();
return {
data,
total: data.length,
};
},
// Implement other methods (getOne, getMany, create, update, deleteOne) similarly
};

export default dataProvider;
import { DataProvider } from "@refinedev/core";

const generateJoin = (joins?: string[]) => {
if (joins && joins.length > 0) {
return joins.map((join, index) => `join=${join}`).join("&");
}
return "";
};

const dataProvider: DataProvider = {
getList: async ({ resource, pagination, filters, sorters, joins }) => {
const { current = 1, pageSize = 10 } = pagination ?? {};
const params: string[] = [];

params.push(`_start=${(current - 1) * pageSize}`);
params.push(`_end=${current * pageSize}`);

if (filters) {
// Add filter logic here
}

if (sorters) {
// Add sorter logic here
}

if (joins) {
params.push(generateJoin(joins));
}

const query = params.join("&");
const response = await fetch(`${url}/${resource}?${query}`);
const data = await response.json();
return {
data,
total: data.length,
};
},
// Implement other methods (getOne, getMany, create, update, deleteOne) similarly
};

export default dataProvider;
Usage in Your Application After customizing the data provider, you can use it in your application as follows:
import { Refine } from "@refinedev/core";
import dataProvider from "./nestjsx-crud-data-provider";

const App = () => {
return (
<Refine
dataProvider={dataProvider}
/* ... */
/>
);
};

export default App;
import { Refine } from "@refinedev/core";
import dataProvider from "./nestjsx-crud-data-provider";

const App = () => {
return (
<Refine
dataProvider={dataProvider}
/* ... */
/>
);
};

export default App;
Additional Resources For more information on customizing data providers, you can refer to the refine documentation: - Customizing the Data Provider (https://refine.dev/docs/data/packages/simple-rest/#customizing-the-data-provider-) - Data Provider Interface (https://refine.dev/docs/guides-concepts/data-fetching/data-provider-interface/) If you encounter any issues or need further assistance, consider opening a new issue on the refine GitHub repository to get more visibility and support from the community. I hope this helps! If you have any more questions, feel free to ask. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@238969575289389057> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyan
eastern-cyan4w ago
@kapa.ai i think i solved the problem with below
RequestQueryBuilder.prototype.query = function (encode: boolean) {
return qs.stringify(this.queryObject, { encode, arrayFormat: "repeat" });
};
RequestQueryBuilder.prototype.query = function (encode: boolean) {
return qs.stringify(this.queryObject, { encode, arrayFormat: "repeat" });
};
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- It looks like you've found a solution to the problem by customizing the RequestQueryBuilder to handle the join parameters correctly. Here's your solution formatted as Markdown: --- Solution to Handle Join Parameters in nestjsx/crud To solve the issue with nestjsx/crud not processing multiple joins correctly, you can customize the RequestQueryBuilder to generate the query string with the proper join clause. Below is the code snippet that achieves this:
RequestQueryBuilder.prototype.query = function (encode: boolean) {
return qs.stringify(this.queryObject, { encode, arrayFormat: "repeat" });
};
RequestQueryBuilder.prototype.query = function (encode: boolean) {
return qs.stringify(this.queryObject, { encode, arrayFormat: "repeat" });
};
Explanation The above code modifies the query method of the RequestQueryBuilder to use the qs.stringify function with the arrayFormat: "repeat" option. This ensures that the join parameters are formatted correctly in the query string. Usage After implementing this customization, your join parameters will be correctly processed by the backend, allowing you to perform multiple joins as needed. Additional Resources For more information on customizing data providers and handling query parameters, you can refer to the refine documentation: - Customizing the Data Provider (https://refine.dev/docs/data/packages/simple-rest/#customizing-the-data-provider-) - Data Provider Interface (https://refine.dev/docs/guides-concepts/data-fetching/data-provider-interface/) If you encounter any issues or need further assistance, consider opening a new issue on the refine GitHub repository to get more visibility and support from the community. --- I hope this helps! If you have any more questions, feel free to ask. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@238969575289389057> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyan
eastern-cyan4w ago
@kapa.ai no look, arrayFormat repeat removes the array indice from the query so instead of join[0] it turns it to join and if you have multiple joins it gives you join join join etc /solved @kapa.ai how can i solve a post?
kapa.ai
kapa.ai4w ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To solve a post (i.e., create a new post record) using refine, you can follow the steps below. This example will use the @refinedev/react-hook-form package to handle form validation and submission. Step-by-Step Guide to Create a Post 1. Install the Required Package: First, ensure you have the @refinedev/react-hook-form package installed:
npm install @refinedev/react-hook-form

npm install @refinedev/react-hook-form

2. Create the create.tsx File: In your pages/posts folder, create a create.tsx file with the following code:
import React from "react";
import { Create } from "@refinedev/chakra-ui";
import {
FormControl,
FormLabel,
FormErrorMessage,
Input,
Textarea,
Select,
} from "@chakra-ui/react";
import { useSelect } from "@refinedev/core";
import { useForm } from "@refinedev/react-hook-form";

export const PostCreate = () => {
const {
refineCore: { formLoading, queryResult },
saveButtonProps,
register,
resetField,
formState: { errors },
} = useForm();

const postsData = queryResult?.data?.data;

const { options: categoryOptions } = useSelect({
resource: "categories",
defaultValue: postsData?.category?.id,
});

React.useEffect(() => {
resetField("category.id");
}, [categoryOptions]);

return (
<Create isLoading={formLoading} saveButtonProps={saveButtonProps}>
<FormControl mb="3" isInvalid={!!(errors as any)?.title}>
<FormLabel>Title</FormLabel>
<Input
type="text"
{...register("title", {
required: "This field is required",
})}
/>
<FormErrorMessage>
{(errors as any)?.title?.message as string}
</FormErrorMessage>
</FormControl>

<FormControl mb="3" isInvalid={!!(errors as any)?.content}>
<FormLabel>Content</FormLabel>
<Textarea
{...register("content", {
required: "This field is required",
})}
/>
<FormErrorMessage>
{(errors as any)?.content?.message as string}
</FormErrorMessage>
</FormControl>
<FormControl mb="3" isInvalid={!!(errors as any)?.category}>
<FormLabel>Category</FormLabel>
<Select
{...register("category.id", {
required: "This field is required",
})}
>
{categoryOptions.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>
<FormErrorMessage>
{(errors as any)?.category?.message as string}
</FormErrorMessage>
</FormControl>
</Create>
);
};

import React from "react";
import { Create } from "@refinedev/chakra-ui";
import {
FormControl,
FormLabel,
FormErrorMessage,
Input,
Textarea,
Select,
} from "@chakra-ui/react";
import { useSelect } from "@refinedev/core";
import { useForm } from "@refinedev/react-hook-form";

export const PostCreate = () => {
const {
refineCore: { formLoading, queryResult },
saveButtonProps,
register,
resetField,
formState: { errors },
} = useForm();

const postsData = queryResult?.data?.data;

const { options: categoryOptions } = useSelect({
resource: "categories",
defaultValue: postsData?.category?.id,
});

React.useEffect(() => {
resetField("category.id");
}, [categoryOptions]);

return (
<Create isLoading={formLoading} saveButtonProps={saveButtonProps}>
<FormControl mb="3" isInvalid={!!(errors as any)?.title}>
<FormLabel>Title</FormLabel>
<Input
type="text"
{...register("title", {
required: "This field is required",
})}
/>
<FormErrorMessage>
{(errors as any)?.title?.message as string}
</FormErrorMessage>
</FormControl>

<FormControl mb="3" isInvalid={!!(errors as any)?.content}>
<FormLabel>Content</FormLabel>
<Textarea
{...register("content", {
required: "This field is required",
})}
/>
<FormErrorMessage>
{(errors as any)?.content?.message as string}
</FormErrorMessage>
</FormControl>
<FormControl mb="3" isInvalid={!!(errors as any)?.category}>
<FormLabel>Category</FormLabel>
<Select
{...register("category.id", {
required: "This field is required",
})}
>
{categoryOptions.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>
<FormErrorMessage>
{(errors as any)?.category?.message as string}
</FormErrorMessage>
</FormControl>
</Create>
);
};

Explanation - Form Setup: The useForm hook from @refinedev/react-hook-form is used to manage form state ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai4w ago
<@238969575289389057> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyan
eastern-cyan4w ago
@kapa.ai no i mean in discord in here