Last active
June 17, 2019 21:04
-
-
Save barisusakli/65a903d037f17aa3c50e212bd557ee2e to your computer and use it in GitHub Desktop.
category info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*globals require, console, process */ | |
'use strict'; | |
var nconf = require('nconf'); | |
var async = require('async'); | |
nconf.file({ | |
file: 'config.json' | |
}); | |
nconf.defaults({ | |
base_dir: __dirname, | |
views_dir: './build/public/templates', | |
upload_path: 'public/uploads', | |
}); | |
var db = require('./src/database'); | |
var result = {}; | |
db.init(function(err) { | |
if (err) { | |
console.log("NodeBB could not connect to your database. Error: " + err.message); | |
process.exit(); | |
} | |
printCategoryTree(function (err) { | |
if (err) { | |
console.error(err); | |
process.exit(); | |
} | |
console.log('done'); | |
console.log(JSON.stringify(result, null, 4)); | |
process.exit(); | |
}); | |
}); | |
function printCategoryTree(callback) { | |
async.waterfall([ | |
function (next) { | |
db.getSortedSetRange('categories:cid', 0, -1, next); | |
}, | |
function (cids, next) { | |
async.eachSeries(cids, function (cid, next) { | |
async.parallel({ | |
category: function (next) { | |
db.getObject('category:' + cid, next); | |
}, | |
children: function (next) { | |
db.getSortedSetRange('cid:' + cid + ':children', 0, -1, next); | |
}, | |
}, function (err, data) { | |
if (err) { | |
return next(err); | |
} | |
result[cid] = { | |
parentCid: data.category.parentCid, | |
children: data.children, | |
}; | |
next(); | |
}); | |
}, next); | |
}, | |
], callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment