所以,我有一个脚本来定义一个服务,我可以在 mongo 上使用更改流。我用 Yargs 做的,所以我可以将所有参数传递给应用程序。但是当我用 pm2 尝试 yo daemon 它时,我很难用我写的所有参数传递命令。这是服务 yargs 设置:
const yargs = require('yargs');
const mongo = require("mongodb").MongoClient;
const fs = require('fs');
const BSON = require('bson');
const argv = yargs
.command(
'listen', 'Listen to a collection for changes and save it to another',{
collection :{
description : 'the collection you want to listen to',
alias : 'c',
type: 'string',
},
database:{
description: 'the database you want to listen to',
alias : 'db',
type: 'string',
},
uri:{
description: 'the connection string for your mongodb instance',
alias : 'uri',
type: 'string',
},
logbase:{
description: 'the database where the changes will be kept',
alias: 'logdb',
type: 'string',
},
log_collection:{
description: 'the collection where the changes will be kept',
alias: 'logc',
type: 'string',
}
})
.option('verbose', {
alias : 'v',
description: 'Writes on the console the information',
type: 'boolean',
})
.help()
.alias('help', 'h')
.argv;
if(argv._.includes('listen')){
if(!argv.uri || !argv.collection || !argv.db || !argv.log_collection || !argv.logbase){
console.log("Missing parameters!")
if(!argv.uri) console.log("--uri is missing!")
if(!argv.collection) console.log("--collection is missing!")
if(!argv.db) console.log("--db is missing!")
if(!argv.log_collection) console.log("--log_collection is missing!")
if(!argv.logbase) console.log("--log_base is missing!")
}
else{
const uri = argv.uri;
const coll = argv.collection;
const database = argv.db;
const logbase = argv.logbase;
const log_collection = argv.log_collection;
const verbose = argv.verbose;
const token = token_reader(logbase, log_collection, verbose)
if(token){
//if there is a resume token
resume_with_token(token ,uri, database, coll, argv.verbose, logbase, log_collection)
}else {
listen (uri, database, coll, argv.verbose, logbase, log_collection)
}
}
} else {
console.log('Commnad not listed')
}
所以基本上我有以下行可以在我的控制台中运行:
node .\generic_collection_listerner.js listen -c collection_name --uri "mongo_connection_url" --db database --logbase logbase --log_collection log_collection --verbose
提前致谢!