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

Handle export default in import data files #105

Open
jessevdp opened this issue Mar 17, 2020 · 5 comments
Open

Handle export default in import data files #105

jessevdp opened this issue Mar 17, 2020 · 5 comments
Labels
area/core Refers to Mongo Seeding library help wanted 🚀 enhancement New feature or request

Comments

@jessevdp
Copy link

I just ran into this issue where all the added data would show up under a single document (with a single ID) in MongoDB. After about 30 minutes of tinkering around, trying stuff, and digging through the source code—I noticed how I exported my data using export default instead of export = [].

Why would export default result in this module thinking that I want to add an array as single document? export = [] doesn't really seem like a pattern many libs use/require?

‼️ Wrong way

export default [
  { name: "My First Item" },
  { name: "My Second Item" },
  { name: "My Third Item" },
  { name: "My Last Item" }
];

✅ Right way

export = [
  { name: "My First Item" },
  { name: "My Second Item" },
  { name: "My Third Item" },
  { name: "My Last Item" }
];

Suggestion

Maybe it's safe to assume that when the user exports an array, they want it to be imported as separate documents in their database?

Overal very useful tool, saves me a bunch of boilerplate code. 😄

@pkosiec
Copy link
Owner

pkosiec commented Mar 17, 2020

Hi @jessevdp,

Thanks for the issue. Let me answer your questions first.

Why would export default result in this module thinking that I want to add an array as single document?

Basically, Mongo Seeding traverses directories in given location and requires all files defined there (using require()).
When requiring a given file, where there is export default defined, then we receive object with single default property. So, for example requiring a single file with simple content export default [] will result in the following object: { default: [] }.

export = [] doesn't really seem like a pattern many libs use/require?

It is used quite widely in TypeScript community - it's an alternative to the module.exports = approach from Node.js. From what I've observed, export default is common pattern for front-end apps, like React one.

Anyway, I agree with you - Mongo Seeding could simply detect a case when someone used export default and then get the content from default property in the required object. It will be easy to implement it - will do it in next version!

@pkosiec pkosiec added area/core Refers to Mongo Seeding library 🚀 enhancement New feature or request labels Mar 17, 2020
@pkosiec pkosiec changed the title Added data all under single document when using export default Handle export default in import data files Mar 17, 2020
@pkosiec pkosiec added this to the 3.5.0 milestone Mar 17, 2020
@jessevdp
Copy link
Author

Ah I see, most of my JS knowledge comes from the frontend world indeed.

Once you get it, that this is the pattern, it’s not a problem, easy. My main concern is that some devs will—like me—be looking for why this wouldn’t work.

will be easy to implement it - will do it in next version!

Awesome 😁

@pkosiec
Copy link
Owner

pkosiec commented Oct 4, 2020

I know it's been a while, but finally I started to work on this. Unfortunately I have to put this feature on hold, as it would change Mongo Seeding API - specifically, it would make readCollectionsFromPath function asynchronous.
It is because of dynamic import (import()), which needs to be used to import ES modules (.mjs files) with ES6 modules syntax.
I would like to keep API changes for 4.0, as I want to totally rewrite Mongo Seeding to have simpler an more convenient API.

As a workaround, one may use Babel to convert ES6 modules to CommonJS files, in order to import them with Mongo Seeding.

@pkosiec pkosiec modified the milestones: 3.5.0, 4.0.0 Oct 4, 2020
@pkosiec pkosiec mentioned this issue Oct 4, 2020
@danielbayerlein
Copy link

Any news to support ESM?

@pkosiec
Copy link
Owner

pkosiec commented Dec 8, 2023

I'm sorry, but it won't land anytime soon. I had actually two attempts (this is the latest one: link) to do it but it became too complex to me. There are some caveats like experimental Jest ESM support, and also I would like to keep the backward compatibility - that is, support both ESM and CJS. This is tricky, especially that I'm out of the TS/JS community for quite a long time.

I'm open for contributions though!

@pkosiec pkosiec removed this from the 4.0.0 milestone Dec 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core Refers to Mongo Seeding library help wanted 🚀 enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants