responsible of synchronizing duplicate data between collections
npm install mongodb-data-sync
npm install mongodb-data-sync -g
`
Then, in the cmd run
`
mongodb-data-sync --key "some key" --url "mongodb connection url"
`
`
Options:
--debug console log important information
-p, --port server port. (default: 6500)
-d, --dbname the database name for the package. (default: "mongodb_data_sync_db")
-k, --key API key to use for authentication of the SDK requests, required
-u, --url MongoDB connection url, required
-h, --help output usage information
`
that's it for running the server, let's jump to the SDK
SDK
You can look at the example on github
Install
`
npm install mongodb-data-sync --save
`
init
first initialize the client , do it as soon as possible in your app
`javascript
const SynchronizerClient = require('mongodb-data-sync');
// settings the communication between you app and the engine.
// use this method the number of Database you want to work on
SynchronizerClient.init({
// your Database name the package should do the synchronization on (required)
dbName: 'mydb',
// the URL for package engine you run (required),
engineUrl: 'http://localhost:6500',
//the authentication key you declared on the engine application (required)
apiKey: 'my cat is brown',
});
`
returns a Promise
getInstance
`javascript
const synchronizerClientInstance = SynchronizerClient.getInstance({
// your Database name you want work on
dbName: 'mydb',
});
``
return an instance related to your db(its not a mongodb db instance) for dependencies operations
addDependency
`javascript
// 'addDependency' allow you to declare a dependency between 2 collections
synchronizerClientInstance.addDependency({
// the dependent collection is the collection that need to get updated automatically (required)
// in case the dependent collection is a mysql table ,its should be writing like this mysql.dbname.tablename
dependentCollection: 'orders',
//the referenced collection is the collection that get updated from your application (required)
refCollection: 'users',
// the dependent collection field to connect with (required)
localField: 'user_id',
// the referenced collection field to connect with, default _id ,using other field then _id will cuz an extra join for each check (optional)
foreignField:"_id" , // default
// an object represents the fields who need to be updated.
// the keys are the fields you want to be updated
// the values are the fields you want to take the value from (required)
fieldsToSync: {
user_first_name:'first_name',
user_last_name:'last_name',
user_email:'email'
},
// the engine uses a resume-token to know from where to continue the change stream.
// in case you had a crash for a long time and the oplog doesn't have this token anymore the engine will start update all the dependencies from the beginning,
// it is recommended to supply an update field (if you have) so the engine will start sync only for dates after the crash
refCollectionLastUpdateField:'last_update'
});
`
return Promise with the id of the Dependency
removeDependency
`javascript
// deletes a dependency based on id
synchronizerClientInstance.removeDependency(id);
`
return Promise
getDependencies
`javascript
// used to get the database dependencies
synchronizerClientInstance.getDependencies();
`
return Promise with all your database dependencies
syncAll
`javascript
// used to sync all the data in your database according to your dependencies.
// most of the time this function needs to be called only if you add a new dependency on an old data
synchronizerClientInstance.syncAll();
`
return Promise
addTrigger
`javascript
synchronizerClientInstance.addTrigger({
// the dependent collection to subscribe triggers on (required)
dependentCollection : "orders",
// the type of the trigger , can be insert,update,replace,delete (required)
triggerType:'insert',
// when triggerType is update define which fields you want to trigger the update
triggerFields : [],
// when knowledge set to true it will retry to fire the event until its get on ok http status
knowledge : false, // default
// the url the trigger will call
url:'http://localhost/insert-trigger'
});
`
return Promise with the id of the Trigger
removeTrigger
`javascript
// deletes a trigger based on id
synchronizerClientInstance.removeTrigger(id);
``