Skip to content

Instantly share code, notes, and snippets.

@mindspank
Created June 15, 2016 18:50
Show Gist options
  • Save mindspank/14bea23989312b16e4f244836d4ce4ae to your computer and use it in GitHub Desktop.
Save mindspank/14bea23989312b16e4f244836d4ce4ae to your computer and use it in GitHub Desktop.
const qsocks = require('qsocks');
const fs = require('fs');
const Promise = require('bluebird');
var config = {
host: 'localhost',
port: 4747,
headers: {
'X-Qlik-User': 'UserDirectory=INTERNAL;UserId=sa_repository'
},
isSecure: true,
cert: fs.readFileSync('./certs/localhost/client.pem'),
key: fs.readFileSync('./certs/localhost/client_key.pem'),
rejectUnauthorized: false
};
const EXCLUSION_LIST = [
'sheet',
'appprops',
'measure',
'dimension',
'hiddenbookmark',
'story',
'slide',
'slideitem',
'LoadModel',
'masterobject',
'snapshot',
'embeddedsnapshot',
'bookmark'
];
createSearchApp().then(doc => {
doSearch(doc, ['Touchdown'])
.then( result => console.log(result) )
.catch(err => console.log(err))
});
function doSearch(doc, term) {
if ( typeof doc !== 'object' ) return new Error('Missing document connection');
var struct = {
qCount: -1,
qOffset: 0,
qGroupOptions: [{
qCount: -1,
qOffset: 0,
qGroupType: 'GenericObjectsType'
}],
qGroupItemOptions: [{
qCount: -1,
qOffset: 0,
qGroupItemType: 'GenericObject'
}]
};
return doc.searchObjects({ qAttributes: ['qProperty'] }, term, struct);
};
function createSearchApp() {
return qsocks.Connect(config)
.then( global => global.getDocList() )
.then( doclist => {
return Promise.all(doclist.map(doc => {
return fetchAppObjects(doc.qDocId)
}));
})
.then(objects => {
var sessionconfig = config;
sessionconfig.identity = 'sessionapp' + Math.random();
return qsocks.Connect(sessionconfig)
.then( global => global.createSessionApp())
.then( doc => {
return Promise.all( objects.reduce( (prev, current) => {
return prev.concat( current );
}, [])
.map( obj => {
return doc.createObject( obj.qProperty )
}))
.then( () => doc )
})
});
};
function fetchAppObjects(appid) {
var c = {} = config;
c.appname = appid;
return qsocks.Connect(c)
.then( global => global.openDoc(appid, '', '', '', true) )
.then( doc => {
return doc.getAllInfos().then( list => {
return Promise.all( list.qInfos.filter(filterAppObjectList).map( object => {
return doc.getObject( object.qId ).then( model => model.getFullPropertyTree() )
}))
})
})
};
function filterAppObjectList(object) {
return EXCLUSION_LIST.indexOf(object.qType) === -1
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment