You can write callable Profound.js modules using typescript with just a couple requirements.
Here is an example of a javascript module (jsmodule.js) calling a typescript module (tsmodule1.ts):
Code Block |
---|
language | js |
---|
title | 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
Code Block |
---|
language | js |
---|
title | 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();
}
} |
Info |
---|
|
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:
Code Block |
---|
language | js |
---|
title | 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.
Code Block |
---|
|
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)
Code Block |
---|
language | js |
---|
title | 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,
Code Block |
---|
language | js |
---|
title | 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");
}
} |