From 6f7e1229a0330429124c911014ce277ba802ecd7 Mon Sep 17 00:00:00 2001 From: Kallesh B S Date: Tue, 25 Feb 2025 13:18:33 +0530 Subject: [PATCH] proj with persistent connection pool --- api.http | 23 + ormconfig.ts | 26 +- package-lock.json | 53 ++ package.json | 3 + src/app.controller.spec.ts | 22 - src/app.module.ts | 4 +- src/db/db.controller.spec.ts | 18 - src/db/db.controller.ts | 43 -- src/db/db.module.ts | 8 +- src/db/db.service.spec.ts | 18 - src/db/db.service.ts | 91 +-- src/main.ts | 2 + src/oracle/oracle.controller.ts | 23 + src/oracle/oracle.dto.ts | 18 + src/oracle/oracle.module.ts | 13 + src/oracle/oracle.service.ts | 144 +++++ src/oraclesp/oraclesp.controller.ts | 12 - src/oraclesp/oraclesp.module.ts | 11 - src/oraclesp/oraclesp.service.ts | 899 ---------------------------- 19 files changed, 345 insertions(+), 1086 deletions(-) create mode 100644 api.http delete mode 100644 src/app.controller.spec.ts delete mode 100644 src/db/db.controller.spec.ts delete mode 100644 src/db/db.controller.ts delete mode 100644 src/db/db.service.spec.ts create mode 100644 src/oracle/oracle.controller.ts create mode 100644 src/oracle/oracle.dto.ts create mode 100644 src/oracle/oracle.module.ts create mode 100644 src/oracle/oracle.service.ts delete mode 100644 src/oraclesp/oraclesp.controller.ts delete mode 100644 src/oraclesp/oraclesp.module.ts delete mode 100644 src/oraclesp/oraclesp.service.ts diff --git a/api.http b/api.http new file mode 100644 index 0000000..0d1122e --- /dev/null +++ b/api.http @@ -0,0 +1,23 @@ +GET http://localhost:3000/oracle/GetRegions + +### + +POST http://localhost:3000/oracle/InsertRegions +Content-Type: application/json + +{ + "p_region":"p_region", + "p_name":"p_name" +} + +### + +PATCH http://localhost:3000/oracle/UpdateRegion +Content-Type: application/json + +{ + "p_regionID": , + "p_name":"p_name" +} + + diff --git a/ormconfig.ts b/ormconfig.ts index d2ef977..e3f6039 100644 --- a/ormconfig.ts +++ b/ormconfig.ts @@ -1,24 +1,24 @@ import { DataSourceOptions } from 'typeorm'; export const OracleConfig: any = { - type: 'oracle', - username: 'system', - password: 'root', - connectString: '192.168.1.96/xe', + // type: 'oracle', + user: 'Carnetsys', + password: 'Carnet1234', + connectString: '172.31.18.76/xe', // entities: [__dirname + '/**/*.entity{.ts,.js}'], // synchronize: true, - autoCommit: true + // autoCommit: true }; - export const MssqlConfig: DataSourceOptions = { - type: 'mssql', - host: 'localhost', - port: 1433, - username: 'mssql1433', - password: 'root', - database: 'demo', + export const MssqlConfig: any = { + // type: 'mssql', + host: '172.31.18.76', + // port: 1433, + user: 'Carnetsys', + password: 'Carnet1234', + database: 'CarnetDB', // entities: [__dirname + '/**/*.entity{.ts,.js}'], - synchronize: true, + // synchronize: true, options: { enableArithAbort: true, trustServerCertificate: true, diff --git a/package-lock.json b/package-lock.json index 61af547..02154a6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,8 @@ "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.0.1", "@nestjs/typeorm": "^11.0.0", + "class-transformer": "^0.5.1", + "class-validator": "^0.14.1", "mssql": "^10.0.4", "mysql2": "^3.12.0", "oracledb": "^6.7.2", @@ -31,6 +33,7 @@ "@swc/core": "^1.10.7", "@types/express": "^5.0.0", "@types/jest": "^29.5.14", + "@types/mssql": "^9.1.7", "@types/node": "^22.10.7", "@types/oracledb": "^6.5.3", "@types/supertest": "^6.0.2", @@ -3415,6 +3418,18 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/mssql": { + "version": "9.1.7", + "resolved": "https://registry.npmjs.org/@types/mssql/-/mssql-9.1.7.tgz", + "integrity": "sha512-eIOEe78nuSW5KctDHImDhLZ9a+jV/z/Xs5RBhcG/jrk+YWqhdNmzBmHVWV7aWQ5fW+jbIGtX6Ph+bbVqfhzafg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*", + "tarn": "^3.0.1", + "tedious": "*" + } + }, "node_modules/@types/node": { "version": "22.13.4", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.4.tgz", @@ -3518,6 +3533,12 @@ "@types/superagent": "^8.1.0" } }, + "node_modules/@types/validator": { + "version": "13.12.2", + "resolved": "https://registry.npmjs.org/@types/validator/-/validator-13.12.2.tgz", + "integrity": "sha512-6SlHBzUW8Jhf3liqrGGXyTJSIFe4nqlJ5A5KaMZ2l/vbM3Wh3KSybots/wfWVzNLK4D1NZluDlSQIbIEPx6oyA==", + "license": "MIT" + }, "node_modules/@types/yargs": { "version": "17.0.33", "resolved": "https://registry.npmjs.org/@types/yargs/-/yargs-17.0.33.tgz", @@ -5080,6 +5101,23 @@ "dev": true, "license": "MIT" }, + "node_modules/class-transformer": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz", + "integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw==", + "license": "MIT" + }, + "node_modules/class-validator": { + "version": "0.14.1", + "resolved": "https://registry.npmjs.org/class-validator/-/class-validator-0.14.1.tgz", + "integrity": "sha512-2VEG9JICxIqTpoK1eMzZqaV+u/EiwEJkMGzTrZf6sU/fwsnOITVgYJ8yojSy6CaXtO9V0Cc6ZQZ8h8m4UBuLwQ==", + "license": "MIT", + "dependencies": { + "@types/validator": "^13.11.8", + "libphonenumber-js": "^1.10.53", + "validator": "^13.9.0" + } + }, "node_modules/cli-cursor": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", @@ -9291,6 +9329,12 @@ "node": ">= 0.8.0" } }, + "node_modules/libphonenumber-js": { + "version": "1.11.20", + "resolved": "https://registry.npmjs.org/libphonenumber-js/-/libphonenumber-js-1.11.20.tgz", + "integrity": "sha512-/ipwAMvtSZRdiQBHqW1qxqeYiBMzncOQLVA+62MWYr7N4m7Q2jqpJ0WgT7zlOEOpyLRSqrMXidbJpC0J77AaKA==", + "license": "MIT" + }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", @@ -13304,6 +13348,15 @@ "node": ">=10.12.0" } }, + "node_modules/validator": { + "version": "13.12.0", + "resolved": "https://registry.npmjs.org/validator/-/validator-13.12.0.tgz", + "integrity": "sha512-c1Q0mCiPlgdTVVVIJIrBuxNicYE+t/7oKeI9MWLj3fh/uq2Pxh/3eeWbVZ4OcGW1TUf53At0njHw5SMdA3tmMg==", + "license": "MIT", + "engines": { + "node": ">= 0.10" + } + }, "node_modules/vary": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", diff --git a/package.json b/package.json index b7f0373..d5bd7d0 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,8 @@ "@nestjs/core": "^11.0.1", "@nestjs/platform-express": "^11.0.1", "@nestjs/typeorm": "^11.0.0", + "class-transformer": "^0.5.1", + "class-validator": "^0.14.1", "mssql": "^10.0.4", "mysql2": "^3.12.0", "oracledb": "^6.7.2", @@ -42,6 +44,7 @@ "@swc/core": "^1.10.7", "@types/express": "^5.0.0", "@types/jest": "^29.5.14", + "@types/mssql": "^9.1.7", "@types/node": "^22.10.7", "@types/oracledb": "^6.5.3", "@types/supertest": "^6.0.2", diff --git a/src/app.controller.spec.ts b/src/app.controller.spec.ts deleted file mode 100644 index d22f389..0000000 --- a/src/app.controller.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { AppController } from './app.controller'; -import { AppService } from './app.service'; - -describe('AppController', () => { - let appController: AppController; - - beforeEach(async () => { - const app: TestingModule = await Test.createTestingModule({ - controllers: [AppController], - providers: [AppService], - }).compile(); - - appController = app.get(AppController); - }); - - describe('root', () => { - it('should return "Hello World!"', () => { - expect(appController.getHello()).toBe('Hello World!'); - }); - }); -}); diff --git a/src/app.module.ts b/src/app.module.ts index 187f876..c4fead2 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,10 +2,10 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { DbModule } from './db/db.module'; -import { OracleSpModule } from './oraclesp/oraclesp.module'; +import { OracleModule } from './oracle/oracle.module'; @Module({ - imports: [DbModule, OracleSpModule], + imports: [DbModule, OracleModule], controllers: [AppController], providers: [AppService], }) diff --git a/src/db/db.controller.spec.ts b/src/db/db.controller.spec.ts deleted file mode 100644 index 28dde04..0000000 --- a/src/db/db.controller.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { DbController } from './db.controller'; - -describe('DbController', () => { - let controller: DbController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [DbController], - }).compile(); - - controller = module.get(DbController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/db/db.controller.ts b/src/db/db.controller.ts deleted file mode 100644 index 0079e9a..0000000 --- a/src/db/db.controller.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Controller, Get, Param } from '@nestjs/common'; -import { DbService } from './db.service'; - -@Controller('db') -export class DbController { - constructor(private readonly dbService: DbService) { } - - @Get('/getConn/:type') - async getConn(@Param('type') type: string) { - try { - const dataSource = this.dbService.getDataSource(); - if (dataSource) { - return 'Connection successfull1'; - } - else { - await this.dbService.initialize(this.dbService.getDbTypeConfig(type)) - const conn = this.dbService.getDataSource(); - if(conn){ - return "Connection successfull2"; - } - else{ - throw new Error('Error while connecting to db..!') - } - } - } - catch (err) { - console.log(err); - } - } - - @Get('/closeConn') - closeConn() { - try { - const dataSource = this.dbService.getDataSource(); - if (dataSource) { - return this.dbService.closeConnection(); - } - } - catch (err) { - return "No Connection Detected to close"; - } - } -} diff --git a/src/db/db.module.ts b/src/db/db.module.ts index dbd865f..b243f5d 100644 --- a/src/db/db.module.ts +++ b/src/db/db.module.ts @@ -1,10 +1,8 @@ import { Module } from '@nestjs/common'; -import { DbService } from './db.service'; -import { DbController } from './db.controller'; +import { MssqlDBService, OracleDBService } from './db.service'; @Module({ - providers: [DbService], - controllers: [DbController], - exports:[DbService] + providers: [OracleDBService,MssqlDBService], + exports:[OracleDBService,MssqlDBService] }) export class DbModule {} diff --git a/src/db/db.service.spec.ts b/src/db/db.service.spec.ts deleted file mode 100644 index d81007f..0000000 --- a/src/db/db.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { DbService } from './db.service'; - -describe('DbService', () => { - let service: DbService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [DbService], - }).compile(); - - service = module.get(DbService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/src/db/db.service.ts b/src/db/db.service.ts index bec995c..825671f 100644 --- a/src/db/db.service.ts +++ b/src/db/db.service.ts @@ -1,54 +1,59 @@ -import { Injectable } from '@nestjs/common'; -import { MssqlConfig, OracleConfig } from 'ormconfig'; -import { DataSource, DataSourceOptions } from 'typeorm'; -import * as oracledb from 'oracledb'; +import { MssqlConfig , OracleConfig} from 'ormconfig'; +import { createPool, Pool, Connection as cob, } from 'oracledb'; +import { Connection, ConnectionPool } from 'mssql'; -@Injectable() -export class DbService { - private dataSource: DataSource | null = null; - private oracleConnection: oracledb.Connection | null = null; +export class OracleDBService { + private pool: Pool; - constructor() {} - - async initialize(options: DataSourceOptions) { - if (options.type === 'oracle') { - this.oracleConnection = await oracledb.getConnection(options); - } else { - this.dataSource = new DataSource({ - ...options, - }); - await this.dataSource.initialize(); - console.log('Data Source has been initialized!'); - } + constructor() { + this.initializePool(); } - getDataSource(): DataSource | oracledb.Connection | null { - if (this.oracleConnection) { - return this.oracleConnection; // Return the Oracle connection - } - return this.dataSource; // Return the TypeORM DataSource + private async initializePool() { + this.pool = await createPool({ + ...OracleConfig, + poolMin: 1, + poolMax: 10, + poolIncrement: 1, + }); } - getDbTypeConfig(type?: string): DataSourceOptions { - if (type === 'oracle') return OracleConfig; - else return MssqlConfig; + async getConnection(): Promise { + const connection = await this.pool.getConnection(); + console.log('Database connection initialized successfully for oracle.'); + return connection; + } +} + +export class MssqlDBService { + private pool: ConnectionPool; + private readonly config = { + ...MssqlConfig, + server: "localhost", + pool: { + max: 10, + min: 0, + idleTimeoutMillis: 30000, + }, + }; + + constructor() { + this.initializePool(); } - async closeConnection() { - if (this.oracleConnection) { - try { - await this.oracleConnection.close(); // Close Oracle connection - console.log('Oracle connection has been closed!'); - } catch (err) { - console.error('Error closing Oracle connection:', err); - } - this.oracleConnection = null; - } else if (this.dataSource) { - await this.dataSource.destroy(); - console.log('Data Source has been closed!'); - this.dataSource = null; - } else { - console.log('Data Source is not initialized.'); + private async initializePool() { + this.pool = new ConnectionPool(this.config); + } + + async getConnection(): Promise { + try { + const connection = await this.pool.connect(); + console.log('Database connection initialized successfully for Mssql.'); + // console.log(`Connection established to server: ${this.config.server}`); // Log server info + return connection; + } catch (error) { + console.error('Error getting connection from pool', error.stack); + throw error; // Rethrow the error after logging } } } \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index f76bc8d..9be6371 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,8 +1,10 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; +import { ValidationPipe } from '@nestjs/common'; async function bootstrap() { const app = await NestFactory.create(AppModule); + app.useGlobalPipes(new ValidationPipe()); await app.listen(process.env.PORT ?? 3000); } bootstrap(); diff --git a/src/oracle/oracle.controller.ts b/src/oracle/oracle.controller.ts new file mode 100644 index 0000000..33d337b --- /dev/null +++ b/src/oracle/oracle.controller.ts @@ -0,0 +1,23 @@ +import { Body, Controller, Get, Patch, Post } from '@nestjs/common'; +import { OracleService } from './oracle.service'; +import { InsertRegionsDto, UpdateRegionDto } from './oracle.dto'; + +@Controller('oracle') +export class OracleController { + constructor(private readonly oarcleService: OracleService) { } + + @Get('/GetRegions') + getRegions() { + return this.oarcleService.getRegions(); + } + + @Post('/InsertRegions') + insertRegions(@Body() body: InsertRegionsDto) { + return this.oarcleService.insertRegions(body.p_region, body.p_name); + } + + @Patch('/UpdateRegion') + updateRegions(@Body() body: UpdateRegionDto) { + return this.oarcleService.updateRegions(body.p_regionID,body.p_name); + } +} diff --git a/src/oracle/oracle.dto.ts b/src/oracle/oracle.dto.ts new file mode 100644 index 0000000..74cdc76 --- /dev/null +++ b/src/oracle/oracle.dto.ts @@ -0,0 +1,18 @@ +import { IsNumber, IsString } from 'class-validator'; + +export class InsertRegionsDto { + @IsString() + p_region: string; + + @IsString() + p_name: string; +} + +export class UpdateRegionDto { + @IsNumber() + p_regionID: number; + + @IsString() + p_name: string; +} + diff --git a/src/oracle/oracle.module.ts b/src/oracle/oracle.module.ts new file mode 100644 index 0000000..720cff1 --- /dev/null +++ b/src/oracle/oracle.module.ts @@ -0,0 +1,13 @@ +import { Module } from '@nestjs/common'; +import { OracleService } from './oracle.service'; +import { OracleController } from './oracle.controller'; +import { OracleDBService } from 'src/db/db.service'; +import { DbModule } from 'src/db/db.module'; + +@Module({ + imports:[DbModule], + providers: [OracleService], + controllers: [OracleController], + exports:[OracleService] +}) +export class OracleModule {} diff --git a/src/oracle/oracle.service.ts b/src/oracle/oracle.service.ts new file mode 100644 index 0000000..cedf52f --- /dev/null +++ b/src/oracle/oracle.service.ts @@ -0,0 +1,144 @@ +import { Injectable } from '@nestjs/common'; +import { OracleDBService } from 'src/db/db.service'; +import * as oracledb from 'oracledb' + +@Injectable() +export class OracleService { + constructor(private readonly oracleDBService: OracleDBService) { } + + async getRegions() { + let connection; + let rows = []; + try { + connection = await this.oracleDBService.getConnection() + if (!connection) { + throw new Error('No DB Connected') + } + + const result = await connection.execute( + `BEGIN + USCIB_Managed_Pkg.GetRegions(:p_cursor); + END;`, + { + p_cursor: { + type: oracledb.CURSOR, + dir: oracledb.BIND_OUT + } + }, + { + outFormat: oracledb.OUT_FORMAT_OBJECT + } + ); + + if (result.outBinds && result.outBinds.p_cursor) { + const cursor = result.outBinds.p_cursor; // The OUT cursor + let rowsBatch; + + do { + rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time + rows = rows.concat(rowsBatch); // Append fetched rows to the main array + } while (rowsBatch.length > 0); + + console.log('Rows fetched:', rows); + + // Close the cursor after you're done + await cursor.close(); + } else { + throw new Error('No cursor returned from the stored procedure'); + } + + return rows; + + } catch (err) { + console.error('Error fetching users: ', err); + throw new Error('Error fetching users'); + } finally { + } + } + + async insertRegions(p_region: String, p_name: String) { + let connection; + try { + // Connect to the Oracle database using oracledb + connection = await this.oracleDBService.getConnection() + if (!connection) { + throw new Error('No DB Connected') + } + + const result = await connection.execute( + `BEGIN + sp1(:p_region,:p_name,:p_cursor); + END;`, { + p_region: { + val: p_region, + type: oracledb.DB_TYPE_VARCHAR + }, + p_name: { + val: p_name, + type: oracledb.DB_TYPE_VARCHAR + }, + p_cursor: { + type: oracledb.CURSOR, + dir: oracledb.BIND_OUT + } + }, { + outFormat: oracledb.OUT_FORMAT_OBJECT + } + ); + + await connection.commit(); + + let fres = await result.outBinds.p_cursor.getRows(); + + return fres + + } catch (err) { + console.error('Error fetching users: ', err); + throw new Error('Error fetching users'); + } finally { + } + } + + async updateRegions(p_regionID: Number, p_name: String) { + let connection; + try { + // Connect to the Oracle database using oracledb + connection = await this.oracleDBService.getConnection() + if (!connection) { + throw new Error('No DB Connected') + } + + const result = await connection.execute( + `BEGIN + sp1(:p_regionID,:p_name,:p_cursor); + END;`, { + p_regionID: { + val: p_regionID, + type: oracledb.DB_TYPE_NUMBER + }, + p_name: { + val: p_name, + type: oracledb.DB_TYPE_VARCHAR + }, + p_cursor: { + type: oracledb.CURSOR, + dir: oracledb.BIND_OUT + } + }, { + outFormat: oracledb.OUT_FORMAT_OBJECT + } + ); + + await connection.commit(); + + let fres = await result.outBinds.p_cursor.getRows(); + + return fres + + } catch (err) { + console.error('Error fetching users: ', err); + throw new Error('Error fetching users'); + } finally { + } + } +} diff --git a/src/oraclesp/oraclesp.controller.ts b/src/oraclesp/oraclesp.controller.ts deleted file mode 100644 index 5bd255c..0000000 --- a/src/oraclesp/oraclesp.controller.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Controller,Get } from '@nestjs/common'; -import { OracleSpService } from './oraclesp.service'; - -@Controller('oracleSP') -export class OracleSpController { - constructor(private readonly oracleSPService:OracleSpService){} - - @Get('/getRegions') - getRegions(){ - return this.oracleSPService.getRegions(); - } -} diff --git a/src/oraclesp/oraclesp.module.ts b/src/oraclesp/oraclesp.module.ts deleted file mode 100644 index 5ae20e4..0000000 --- a/src/oraclesp/oraclesp.module.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Module } from '@nestjs/common'; -import { OracleSpService } from './oraclesp.service'; -import { OracleSpController } from './oraclesp.controller'; -import { DbModule } from 'src/db/db.module'; - -@Module({ - imports:[DbModule], - providers: [OracleSpService], - controllers: [OracleSpController] -}) -export class OracleSpModule {} diff --git a/src/oraclesp/oraclesp.service.ts b/src/oraclesp/oraclesp.service.ts deleted file mode 100644 index fb5efbb..0000000 --- a/src/oraclesp/oraclesp.service.ts +++ /dev/null @@ -1,899 +0,0 @@ -import { Inject, Injectable } from '@nestjs/common'; -import { DbService } from 'src/db/db.service'; -import * as oracledb from 'oracledb' - -@Injectable() -export class OracleSpService { - private dbService: DbService; - - constructor(@Inject(DbService) dbService: DbService) { - this.dbService = dbService; - } - - async getRegions() { - let connection; - let rows = []; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.GetRegions(:p_cursor); - END;`, - { - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, - { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - - if (result.outBinds && result.outBinds.p_cursor) { - const cursor = result.outBinds.p_cursor; // The OUT cursor - let rowsBatch; - - do { - rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time - rows = rows.concat(rowsBatch); // Append fetched rows to the main array - } while (rowsBatch.length > 0); - - console.log('Rows fetched:', rows); - - // Close the cursor after you're done - await cursor.close(); - } else { - throw new Error('No cursor returned from the stored procedure'); - } - - return rows; - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - } - - async InsertRegions() { - let connection; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - sp1(:p_region,:p_name,:p_cursor); - END;`, { - p_region: { - val: "03", - type: oracledb.DB_TYPE_VARCHAR - }, - p_name: { - val: "", - type: oracledb.DB_TYPE_VARCHAR - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - let fres = await result.outBinds.p_cursor.getRows(); - - return fres - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - } - - async updateRegions() { - let connection; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - sp1(:p_regionID,:p_name,:p_cursor); - END;`, { - p_regionID: { - val: 3, - type: oracledb.DB_TYPE_NUMBER - }, - p_name: { - val: 'indu1', - type: oracledb.DB_TYPE_VARCHAR - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - let fres = await result.outBinds.p_cursor.getRows(); - - return fres - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - } - - async insertNewServiceProider( - p_name: String, - p_lookupcode: String, - p_address1: String, - p_address2: String, - p_city: String, - p_state: String, - p_zip: String, - p_country: String, - p_issuingregion: String, - p_replacementregion: String, - p_bondsurety: String, - p_cargopolicyno: String, - p_cargosurety: String, - p_user_id: String, - ) { - - let connection; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.InsertNewSP( - :p_name, - :p_lookupcode, - :p_address1, - :p_address2, - :p_city, - :p_state, - :p_zip, - :p_country, - :p_issuingregion, - :p_replacementregion, - :p_bondsurety, - :p_cargopolicyno, - :p_cargosurety, - :p_user_id, - :p_cursor); - END;`, { - p_name: { - val: p_name, - type: oracledb.DB_TYPE_VARCHAR - }, - p_lookupcode: { - val: p_lookupcode, - type: oracledb.DB_TYPE_VARCHAR - }, - p_address1: { - val: p_address1, - type: oracledb.DB_TYPE_VARCHAR - }, - p_address2: { - val: p_address2, - type: oracledb.DB_TYPE_VARCHAR - }, - p_city: { - val: p_city, - type: oracledb.DB_TYPE_VARCHAR - }, - p_state: { - val: p_state, - type: oracledb.DB_TYPE_VARCHAR - }, - p_zip: { - val: p_zip, - type: oracledb.DB_TYPE_VARCHAR - }, - p_country: { - val: p_country, - type: oracledb.DB_TYPE_VARCHAR - }, - p_issuingregion: { - val: p_issuingregion, - type: oracledb.DB_TYPE_VARCHAR - }, - p_replacementregion: { - val: p_replacementregion, - type: oracledb.DB_TYPE_VARCHAR - }, - p_bondsurety: { - val: p_bondsurety, - type: oracledb.DB_TYPE_VARCHAR - }, - p_cargopolicyno: { - val: p_cargopolicyno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_cargosurety: { - val: p_cargosurety, - type: oracledb.DB_TYPE_VARCHAR - }, - p_user_id: { - val: p_user_id, - type: oracledb.DB_TYPE_VARCHAR - }, - - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - let fres = await result.outBinds.res.getRows(); - - return fres - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - } - - async updateServiceProider( - p_spid: Number, - p_name: String, - p_lookupcode: String, - p_address1: String, - p_address2: String, - p_city: String, - p_state: String, - p_zip: String, - p_country: String, - p_issuingregion: String, - p_replacementregion: String, - p_bondsurety: String, - p_cargopolicyno: String, - p_cargosurety: String, - p_user_id: String - ) { - - let connection; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.UpdateSP( - :p_spid, - :p_name, - :p_lookupcode, - :p_address1, - :p_address2, - :p_city, - :p_state, - :p_zip, - :p_country, - :p_issuingregion, - :p_replacementregion, - :p_bondsurety, - :p_cargopolicyno, - :p_cargosurety, - :p_user_id, - :p_cursor); - END;`, { - p_spid: { - val: p_spid, - type: oracledb.DB_TYPE_NUMBER - }, - p_name: { - val: p_name, - type: oracledb.DB_TYPE_VARCHAR - }, - p_lookupcode: { - val: p_lookupcode, - type: oracledb.DB_TYPE_VARCHAR - }, - p_address1: { - val: p_address1, - type: oracledb.DB_TYPE_VARCHAR - }, - p_address2: { - val: p_address2, - type: oracledb.DB_TYPE_VARCHAR - }, - p_city: { - val: p_city, - type: oracledb.DB_TYPE_VARCHAR - }, - p_state: { - val: p_state, - type: oracledb.DB_TYPE_VARCHAR - }, - p_zip: { - val: p_zip, - type: oracledb.DB_TYPE_VARCHAR - }, - p_country: { - val: p_country, - type: oracledb.DB_TYPE_VARCHAR - }, - p_issuingregion: { - val: p_issuingregion, - type: oracledb.DB_TYPE_VARCHAR - }, - p_replacementregion: { - val: p_replacementregion, - type: oracledb.DB_TYPE_VARCHAR - }, - p_bondsurety: { - val: p_bondsurety, - type: oracledb.DB_TYPE_VARCHAR - }, - p_cargopolicyno: { - val: p_cargopolicyno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_cargosurety: { - val: p_cargosurety, - type: oracledb.DB_TYPE_VARCHAR - }, - p_user_id: { - val: p_user_id, - type: oracledb.DB_TYPE_VARCHAR - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - let fres = await result.outBinds.p_cursor.getRows(); - - return fres - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - } - - async getAllServiceproviders() { - - let connection; - let rows = []; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.GetAllSPs(:p_cursor); - END;`, - { - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, - { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - - if (result.outBinds && result.outBinds.p_cursor) { - const cursor = result.outBinds.p_cursor; // The OUT cursor - let rowsBatch; - - do { - rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time - rows = rows.concat(rowsBatch); // Append fetched rows to the main array - } while (rowsBatch.length > 0); - - console.log('Rows fetched:', rows); - - // Close the cursor after you're done - await cursor.close(); - } else { - throw new Error('No cursor returned from the stored procedure'); - } - - return rows; - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - } - - async getSelectedServiceprovider(p_spid: Number) { - - let connection; - let rows = []; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.GetSPbySPID(:p_spid,:p_cursor); - END;`, - { - p_spid: { - val: p_spid, - type: oracledb.DB_TYPE_NUMBER, - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, - { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - - if (result.outBinds && result.outBinds.p_cursor) { - const cursor = result.outBinds.p_cursor; // The OUT cursor - let rowsBatch; - - do { - rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time - rows = rows.concat(rowsBatch); // Append fetched rows to the main array - } while (rowsBatch.length > 0); - - console.log('Rows fetched:', rows); - - // Close the cursor after you're done - await cursor.close(); - } else { - throw new Error('No cursor returned from the stored procedure'); - } - - return rows; - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - } - - async insertSPContacts( - p_spid: Number, - p_firstname: String, - p_lastname: String, - p_title: String, - p_phoneno: String, - p_mobileno: String, - p_faxno: String, - p_emailaddress: String, - p_user_id: String - ) { - - let connection; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.InsertSPContacts( - :p_spid, - :p_firstname, - :p_lastname, - :p_title, - :p_phoneno, - :p_mobileno, - :p_faxno, - :p_emailaddress, - :p_user_id, - :p_cursor); - END;`, { - p_spid: { - val: p_spid, - type: oracledb.DB_TYPE_NUMBER - }, - p_firstname: { - val: p_firstname, - type: oracledb.DB_TYPE_VARCHAR - }, - p_lastname: { - val: p_lastname, - type: oracledb.DB_TYPE_VARCHAR - }, - p_title: { - val: p_title, - type: oracledb.DB_TYPE_VARCHAR - }, - p_phoneno: { - val: p_phoneno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_mobileno: { - val: p_mobileno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_faxno: { - val: p_faxno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_emailaddress: { - val: p_emailaddress, - type: oracledb.DB_TYPE_VARCHAR - }, - p_user_id: { - val: p_user_id, - type: oracledb.DB_TYPE_VARCHAR - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - let fres = await result.outBinds.p_cursor.getRows(); - - return fres - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - } - - async updateSPContacts( - p_spcontactid: Number, - p_firstname: String, - p_lastname: String, - p_title: String, - p_phoneno: String, - p_mobileno: String, - p_faxno: String, - p_emailaddress: String, - p_user_id: String, - ) { - - let connection; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.UpdateSPContacts( - :p_spcontactid, - :p_firstname, - :p_lastname, - :p_title, - :p_phoneno, - :p_mobileno, - :p_faxno, - :p_emailaddress, - :p_user_id, - :p_cursor); - END;`, { - p_spcontactid: { - val: p_spcontactid, - type: oracledb.DB_TYPE_NUMBER - }, - p_firstname: { - val: p_firstname, - type: oracledb.DB_TYPE_VARCHAR - }, - p_lastname: { - val: p_lastname, - type: oracledb.DB_TYPE_VARCHAR - }, - p_title: { - val: p_title, - type: oracledb.DB_TYPE_VARCHAR - }, - p_phoneno: { - val: p_phoneno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_mobileno: { - val: p_mobileno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_faxno: { - val: p_faxno, - type: oracledb.DB_TYPE_VARCHAR - }, - p_emailaddress: { - val: p_emailaddress, - type: oracledb.DB_TYPE_VARCHAR - }, - p_user_id: { - val: p_user_id, - type: oracledb.DB_TYPE_VARCHAR - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - let fres = await result.outBinds.p_cursor.getRows(); - - return fres - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - } - - async getSPcontacts(p_SPid: Number) { - let connection; - let rows = []; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.GetSPAllContacts( p_SPid,:p_cursor); - END;`, - { - p_SPid: { - val: p_SPid, - type: oracledb.DB_TYPE_NUMBER - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, - { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - - if (result.outBinds && result.outBinds.p_cursor) { - const cursor = result.outBinds.p_cursor; // The OUT cursor - let rowsBatch; - - do { - rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time - rows = rows.concat(rowsBatch); // Append fetched rows to the main array - } while (rowsBatch.length > 0); - - console.log('Rows fetched:', rows); - - // Close the cursor after you're done - await cursor.close(); - } else { - throw new Error('No cursor returned from the stored procedure'); - } - - return rows; - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - - } - - async setSPDefaultcontact(p_spcontactid: Number) { - let connection; - let rows = []; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.SetDefaultContact(:p_spcontactid); - END;`, - { - p_cursor: { - val: p_spcontactid, - type: oracledb.DB_TYPE_NUMBER, - } - }, - // { - // outFormat: oracledb.OUT_FORMAT_OBJECT - // } - ); - - // if (result.outBinds && result.outBinds.p_cursor) { - // const cursor = result.outBinds.p_cursor; // The OUT cursor - // let rowsBatch; - - // do { - // rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time - // rows = rows.concat(rowsBatch); // Append fetched rows to the main array - // } while (rowsBatch.length > 0); - - // console.log('Rows fetched:', rows); - - // // Close the cursor after you're done - // await cursor.close(); - // } else { - // throw new Error('No cursor returned from the stored procedure'); - // } - - return 'rows'; - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - - } - - async inactivateSPContact(p_spcontactid: Number) { - let connection; - let rows = []; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.InActivateSPContacts(:p_spcontactid); - END;`, - { - p_cursor: { - val: p_spcontactid, - type: oracledb.DB_TYPE_NUMBER, - } - }, - // { - // outFormat: oracledb.OUT_FORMAT_OBJECT - // } - ); - - // if (result.outBinds && result.outBinds.p_cursor) { - // const cursor = result.outBinds.p_cursor; // The OUT cursor - // let rowsBatch; - - // do { - // rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time - // rows = rows.concat(rowsBatch); // Append fetched rows to the main array - // } while (rowsBatch.length > 0); - - // console.log('Rows fetched:', rows); - - // // Close the cursor after you're done - // await cursor.close(); - // } else { - // throw new Error('No cursor returned from the stored procedure'); - // } - - return 'rows'; - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - } - - async getSPDefaultcontact(p_SPid: Number) { - - let connection; - let rows = []; - try { - // Connect to the Oracle database using oracledb - connection = await this.dbService.getDataSource() - if (!connection) { - throw new Error('No DB Connected') - } - - const result = await connection.execute( - `BEGIN - USCIB_Managed_Pkg.GetSPDefaultContacts( p_SPid,:p_cursor); - END;`, - { - p_SPid: { - val: p_SPid, - type: oracledb.DB_TYPE_NUMBER - }, - p_cursor: { - type: oracledb.CURSOR, - dir: oracledb.BIND_OUT - } - }, - { - outFormat: oracledb.OUT_FORMAT_OBJECT - } - ); - - if (result.outBinds && result.outBinds.p_cursor) { - const cursor = result.outBinds.p_cursor; // The OUT cursor - let rowsBatch; - - do { - rowsBatch = await cursor.getRows(100); // Fetch 100 rows at a time - rows = rows.concat(rowsBatch); // Append fetched rows to the main array - } while (rowsBatch.length > 0); - - console.log('Rows fetched:', rows); - - // Close the cursor after you're done - await cursor.close(); - } else { - throw new Error('No cursor returned from the stored procedure'); - } - - return rows; - - } catch (err) { - console.error('Error fetching users: ', err); - throw new Error('Error fetching users'); - } finally { - } - - } - -}