picorules-compiler-js-core
v1.0.7
Published
Pure JavaScript/TypeScript compiler for Picorules clinical decision support language
Maintainers
Readme
Picorules Compiler JS Core
A pure JavaScript/TypeScript compiler for the Picorules clinical decision support language, designed to compile Picorules ruleblocks into optimized SQL for Oracle PL/SQL, SQL Server T-SQL, and PostgreSQL.
Status
✅ v1.0.0 - Production Ready
Features
- ✅ Pure TypeScript: Full type safety and IDE support
- ✅ Multi-Dialect: Supports Oracle PL/SQL, SQL Server T-SQL, and PostgreSQL
- ✅ 24 Functions: Complete Picorules function library
- ✅ Cross-Ruleblock References: Automatic dependency resolution
- ✅ Transformation Pipeline: Subset selection and pruning
- ✅ Zero Dependencies: Only runtime dependency is Zod for validation
- ✅ Tree-Shakeable: ESM and CJS builds
- ✅ Fully Tested: 104 tests with 100% success rate
Installation
npm install @picorules/compiler-coreQuick Start
import { compile, Dialect } from '@picorules/compiler-core';
const ruleblocks = [
{
name: 'ckd',
text: `
egfr_last => eadv.lab_bld_egfr.val.last();
has_ckd : {egfr_last < 60 => 1}, {=> 0};
`,
isActive: true,
},
];
const result = compile(ruleblocks, { dialect: Dialect.ORACLE });
if (result.success) {
console.log(result.sql[0]);
// Outputs optimized Oracle SQL with CTEs
} else {
console.error(result.errors);
}Supported Functions
Basic (3): last(), first(), count()
Aggregation (6): sum(), avg(), min(), max(), median(), distinct_count()
Date-Value Window (4): lastdv(), firstdv(), maxldv(), minldv()
Positional Window (2): nth(n), minfdv()
String (4): serialize(delimiter), serialize2(delimiter), serializedv(delimiter), serializedv2(delimiter)
Statistical (4): regr_slope(), regr_intercept(), regr_r2(), stats_mode()
Existence (1): exists()
Advanced (2): max_neg_delta_dv(), temporal_regularity()
Advanced Usage
Cross-Ruleblock References
const ruleblocks = [
{
name: 'base',
text: 'egfr_last => eadv.lab_bld_egfr.val.last();',
isActive: true,
},
{
name: 'derived',
text: `
egfr => rout_base.egfr_last.val.bind();
has_ckd : {egfr < 60 => 1}, {=> 0};
`,
isActive: true,
},
];
const result = compile(ruleblocks, { dialect: Dialect.ORACLE });
// Automatically orders: base first, then derivedSubset Selection
Compile only specific ruleblocks:
compile(ruleblocks, {
dialect: Dialect.ORACLE,
subset: ['ckd', 'anemia'], // Only compile these
});Pruning Transformations
Output Pruning - Keep only ancestors:
compile(ruleblocks, {
dialect: Dialect.ORACLE,
pruneOutputs: ['dashboard'], // Keep dashboard + its dependencies
});Input Pruning - Keep only descendants:
compile(ruleblocks, {
dialect: Dialect.ORACLE,
pruneInputs: ['source'], // Keep source + what depends on it
});Combined - Keep only the path:
compile(ruleblocks, {
dialect: Dialect.ORACLE,
pruneInputs: ['eadv'],
pruneOutputs: ['report'], // Only the path from eadv to report
});API Reference
compile(ruleblocks, options)
Parameters:
ruleblocks: RuleblockInput[]name: string- Ruleblock nametext: string- Picorules codeisActive: boolean- Whether active
options: CompilerOptionsdialect: Dialect- ORACLE, MSSQL, or POSTGRESQLsubset?: string[]- Filter ruleblockspruneInputs?: string[]- Keep descendantspruneOutputs?: string[]- Keep ancestors
Returns: CompilationResult
success: booleansql: string[]errors: Error[]warnings: Warning[]metrics?: Metrics
SQL Dialect Support
Oracle PL/SQL
CREATE TABLE ASfor table creationLISTAGGfor string aggregationMEDIAN()built-in functionREGR_*built-in regression functionsUSING (eid)for join syntaxSYSDATEfor current date
SQL Server T-SQL
SELECT INTOfor table creationSTRING_AGGfor string aggregationPERCENTILE_CONTfor median- Manual regression formulas
ON ... =for join syntaxGETDATE()for current date
PostgreSQL
CREATE TABLE ASfor table creationSTRING_AGGwith embeddedORDER BYfor string aggregationPERCENTILE_CONTfor median- Manual regression formulas
USING (eid)for join syntaxCURRENT_DATEfor current dateINTERVALsyntax for date arithmetic::typecasting syntax
Development
# Install
npm install
# Build
npm run build
# Test
npm test
# Lint
npm run lintPerformance
- Single ruleblock: < 1ms
- 10 ruleblocks: < 10ms
- 100 ruleblocks: < 100ms
Architecture
4-stage pipeline:
- Parse: Text → AST
- Link: Dependency resolution
- Transform: Subset/pruning
- Generate: AST → SQL
License
MIT
Credits
Developed for The Kidney Centre (TKC) clinical decision support system.
