Skip to main content
All CollectionsGeneral questionsSection heading
How to retrieve all the user-owned custom entities
How to retrieve all the user-owned custom entities

saving user-owned custom entities to a global property

Jason Liang avatar
Written by Jason Liang
Updated over 3 years ago

For the case you want to save user-owned custom entities, you can follow the steps below to achieve it.

  • Go to Design | Custom Config | Global Properties page of brainCloud portal, add a new property. We will use this property to store data of user-owned custom entities.

  • Create a cloud code script that retrieves current user-owned custom entities and append the data to the global property we created.

"use strict";

function main() {

var response = {};

bridge.logDebugJson("Script inputs", data);

var propname = "backupCustomEntities";
var prop = bridge.getGlobalProperty(propname);

var propJson = JSON.parse(prop);

var aEntitiesWithTypes = [];

//need put the data retreive time to out put
var timeProxy = bridge.getTimeServiceProxy();
var timeResult = timeProxy.readServerTime();

if (timeResult.status == 200) {
var retrivedTimeMilli = timeResult.data.server_time;
//convert to date in UTC
var retrivedTime = new Date(retrivedTimeMilli);
// var retrivedTime = timeResult.data.server_time;

var profileId = bridge.getProfileId();

//use pagination to determin if need to get next page
var maxperpage = 100;
var pageNumber = 1;

var customEntityProxy = bridge.getCustomEntityServiceProxy();

var collectionResult = customEntityProxy.sysListCollections();
if (collectionResult.status == 200) {
collectionResult.data.collections.forEach(item=>{
bridge.logInfo("item", Object.keys(item)[0]);

var entityType = Object.keys(item)[0];
var aEntities = [];
var context = {
"pagination": {
"rowsPerPage": maxperpage,
"pageNumber": pageNumber
},
"searchCriteria": {},
"sortCriteria": {
"createdAt": 1
}
};


bridge.logInfo("item.collectionOptions.isOwned", item[entityType].collectionOptions.isOwned);

// filter the types which only isOwned as true
if (item[entityType].collectionOptions.isOwned){
var customEntityProxy = bridge.getCustomEntityServiceProxy();

var firstPageResult = customEntityProxy.getEntityPage(entityType, context);
var count = firstPageResult.data.results.count;

// response.count = count;

if(count>0){

firstPageResult.data.results.items.forEach(entity=>{
if (entity.ownerId == profileId) aEntities.push(entity);
});

//if return items count great than maxperpage will get into belows for loop to call next page, untill scan all
var remainder = 0;
var numberpage = 0;
remainder = count % maxperpage;
numberpage = (count-remainder)/maxperpage;

for (var n = 0; n < numberpage; n++ ){

pageNumber ++;

context.pagination.pageNumber = pageNumber;

var nextPageResult = customEntityProxy.getEntityPage(entityType, context);
if (nextPageResult.status == 200) {
nextPageResult.data.results.items.forEach(entity=>{
if (entity.ownerId == profileId) aEntities.push(entity);
});
}
}
}

if(aEntities.length > 0){
var objEntitiesWithType = {};

objEntitiesWithType[entityType+" count"] = count;

objEntitiesWithType[entityType] = aEntities;

aEntitiesWithTypes.push(objEntitiesWithType);

// aEntitiesWithTypes.push({[entityType]: aEntities});
}
}
});
}

bridge.logInfoJson("aEntitiesWithTypes", {"aEntitiesWithTypes":aEntitiesWithTypes});

var objProfile = {};

objProfile[profileId] = aEntitiesWithTypes;

var objretrivedTime = {};

objretrivedTime[retrivedTime + profileId] = objProfile;

Object.assign(propJson,objretrivedTime);

}

response.propJson = propJson;

var globalAppProxy = bridge.getGlobalAppServiceProxy();

globalAppProxy.sysUpdatePropertyJson(propname, propJson);

return response;

}

main();
  • Browse to S2S Explorer and call RunBatchUserScript() method, put the script name to script name field, leave other fields blank as below. (Note: if you have not set any servers yet, you need to create one from My Servers Setting page)

  • The above operation will loop through all the users in your app and append user-owned custom entities to this global property, then you can easily copy down and back up the data to any place you want.

{
"purpose": "backup users custom entities",
"Wed Jan 20 2021 04:44:52 GMT-0000 (UTC)bb8895c6-87a6-44b9-9cb7-ffc5ccb0b7c9": {
"bb8895c6-87a6-44b9-9cb7-ffc5ccb0b7c9": [
{
"athletes3 count": 1,
"athletes3": [
{
"entityId": "e3de6c51-6610-48fe-97c3-5552377779e6",
"version": 3,
"acl": {
"other": 1
},
"ownerId": "bb8895c6-87a6-44b9-9cb7-ffc5ccb0b7c9",
"expiresAt": null,
"timeToLive": null,
"createdAt": 1.605760450316E12,
"updatedAt": 1.605760954229E12,
"entityType": "athletes3",
"data": {
"playerName": "Randy Traywick",
"playerId": 123456,
"teamName": "Super Stars",
"jerseyNum": 44
}
}
]
}
]
},
"Wed Jan 20 2021 04:44:52 GMT-0000 (UTC)66309efe-e35a-426d-9334-7ac4feaffb05": {
"66309efe-e35a-426d-9334-7ac4feaffb05": []
},
"Wed Jan 20 2021 04:44:52 GMT-0000 (UTC)23505cd6-b07c-4304-bb07-87e882b6a525": {
"23505cd6-b07c-4304-bb07-87e882b6a525": [
{
"athletes2 count": 2,
"athletes2": [
{
"entityId": "f95be184-63b8-4b6f-a5a6-e20eaa37e58f",
"version": 1,
"acl": {
"other": 1
},
"ownerId": "23505cd6-b07c-4304-bb07-87e882b6a525",
"expiresAt": null,
"timeToLive": null,
"createdAt": 1.604070305523E12,
"updatedAt": 1.604070305523E12,
"entityType": "athletes2",
"data": {
"playerName": "Jacky Traywick",
"playerId": 123456,
"teamName": "Super Stars",
"jerseyNum": 44
}
},
{
"entityId": "19dc82d2-3d24-48ea-be51-7e5e6fa5dbae",
"version": 1,
"acl": {
"other": 1
},
"ownerId": "23505cd6-b07c-4304-bb07-87e882b6a525",
"expiresAt": null,
"timeToLive": null,
"createdAt": 1.604070316183E12,
"updatedAt": 1.604070316183E12,
"entityType": "athletes2",
"data": {
"playerName": "Jacy Traywick",
"playerId": 123456,
"teamName": "Super Stars",
"jerseyNum": 44
}
}
]
}
]
},
"Wed Jan 20 2021 04:44:52 GMT-0000 (UTC)5bc21dfd-610b-49b6-9f82-245d3d0187e8": {
"5bc21dfd-610b-49b6-9f82-245d3d0187e8": []
},
"Wed Jan 20 2021 04:44:52 GMT-0000 (UTC)fb9c3874-07d1-48fd-8216-1bb7561ae2b4": {
"fb9c3874-07d1-48fd-8216-1bb7561ae2b4": []
},
"Wed Jan 20 2021 04:44:52 GMT-0000 (UTC)35620730-e2f0-468f-bc1d-700942de0b87": {
"35620730-e2f0-468f-bc1d-700942de0b87": []
},
"Wed Jan 20 2021 04:44:52 GMT-0000 (UTC)493dd9ad-5196-4494-aab1-02fbffa4c7af": {
"493dd9ad-5196-4494-aab1-02fbffa4c7af": []
},
"Wed Jan 20 2021 04:44:53 GMT-0000 (UTC)16dfe0a9-2efa-47c7-9a73-70ca9dda2d60": {
"16dfe0a9-2efa-47c7-9a73-70ca9dda2d60": []
}
}
Did this answer your question?