@gzl10/nexus-sdk 0.8.1 → 0.10.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.1");
153
+ var program = new Command2().name("nexus-sdk").description("Nexus SDK CLI - Code generation from EntityDefinitions").version("0.10.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,6 +663,255 @@ 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
+ * Hooks para personalizar comportamiento de servicios de entidad
844
+ * Permite a plugins añadir lógica sin necesitar herencia
845
+ */
846
+ interface EntityServiceHooks<T = unknown> {
847
+ /**
848
+ * Ejecutado antes de crear un registro
849
+ * @param data Datos a insertar
850
+ * @returns Datos modificados
851
+ */
852
+ beforeCreate?: (data: Partial<T>) => Promise<Partial<T>> | Partial<T>;
853
+ /**
854
+ * Ejecutado después de crear un registro
855
+ * @param record Registro creado
856
+ */
857
+ afterCreate?: (record: T) => Promise<void> | void;
858
+ /**
859
+ * Ejecutado antes de actualizar un registro
860
+ * @param id ID del registro
861
+ * @param data Datos a actualizar
862
+ * @returns Datos modificados
863
+ */
864
+ beforeUpdate?: (id: string, data: Partial<T>) => Promise<Partial<T>> | Partial<T>;
865
+ /**
866
+ * Ejecutado después de actualizar un registro
867
+ * @param record Registro actualizado
868
+ */
869
+ afterUpdate?: (record: T) => Promise<void> | void;
870
+ /**
871
+ * Ejecutado antes de eliminar un registro
872
+ * @param id ID del registro
873
+ */
874
+ beforeDelete?: (id: string) => Promise<void> | void;
875
+ /**
876
+ * Ejecutado después de eliminar un registro
877
+ * @param id ID eliminado
878
+ */
879
+ afterDelete?: (id: string) => Promise<void> | void;
880
+ /**
881
+ * Ejecutado después de obtener un registro por ID
882
+ * @param record Registro obtenido (o null)
883
+ * @returns Registro modificado
884
+ */
885
+ afterFindById?: (record: T | null) => Promise<T | null> | T | null;
886
+ /**
887
+ * Ejecutado después de obtener lista paginada
888
+ * @param result Resultado paginado
889
+ * @returns Resultado modificado
890
+ */
891
+ afterFindAll?: (result: PaginatedResult<T>) => Promise<PaginatedResult<T>> | PaginatedResult<T>;
892
+ }
893
+ /**
894
+ * Opciones para createEntityService
895
+ */
896
+ interface CreateEntityServiceOptions<T = unknown> {
897
+ /** Hooks para personalizar comportamiento */
898
+ hooks?: EntityServiceHooks<T>;
899
+ }
900
+ /**
901
+ * Handler de entidad para Express
902
+ */
903
+ type EntityHandler = (req: Request, res: Response) => Promise<void>;
904
+ /**
905
+ * Controller de entidad con handlers CRUD
906
+ */
907
+ interface EntityController {
908
+ list: EntityHandler;
909
+ get: EntityHandler;
910
+ create?: EntityHandler;
911
+ update?: EntityHandler;
912
+ delete?: EntityHandler;
913
+ execute?: EntityHandler;
914
+ }
664
915
  /**
665
916
  * Contexto inyectado a los módulos
666
917
  */
@@ -686,7 +937,13 @@ interface ModuleContext {
686
937
  /** Sistema de eventos compatible con EventEmitter2 */
687
938
  events: EventEmitterLike;
688
939
  /** Servicios de módulos (inyectados por backend) */
689
- services: Record<string, unknown>;
940
+ services: CoreServices;
941
+ /** Factory para crear servicios de entidades (detecta tipo automáticamente) */
942
+ createEntityService<T = unknown>(definition: EntityDefinition, options?: CreateEntityServiceOptions<T>): BaseEntityService<T>;
943
+ /** Factory para crear controller de entidad con CASL y validación */
944
+ createEntityController(service: BaseEntityService, definition: EntityDefinition): EntityController;
945
+ /** Factory para crear router de entidad con rutas CRUD */
946
+ createEntityRouter(controller: EntityController, definition: EntityDefinition): Router;
690
947
  }
691
948
  /**
692
949
  * Manifest de un módulo Nexus
@@ -749,4 +1006,4 @@ interface PluginManifest {
749
1006
  modules: ModuleManifest[];
750
1007
  }
751
1008
 
752
- 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 };
1009
+ 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 CreateEntityServiceOptions, type DbType, type EntityCaslConfig, type EntityController, type EntityDefinition, type EntityHandler, type EntityIndex, type EntityQuery, type EntityServiceHooks, 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.1",
3
+ "version": "0.10.0",
4
4
  "description": "SDK types for creating Nexus plugins and modules",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",