av6-core
v1.6.5
Published
All utility function for av6 node js projects.
Readme
AV6 Core
A comprehensive utility library for AV6 Node.js projects, providing common functionality for data operations, caching, and Excel handling.
Features
- Dynamic Data Operations: Flexible search, fetch, and CRUD operations with support for dynamic models
- Caching Support: Built-in Redis caching for improved performance
- Excel Import/Export: Seamless Excel file handling for data import and export
- Pagination: Built-in pagination for search results
- DTO Mapping: Support for Data Transfer Object mapping
- Type Safety: Written in TypeScript with comprehensive type definitions
- UIN Config Management: Unique Identification Number generation with configurable segments and automatic reset policies
Installation
npm install av6-coreUsage
Basic Setup
To use the library, you need to provide the necessary dependencies:
import { commonService, Deps } from 'av6-core';
import { PrismaClient } from '@prisma/client';
import winston from 'winston';
import { AsyncLocalStorage } from 'async_hooks';
// Initialize dependencies
const deps: Deps = {
config: {
REDIS_PREFIX: 'your-prefix:',
CACHE_KEY_NAME: 'your-cache-key'
},
mapper: {
dtoMapping: {}, // Your DTO mapping functions
mappingExport: {}, // Your export mapping functions
mappingImport: {}, // Your import mapping functions
},
helpers: {
generateErrorMessage: (type, ...variables) => {
// Your error message generation logic
},
ErrorHandler: class ErrorHandler extends Error {
// Your error handler implementation
}
},
logger: winston.createLogger({
// Your logger configuration
}),
cacheAdapter: {
// Your cache adapter implementation
},
requestStorage: new AsyncLocalStorage(),
db: new PrismaClient()
};
// Initialize the service
const service = commonService(deps);Search Operations
// Perform a search operation
const searchResult = await service.search({
pageNo: 1,
pageSize: 10,
shortCode: 'USER',
searchColumns: ['name', 'email'],
searchText: 'john',
sortBy: 'createdAt',
sortDir: 'DESC',
shortCodeData: {
shortCode: 'USER',
tableName: 'user',
isDTO: true,
isCacheable: true
}
});Fetch Record
// Fetch a specific record
const record = await service.fetch({
shortCode: 'USER',
id: 123,
shortCodeData: {
shortCode: 'USER',
tableName: 'user',
isDTO: true,
isCacheable: true
}
});Excel Operations
// Import data from Excel
const importResult = await service.commonExcelImport({
shortCode: 'USER',
file: excelFile, // File object from upload
shortCodeData: {
shortCode: 'USER',
tableName: 'user'
}
});
// Export data to Excel
const workbook = await service.commonExcelExport({
shortCode: 'USER',
isSample: false,
shortCodeData: {
shortCode: 'USER',
tableName: 'user'
}
});UIN Config Operations
import { uinConfigService, UinDeps, UIN_RESET_POLICY, UinShortCode } from 'av6-core';
// Initialize UIN Config dependencies
const uinDeps: UinDeps = {
config: {
REDIS_PREFIX: 'your-prefix:',
CACHE_KEY_NAME: 'your-cache-key'
},
helpers: {
generateErrorMessage: (type, ...variables) => {
// Your error message generation logic
},
ErrorHandler: class ErrorHandler extends Error {
// Your error handler implementation
}
},
logger: winston.createLogger({
// Your logger configuration
}),
cacheAdapter: {
// Your cache adapter implementation
},
requestStorage: new AsyncLocalStorage(),
db: new PrismaClient(),
prisma: PrismaNamespace,
shortCode: 'UIN_CONFIG',
cacheKey: 'uin-config'
};
// Initialize the UIN Config service
const uinService = uinConfigService(uinDeps);
// Create a new UIN Config
const newUinConfig = await uinService.createUINConfig({
shortCode: UinShortCode.INVOICE,
seqResetPolicy: UIN_RESET_POLICY.monthly,
description: 'Invoice number generator',
uinSegments: [
{ order: 1, type: 'text', value: 'INV-' },
{ order: 2, type: 'dateFormat', value: 'YYYYMM' },
{ order: 3, type: 'separator', value: '-' },
{ order: 4, type: 'sequenceNo', minSeqLength: 5 }
]
});
// Generate a UIN preview
const uinPreview = await uinService.previewUIN({
uinSegments: [
{ order: 1, type: 'text', value: 'INV-' },
{ order: 2, type: 'dateFormat', value: 'YYYYMM' },
{ order: 3, type: 'separator', value: '-' },
{ order: 4, type: 'sequenceNo', minSeqLength: 5 }
]
});
// Get a UIN Config by ID
const uinConfig = await uinService.getUINConfigById(1);
// Generate a new UIN
const generatedUin = await uinService.generateUIN(UinShortCode.INVOICE);API Reference
Common Service
The library provides a comprehensive service for common operations:
search: Search records with pagination and filteringdropdownSearch: Search records for dropdown componentsfixedSearch: Search with fixed criteriafixedSearchWoPaginationService: Search with fixed criteria without paginationcommonExcelService: Generate Excel workbooksfetch: Fetch a specific record by IDcommonExcelImport: Import data from Excel filescommonExcelExport: Export data to Excel filesdelete: Delete a recordupdateStatus: Update the status of a recordfetchImageStream: Fetch an image as a stream
UIN Config Service
The library provides a service for managing Unique Identification Numbers:
createUINConfig: Create a new UIN configurationupdateUINConfig: Update an existing UIN configurationgetUINConfigById: Retrieve a UIN configuration by IDgetAllUINConfig: Retrieve all UIN configurationsdeleteUINConfig: Delete a UIN configurationgenerateUIN: Generate a new UIN based on a shortcodepreviewUIN: Preview a UIN based on segment configuration
Utility Functions
The library also provides utility functions:
customOmit: Omit specific keys from an objectobjectTo2DArray: Convert an object to a 2D arraytoRelativeImageUrl: Convert an absolute path to a relative image URL
License
ISC
