Skip to content

Instantly share code, notes, and snippets.

@barisusakli
Last active June 17, 2019 21:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barisusakli/65a903d037f17aa3c50e212bd557ee2e to your computer and use it in GitHub Desktop.
Save barisusakli/65a903d037f17aa3c50e212bd557ee2e to your computer and use it in GitHub Desktop.
category info
/*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