Refactor: Centralize Prisma schema and restrict DB access

This commit refactors the project to use a shared Prisma schema and restricts direct database access to the API service.

Key changes:

- Created a new shared package `core/prisma` containing the Prisma schema, generated client, and types.
- Configured the monorepo to use NPM workspaces, including `core/prisma` and all services.
- Updated all services (`api`, `ui`, `mcp`, `agent`, and the background processing service) to depend on `@empresa/prisma-schema`.
- The API service now imports `PrismaClient` from `@empresa/prisma-schema/client`.
- Other services import only types from `@empresa/prisma-schema`.
- Removed redundant Prisma configurations from `api` and the background processing service.
- Updated the background processing service's `sync-empleados.js` to fetch data via an API call instead of direct database access.
- Updated TypeScript configurations (`tsconfig.base.json` and service-specific ones) to support the new structure and path aliases.
- Updated `README.md` to reflect the new architecture and added convenience scripts for Prisma operations.

This change promotes a single source of truth for data models, reduces code duplication, and improves the overall architecture by centralizing database operations within the API service.
This commit is contained in:
google-labs-jules[bot]
2025-05-30 23:40:00 +00:00
parent 4f1ec58a99
commit 242dc66983
26 changed files with 381 additions and 243 deletions

2
core/prisma/client.ts Normal file
View File

@@ -0,0 +1,2 @@
// This file exports the PrismaClient instance for use in Node.js environments (specifically the API).
export { PrismaClient } from './generated/client';

7
core/prisma/index.ts Normal file
View File

@@ -0,0 +1,7 @@
// This file serves as the entry point for the @empresa/prisma-schema package.
// It re-exports only the generated types by default.
// For PrismaClient instance, import from '@empresa/prisma-schema/client'.
export * from './generated/client/index-browser'; // Exports types for browser/non-Node.js environments
// If you need all types including Node.js specific ones (less common for shared types):
// export * from './generated/client';

27
core/prisma/package.json Normal file
View File

@@ -0,0 +1,27 @@
{
"name": "@empresa/prisma-schema",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./client": {
"types": "./dist/client.d.ts",
"default": "./dist/client.js"
}
},
"scripts": {
"db:generate": "prisma generate",
"prisma:migrate:dev": "prisma migrate dev",
"prisma:deploy": "prisma migrate deploy"
},
"devDependencies": {
"prisma": "^5.10.2"
},
"dependencies": {
"@prisma/client": "^5.10.2"
}
}

88
core/prisma/schema.prisma Normal file
View File

@@ -0,0 +1,88 @@
generator client {
provider = "prisma-client-js"
output = "./generated/client"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Cliente {
id BigInt @id @default(autoincrement())
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
updated_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')")) @updatedAt
name String
cedula BigInt @unique
ubicacion String @default(".")
grupo_estudio String?
empleado Boolean @default(false)
avatar_url String?
telefono String?
idciat String?
asistencias Asistencia[]
tareasRealizadas TareaRealizada[]
planillas Planilla[]
}
model Planilla {
id BigInt @id @default(autoincrement())
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
updated_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')")) @updatedAt
fecha_desde DateTime
fecha_hasta DateTime
titulo String
total Decimal?
estado String @default("pagado")
fecha_anulado DateTime?
tareasRealizadas TareaRealizada[]
empleado_id BigInt
empleado Cliente @relation(fields: [empleado_id], references: [id])
creador_id String? @default(uuid()) @db.Uuid
anulador_id String? @db.Uuid
}
model TareaRealizada {
id BigInt @id @default(autoincrement())
titulo String
precio Float?
estado String @default("pendiente")
observacion String?
fecha DateTime
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
tipo String @default("")
fecha_anulado DateTime?
planilla_id BigInt?
planilla Planilla? @relation(fields: [planilla_id], references: [id])
empleado_id BigInt
empleado Cliente @relation(fields: [empleado_id], references: [id])
creador_id String @default(uuid()) @db.Uuid
anulador_id String? @db.Uuid
}
model Asistencia {
id BigInt @id @default(autoincrement())
created_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
updated_at DateTime @default(dbgenerated("(now() AT TIME ZONE 'utc')")) @updatedAt
entrada DateTime? @default(dbgenerated("(now() AT TIME ZONE 'utc')"))
salida DateTime?
historial Json?
observacion String?
estado String? @default("pendiente")
fecha_anulado DateTime?
empleado_id BigInt
empleado Cliente @relation(fields: [empleado_id], references: [id])
creador_id String @default(uuid()) @db.Uuid
modificado_id String? @db.Uuid
anulador_id String? @db.Uuid
}

26
core/prisma/tsconfig.json Normal file
View File

@@ -0,0 +1,26 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"composite": true,
"target": "es2017",
"module": "commonjs",
"moduleResolution": "node",
"outDir": "dist",
"declaration": true,
"emitDeclarationOnly": true,
"declarationDir": "dist",
// Overriding base to be specific for this package
"baseUrl": ".",
"paths": {} // Clear paths from base, not needed here
},
"include": ["./schema.prisma", "./generated/client", "index.ts", "client.ts"],
"exclude": [
"node_modules",
"dist",
".next",
".turbo",
"coverage",
"build",
"prisma/seed.ts"
]
}