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

Enable to import vac as CSV #993

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

AliKdhim87
Copy link
Contributor

@AliKdhim87 AliKdhim87 commented Jan 10, 2025

Strapi v4 does not natively support bulk creation endpoints, which can make handling large numbers of entries inefficient. While a custom controller could be implemented to handle multiple entries in a single request, this would require backend adjustments and might not be worth the effort, as this feature is only needed once. Additionally, bulk creation support may be available in Strapi v5, making this solution more temporary.

To address this, I implemented a loop that processes each entry individually while controlling concurrency with p-limit. This ensures no more than 5 requests are sent at once, preventing server overload and improving performance. By using p-limit, the system remains stable and responsive, even when processing multiple entries concurrently.

issue

Copy link

vercel bot commented Jan 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
cms-frameless-io ❌ Failed (Inspect) Jan 14, 2025 3:54pm
tiptap ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 14, 2025 3:54pm

Copy link

codecov bot commented Jan 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Files with missing lines Coverage Δ
...trapi-admin-extensions/src/utils/processCsvFile.ts 100.00% <100.00%> (ø)
.../strapi-admin-extensions/src/utils/sanitizeHTML.ts 100.00% <100.00%> (ø)

return DOMPurify(window);
});
const domPurify = createDOMPurify();
export const sanitizeHTML = memoize((html: string) => domPurify.sanitize(html, { FORBID_ATTR: ['style'] }));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ik zou geen memoize gebruiken hier, om memory usage te verminderen.

@@ -46,7 +48,12 @@ const corsOption: CorsOptions = {
};
const apiSpec = path.join(__dirname, './docs/openapi.yaml');
const app = express();
// Multer file upload middleware.
// The order is important, so this should be before the express.json() middleware to parse the file.
const upload = multer({ dest: 'tmp/uploads/' });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deze temporary file moet ook weer verwijderd worden.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Het is opgelost door gebruik te maken van await fs.promises.unlink(filePath) in plaats van fs.unlinkSync(filePath).

https://github.com/frameless/strapi/blob/enable-to-import-vac-as-csv/apps/overige-objecten-api/src/controllers/import/index.ts#L50

.on('end', () => {
if (hasRequiredColumns) {
const sanitizedResults = results.map((result) => {
const DOMPurifyHTML = sanitizeHTML(result?.antwoord);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je gebruikt nu "constructor case" voor een variable. Ik stel voor om domPurifyHTML te gebruiken, om duidelijk te maken dat het geen constructor is maar een waarde.

apps/overige-objecten-api/src/utils/processCsvFile.ts Outdated Show resolved Hide resolved
let hasRequiredColumns = true;

// Create read stream for CSV file
return new Promise<any[]>((resolve, reject) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kunnen we any aanpassen naar een type?

(fs.createReadStream as jest.Mock).mockReturnValue(mockStream);

const result = await processCsvFile(filePath, ['vraag', 'antwoord']);
expect(result).toEqual([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ik zou hier ook even de type controleren, met bijv: const expectedResults: SomeType[] = ...

try {
// Process the CSV file and sanitize results
const isAuthHasToken = req.headers?.authorization?.startsWith('Token');
const tokenAuth = isAuthHasToken ? req.headers?.authorization?.split(' ')[1] : req.headers?.authorization;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Als je dan toch al split doet, zou ik startsWith('Token') vervangen door arr[0].
Je kunt iets meer flexibel zijn met split(/\s+/)

@AliKdhim87 AliKdhim87 force-pushed the enable-to-import-vac-as-csv branch from 09a22e7 to b51909e Compare January 14, 2025 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Maak het mogelijk om VAC als een CSV-bestand te importeren via de API.
2 participants