feat: implement inventory, product brand, product category, product info, product, role, store, user, and vendor modules with CRUD operations
- Added Create and Update DTOs for Inventory, Product Brand, Product Category, Product Info, Product, Role, Store, User, and Vendor. - Implemented InventoriesController, ProductBrandsController, ProductCategoriesController, ProductInfoController, ProductsController, RolesController, StoresController, UsersController, and VendorsController with respective CRUD endpoints. - Developed InventoriesService, ProductBrandsService, ProductCategoriesService, ProductInfoService, ProductsService, RolesService, StoresService, UsersService, and VendorsService to handle business logic. - Created PrismaModule and PrismaService for database interactions using Prisma with MariaDB adapter. - Configured application with Swagger for API documentation and added global interceptors for logging and response mapping. - Established e2e testing setup with Jest for the application.
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Prisma 7 config helper.
|
||||
*
|
||||
* Purpose: centralize PrismaClient options for Nest and tests.
|
||||
* This reads environment variables (e.g. PRISMA_ACCELERATE_URL, PRISMA_CLIENT_LOG)
|
||||
* and returns a valid options object to pass to `new PrismaClient(options)`.
|
||||
*
|
||||
* Keep this file small and dependency-free so it can be used in scripts.
|
||||
*/
|
||||
|
||||
const pkg = require('@prisma/adapter-mariadb')
|
||||
|
||||
export function getPrismaOptions(): any {
|
||||
const options: any = {}
|
||||
|
||||
// Logging: allow configuring via PRISMA_CLIENT_LOG (comma separated levels)
|
||||
// default to query/info/warn/error during development
|
||||
const envLog = process.env.PRISMA_CLIENT_LOG
|
||||
if (envLog && envLog.trim() !== '') {
|
||||
const levels = envLog
|
||||
.split(',')
|
||||
.map(s => s.trim())
|
||||
.filter(Boolean)
|
||||
options.log = levels.map((level: string) => ({ emit: 'stdout', level }))
|
||||
} else {
|
||||
options.log = [
|
||||
{ emit: 'stdout', level: 'query' },
|
||||
{ emit: 'stdout', level: 'info' },
|
||||
{ emit: 'stdout', level: 'warn' },
|
||||
{ emit: 'stdout', level: 'error' },
|
||||
]
|
||||
}
|
||||
|
||||
// Prisma Accelerate: only add when provided and non-empty (Prisma 7 validation requires non-empty string)
|
||||
const accel = process.env.PRISMA_ACCELERATE_URL
|
||||
if (accel && accel.trim() !== '') {
|
||||
options.accelerateUrl = accel
|
||||
}
|
||||
|
||||
try {
|
||||
// dynamic require so this file doesn't hard-depend on adapters at compile time
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
if (pkg) {
|
||||
// Try common export patterns: default function, module function, or factory named createAdapter/getAdapter
|
||||
if (typeof pkg === 'function') {
|
||||
options.adapter = pkg()
|
||||
} else if (pkg.default && typeof pkg.default === 'function') {
|
||||
options.adapter = pkg.default()
|
||||
} else if (pkg.createAdapter && typeof pkg.createAdapter === 'function') {
|
||||
options.adapter = pkg.createAdapter()
|
||||
} else if (pkg.getAdapter && typeof pkg.getAdapter === 'function') {
|
||||
options.adapter = pkg.getAdapter()
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
// package not found or failed to initialize; try next candidate
|
||||
}
|
||||
|
||||
// Error format (optional)
|
||||
if (process.env.PRISMA_ERROR_FORMAT) {
|
||||
options.errorFormat = process.env.PRISMA_ERROR_FORMAT as any
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Global, Module } from '@nestjs/common'
|
||||
import { PrismaService } from './prisma.service'
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [PrismaService],
|
||||
exports: [PrismaService],
|
||||
})
|
||||
export class PrismaModule {}
|
||||
@@ -0,0 +1,33 @@
|
||||
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common'
|
||||
import { PrismaMariaDb } from '@prisma/adapter-mariadb'
|
||||
import 'dotenv/config'
|
||||
import { env } from 'prisma/config'
|
||||
import { PrismaClient } from '../generated/prisma/client'
|
||||
|
||||
@Injectable()
|
||||
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
|
||||
constructor() {
|
||||
const adapter = new PrismaMariaDb({
|
||||
host: env('DATABASE_HOST'),
|
||||
user: env('DATABASE_USER'),
|
||||
password: env('DATABASE_PASSWORD'),
|
||||
database: env('DATABASE_NAME'),
|
||||
connectionLimit: 5,
|
||||
})
|
||||
super({ adapter })
|
||||
}
|
||||
|
||||
async onModuleInit() {
|
||||
await this.$connect()
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.$disconnect()
|
||||
}
|
||||
|
||||
// async enableShutdownHooks(app: any) {
|
||||
// this.$on('beforeExit', async () => {
|
||||
// await app.close()
|
||||
// })
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user