├── .gitignore ├── src ├── ile │ ├── sum.rpgle │ ├── sum.sql │ └── people.sqlrpgle ├── index.ts ├── routes │ └── root.ts └── db │ └── index.ts ├── .env.example ├── package.json ├── .vscode ├── launch.json └── actions.json ├── README.md └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | build 3 | .env -------------------------------------------------------------------------------- /src/ile/sum.rpgle: -------------------------------------------------------------------------------- 1 | **FREE 2 | 3 | Dcl-Pi SUM; 4 | numa int(10); 5 | numb int(10); 6 | result int(10); 7 | End-Pi; 8 | 9 | result = numa + numb; 10 | 11 | Return; -------------------------------------------------------------------------------- /.env.example: -------------------------------------------------------------------------------- 1 | PORT=3000 2 | DB_HOST=hostname 3 | DB_ID=userprofile 4 | DB_PASSWORD=x 5 | 6 | # DB_DBQ can be used if you want to set a path list/library list for each job in the pool -------------------------------------------------------------------------------- /src/ile/sum.sql: -------------------------------------------------------------------------------- 1 | create or replace procedure X.sumpgm (IN numa INT, IN numb INT, OUT result INT) 2 | LANGUAGE RPGLE 3 | EXTERNAL NAME X.SUM GENERAL; 4 | 5 | call X.SUMPGM(1, 2, default); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "scripts": { 3 | "build": "tsc", 4 | "build:dev": "tsc --sourceMap", 5 | "start": "tsc && node build/index.js" 6 | }, 7 | "devDependencies": { 8 | "@types/express": "^4.17.17", 9 | "@types/node": "^20.2.5", 10 | "typescript": "^5.1.3" 11 | }, 12 | "dependencies": { 13 | "body-parser": "^1.20.2", 14 | "express": "^4.18.2", 15 | "odbc": "^2.4.9" 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import { json } from "body-parser"; 3 | 4 | import db, { connectionString } from "./db"; 5 | import root from "./routes/root"; 6 | 7 | const app = express(); 8 | const port = process.env.PORT || 3000; 9 | 10 | app.use(json()); 11 | 12 | app.use(root); 13 | 14 | db.connect(connectionString).then(() => { 15 | app.listen(port, () => { 16 | console.log(`Example app listening on port ${port}`) 17 | }); 18 | }); -------------------------------------------------------------------------------- /src/ile/people.sqlrpgle: -------------------------------------------------------------------------------- 1 | **FREE 2 | 3 | Dcl-Pi RESULTTEST; 4 | End-Pi; 5 | 6 | Dcl-S rowCount Int(10); 7 | Dcl-Ds resultSet Dim(5) Qualified; 8 | Name varchar(20); 9 | Money packed(11:2); //SQL decimal 10 | Email varchar(32); 11 | End-Ds; 12 | 13 | resultSet(1).Name = 'Liam'; 14 | resultSet(1).Money = 20.00; 15 | resultSet(1).Email = 'liam@me.com'; 16 | 17 | resultSet(2).Name = 'Beth'; 18 | resultSet(2).Money = 9876543.21; 19 | resultSet(2).Email = 'beth@test.com'; 20 | 21 | resultSet(3).Name = 'Steph'; 22 | resultSet(3).Money = 12345678.90; 23 | resultSet(3).Email = 'steph@test.com'; 24 | 25 | rowCount = 3; 26 | 27 | Exec SQL Set Result Sets Array :resultSet For :rowCount Rows; 28 | 29 | Return; -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | "version": "0.2.0", 6 | "configurations": [ 7 | { 8 | "type": "node", 9 | "request": "launch", 10 | "name": "Launch Program", 11 | "skipFiles": [ 12 | "/**" 13 | ], 14 | "program": "${workspaceFolder}/build/index.js", 15 | "sourceMaps": true, 16 | "outFiles": [ 17 | "${workspaceFolder}/build/**/*.js" 18 | ], 19 | "preLaunchTask": "npm: build:dev", 20 | "envFile": "${workspaceFolder}/.env" 21 | } 22 | ] 23 | } -------------------------------------------------------------------------------- /src/routes/root.ts: -------------------------------------------------------------------------------- 1 | import express from "express"; 2 | import db from "../db"; 3 | 4 | const root = express.Router(); 5 | 6 | root.get('/', async (req, res) => { 7 | //let result = await db.callProcedure(null, 'LIBRARY', 'PROCEDURE', ["P", 4, "R", 4, "M", 3, "T", 3, "R", 2]); 8 | //let result = await db.query("SELECT * FROM TABLE WHERE COLUMN = ?", [1]); 9 | res.send('Hello world!'); 10 | }); 11 | 12 | // root.get('/test', async (req, res) => { 13 | // let result = await db.query("SELECT * FROM SAMPLE.EMPLOYEE"); 14 | 15 | // res.json(result); 16 | // }); 17 | 18 | // root.get('/people', async (req, res) => { 19 | // let result = await db.query("call X.people()"); 20 | 21 | // res.json(result); 22 | // }); 23 | 24 | // root.get(`/sum`, async (req, res) => { 25 | // const numa = Number(req.query.numa); 26 | // const numb = Number(req.query.numb); 27 | 28 | // let result = await db.callProcedure(null, `X`, `SUMPGM`, [numa, numb, 0]); 29 | 30 | // res.json({ 31 | // result: result.parameters[2] 32 | // }) 33 | // }); 34 | 35 | export default root; -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## nodejs-ibmi-app Template 2 | 3 | A Node.js API template, built specifically for using TypeScript, creating APIs with express, and ODBC to connect to IBM i. 4 | 5 | ### Create the repo from the template 6 | 7 | Select 'Use this template' and 'Create a new repository'. Fill out your new repository information and when it has been created, clone it to your local device. 8 | 9 | image 10 | 11 | ### Setup 12 | 13 | 1. After cloning the repo, change directory to it and use `npm i` to fetch the dependencies 14 | 2. Create a copy of `.env.example` named `.env` and update the environment variables with the values you would use to connect to an IBM i 15 | 16 | ### Testing 17 | 18 | Use 'Launch Program' under the debug tab to launch to application. 19 | 20 | image 21 | 22 | You can also use `npm run start` to launch it outside of VS Code. 23 | -------------------------------------------------------------------------------- /src/db/index.ts: -------------------------------------------------------------------------------- 1 | import odbc from "odbc"; 2 | 3 | export default class { 4 | private static pool: odbc.Pool; 5 | 6 | static async connect(connectionString: string) { 7 | this.pool = await odbc.pool(connectionString); 8 | } 9 | 10 | /** 11 | * @throws Will crash if query is invalid 12 | */ 13 | /* 14 | static query(statement: string, bindingsValues: (number|string)[] = []): Promise { 15 | return this.pool.query(statement, bindingsValues) as Promise; 16 | } 17 | */ 18 | static async query(statement: string, bindingsValues: (number|string)[] = []) { 19 | return this.pool.query(statement, bindingsValues); 20 | } 21 | 22 | static async callProcedure(catalog: string|null, library: string, procedure: string, bindingsValues: (number|string)[] = []) { 23 | const connection = await this.pool.connect(); 24 | return connection.callProcedure(catalog, library, procedure, bindingsValues); 25 | } 26 | } 27 | 28 | export const connectionString = [ 29 | `DRIVER=IBM i Access ODBC Driver`, 30 | `SYSTEM=${process.env.DB_HOST}`, 31 | `UID=${process.env.DB_ID}`, 32 | `Password=${process.env.DB_PASSWORD}`, 33 | `Naming=1`, 34 | `DBQ=,${process.env[`DB_DBQ`] ? process.env[`DB_DBQ`] : `*USRLIBL`}`, 35 | ].join(`;`); -------------------------------------------------------------------------------- /.vscode/actions.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "Create RPGLE Program", 4 | "command": "CRTBNDRPG PGM(&CURLIB/&NAME) SRCSTMF('&RELATIVEPATH') OPTION(*EVENTF) DBGVIEW(*SOURCE) TGTCCSID(*JOB)", 5 | "deployFirst": true, 6 | "environment": "ile", 7 | "extensions": [ 8 | "RPGLE" 9 | ] 10 | }, 11 | { 12 | "name": "Create RPGLE Module", 13 | "command": "CRTRPGMOD MODULE(&CURLIB/&NAME) SRCSTMF('&RELATIVEPATH') OPTION(*EVENTF) DBGVIEW(*SOURCE) TGTCCSID(*JOB)", 14 | "deployFirst": true, 15 | "environment": "ile", 16 | "extensions": [ 17 | "RPGLE" 18 | ] 19 | }, 20 | { 21 | "name": "Create SQLRPGLE Program", 22 | "command": "CRTSQLRPGI OBJ(&CURLIB/&NAME) SRCSTMF('&RELATIVEPATH') OPTION(*EVENTF) DBGVIEW(*SOURCE) CLOSQLCSR(*ENDMOD) CVTCCSID(*JOB) COMPILEOPT('TGTCCSID(*JOB)') RPGPPOPT(*LVL2)", 23 | "deployFirst": true, 24 | "environment": "ile", 25 | "extensions": [ 26 | "SQLRPGLE" 27 | ] 28 | }, 29 | { 30 | "name": "Create SQLRPGLE Module", 31 | "command": "CRTSQLRPGI OBJ(&CURLIB/&NAME) SRCSTMF('&RELATIVEPATH') OBJTYPE(*MODULE) OPTION(*EVENTF) DBGVIEW(*SOURCE) CLOSQLCSR(*ENDMOD) CVTCCSID(*JOB) COMPILEOPT('TGTCCSID(*JOB)') RPGPPOPT(*LVL2)", 32 | "deployFirst": true, 33 | "environment": "ile", 34 | "extensions": [ 35 | "SQLRPGLE" 36 | ] 37 | }, 38 | { 39 | "extensions": [ 40 | "SQL", 41 | "TABLE", 42 | "VIEW", 43 | "SQLPRC", 44 | "SQLUDF", 45 | "SQLUDT", 46 | "SQLTRG", 47 | "SQLALIAS", 48 | "SQLSEQ" 49 | ], 50 | "name": "Run SQL Statements (RUNSQLSTM)", 51 | "command": "RUNSQLSTM SRCSTMF('&FULLPATH') COMMIT(*NONE) NAMING(*SQL)", 52 | "deployFirst": true, 53 | "environment": "ile" 54 | } 55 | ] -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Visit https://aka.ms/tsconfig to read more about this file */ 4 | 5 | /* Projects */ 6 | // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */ 7 | // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */ 8 | // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */ 9 | // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */ 10 | // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */ 11 | // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */ 12 | 13 | /* Language and Environment */ 14 | "target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ 15 | "lib": ["es6"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */ 16 | // "jsx": "preserve", /* Specify what JSX code is generated. */ 17 | // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */ 18 | // "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */ 19 | // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */ 20 | // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */ 21 | // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */ 22 | // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */ 23 | // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */ 24 | // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */ 25 | // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */ 26 | 27 | /* Modules */ 28 | "module": "commonjs", /* Specify what module code is generated. */ 29 | "rootDir": "src", /* Specify the root folder within your source files. */ 30 | // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ 31 | // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */ 32 | // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */ 33 | // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */ 34 | // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */ 35 | // "types": [], /* Specify type package names to be included without being referenced in a source file. */ 36 | // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ 37 | // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ 38 | // "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */ 39 | // "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */ 40 | // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ 41 | // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ 42 | "resolveJsonModule": true, /* Enable importing .json files. */ 43 | // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ 44 | // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */ 45 | 46 | /* JavaScript Support */ 47 | "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */ 48 | // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */ 49 | // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */ 50 | 51 | /* Emit */ 52 | // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */ 53 | // "declarationMap": true, /* Create sourcemaps for d.ts files. */ 54 | // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */ 55 | // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ 56 | // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ 57 | // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ 58 | "outDir": "build", /* Specify an output folder for all emitted files. */ 59 | // "removeComments": true, /* Disable emitting comments. */ 60 | // "noEmit": true, /* Disable emitting files from a compilation. */ 61 | // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ 62 | // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */ 63 | // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */ 64 | // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */ 65 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 66 | // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */ 67 | // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */ 68 | // "newLine": "crlf", /* Set the newline character for emitting files. */ 69 | // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */ 70 | // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */ 71 | // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */ 72 | // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */ 73 | // "declarationDir": "./", /* Specify the output directory for generated declaration files. */ 74 | // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */ 75 | 76 | /* Interop Constraints */ 77 | // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */ 78 | // "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */ 79 | // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */ 80 | "esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ 81 | // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */ 82 | "forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ 83 | 84 | /* Type Checking */ 85 | "strict": true, /* Enable all strict type-checking options. */ 86 | "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */ 87 | // "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */ 88 | // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */ 89 | // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */ 90 | // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */ 91 | // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */ 92 | // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */ 93 | // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */ 94 | // "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */ 95 | // "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */ 96 | // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */ 97 | // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */ 98 | // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */ 99 | // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */ 100 | // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */ 101 | // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */ 102 | // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */ 103 | // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */ 104 | 105 | /* Completeness */ 106 | // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ 107 | "skipLibCheck": true /* Skip type checking all .d.ts files. */ 108 | } 109 | } 110 | --------------------------------------------------------------------------------