letsaveData=awaitg.sys.db.saveSingleOrMultiple({instance:"INSTANCE_NAME",database:'DB_NAME',collection:"COLLECTION_NAME",saveData:{},headers:{}},true);if(saveData.success===false)throw'Your data is not saved!';elsereturnsaveData;
import*asTfrom'types';import*asfsfrom'fs';import*aspathfrom'path';asyncfunctionmain(g:T.IAMGlobal){// always write file in uploads directlry which you want to send to user.// you can write other files in any directory which you don't want to send to user.letfilePath=path.join(__dirname,'uploads','myfile.txt');awaitfs.promises.writeFile(filePath,'file 1 content',{encoding:'utf8'});return{// just provide file name which is written to "uploads" directory__am__downloadFilePath:'myfile.txt',__am__downloadFolderFileName:'newName.txt',// download file will have this name.}};module.exports=main;
import*asTfrom'types';import*asfsfrom'fs';import*aspathfrom'path';asyncfunctionmain(g:T.IAMGlobal){return{__am__downloadFileOrFolderPaths:[{fsSource:path.join('folder1','folder2','folder3','data_folder'),// path into uploads folderarchiveDestination:'data_folder',// path in zip file, so data_folder folder will be directly available in zip file.},],__am__downloadFolderFileName:`output.zip`,// download file will have this name.}};module.exports=main;
import*asTfrom'types';import*asfsfrom'fs';import*aspathfrom'path';asyncfunctionmain(g:T.IAMGlobal){// always write file in uploads directlry which you want to send to user.// you can write other files in any directory which you don't want to send to user.letfilePath=path.join(__dirname,'uploads','myfile.txt');awaitfs.promises.writeFile(filePath,'file 1 content',{encoding:'utf8'});return{// You can provide list of files or folders, it will will make zip and send to user.__am__downloadFileOrFolderPaths:['myfile.txt',// file name'someFolder',// folder name],__am__downloadFolderFileName:'zipFile.zip',// this will be zip file name}};module.exports=main;
asyncfunctiondownloadFileFromBrowser(){constrequestPayload={};constrequestHeaders={};constresp=<any>awaitthis.http.post(`API Maker custom API endpoint`,requestPayload,{headers:requestHeaders,responseType:<any>'text',observe:'response',}).toPromise();constfileName=resp.headers.get('content-filename');awaitthis.downloadFileFromBlob('application/zip',resp.body,fileName);returnresp;}asyncfunctiondownloadFileFromBlob(contentType,base64Data:any,fileName){consta=document.createElement('a');document.body.appendChild(a);a.style.display='none';letblob;if(base64DatainstanceofBlob){blob=base64Data;}else{constbase64Response=awaitfetch(`data:${contentType};base64,`+base64Data);blob=awaitbase64Response.blob();}consturl=window.URL.createObjectURL(blob);a.href=url;a.download=fileName;a.click();window.URL.revokeObjectURL(url);}
It reads the tenant in g.req.params, and the APIs it calls with g.sys.db and g.sys.system work on the database of that tenant: the header is passed on, and each instance finds the tenant in its own tenants table.
instance: 'crm::globex' names another tenant for one call, and an empty tenant header names the structure database. See Custom APIs of a multi-tenant instance.
import*asTfrom'types';asyncfunctionmain(g:T.IAMGlobal){consttenant=g.req.params.tenantUsername;// 'acme'constcustomers=awaitg.sys.db.getAll({instance:'crm',database:'crm',collection:'customers'});// from the database of acmeconstcustomersOfGlobex=awaitg.sys.db.count({instance:'crm::globex',database:'crm',collection:'customers'});return{tenant,customers,customersOfGlobex};}module.exports=main;