Skip to content

Commit

Permalink
add ing support of partial load of objects
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanBluefox committed Apr 24, 2024
1 parent 219a57f commit 83c3ca0
Show file tree
Hide file tree
Showing 3 changed files with 2,900 additions and 5 deletions.
58 changes: 55 additions & 3 deletions lib/socketCommands.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,9 @@ class SocketCommands {
}
}
return false;
} else {
return true;
}

return true;
}

publish(socket, type, id, obj) {
Expand Down Expand Up @@ -1653,7 +1653,59 @@ class SocketCommands {
if (typeof callback === 'function') {
if (this._checkPermissions(socket, 'getObjectView', callback, search)) {
try {
this.adapter.getObjectView(design, search, params, { user: socket._acl.user }, callback);
if (params.root || params.depth) {
// To save the bandwidth, the request can define root and depth. Default is depth 1.
this.adapter.getObjectView(design, search, params, { user: socket._acl.user }, (err, result) => {
if (result?.rows?.length && result.rows[0].value?._id) {
const rows = [];
// filter rows
const depth = params.depth || 1;
let root = params.root || params.startkey;
let rootWithoutDot;
if (!root.endsWith('.')) {
root += '.';
rootWithoutDot = root;
} else {
rootWithoutDot = root.substring(0, root.length - 1);
}

const rootDepth = root.split('.').length;
const virtualObjects = {};
for (let r = 0; r < result.rows.length; r++) {
if (result.rows[r].value._id.startsWith(root) || result.rows[r].value._id === rootWithoutDot) {
const parts = result.rows[r].id.split('.');
if (parts.length - rootDepth <= depth) {
rows.push(result.rows[r]);
} else {
// create virtual objects to show that there are more objects
for (let d = depth; d < parts.length - rootDepth; d++) {
const id = parts.slice(0, rootDepth + d).join('.');
if (!virtualObjects[id]) {
virtualObjects[id] = {
id,
value: {
_id: id,
common: {},
type: 'folder',
virtual: true,
hasChildren: 1,
},
};
rows.push(virtualObjects[id]);
} else {
virtualObjects[id].value.hasChildren++;
}
}
}
}
}
result.rows = rows;
}
callback(err, result);
});
} else {
this.adapter.getObjectView(design, search, params, { user: socket._acl.user }, callback);
}
} catch (error) {
this.adapter.log.error(`[getObjectView] ERROR: ${error.toString()}`);
SocketCommands._fixCallback(callback, error);
Expand Down
Loading

0 comments on commit 83c3ca0

Please sign in to comment.