This is a POC. DO NOT EVER USE IN PRODUCTION.
- node v8.x
- mongodb v3.6.2
Usage
npm install tiny-octopus
const TinyOctopus = require('tiny-octopus');
// Create a new stream.
TinyOctopus(connection, task);
connection
Object
Mongodb connection object
task
Object
A task object typically includes source collection
, destination collections
, source_field
, destination field
, fields
.
Very roughly:
{
source_c: "users",
destination_c: ['posts', 'comments'],
source_f: "_id",
destination_f: "author.id",
fields: {
'username': "author.username"
}
};
It's best suited for Distributed Data Model
i.e. if there are collections like users, posts, comments and user's data is replicated over other collections.
// users
{
"_id": "1",
"username": "prateek",
"city": "Pune"
}
// posts
{
"_id": "1",
"title": "Sarcasm is weird. Even not in acting, in life I feel like 'sarcastic' is a word that people use to describe me sometimes so when I meet someone, it's almost like they feel like they have to also be sarcastic, but it can sometimes just come off as mean if it's not used in the right way. ",
"tags": [
{ "id": "1", "title": "News"},
{ "id": "2", "title": "Spots"}
],
"author": {
"id": "1",
"username": "prateek"
}
}
// comments
{
"_id": "1",
"parent_id": "2",
"text": "Sarcasm is like cheap wine - it leaves a terrible aftertaste.",
"author": {
"id": "1",
"username": "prateek"
}
}
const MongoClient = require('mongodb').MongoClient;
const TinyOctopus = require('tiny-octopus');
MongoClient.connect(db_url, function(err, client) {
const connection = client.db(db_name)
TinyOctopus(connection, {
source_c: "users",
destination_c: ['posts', 'comments'],
source_f: "_id",
destination_f: "author.id",
fields: {
'username': "author.username"
}
});
});
This will replicate any changes made to the users
collection's username field to posts
and comments
collections.
db.users.update({ "_id": "1"}, {$set: {"username": "Karan"}});
For more details checkout the example section.
Thanks to: @wawhal, @karthikvt26 for the feedback.