node-client-api icon indicating copy to clipboard operation
node-client-api copied to clipboard

marklogic.config.transform.write can't provide transform parameter metadata

Open rjdew-progress opened this issue 1 month ago • 0 comments

The REST API allows setting metadata for parameters the transform uses, but the NodeJS client doesn't provide the same capability.

See REST API trans:{name}* in the documentation at https://docs.marklogic.com/REST/PUT/v1/config/transforms/[name].

Workaround:

import path from 'path';

const marklogic = require('marklogic');
const requester = require(path.join(__dirname, '..', 'node_modules', 'marklogic', 'lib', 'requester'));
const Operation = require(path.join(__dirname, '..', 'node_modules', 'marklogic', 'lib', 'operation'));


function writeTransform(db: any, {name, title, description, provider, version, format, source, parameters = []}: any) {
  if ((name == null) || (format == null) ||
      (source == null)) {
    throw new Error('must specify name, format, and source when writing a transform');
  }

  let contentType = null;
  switch(format) {
  case 'javascript':
    contentType = 'application/javascript';
    break;
  case 'xquery':
    contentType = 'application/xquery';
    break;
  case 'xslt':
    contentType = 'application/xslt+xml';
    break;
  default:
    throw new Error('unsupported transform format '+format);
  }

  let endpoint = '/v1/config/transforms/'+encodeURIComponent(name);

  let sep = '?';
  if (title != null) {
    endpoint += sep+'title='+encodeURIComponent(title);
    sep = '&';
  }
  if (description != null) {
    endpoint += sep+'description='+encodeURIComponent(description);
    if (sep === '?') {sep = '&';}
  }
  if (provider != null) {
    endpoint += sep+'provider='+encodeURIComponent(provider);
    if (sep === '?') {sep = '&';}
  }
  if (version != null) {
    endpoint += sep+'version='+encodeURIComponent(version);
    if (sep === '?') {sep = '&';}
  }
  // custom code for parameter metadata
  for (const parameter of parameters) {
    endpoint += sep+'trans:'+encodeURIComponent(parameter['parameter-name'])+'='+encodeURIComponent(parameter['parameter-type']);
    if (sep === '?') {sep = '&';}
  }

  const requestOptions = {...db.getConnectionParams()};
  requestOptions.method = 'PUT';
  requestOptions.headers = {
      'Content-Type': contentType
  };
  requestOptions.path = endpoint;

  const operation = new Operation(
      'write transform', db, requestOptions, 'single', 'empty'
      );
  operation.name            = name;
  operation.requestBody     = source;
  operation.outputTransform = () => ({name});
  operation.errorTransform  = (message => message+' (on '+name+' transform)');

  return requester.startRequest(operation);
}

rjdew-progress avatar Nov 04 '25 16:11 rjdew-progress