@gzl10/nexus-sdk 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli/index.js CHANGED
@@ -150,6 +150,6 @@ New file: ${path}`));
150
150
  }
151
151
 
152
152
  // src/cli/index.ts
153
- var program = new Command2().name("nexus-sdk").description("Nexus SDK CLI - Code generation from EntityDefinitions").version("0.8.0");
153
+ var program = new Command2().name("nexus-sdk").description("Nexus SDK CLI - Code generation from EntityDefinitions").version("0.9.0");
154
154
  program.addCommand(createGenerateCommand());
155
155
  program.parse();
package/dist/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Knex } from 'knex';
2
2
  export { Knex } from 'knex';
3
- import { Request, Router, RequestHandler } from 'express';
3
+ import { Request, Router, RequestHandler, Response } from 'express';
4
4
  export { CookieOptions, NextFunction, Request, RequestHandler, Response, Router } from 'express';
5
5
  import { Logger } from 'pino';
6
6
 
@@ -628,6 +628,8 @@ interface ContextHelpers {
628
628
  addTimestamps: (table: Knex.CreateTableBuilder, db: Knex) => void;
629
629
  addAuditFieldsIfMissing: (db: Knex, tableName: string) => Promise<void>;
630
630
  addColumnIfMissing: (db: Knex, tableName: string, columnName: string, columnBuilder: (table: Knex.AlterTableBuilder) => void) => Promise<boolean>;
631
+ getLibPath: () => string;
632
+ getProjectPath: () => string;
631
633
  }
632
634
  /**
633
635
  * Schema de validación genérico (compatible con Zod sin depender de él)
@@ -661,13 +663,203 @@ interface EventEmitterLike {
661
663
  once: (event: string, listener: (...args: unknown[]) => void) => this;
662
664
  onAny?: (listener: (event: string, ...args: unknown[]) => void) => this;
663
665
  }
666
+ /**
667
+ * Reporter mínimo para errores (Sentry/GlitchTip, etc.)
668
+ */
669
+ interface LoggerReporter {
670
+ captureException: (error: Error, context?: Record<string, unknown>) => void;
671
+ }
672
+ /**
673
+ * Query parameters for entity list operations
674
+ */
675
+ interface EntityQuery {
676
+ page?: number;
677
+ limit?: number;
678
+ sort?: string;
679
+ order?: 'asc' | 'desc';
680
+ search?: string;
681
+ filters?: Record<string, unknown>;
682
+ }
683
+ /**
684
+ * Interfaz base para sub-servicio de roles
685
+ * El backend extiende esto con tipos concretos
686
+ */
687
+ interface BaseRolesService {
688
+ findAll(query?: EntityQuery): Promise<PaginatedResult<unknown>>;
689
+ findById(id: string): Promise<unknown>;
690
+ findByName(name: string): Promise<unknown | undefined>;
691
+ getPermissionsByRoleId(roleId: string): Promise<unknown[]>;
692
+ create(data: Record<string, unknown>, userId?: string): Promise<unknown>;
693
+ update(id: string, data: Record<string, unknown>, userId?: string): Promise<unknown>;
694
+ delete(id: string): Promise<void>;
695
+ updatePermissions(id: string, permissions: unknown[], userId?: string): Promise<unknown>;
696
+ }
697
+ /**
698
+ * Interfaz base para servicio de usuarios
699
+ * El backend extiende esto con tipos concretos (UserWithRole, etc.)
700
+ */
701
+ interface BaseUsersService {
702
+ findAll(query?: EntityQuery): Promise<PaginatedResult<unknown>>;
703
+ findById(id: string): Promise<unknown>;
704
+ findByIdWithRole(id: string): Promise<unknown | null>;
705
+ create(data: Record<string, unknown>, userId?: string): Promise<unknown>;
706
+ update(id: string, data: Record<string, unknown>, userId?: string): Promise<unknown>;
707
+ delete(id: string): Promise<void>;
708
+ roles: BaseRolesService;
709
+ }
710
+ /**
711
+ * Opciones para envío de email
712
+ */
713
+ interface SendMailOptions {
714
+ to: string | string[];
715
+ subject: string;
716
+ title?: string;
717
+ message?: string;
718
+ html?: string;
719
+ text?: string;
720
+ from?: string;
721
+ replyTo?: string;
722
+ actions?: Array<{
723
+ label: string;
724
+ url: string;
725
+ }>;
726
+ logoUrl?: string;
727
+ attachments?: Array<{
728
+ filename: string;
729
+ content: Buffer | string;
730
+ contentType?: string;
731
+ }>;
732
+ }
733
+ /**
734
+ * Resultado del envío de email
735
+ */
736
+ interface SendMailResult {
737
+ messageId: string;
738
+ accepted: string[];
739
+ rejected: string[];
740
+ }
741
+ /**
742
+ * Interfaz base para servicio de email
743
+ */
744
+ interface BaseMailService {
745
+ send(options: SendMailOptions): Promise<SendMailResult | null>;
746
+ verify(): Promise<boolean>;
747
+ }
748
+ /**
749
+ * Servicios core disponibles en ctx.services
750
+ * Los plugins pueden extender con servicios adicionales
751
+ */
752
+ interface CoreServices {
753
+ logger?: LoggerReporter;
754
+ users?: BaseUsersService;
755
+ mail?: BaseMailService;
756
+ [key: string]: unknown;
757
+ }
758
+ /**
759
+ * Interfaz base para todos los servicios de entidades
760
+ */
761
+ interface BaseEntityService<T = unknown> {
762
+ findAll(query?: EntityQuery): Promise<PaginatedResult<T>>;
763
+ findById(id: string): Promise<T | null>;
764
+ readonly definition: EntityDefinition;
765
+ }
766
+ /**
767
+ * Servicio para entidades Collection (CRUD completo)
768
+ */
769
+ interface CollectionEntityService<T = unknown> extends BaseEntityService<T> {
770
+ create(data: Partial<T>, userId?: string): Promise<T>;
771
+ update(id: string, data: Partial<T>, userId?: string): Promise<T>;
772
+ delete(id: string): Promise<void>;
773
+ }
774
+ /**
775
+ * Servicio para entidades Temp (TTL auto-cleanup)
776
+ */
777
+ interface TempEntityService<T = unknown> extends BaseEntityService<T> {
778
+ create(data: Partial<T>): Promise<T>;
779
+ update(id: string, data: Partial<T>, extendTtl?: boolean): Promise<T>;
780
+ delete(id: string): Promise<void>;
781
+ extendTtl(id: string, additionalSeconds?: number): Promise<T>;
782
+ cleanup(): Promise<number>;
783
+ getRemainingTtl(id: string): Promise<number | null>;
784
+ isValid(id: string): Promise<boolean>;
785
+ }
786
+ /**
787
+ * Servicio para entidades Event (append-only, immutable)
788
+ */
789
+ interface EventEntityService<T = unknown> extends BaseEntityService<T> {
790
+ create(data: Partial<T>): Promise<T>;
791
+ append(data: Partial<T>): Promise<T>;
792
+ runRetentionCleanup(): Promise<number>;
793
+ findByDateRange(start: Date, end: Date, query?: EntityQuery): Promise<PaginatedResult<T>>;
794
+ }
795
+ /**
796
+ * Servicio para entidades Single (registro único, update/read)
797
+ */
798
+ interface SingleEntityService<T = unknown> extends BaseEntityService<T> {
799
+ get(): Promise<T | null>;
800
+ update(data: Partial<T>, userId?: string): Promise<T>;
801
+ }
802
+ /**
803
+ * Servicio para entidades Config (configuración clave-valor)
804
+ */
805
+ interface ConfigEntityService<T = unknown> extends BaseEntityService<T> {
806
+ get(key: string): Promise<T | null>;
807
+ set(key: string, value: unknown, userId?: string): Promise<T>;
808
+ getAll(): Promise<Record<string, unknown>>;
809
+ }
810
+ /**
811
+ * Servicio para entidades Reference (lectura con join automático)
812
+ */
813
+ interface ReferenceEntityService<T = unknown> extends BaseEntityService<T> {
814
+ }
815
+ /**
816
+ * Servicio para entidades View (vista SQL, solo lectura)
817
+ */
818
+ interface ViewEntityService<T = unknown> extends BaseEntityService<T> {
819
+ }
820
+ /**
821
+ * Servicio para entidades Computed (campos calculados, solo lectura)
822
+ */
823
+ interface ComputedEntityService<T = unknown> extends BaseEntityService<T> {
824
+ }
825
+ /**
826
+ * Servicio para entidades External (datos de API externa)
827
+ */
828
+ interface ExternalEntityService<T = unknown> extends BaseEntityService<T> {
829
+ }
830
+ /**
831
+ * Servicio para entidades Virtual (orquestación de múltiples fuentes)
832
+ */
833
+ interface VirtualEntityService<T = unknown> extends BaseEntityService<T> {
834
+ }
835
+ /**
836
+ * Servicio para entidades Action (ejecutar acciones)
837
+ */
838
+ interface ActionEntityService<T = unknown> {
839
+ execute(data: Partial<T>): Promise<unknown>;
840
+ readonly definition: EntityDefinition;
841
+ }
842
+ /**
843
+ * Handler de entidad para Express
844
+ */
845
+ type EntityHandler = (req: Request, res: Response) => Promise<void>;
846
+ /**
847
+ * Controller de entidad con handlers CRUD
848
+ */
849
+ interface EntityController {
850
+ list: EntityHandler;
851
+ get: EntityHandler;
852
+ create?: EntityHandler;
853
+ update?: EntityHandler;
854
+ delete?: EntityHandler;
855
+ execute?: EntityHandler;
856
+ }
664
857
  /**
665
858
  * Contexto inyectado a los módulos
666
859
  */
667
860
  interface ModuleContext {
668
861
  db: Knex;
669
862
  logger: Logger;
670
- dbType: 'sqlite' | 'postgresql' | 'mysql';
671
863
  helpers: ContextHelpers;
672
864
  createRouter: () => Router;
673
865
  middleware: ModuleMiddlewares;
@@ -687,7 +879,13 @@ interface ModuleContext {
687
879
  /** Sistema de eventos compatible con EventEmitter2 */
688
880
  events: EventEmitterLike;
689
881
  /** Servicios de módulos (inyectados por backend) */
690
- services: Record<string, unknown>;
882
+ services: CoreServices;
883
+ /** Factory para crear servicios de entidades (detecta tipo automáticamente) */
884
+ createEntityService<T = unknown>(definition: EntityDefinition): BaseEntityService<T>;
885
+ /** Factory para crear controller de entidad con CASL y validación */
886
+ createEntityController(service: BaseEntityService, definition: EntityDefinition): EntityController;
887
+ /** Factory para crear router de entidad con rutas CRUD */
888
+ createEntityRouter(controller: EntityController, definition: EntityDefinition): Router;
691
889
  }
692
890
  /**
693
891
  * Manifest de un módulo Nexus
@@ -750,4 +948,4 @@ interface PluginManifest {
750
948
  modules: ModuleManifest[];
751
949
  }
752
950
 
753
- export { type AbilityLike, type ActionEntityDefinition, type AuthRequest, type BaseUser, type CaslAction, type CollectionEntityDefinition, type ComputedEntityDefinition, type ConfigEntityDefinition, type ContextHelpers, type DbType, type EntityCaslConfig, type EntityDefinition, type EntityIndex, type EntityType, type EventEmitterLike, type EventEntityDefinition, type ExternalEntityDefinition, type FieldCaslAccess, type FieldDbConfig, type FieldDefinition, type FieldMeta, type FieldOptions, type FieldRelation, type FieldStorageConfig, type FieldValidationConfig, type ForbiddenErrorConstructor, type ForbiddenErrorInstance, GENERATED_DIR, type InputType, type KnexAlterTableBuilder, type KnexCreateTableBuilder, type KnexTransaction, type ModuleAbilities, type ModuleContext, type ModuleManifest, type ModuleMiddlewares, type ModuleRequirements, type NonPersistentEntityDefinition, type OwnershipCondition, type PaginatedResult, type PaginationParams, type PersistentEntityDefinition, type PluginAuthRequest, type PluginCategory, type PluginManifest, type ReferenceEntityDefinition, type RolePermission, type SingleEntityDefinition, type TempEntityDefinition, type ValidateSchemas, type ValidationSchema, type ViewEntityDefinition, type VirtualEntityDefinition, generateModel, generateModelsFile, generateReadOnlyModel, getEntityName, getEntitySubject, hasTable, isPersistentEntity, isSingletonEntity };
951
+ export { type AbilityLike, type ActionEntityDefinition, type ActionEntityService, type AuthRequest, type BaseEntityService, type BaseMailService, type BaseRolesService, type BaseUser, type BaseUsersService, type CaslAction, type CollectionEntityDefinition, type CollectionEntityService, type ComputedEntityDefinition, type ComputedEntityService, type ConfigEntityDefinition, type ConfigEntityService, type ContextHelpers, type CoreServices, type DbType, type EntityCaslConfig, type EntityController, type EntityDefinition, type EntityHandler, type EntityIndex, type EntityQuery, type EntityType, type EventEmitterLike, type EventEntityDefinition, type EventEntityService, type ExternalEntityDefinition, type ExternalEntityService, type FieldCaslAccess, type FieldDbConfig, type FieldDefinition, type FieldMeta, type FieldOptions, type FieldRelation, type FieldStorageConfig, type FieldValidationConfig, type ForbiddenErrorConstructor, type ForbiddenErrorInstance, GENERATED_DIR, type InputType, type KnexAlterTableBuilder, type KnexCreateTableBuilder, type KnexTransaction, type LoggerReporter, type ModuleAbilities, type ModuleContext, type ModuleManifest, type ModuleMiddlewares, type ModuleRequirements, type NonPersistentEntityDefinition, type OwnershipCondition, type PaginatedResult, type PaginationParams, type PersistentEntityDefinition, type PluginAuthRequest, type PluginCategory, type PluginManifest, type ReferenceEntityDefinition, type ReferenceEntityService, type RolePermission, type SendMailOptions, type SendMailResult, type SingleEntityDefinition, type SingleEntityService, type TempEntityDefinition, type TempEntityService, type ValidateSchemas, type ValidationSchema, type ViewEntityDefinition, type ViewEntityService, type VirtualEntityDefinition, type VirtualEntityService, generateModel, generateModelsFile, generateReadOnlyModel, getEntityName, getEntitySubject, hasTable, isPersistentEntity, isSingletonEntity };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gzl10/nexus-sdk",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "description": "SDK types for creating Nexus plugins and modules",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",