Table of Contents
Mopo is a simple cli tool for replicating MongoDB to PostgreSQL in realtime.
git clone https://github.com/rebellionpay/mopo mopo && cd mopo
npm i
npm run build
Mopo uses Replica Set feature in MongoDB. But you don't have to replicate between MongoDB actually. Just follow the steps below.
Start a new mongo instance with no data:
$ mongod --replSet "rs0" --oplogSize 100Open another terminal, and go to MongoDB Shell:
$ mongo
....
> rs.initiate()rs.initiate() command prepare the collections that is needed for replication.
Launch postgres instance, and create the new database to use.
Create a new .mopo.json file like this:
{
"mongo": {
"connection": {
"uri": "<MONGOQUERY>",
"options": {
"useNewUrlParser": true,
"useUnifiedTopology": true,
"auto_reconnect": true
}
},
"collectionModels": {
"users": {
"_id": {
"type": "TEXT",
"primary": true
},
"email": {
"type": "TEXT"
},
"name": {
"type": "TEXT"
},
"createdAt": {
"type": "TIMESTAMP"
},
"updatedAt": {
"type": "TIMESTAMP"
}
}
}
},
"postgres": {
"connection": {
"config": {
"host": "localhost",
"database": "exampleDB"
}
}
},
"sync": [
{
"collection": "users",
"syncAll": true,
"watchOperations": [
"INSERT",
"UPDATE"
]
}
]
}_id field is required for each collection and should be string.
Currently these native types are supported:
BIGINTBOOLEANVARCHARDATETIMESTAMPTEXT
node dist/index.js --start .mopo.json --log-level debug
Usage: index [options]
Options:
-s, --start <config file> start sync with config file
-sa, --sync-all Sync all data first if schema syncAll enabled
-bi, --bulk-insert <number> Number of documents to insert at once (only works if --sync-all enabled). Default 10.
-l, --log-level <level> Log level
-h, --help display help for command