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

TS example - ENOENT ERROR #83

Closed
IrvingArmenta opened this issue Aug 19, 2019 · 7 comments
Closed

TS example - ENOENT ERROR #83

IrvingArmenta opened this issue Aug 19, 2019 · 7 comments
Labels
area/core Refers to Mongo Seeding library needs more info

Comments

@IrvingArmenta
Copy link

I am not completely sure if this is an actual issue or is just that I'm missing something, but the npm script is not working, I keep getting

MongoSeedingError: Error: ENOENT: no such file or directory, scandir '/Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/data-seed/data.ts'
    at wrapError (/Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/node_modules/mongo-seeding/src/index.ts:125:17)
    at Seeder.readCollectionsFromPath (/Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/node_modules/mongo-seeding/src/index.ts:58:13)
    at Object.<anonymous> (/Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/index.js:13:28)
    at Module._compile (internal/modules/cjs/loader.js:777:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:840:10)
    at internal/main/run_main_module.js:17:11

I have tried multiple file names on the index.js file but it keeps saying that it does not found the data file

This is the index.js file:

require('ts-node').register();

const path = require('path');
const { Seeder } = require('mongo-seeding');

const config = {
  database: 'mongodb://localhost/dashb',
  dropDatabase: true,
};

const seeder = new Seeder(config);

const collections = seeder.readCollectionsFromPath(
  path.resolve('./data-seed/data.ts'),
  {
    extensions: ['js', 'json', 'ts'],
    transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],
  },
);

seeder
  .import(collections)
  .then(() => {
    console.log('Success');
  })
  .catch(err => {
    console.log('Error', err);
  });

and this is the data.ts file that is in the same directory as index.js

import { getObjectId } from './helpers';
import { seedUser } from '../src/api/methods';

const names = ["Hanamichi", "Rukawa", "Haruko", "Akagi", "Mitsuki"];

const users: seedUser[] = names.map((name, i) => {
  return {
    id: getObjectId(name),
    name,
    email: `${name}@email.com`,
    password: `password${i}`,
    role: 'user'
  }
});

export = users;

I tried following the ts example, any ideas why I keep getting ENOENT ?

my directory tree is:

project folder > 
         src 
         data-seed 
         // other stuff

data-seed has it's own package.json and also the main project folder too, has it's own package.json

@pkosiec
Copy link
Owner

pkosiec commented Aug 20, 2019

Hi @IrvingArmenta,

I believe the path is wrong. Please notice, that you need to provide path to directory, not a single file, as in the example: https://github.com/pkosiec/mongo-seeding/tree/master/examples/import-data-ts

Changing path.resolve('./data-seed/data.ts'), to path.resolve('./data-seed'), should help.

Let me know if that works. Cheers!

@pkosiec pkosiec added area/core Refers to Mongo Seeding library discussion labels Aug 20, 2019
@IrvingArmenta
Copy link
Author

Hello @pkosiec

It seems to be working correctly now, thanks, however,

I'm not sure but what I get is a collection called node_modules , what I want to seed is a collection called users , where do I setup the collection to be seeded ?

also, what I get in the node_modules mongo collection is:

Which is nothing like the data I'm trying to seed.

{  
   "_id":ObjectId("5d5bc1bfd0f9dc3d2f862e4a"),
   "IDENT_RE":"[a-zA-Z]\\w*",
   "UNDERSCORE_IDENT_RE":"[a-zA-Z_]\\w*",
   "NUMBER_RE":"\\b\\d+(\\.\\d+)?",
   "C_NUMBER_RE":"(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)",
   "BINARY_NUMBER_RE":"\\b(0b[01]+)",
   "RE_STARTERS_RE":"!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~",
   "BACKSLASH_ESCAPE":{  
      "begin":"\\\\[\\s\\S]",
      "relevance":0
   },
   "APOS_STRING_MODE":{  
      "className":"string",
      "begin":"'",
      "end":"'",
      "illegal":"\\n",
      "contains":[  
         {  
            "begin":"\\\\[\\s\\S]",
            "relevance":0
         }
      ]
   },
   "QUOTE_STRING_MODE":{  
      "className":"string",
      "begin":"\"",
      "end":"\"",
      "illegal":"\\n",
      "contains":[  
         {  
            "begin":"\\\\[\\s\\S]",
            "relevance":0
         },
         {  
            "className":"subst",
            "begin":"\\\\[abfnrtv]\\|\\\\x[0-9a-fA-F]*\\\\\\|%[-+# *.0-9]*[dioxXucsfeEgGp]",
            "relevance":0
         }
      ]
   },
   "PHRASAL_WORDS_MODE":{  
      "begin":/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/
   },
   "C_LINE_COMMENT_MODE":{  
      "className":"comment",
      "begin":"//",
      "end":"$",
      "contains":[  
         {  
            "begin":/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/
         },
         {  
            "className":"doctag",
            "begin":"(?:TODO|FIXME|NOTE|BUG|XXX):",
            "relevance":0
         }
      ]
   },
   "C_BLOCK_COMMENT_MODE":{  
      "className":"comment",
      "begin":"/\\*",
      "end":"\\*/",
      "contains":[  
         {  
            "begin":/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/
         },
         {  
            "className":"doctag",
            "begin":"(?:TODO|FIXME|NOTE|BUG|XXX):",
            "relevance":0
         }
      ]
   },
   "HASH_COMMENT_MODE":{  
      "className":"comment",
      "begin":"#",
      "end":"$",
      "contains":[  
         {  
            "begin":/\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|they|like|more)\b/
         },
         {  
            "className":"doctag",
            "begin":"(?:TODO|FIXME|NOTE|BUG|XXX):",
            "relevance":0
         }
      ]
   },
   "NUMBER_MODE":{  
      "className":"number",
      "begin":"\\b\\d+(\\.\\d+)?",
      "relevance":0
   },
   "C_NUMBER_MODE":{  
      "className":"number",
      "begin":"(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)",
      "relevance":0
   },
   "BINARY_NUMBER_MODE":{  
      "className":"number",
      "begin":"\\b(0b[01]+)",
      "relevance":0
   },
   "CSS_NUMBER_MODE":{  
      "className":"number",
      "begin":"\\b\\d+(\\.\\d+)?(%|em|ex|ch|rem|vw|vh|vmin|vmax|cm|mm|in|pt|pc|px|deg|grad|rad|turn|s|ms|Hz|kHz|dpi|dpcm|dppx)?",
      "relevance":0
   },
   "REGEXP_MODE":{  
      "className":"regexp",
      "begin":/\//,
      "end":/\/      [  
         gimuy
      ]      */,
      "illegal":/\n/,
      "contains":[  
         {  
            "begin":"\\\\[\\s\\S]",
            "relevance":0
         },
         {  
            "begin":/\            [  
               /,
               "end":/\
            ]            /,
            "relevance":0,
            "contains":[  
               {  
                  "begin":"\\\\[\\s\\S]",
                  "relevance":0
               }
            ]
         }
      ]
   },
   "TITLE_MODE":{  
      "className":"title",
      "begin":"[a-zA-Z]\\w*",
      "relevance":0
   },
   "UNDERSCORE_TITLE_MODE":{  
      "className":"title",
      "begin":"[a-zA-Z_]\\w*",
      "relevance":0
   },
   "METHOD_GUARD":{  
      "begin":"\\.\\s*[a-zA-Z_]\\w*",
      "relevance":0
   }
}

@pkosiec
Copy link
Owner

pkosiec commented Aug 20, 2019

@IrvingArmenta It's because you have node_modules directory (containing all JavaScript dependencies) in the directory you provide as an input. The solution would be to create another subdirectories inside ./data-seed, like ./data-seed/import-data/sample, copy the data.ts file there and then change in your code: path.resolve('./data-seed/import-data').

In this case, Mongo Seeding will import your data.ts file into collection sample.

You have to make sure that the input directory for Mongo Seeding contains only subdirectories, which has files to seed and their names are equal to your desired collection names.

@IrvingArmenta
Copy link
Author

@pkosiec
I followed your advice and setup the folder like: /data-seed/import-data/users and the path as path.resolve('./data-seed/import-data') but now I get a syntax error , not sure if it's related to typescript:

  mongo-seeding Reading collections from /Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/import-data... +0ms
MongoSeedingError: SyntaxError: Unexpected token {
    at wrapError (/Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/node_modules/mongo-seeding/src/index.ts:125:17)
    at Seeder.readCollectionsFromPath (/Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/node_modules/mongo-seeding/src/index.ts:58:13)
    at Object.<anonymous> (/Users/irvingarmenta/Documents/dashb-mongo-ts/data-seed/index.js:13:28)
    at Module._compile (internal/modules/cjs/loader.js:777:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:788:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:840:10)
    at internal/main/run_main_module.js:17:11

my npm script DEBUG=mongo-seeding node ./data-seed/index.js and my index.js file is:

require('ts-node').register();

const path = require('path');
const { Seeder } = require('mongo-seeding');

const config = {
  database: 'mongodb://localhost/dashb',
  dropDatabase: true,
};

const seeder = new Seeder(config);

const collections = seeder.readCollectionsFromPath(
  path.resolve('./data-seed/import-data'),
  {
    extensions: ['js', 'json', 'ts'],
    transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],
  },
);

seeder
  .import(collections)
  .then(() => {
    console.log('Success');
  })
  .catch(err => {
    console.log('Error', err);
  });

And the data.ts file:

import { seedUser } from '../../../src/api';
import { getObjectId } from '../../helpers';

const names = ["Hanamichi", "Rukawa", "Haruko", "Akagi", "Mitsuki"];

const users: seedUser[] = names.map((name, i) => {
  return {
    id: getObjectId(name),
    name,
    email: `${name}@email.com`,
    password: `password${i}`,
    role: 'user'
  }
});

export default users;

@pkosiec
Copy link
Owner

pkosiec commented Aug 20, 2019

I've just tried the TS example from this repository and it works well: https://github.com/pkosiec/mongo-seeding/tree/master/examples/import-data-ts
Could you please try it out on your local machine, to make sure it's not related to the library itself?

@pkosiec
Copy link
Owner

pkosiec commented Sep 14, 2019

Hi @IrvingArmenta, did you resolve the problem you had? Can I close this issue?

@pkosiec
Copy link
Owner

pkosiec commented Sep 17, 2019

Okay, I assume that your problem are solved. If you have any further questions, comment this thread or create another issue 🙂Cheers!

@pkosiec pkosiec closed this as completed Sep 17, 2019
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 needs more info
Projects
None yet
Development

No branches or pull requests

2 participants