Server-side TypeScript
Overview
You can write callable Profound.js modules using TypeScript with just a few requirements:
Implement your logic within the class constructor - Callable modules are just a single function call, and the constructor is what Profound.js runs. When the module is loaded, Profound.js compiles the TypeScript, unwraps the compiled class, and exports it as the module’s
runentry point (and also under the class name), so the constructor body is the module.Declare global variables outside of the class - Within Profound.js, you strongly define variables with the
pjs.define()API. Thedeclarestatements are needed because the TypeScript compiler requires all variables to be defined.Include the
.tsfile extension - When calling a TypeScript module, you must include the.tsextension on the pjs.call() API.
TypeScript modules are compiled with --target ES5 --module commonjs. Newer language features are down-levelled to ES5 by the compiler, so a module that depends on native ES2015+ runtime behavior may not behave the same way it would under a newer target.
Calling a TypeScript Module from JavaScript
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.trim().length; // trim() first: char fields are blank-padded to their full length
p3 = new Date();
}
}Running jsmodule.js prints the three values that tsmodule1.ts placed in the parameters. field1 is blank-padded to 25 characters because char fields are fixed length, which is also why tsmodule1.ts calls trim() before taking the length:
Expected output
field1: Here is some text
field2: 17
field3: 2026-09-15T14:32:07.412ZImportant:
Notice that the variable
pjsis declared outside of the class.Notice that the logic is implemented within the class constructor.
Using a TypeScript Module as a Web Service
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 web service in Profound.js, open your start.js and add the standard profoundjs.express() line. Just remember to include the .ts file extension:
start.js
app.get("/tsmodule2", profoundjs.express("pjstest/tsmodule2.ts"));To register this as a web service in Profound.js Spaces, right-click the file, choose Properties, and then set up the routing path.
Calling TypeScript Classes from a TypeScript Module
Here is an example of a callable TypeScript module calling functions of a TypeScript class that does not use any Profound.js 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));
}
}Loading the class with pjs.require() (as shown above) makes the file eligible for hot module reloading.
Importing Third-Party Packages
Here is an example of a callable TypeScript module calling functions, or accessing properties, of a TypeScript class from a third-party package import:
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");
}
}Debugging Compiled TypeScript
When a TypeScript module is loaded, Profound.js compiles it to JavaScript in a .ts-out directory alongside modules, transforms that JavaScript, and then deletes the compiled file. To keep the compiled JavaScript on disk so you can inspect what the compiler produced, start the server with the --debugts argument:
Keep compiled TypeScript output
node start --debugts