mongoDB

    0

    0

    import { MongoClient } from 'mongodb';
    import { faker } from '@faker-js/faker';
    import dotenv from 'dotenv';
    
    dotenv.config();
    
    const setup = async () => {
      let client;
    
      try {
        client = new MongoClient(process.env.MONGODB_URI);
        await client.connect();
    
        const hasData = await client
          .db('test')
          .collection('users')
          .countDocuments();
    
        if (hasData) {
          console.log('Database already exists with data');
          client.close();
          return;
        }
    
        const records = [...Array(10)].map(() => {
          const [fName, lName] = faker.name.findName().split(' ');
          const username = faker.internet.userName(fName, lName);
          const email = faker.internet.email(fName, lName);
          const image = faker.image.people(640, 480, true);
    
          return {
            name: `${fName} ${lName}`,
            username,
            email,
            image,
            followers: 0,
            emailVerified: null
          };
        });
    
        const insert = await client
          .db('test')
          .collection('users')
          .insertMany(records);
    
        if (insert.acknowledged) {
          console.log('Successfully inserted records');
        }
      } catch (error) {
        return 'Database is not ready yet';
      } finally {
        if (client) {
          await client.close();
        }
      }
    };
    
    try {
      setup();
    } catch {
      console.warn('Database is not ready yet. Skipping seeding...');
    }
    
    export { setup };
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.