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.
- Node.js 13+
- MongoDb 4+
- PostgreSQL 12+
$ yarn add mongo-to-postgres
OR
$ npm i mongo-to-postgres
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']
}
}
]
});
{
"type": "module",
"dependencies": {
"mongo-to-postgres": "^0.0.5"
}
}
$ node migrate.js