npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@emiliosel/sql-to-mongo

v0.3.5

Published

Migrate data from sql database to mongodb

Downloads

82

Readme

sql-to-mongo

Migrate data from sql database to mongo

A simple node.js module that Migrate data from sql database to mongo

Installation

npm install @emiliosel/sql-to-mongo

Usage

const Migration = require('@emiliosel/sql-to-mongo');

Config for mysql connection:

const knexConfig = {
  client: 'mysql',
  connection: {
    host: '0.0.0.0',
    port: '3306',
    user: 'root',
    password: 'secret',
    database: 'world'
  }
}

Config for mysql connection:

const monooseConfig = {
  host: 'localhost',
  port: '27017',
  database: 'myWorld'
}

Options for running migration of data:

const options = {
  fromTable: 'city',
  toCollection: 'mongoCity',
  paginate: 1000,                            // paginate the query by 1000 items
  columns: {                                 // are the fields that will be created to collection
    title: {                                 // the name that will give to the field of collection
      type: String,                          // accepts all the types of mongoose.Schema         
      from: 'Name'                           // the column name from sql table
    },
    name: {
      type: String,
      from: 'Name'
    },
    countryCode: {                            // 'beforeSave' callback is called at mongoose
      type: String,                           // middleware post('validate') 
      from: 'CountryCode',                    // 'doc' is the current document that passed mongoose
                                              // validation and is about to save
                                              // we can change or format the properties of doc
      beforeSave: async (doc, mongoose, knex) => { 
        doc.countryCode = doc.countryCode.toLowerCase()
      }
    },
    Population: {                             // here is not provided 'from' so the name of the
      type: Number                            // collection field 'Population' is the same with
    },                                        // sql column
    customColumn: {
      type: {                                 // custom field added to mongo collection that
        type: String,                         // does not exist to sql table
        default: 'test'                       // with default value 'test'
      },
      custom: true
    },
    anotherCustomField: {                      // custom field that we populate with a custom 
      type: Array,                             // query using knex. We could also use mongoose
      custom: true,                             
      beforeSave: async (doc, mongoose, knex) => {
        let someValues = await knex.select().from('someTable')
        doc['anotherCustomField'] = someValues
      }
    }
  }
}

Instantiate the Migration with the databases config:

let migration = new Migration({
  sql: knexConfig,
  mongo: mongooseConfig
})

Run migration passing the options object as param:

migration.up(options).then(() => {
  console.log('succesfully')
})
.catch((er) => {
  console.log('error')
  
})

Run delete created collection:

migration.down().then(() => {
  console.log('ok')
})
.catch((er) => {
  console.log('error')
})

Run custom migration:

migration.up(async (knex, mongoose) => {
  let data = await knex.select().from('tableName')
  let schema = new mongoose.Schema({
    title: String,
    date: Date
  })
  let model = mongoose.model('ModelName', schema)
  await model.insertMany(data)
})

Tests

docker-compose up

npm test