I would like to develop a web app where a user logs in and has to complete different steps uploading files and submitting forms. How could I create a database structure where all the information and files that the user submits is stored in the same place associated to the userId or username?
You would have to create a new node per user in the item layer and then either create subnodes or use properties of the node to store data (one or another, based on data complexity).
Something like this:
var nodeLib = require('/lib/xp/node');
var authLib = require('/lib/xp/auth');
var valueLib = require('/lib/xp/value');
var REPO_NAME = 'my_repo';
var NODE_PATH = 'my_path';
var ROOT_PERMISSIONS = [
{
principal: 'role:system.everyone',
allow: [
'READ',
'CREATE',
'MODIFY',
'DELETE',
'PUBLISH',
'READ_PERMISSIONS',
'WRITE_PERMISSIONS'
],
deny: []
}
];
function connect() {
return nodeLib.connect({
repoId: REPO_NAME,
branch: 'master'
});
}
function createUserNode(repoConn, userId) {
var result = repoConn.create({
_permissions: ROOT_PERMISSIONS,
_parentPath: NODE_PATH,
userId: userId
});
return result;
}
function createSubNode(repoConn, userNode, dataObj) {
var result = repoConn.create({
_parentPath: userNode._path,
_permissions: ROOT_PERMISSIONS,
data: dataObj,
userId: userNode.userId
});
return result;
}
function getUserNode(repoConn, userId) {
var queryResult = repoConn.query({
start: 0,
count: 1,
query: '_parentPath = "' + NODE_PATH + '" and userId = "' + userId + '"'
});
if (queryResult.count > 0) {
return repoConn.get(queryResult.hits[0].id);
}
return null;
}
function saveDataObj(user, dataObj) {
var repoConn = connect();
var userNode = getUserNode(repoConn, user.key);
if (!userNode) {
userNode = createUserNode(repoConn, user.key);
}
createSubNode(repoConn, user, dataObj);
}
Use the built-in security mechanisms of the node layer (create the node so only special users can read an item)
Tag each item with a propety that holds the username
Make a structure (like alan says) per user (could be combined with 1 and 2)
In the end, it’s always nice to see how other solusions do this: I recommend you check out how the office league app stores data. And maybe also see how the CMS does this. It’s all open source.
Use the data toolbox utility from market to look at the data!