Weaver: Clone n-ary relationship sets across distinct databases
Weaver: Clone n-ary relationship sets across distinct databases.
Debugging DB objects with n-ary relationships is often done manually, this tool is meant to automate copying these objects into desired databases.
For example, a database has the following collections/documents:
// db.users.findOne({_id: ObjectId('abcdef78901234abcdef1234')})
{
_id: ObjectId('abcdef78901234abcdef1234'),
name: 'John',
orders: [ { orderId: '4321fedcbafedcba67890123' } ]
}
// db.orders.findOne({_id: ObjectId('4321fedcbafedcba67890123')})
{
_id: ObjectId('4321fedcbafedcba67890123'),
cartId: 'fedcba67890123fedcba4321'
}
// db.carts.findOne({_id: ObjectId('fedcba67890123fedcba4321')})
{
_id: ObjectId('fedcba67890123fedcba4321'),
userId: 'abcdef78901234abcdef1234'
}
Note how user
relates to order
, and order
relates to cart
.
If you wanted to migrate the orders
and carts
associated to the user
to your local environment, you’d need to:
[users|orders|carts] collection
and find the document.This tool finds the relationships, and migrates them to a destination db:
Query | source db | target db |
---|---|---|
db.users.findOne(ObjectId('abcdef78901234abcdef1234')) |
1 | 1 |
db.orders.findOne(ObjectId('4321fedcbafedcba67890123')) |
1 | 1 |
db.carts.findOne(ObjectId('fedcba67890123fedcba4321')) |
1 | 1 |
Weaver runs on NodeJS >= 10
npm i -g @skelogh/weaver
npm run build
.npm pack
on the project root.weaver-<VERSION>.tgz
will be created.npm install -g weaver-<VERSION>.tgz
, it will install the package globally.weaver
command, the CLI help will display the CLI help:Usage: weaver [OPTIONS] COMMAND [ARG...]
weaver [ --help | -v | --version ]
Commands:
weaver add Creation of client, query or ignore
weaver remove Removal of clients, queries or ignores
weaver run Runs the app with the loaded configuration
Options:
--version, -v Print version information and quit
--config, -c Read or set path of config file, default: undefined
--queries, --qq Document ids to get relationships from, e.g.: 2a3b4c5d6e7f8g9h2a3b4c5d e7f8g9h2a3b4c5d2a3b4c5d6
--verbose, -V Enable highest level of logging, same as DEBUG=*
--help Show help
In the CLI, run weaver add client
, the following help will display:
Usage: weaver add client -fntou --options.<option_name>
-f [mongodb] <String> The client db family, mongodb is only supported for now.
-n <name> <String> The client db name.
-t [source|target] <String> source if the data will be pulled from it, target otherwise.
-o [<source.name>] <String> The target's origin db where the data will be copied from.
-u <url> <String> The client db URL.
--options.<opt_name> <Any> Client db-specific options, for now MongoClient options, use dot notation to set each option
Example: --options.readPreference secondaryPreferred
To pull data from a db into another, you’ll need to add both the source
and target
clients. The source
is where the document we want is stored, and target
is where you want to copy to.
Example:
Assume the following remote server settings for source
:
weaver add client -f mongodb -n production -t source -u mongodb://172.17.0.4:27017
Assume the following local server settings for target
:
source
and target
databases.)weaver add client -f mongodb -n development -t target -o production -u mongodb://127.0.0.1:27017
From the source
database, find the document you want to copy to the target
database and copy the stringified _id
:
// Assume the following mongodb document in the `production` database, `pets` collection:
{
_id: ObjectId("507f1f77bcf86cd799439011"),
name: "fluffy"
}
Once steps 1 & 2 are done, just run the following command:
weaver run --queries 507f1f77bcf86cd799439011
If all is setup correctly, you should now have a copy of the document in your target
database, it will create the collection even if it didn’t exist before:
> mongodb
mongodb> use development
mongodb> db.pets.find(ObjectId("507f1f77bcf86cd799439011"))
{
_id: ObjectId("507f1f77bcf86cd799439011"),
name: "fluffy"
}
>
weaver
CLI (follow the project)~
npm i -g @skelogh/weaver
:tada:Questions? create an issue!.