How to upload document in _initialData?


Please give an example of how to upload content of a document attribute of entity in _initialData.

It would also be good to have an example of what call of “setDocument” shall look like in UB-UB client.

UPD: I figured it out, but I think we god some room for improvement here.

First of all, we DO need better documentation on “setDocument” app-level method. I also think we desperately need to master art of working with documents:

  • conception of temp store
    • when to set content of temp store
    • who and when cleans it up
  • work with documents on server, example
  • work with documents on client, web client and ub client, example

Next, we have a bug in UBConnection.runCustom - the aURLParams are not passed down to the xhr method, which is crucial for call runCustom with setDocument endpoint name.

As a workaround, I used the xhr method directly, but it would be good to fix the runCustom.

Another question - whether setDocument call deservce it’s own method in UBConnection?


Code sample I came up for myself is the following:

   fs = require('fs'),
   path = require('path');

function uploadDocument(conn, entityName, code, fn) {
   const result = conn.Repository(entityName)
	    .attrs('ID', 'mi_modifyDate')
	    .where('code', '=', code)

    if (result == null) {
	    console.error(`Cannot find ${entityName} with code ${code}`);
    const {ID, mi_modifyDate} = result;

    const fileName = path.join(__dirname, 'files-to-upload-dir', fn);
    const tempStoreResult = conn.xhr(
		   HTTPMethod: 'POST',
		    endpoint: 'setDocument',
		    data: fs.readFileSync(fileName, {encoding: 'bin'}),
		    URLParams: {
			    entity: entityName,
			    attribute: 'docAttrName',
			    id: ID,
			    origName: fn,
			    filename: fn

	    entity: entityName,
	    method: 'update',
	    execParams: {ID, configuration: JSON.stringify(tempStoreResult.result), mi_modifyDate}


Thanks for sharing code and patch. Merged by this commit. New method UBConnection.prototype.setDocument will be accessible in @unitybase/base@4.2.13