Git Product home page Git Product logo

mongo-to-postgres's Introduction

Simple migrate data from MongoDb to PostgreSQL

This tiny tool is used to simple migrate data from MongoDb database to PostgreSQL database. It uses appropriate ORMs: mongoose and knex respectively to transfer data.

Requirements

  • Node.js 13+ (NOT 14!!!)
  • MongoDb 4+
  • PostgreSQL 12+

Usage

1. Install package

$ yarn add mongo-to-postgres

OR

$ npm i mongo-to-postgres

2. Create file migrate.js, set migration settings in it

IMPORTANT NOTES

1. You MUST respect the order of the tables. Tables with foreign keys MUST BE placed AFTER tables, from which these keys are.

2. This sample assumes that you have postgres database with schema, containing empty tables.

import migrate from 'mongo-to-postgres';

migrate({
  // Define connection strings
  connections: {
    mongo: 'mongodb://localhost/dbname',
    postgres: 'postgres://postgres:secret@localhost:5432/dbname'
  },
  // Define your database migration settings
  collections: [
    {
    collectionName: 'department',  // collection name in Mongo
    tableName: 'departments',      // table name in Postgres
    fieldsRename: [
      ['createdAt', 'created_at'], // set new name for field (optional)
      ['updatedAt', 'updated_at']  // set new name for other field (optional)
    ],
    fieldsRedefine: [
      ['dep_type', 1]              // force to set value for all records (optional)
    ]
  },
  {
    collectionName: 'award',       // collection name in Mongo
    tableName: 'awards',           // table name in Postgres
  },
  {
    collectionName: 'employee',    // collection name in Mongo
    tableName: 'employees',        // table name in Postgres
    foreignKeys: {
      department: 'department',    // foreign keys (field: collection) (optional)
    },
    links: {                       // "many-to-many" links
      awards: ['emplyees__awards', 'employee_id', 'award_id']
    }
  }
  ]
});

3. Set package.json "type" field

{
  "type": "module",
  "dependencies": {
    "mongo-to-postgres": "^0.0.4"
  }
}

4. Run migration

$ node migrate.js

mongo-to-postgres's People

Contributors

alxnkt avatar dependabot[bot] avatar navichansy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.