https://github.com/alxnkt/mongo-to-postgres
Simple data migration from MongoDb to PostgreSQL
https://github.com/alxnkt/mongo-to-postgres
database migration mongo postgres
Last synced: 8 months ago
JSON representation
Simple data migration from MongoDb to PostgreSQL
- Host: GitHub
- URL: https://github.com/alxnkt/mongo-to-postgres
- Owner: alxnkt
- License: mit
- Created: 2020-04-19T17:19:12.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-03-23T12:52:21.000Z (over 2 years ago)
- Last Synced: 2024-04-23T14:43:28.980Z (about 2 years ago)
- Topics: database, migration, mongo, postgres
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/mongo-to-postgres
- Size: 508 KB
- Stars: 11
- Watchers: 3
- Forks: 9
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.**
```javascript
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
```json
{
"type": "module",
"dependencies": {
"mongo-to-postgres": "^0.0.5"
}
}
```
### 4. Run migration
`$ node migrate.js`