New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

mongo-to-postgres

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-to-postgres

Simple data migration from MongoDb to PostgreSQL

latest
npmnpm
Version
0.0.6
Version published
Weekly downloads
11
1000%
Maintainers
1
Weekly downloads
 
Created
Source

Simple data migration from MongoDb to PostgreSQL

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

Requirements

  • Node.js 13+
  • 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.6"
  }
}

4. Run migration

$ node migrate.js

Keywords

mongo

FAQs

Package last updated on 05 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts