Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Current »

Content Freeze

As of July 25th, 2023, there is a content freeze on this page.

You can write callable Profound.js modules using typescript with just a couple requirements.

  • Implement your logic within the class's constructor

    • Callable modules are just a single function call
  • Define global variables outside of the class

    • Within Profound.js, you strongly define variables with the pjs.define() API.
    • This is needed because typescript compile requires all variables to be defined
  • When calling a typescript module, you must include the ".ts" file extension on the pjs.call() API.


Here is an example of a javascript module (jsmodule.js) calling a typescript module (tsmodule1.ts):

jsmodule.js
function jsmodule() {
  pjs.define("field1", { type: 'char', length: 25 });
  pjs.define("field2", { type: 'integer', length: 10 });
  pjs.define("field3", { type: 'timestamp'});

  // Call a Typescript module and get back values
  pjs.call("tsmodule1.ts", field1, field2, field3);

  // See the results that were returned
  console.log("field1:", field1);
  console.log("field2:", field2);
  console.log("field3:", field3.toISOString());
  
}
exports.run = jsmodule;


Here is that typescript module

tsmodule1.ts
declare var pjs;

export class tsmodule1 {
    constructor(p1:any, p2:any, p3:any) {
        pjs.define("p1", { type: 'char', length: 25, refParm: p1 });
        pjs.define("p2", { type: 'integer', length: 10, refParm: p2 });
        pjs.define("p3", { type: 'timestamp', refParm: p3 });

        p1 = "Here is some text";
        p2 = p1.length;
        p3 = new Date();
    }
}

Important

1) Notice that variable pjs is declared outside of the class.

2) Notice that the logic is implemented within the class constructor


Here is an example of a typescript module used as a web service:

tsmodule2.ts
declare var pjs;
declare var productsp;
declare var pds;

export class tsmodule2 {
    constructor(request: any, res: any) {
        pjs.defineTable("productsp", "pjstest/productsp", { read: true, keyed: true });
        pjs.define("pds", { type: 'data structure', likeRec: 'products' });

        var pid = request.query.pid;
        if (pid)
            pid = Number(pid.trim());

        if (!pid || isNaN(pid)) {
            res.send(`Query parameter pid is required to be a number. Value sent was [${request.query.pid}]`);
            return;
        }

        productsp.getRecord([pid], false, null, pjs.ds("pds"));

        res.status(200).json(pjs.toObject(pds));
    }
}

To register this as a webservice in Profound.js, open your start.js and add the standard line, just remember to add the .ts file extension.

start.js
app.get("/tsmodule2", profoundjs.express("pjstest/tsmodule2.js"));

To register this as a webservice in Profound.js Spaces, right click on that file and choose properties, and then setup the routing path.


Here is an example of a callable typescript module calling some functions of a typescript class (that does not use any pjs APIs)

tsmodule3.ts
declare var pjs;
declare var productsp;
declare var pds;

export class tsmodule3 {
    constructor(request: any, res: any) {
        pjs.defineTable("productsp", "pjstest/productsp", { read: true, keyed: true });
        pjs.define("pds", { type: 'data structure', likeRec: 'products' });

        var pid = request.query.pid;
        if (pid) {
			pid = Number(pid.trim());
		}

        if (!pid || isNaN(pid)) {
            res.send(`Query parameter pid is required to be a number. Value sent was [${request.query.pid}]`);
            return;
        }



        // Using the pjs.require API allows for this file to be eligible for hotReloading
        let tsclass2: any = pjs.require("tsclass2.ts");
        pds = tsclass2.getRecord(pid);


        res.status(200).json(pjs.toObject(pds));
    }
}


Here is an example of a callable typescript module calling some functions, or accessing properties of a typescript class, such as 3rd party files imports,

tsmodule4.ts
import { ts1, ts2, ts3 } from "anotherPackage";


export class tsmodule4 {
    constructor(request: any, response: any) {

		let t1:any = ts1.myFunc(request);
        if (ts1.isValid) {
			response.status(200).json(t1.result);
			return;
		}


        if (!ts1.isValid) {
			let t2:any = ts2.myFunc(request);
	        response.status(200).json(t2.result);
			return;
        }


        response.status(500).send("Unknown error occurred");
    }
}




  • No labels