@designliquido/delegua-entidades 0.0.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.
Files changed (206) hide show
  1. package/README.md +96 -0
  2. package/fontes/analisador-dicas.d.ts +49 -0
  3. package/fontes/analisador-dicas.d.ts.map +1 -0
  4. package/fontes/analisador-dicas.js +145 -0
  5. package/fontes/analisador-dicas.js.map +1 -0
  6. package/fontes/cache-consultas.d.ts +39 -0
  7. package/fontes/cache-consultas.d.ts.map +1 -0
  8. package/fontes/cache-consultas.js +98 -0
  9. package/fontes/cache-consultas.js.map +1 -0
  10. package/fontes/carregador-lotes.d.ts +64 -0
  11. package/fontes/carregador-lotes.d.ts.map +1 -0
  12. package/fontes/carregador-lotes.js +148 -0
  13. package/fontes/carregador-lotes.js.map +1 -0
  14. package/fontes/carregador-preguicoso.d.ts +34 -0
  15. package/fontes/carregador-preguicoso.d.ts.map +1 -0
  16. package/fontes/carregador-preguicoso.js +157 -0
  17. package/fontes/carregador-preguicoso.js.map +1 -0
  18. package/fontes/colecao.d.ts +74 -0
  19. package/fontes/colecao.d.ts.map +1 -0
  20. package/fontes/colecao.js +374 -0
  21. package/fontes/colecao.js.map +1 -0
  22. package/fontes/construtor-consulta.d.ts +129 -0
  23. package/fontes/construtor-consulta.d.ts.map +1 -0
  24. package/fontes/construtor-consulta.js +446 -0
  25. package/fontes/construtor-consulta.js.map +1 -0
  26. package/fontes/contexto-entidades.d.ts +119 -0
  27. package/fontes/contexto-entidades.d.ts.map +1 -0
  28. package/fontes/contexto-entidades.js +329 -0
  29. package/fontes/contexto-entidades.js.map +1 -0
  30. package/fontes/detector-n-mais-um.d.ts +87 -0
  31. package/fontes/detector-n-mais-um.d.ts.map +1 -0
  32. package/fontes/detector-n-mais-um.js +225 -0
  33. package/fontes/detector-n-mais-um.js.map +1 -0
  34. package/fontes/entidade.d.ts +82 -0
  35. package/fontes/entidade.d.ts.map +1 -0
  36. package/fontes/entidade.js +467 -0
  37. package/fontes/entidade.js.map +1 -0
  38. package/fontes/erros/erro-concorrencia.d.ts +11 -0
  39. package/fontes/erros/erro-concorrencia.d.ts.map +1 -0
  40. package/fontes/erros/erro-concorrencia.js +18 -0
  41. package/fontes/erros/erro-concorrencia.js.map +1 -0
  42. package/fontes/erros/erro-tabela-nao-encontrada.d.ts +4 -0
  43. package/fontes/erros/erro-tabela-nao-encontrada.d.ts.map +1 -0
  44. package/fontes/erros/erro-tabela-nao-encontrada.js +11 -0
  45. package/fontes/erros/erro-tabela-nao-encontrada.js.map +1 -0
  46. package/fontes/erros/erro-validacao.d.ts +9 -0
  47. package/fontes/erros/erro-validacao.d.ts.map +1 -0
  48. package/fontes/erros/erro-validacao.js +13 -0
  49. package/fontes/erros/erro-validacao.js.map +1 -0
  50. package/fontes/erros/index.d.ts +4 -0
  51. package/fontes/erros/index.d.ts.map +1 -0
  52. package/fontes/erros/index.js +20 -0
  53. package/fontes/erros/index.js.map +1 -0
  54. package/fontes/gerenciador-cache.d.ts +87 -0
  55. package/fontes/gerenciador-cache.d.ts.map +1 -0
  56. package/fontes/gerenciador-cache.js +147 -0
  57. package/fontes/gerenciador-cache.js.map +1 -0
  58. package/fontes/ilc/gerador-definicoes.d.ts +13 -0
  59. package/fontes/ilc/gerador-definicoes.d.ts.map +1 -0
  60. package/fontes/ilc/gerador-definicoes.js +96 -0
  61. package/fontes/ilc/gerador-definicoes.js.map +1 -0
  62. package/fontes/ilc/gerador-entidades.d.ts +10 -0
  63. package/fontes/ilc/gerador-entidades.d.ts.map +1 -0
  64. package/fontes/ilc/gerador-entidades.js +318 -0
  65. package/fontes/ilc/gerador-entidades.js.map +1 -0
  66. package/fontes/ilc/leitor-configuracao.d.ts +32 -0
  67. package/fontes/ilc/leitor-configuracao.d.ts.map +1 -0
  68. package/fontes/ilc/leitor-configuracao.js +124 -0
  69. package/fontes/ilc/leitor-configuracao.js.map +1 -0
  70. package/fontes/ilc/migracoes.d.ts +3 -0
  71. package/fontes/ilc/migracoes.d.ts.map +1 -0
  72. package/fontes/ilc/migracoes.js +215 -0
  73. package/fontes/ilc/migracoes.js.map +1 -0
  74. package/fontes/ilc/semeadura.d.ts +2 -0
  75. package/fontes/ilc/semeadura.d.ts.map +1 -0
  76. package/fontes/ilc/semeadura.js +50 -0
  77. package/fontes/ilc/semeadura.js.map +1 -0
  78. package/fontes/indicadores-performance/executor-analise-performance.d.ts +80 -0
  79. package/fontes/indicadores-performance/executor-analise-performance.d.ts.map +1 -0
  80. package/fontes/indicadores-performance/executor-analise-performance.js +212 -0
  81. package/fontes/indicadores-performance/executor-analise-performance.js.map +1 -0
  82. package/fontes/indicadores-performance/index.d.ts +26 -0
  83. package/fontes/indicadores-performance/index.d.ts.map +1 -0
  84. package/fontes/indicadores-performance/index.js +199 -0
  85. package/fontes/indicadores-performance/index.js.map +1 -0
  86. package/fontes/interfaces-tipos/analise-n1-interface.d.ts +13 -0
  87. package/fontes/interfaces-tipos/analise-n1-interface.d.ts.map +1 -0
  88. package/fontes/interfaces-tipos/analise-n1-interface.js +3 -0
  89. package/fontes/interfaces-tipos/analise-n1-interface.js.map +1 -0
  90. package/fontes/interfaces-tipos/atributo-interface.d.ts +30 -0
  91. package/fontes/interfaces-tipos/atributo-interface.d.ts.map +1 -0
  92. package/fontes/interfaces-tipos/atributo-interface.js +3 -0
  93. package/fontes/interfaces-tipos/atributo-interface.js.map +1 -0
  94. package/fontes/interfaces-tipos/coluna-computada-interface.d.ts +16 -0
  95. package/fontes/interfaces-tipos/coluna-computada-interface.d.ts.map +1 -0
  96. package/fontes/interfaces-tipos/coluna-computada-interface.js +3 -0
  97. package/fontes/interfaces-tipos/coluna-computada-interface.js.map +1 -0
  98. package/fontes/interfaces-tipos/configuracao-banco-dados-interface.d.ts +48 -0
  99. package/fontes/interfaces-tipos/configuracao-banco-dados-interface.d.ts.map +1 -0
  100. package/fontes/interfaces-tipos/configuracao-banco-dados-interface.js +3 -0
  101. package/fontes/interfaces-tipos/configuracao-banco-dados-interface.js.map +1 -0
  102. package/fontes/interfaces-tipos/entidade-interface.d.ts +37 -0
  103. package/fontes/interfaces-tipos/entidade-interface.d.ts.map +1 -0
  104. package/fontes/interfaces-tipos/entidade-interface.js +3 -0
  105. package/fontes/interfaces-tipos/entidade-interface.js.map +1 -0
  106. package/fontes/interfaces-tipos/ganchos.d.ts +12 -0
  107. package/fontes/interfaces-tipos/ganchos.d.ts.map +1 -0
  108. package/fontes/interfaces-tipos/ganchos.js +3 -0
  109. package/fontes/interfaces-tipos/ganchos.js.map +1 -0
  110. package/fontes/interfaces-tipos/index.d.ts +17 -0
  111. package/fontes/interfaces-tipos/index.d.ts.map +1 -0
  112. package/fontes/interfaces-tipos/index.js +33 -0
  113. package/fontes/interfaces-tipos/index.js.map +1 -0
  114. package/fontes/interfaces-tipos/indice-interface.d.ts +16 -0
  115. package/fontes/interfaces-tipos/indice-interface.d.ts.map +1 -0
  116. package/fontes/interfaces-tipos/indice-interface.js +3 -0
  117. package/fontes/interfaces-tipos/indice-interface.js.map +1 -0
  118. package/fontes/interfaces-tipos/informacao-consulta-interface.d.ts +12 -0
  119. package/fontes/interfaces-tipos/informacao-consulta-interface.d.ts.map +1 -0
  120. package/fontes/interfaces-tipos/informacao-consulta-interface.js +3 -0
  121. package/fontes/interfaces-tipos/informacao-consulta-interface.js.map +1 -0
  122. package/fontes/interfaces-tipos/muito-para-muitos-interface.d.ts +33 -0
  123. package/fontes/interfaces-tipos/muito-para-muitos-interface.d.ts.map +1 -0
  124. package/fontes/interfaces-tipos/muito-para-muitos-interface.js +3 -0
  125. package/fontes/interfaces-tipos/muito-para-muitos-interface.js.map +1 -0
  126. package/fontes/interfaces-tipos/opcoes-serializacao-interface.d.ts +14 -0
  127. package/fontes/interfaces-tipos/opcoes-serializacao-interface.d.ts.map +1 -0
  128. package/fontes/interfaces-tipos/opcoes-serializacao-interface.js +3 -0
  129. package/fontes/interfaces-tipos/opcoes-serializacao-interface.js.map +1 -0
  130. package/fontes/interfaces-tipos/polimorfico-interface.d.ts +32 -0
  131. package/fontes/interfaces-tipos/polimorfico-interface.d.ts.map +1 -0
  132. package/fontes/interfaces-tipos/polimorfico-interface.js +3 -0
  133. package/fontes/interfaces-tipos/polimorfico-interface.js.map +1 -0
  134. package/fontes/interfaces-tipos/relacionamento-interface.d.ts +23 -0
  135. package/fontes/interfaces-tipos/relacionamento-interface.d.ts.map +1 -0
  136. package/fontes/interfaces-tipos/relacionamento-interface.js +3 -0
  137. package/fontes/interfaces-tipos/relacionamento-interface.js.map +1 -0
  138. package/fontes/interfaces-tipos/restricao-interface.d.ts +14 -0
  139. package/fontes/interfaces-tipos/restricao-interface.d.ts.map +1 -0
  140. package/fontes/interfaces-tipos/restricao-interface.js +3 -0
  141. package/fontes/interfaces-tipos/restricao-interface.js.map +1 -0
  142. package/fontes/interfaces-tipos/semente-interface.d.ts +7 -0
  143. package/fontes/interfaces-tipos/semente-interface.d.ts.map +1 -0
  144. package/fontes/interfaces-tipos/semente-interface.js +3 -0
  145. package/fontes/interfaces-tipos/semente-interface.js.map +1 -0
  146. package/fontes/interfaces-tipos/tabela-interface.d.ts +12 -0
  147. package/fontes/interfaces-tipos/tabela-interface.d.ts.map +1 -0
  148. package/fontes/interfaces-tipos/tabela-interface.js +3 -0
  149. package/fontes/interfaces-tipos/tabela-interface.js.map +1 -0
  150. package/fontes/interfaces-tipos/transacao-interface.d.ts +35 -0
  151. package/fontes/interfaces-tipos/transacao-interface.d.ts.map +1 -0
  152. package/fontes/interfaces-tipos/transacao-interface.js +3 -0
  153. package/fontes/interfaces-tipos/transacao-interface.js.map +1 -0
  154. package/fontes/migracoes/executor-migracoes.d.ts +14 -0
  155. package/fontes/migracoes/executor-migracoes.d.ts.map +1 -0
  156. package/fontes/migracoes/executor-migracoes.js +177 -0
  157. package/fontes/migracoes/executor-migracoes.js.map +1 -0
  158. package/fontes/migracoes/gerador-migracoes.d.ts +15 -0
  159. package/fontes/migracoes/gerador-migracoes.d.ts.map +1 -0
  160. package/fontes/migracoes/gerador-migracoes.js +108 -0
  161. package/fontes/migracoes/gerador-migracoes.js.map +1 -0
  162. package/fontes/migracoes/migracao.d.ts +35 -0
  163. package/fontes/migracoes/migracao.d.ts.map +1 -0
  164. package/fontes/migracoes/migracao.js +66 -0
  165. package/fontes/migracoes/migracao.js.map +1 -0
  166. package/fontes/migracoes/semeador.d.ts +18 -0
  167. package/fontes/migracoes/semeador.d.ts.map +1 -0
  168. package/fontes/migracoes/semeador.js +109 -0
  169. package/fontes/migracoes/semeador.js.map +1 -0
  170. package/fontes/rastreador-mudancas.d.ts +27 -0
  171. package/fontes/rastreador-mudancas.d.ts.map +1 -0
  172. package/fontes/rastreador-mudancas.js +94 -0
  173. package/fontes/rastreador-mudancas.js.map +1 -0
  174. package/fontes/relacionamento.d.ts +11 -0
  175. package/fontes/relacionamento.d.ts.map +1 -0
  176. package/fontes/relacionamento.js +15 -0
  177. package/fontes/relacionamento.js.map +1 -0
  178. package/fontes/roteador-bancos.d.ts +50 -0
  179. package/fontes/roteador-bancos.d.ts.map +1 -0
  180. package/fontes/roteador-bancos.js +90 -0
  181. package/fontes/roteador-bancos.js.map +1 -0
  182. package/fontes/serializador.d.ts +46 -0
  183. package/fontes/serializador.d.ts.map +1 -0
  184. package/fontes/serializador.js +79 -0
  185. package/fontes/serializador.js.map +1 -0
  186. package/fontes/taquigrafia.d.ts +41 -0
  187. package/fontes/taquigrafia.d.ts.map +1 -0
  188. package/fontes/taquigrafia.js +96 -0
  189. package/fontes/taquigrafia.js.map +1 -0
  190. package/fontes/tipos.d.ts +3 -0
  191. package/fontes/tipos.d.ts.map +1 -0
  192. package/fontes/tipos.js +3 -0
  193. package/fontes/tipos.js.map +1 -0
  194. package/fontes/transacao.d.ts +81 -0
  195. package/fontes/transacao.d.ts.map +1 -0
  196. package/fontes/transacao.js +243 -0
  197. package/fontes/transacao.js.map +1 -0
  198. package/fontes/validacoes/validador.d.ts +19 -0
  199. package/fontes/validacoes/validador.d.ts.map +1 -0
  200. package/fontes/validacoes/validador.js +157 -0
  201. package/fontes/validacoes/validador.js.map +1 -0
  202. package/index.d.ts +36 -0
  203. package/index.d.ts.map +1 -0
  204. package/index.js +59 -0
  205. package/index.js.map +1 -0
  206. package/package.json +55 -0
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ <br>
2
+
3
+ <p align="center">
4
+ <h3 align="center">Delégua Entidades</h3>
5
+ <p align="center">
6
+ Biblioteca Node.js com o intuito de trabalhar com entidades de dados, com base nas instruções da <a href="https://github.com/DesignLiquido/delegua" target="_blank">Linguagem Delégua</a>, junto com o framework <a href="https://github.com/DesignLiquido/liquido" target="_blank">Liquido</a>.
7
+ </p>
8
+ </p>
9
+
10
+ <p align="center">
11
+ <a href="https://github.com/DesignLiquido/delegua-entidades/issues" target="_blank">
12
+ <img src="https://img.shields.io/github/issues/DesignLiquido/delegua-entidades" />
13
+ </a>
14
+ <img src="https://img.shields.io/github/stars/DesignLiquido/delegua-entidades" />
15
+ <img src="https://img.shields.io/github/forks/DesignLiquido/delegua-entidades" />
16
+ <a href="https://www.npmjs.com/package/DesignLiquido/delegua-entidades" target="_blank">
17
+ <img src="https://img.shields.io/npm/v/DesignLiquido/delegua-entidades" />
18
+ </a>
19
+ <img src="https://img.shields.io/npm/dw/DesignLiquido/delegua-entidades" />
20
+ <img src="https://img.shields.io/github/license/DesignLiquido/delegua-entidades" />
21
+ </p>
22
+
23
+ ## Motivação
24
+
25
+ [LinConEs](https://github.com/DesignLiquido/LinConEs) nasceu da necessidade de uma linguagem de consulta em português. No entanto, escrever código de produção diretamente com uma linguagem de consulta pode ocasionar uma série de problemas, como por exemplo ataques por _SQL Injection_, uma técnica bem conhecida em que uma consulta pode ser forjada para executar operações não desejadas pelos desenvolvedores originais.
26
+
27
+ Entidades é uma biblioteca para Delégua que trabalha com estruturas de manipulação de dados em alto nível, e que torna possível uma série de funcionalidades, como por exemplo:
28
+
29
+ - Gerar código em linguagem de consulta de maneira segura, a partir de classes e objetos em Delégua, observando parâmetros e impedindo a injeção de outras consultas indesejadas nas operações da sua aplicação;
30
+ - Trabalhar de maneira agnóstica, não importando qual tecnologia de dados esteja sendo usada, seja a própria memória do servidor, um banco de dados, etc.
31
+
32
+ Essa biblioteca usa uma outra biblioteca chamada `lincones-js`, onde uma lógica menos especializada é implementada para ler sentenças escritas em LinConEs e traduzi-las para SQL. Como um passo intermediário dessa tradução, há a geração de objetos de alto nível que representam comandos a serem executados em SQL. Esta biblioteca, portanto, utiliza código Delégua para também gerar esses objetos de operações de bancos de dados de alto nível, que então são utilizados por bibliotecas especializadas em uma ou outra tecnologia para a geração do SQL final.
33
+
34
+ ### Inspiração
35
+
36
+ - [Active Record (Ruby on Rails)](https://guides.rubyonrails.org/active_record_basics.html);
37
+ - [SQLAlchemy (Python)](https://www.sqlalchemy.org/)
38
+ - [Entity Framework (.NET)](https://learn.microsoft.com/en-us/aspnet/entity-framework)
39
+
40
+ ## Utilização
41
+
42
+ Esta biblioteca funciona por si só apenas com [Delégua](https://github.com/DesignLiquido/delegua), mas seu maior proveito ocorre com a utilização de outras bibliotecas, usadas para escrever diversas aplicações, como sistemas Web usando [Líquido](https://github.com/DesignLiquido/liquido). Ao usar apenas por si só, toda a geração de sentenças pode ser feita ou em LinConEs, ou em SQL ANSI. Ao ser combinadas com outras bibliotecas, pode ser usada na geração de SQL mais direcionado, como por exemplo para SQLite ou PostgreSQL.
43
+
44
+ O ponto de entrada dessa biblioteca é uma classe chamada `ContextoEntidades`. Esta classe contém uma série do que chamamos de coleções de dados. Cada coleção é mapeada ou como uma _collection_ de documentos ou registros (por exemplo, se usamos NoSQL), ou como uma tabela de banco de dados (quando o banco de dados é relacional). É cada uma dessas classes de coleções que geram as sentenças que as bibliotecas tecnológicas utilizam.
45
+
46
+ Para melhor ilustrar este funcionamento, vamos supor uma tabela de banco de dados de artigos. Cada artigo possui um identificador (abreviado como `id`), um título (texto) e um conteúdo (também texto). Em Delégua, cada linha desta tabela pode ser mapeada como a seguinte classe:
47
+
48
+ ```js
49
+ classe Artigo {
50
+ id: numero
51
+ titulo: texto
52
+ conteudo: texto
53
+ }
54
+ ```
55
+
56
+ Se queremos selecionar todos os artigos dessa classe e imprimi-los usando apenass Delégua, podemos fazê-lo da seguinte forma:
57
+
58
+ ```js
59
+ const entidades = importar('entidades')
60
+ const contexto = entidades.Contexto()
61
+ const artigos = contexto.colecao(Artigo).todos()
62
+ escreva(artigos) // Imprime a lista de todos os artigos encontrados.
63
+ ```
64
+
65
+ ### Colunas computadas
66
+
67
+ Colunas computadas podem ser declaradas com o decorador `@computada`, informando a expressao SQL e se a coluna e persistida:
68
+
69
+ ```js
70
+ classe Pedido {
71
+ id: numero
72
+ quantidade: numero
73
+ preco: numero
74
+
75
+ @computada({ expressao: "quantidade * preco", persistida: verdadeiro })
76
+ total: numero
77
+ }
78
+ ```
79
+
80
+ Colunas computadas nao sao incluídas em inserts/updates, mas podem ser lidas normalmente nas consultas.
81
+
82
+ ### Comandos
83
+
84
+ Entidades conta com alguns comandos de seleção e manipulação de dados:
85
+
86
+ - `contexto.colecao(Modelo).todos()`
87
+ - `contexto.colecao(Modelo).obterPorId()`
88
+ - `contexto.colecao(Modelo).obterPorCondicao()`
89
+
90
+ Há também comandos de geração de SQL, se for interessante obter a consulta gerada antes da execução:
91
+
92
+ - `contexto.modelo(Modelo).gerarSQLSelecionar()`
93
+
94
+ ## Execução por linha de comando
95
+
96
+ Este pacote não funciona sozinho em modo por linha de comando. É necessário também instalar um dos pacotes específicos de tecnologia de LinConEs.
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Sistema de Dicas de Otimização
3
+ *
4
+ * Fornece recomendações para otimizar consultas baseado em padrões detectados.
5
+ */
6
+ export declare enum TipoDica {
7
+ INDICE_AUSENTE = "INDICE_AUSENTE",
8
+ N_MAIS_1 = "N_MAIS_1",
9
+ SELECAO_DESNECESSARIA = "SELECAO_DESNECESSARIA",
10
+ JUNCAO_REDUNDANTE = "JUNCAO_REDUNDANTE",
11
+ PAGINACAO_FALTANTE = "PAGINACAO_FALTANTE",
12
+ ORDEM_INEFICIENTE = "ORDEM_INEFICIENTE",
13
+ USAR_CARGA_ANTECIPADA = "USAR_CARGA_ANTECIPADA",
14
+ USAR_CARGA_EM_LOTE = "USAR_CARGA_EM_LOTE",
15
+ CACHE_RECOMENDADA = "CACHE_RECOMENDADA"
16
+ }
17
+ export declare enum NivelSeveridade {
18
+ CRITICO = "CRITICO",
19
+ ALTO = "ALTO",
20
+ MEDIO = "MEDIO",
21
+ BAIXO = "BAIXO",
22
+ INFO = "INFO"
23
+ }
24
+ export interface Dica {
25
+ tipo: TipoDica;
26
+ severidade: NivelSeveridade;
27
+ mensagem: string;
28
+ sugestao: string;
29
+ metrica?: {
30
+ nome: string;
31
+ valor: number;
32
+ unidade: string;
33
+ };
34
+ }
35
+ export declare class AnalisadorDicas {
36
+ /**
37
+ * Analisa consulta e retorna dicas de otimização
38
+ */
39
+ static analisar(sql: string, metricasExecucao?: {
40
+ tempoMs: number;
41
+ linhas: number;
42
+ contagemJuncoes: number;
43
+ }): Dica[];
44
+ /**
45
+ * Formata dicas para exibição
46
+ */
47
+ static formatarDicas(dicas: Dica[]): string;
48
+ }
49
+ //# sourceMappingURL=analisador-dicas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"analisador-dicas.d.ts","sourceRoot":"","sources":["../../fontes/analisador-dicas.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,oBAAY,QAAQ;IAChB,cAAc,mBAAmB;IACjC,QAAQ,aAAa;IACrB,qBAAqB,0BAA0B;IAC/C,iBAAiB,sBAAsB;IACvC,kBAAkB,uBAAuB;IACzC,iBAAiB,sBAAsB;IACvC,qBAAqB,0BAA0B;IAC/C,kBAAkB,uBAAuB;IACzC,iBAAiB,sBAAsB;CAC1C;AAED,oBAAY,eAAe;IACvB,OAAO,YAAY;IACnB,IAAI,SAAS;IACb,KAAK,UAAU;IACf,KAAK,UAAU;IACf,IAAI,SAAS;CAChB;AAED,MAAM,WAAW,IAAI;IACjB,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,EAAE,eAAe,CAAC;IAC5B,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE;QACN,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,CAAC;KACnB,CAAC;CACL;AAED,qBAAa,eAAe;IACxB;;OAEG;IACH,MAAM,CAAC,QAAQ,CACX,GAAG,EAAE,MAAM,EACX,gBAAgB,CAAC,EAAE;QACf,OAAO,EAAE,MAAM,CAAC;QAChB,MAAM,EAAE,MAAM,CAAC;QACf,eAAe,EAAE,MAAM,CAAC;KAC3B,GACF,IAAI,EAAE;IAoET;;OAEG;IACH,MAAM,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,MAAM;CAwD9C"}
@@ -0,0 +1,145 @@
1
+ "use strict";
2
+ /**
3
+ * Sistema de Dicas de Otimização
4
+ *
5
+ * Fornece recomendações para otimizar consultas baseado em padrões detectados.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.AnalisadorDicas = exports.NivelSeveridade = exports.TipoDica = void 0;
9
+ var TipoDica;
10
+ (function (TipoDica) {
11
+ TipoDica["INDICE_AUSENTE"] = "INDICE_AUSENTE";
12
+ TipoDica["N_MAIS_1"] = "N_MAIS_1";
13
+ TipoDica["SELECAO_DESNECESSARIA"] = "SELECAO_DESNECESSARIA";
14
+ TipoDica["JUNCAO_REDUNDANTE"] = "JUNCAO_REDUNDANTE";
15
+ TipoDica["PAGINACAO_FALTANTE"] = "PAGINACAO_FALTANTE";
16
+ TipoDica["ORDEM_INEFICIENTE"] = "ORDEM_INEFICIENTE";
17
+ TipoDica["USAR_CARGA_ANTECIPADA"] = "USAR_CARGA_ANTECIPADA";
18
+ TipoDica["USAR_CARGA_EM_LOTE"] = "USAR_CARGA_EM_LOTE";
19
+ TipoDica["CACHE_RECOMENDADA"] = "CACHE_RECOMENDADA";
20
+ })(TipoDica || (exports.TipoDica = TipoDica = {}));
21
+ var NivelSeveridade;
22
+ (function (NivelSeveridade) {
23
+ NivelSeveridade["CRITICO"] = "CRITICO";
24
+ NivelSeveridade["ALTO"] = "ALTO";
25
+ NivelSeveridade["MEDIO"] = "MEDIO";
26
+ NivelSeveridade["BAIXO"] = "BAIXO";
27
+ NivelSeveridade["INFO"] = "INFO";
28
+ })(NivelSeveridade || (exports.NivelSeveridade = NivelSeveridade = {}));
29
+ class AnalisadorDicas {
30
+ /**
31
+ * Analisa consulta e retorna dicas de otimização
32
+ */
33
+ static analisar(sql, metricasExecucao) {
34
+ const dicas = [];
35
+ // Detecta SELECT *
36
+ if (sql.toLowerCase().includes("select *")) {
37
+ dicas.push({
38
+ tipo: TipoDica.SELECAO_DESNECESSARIA,
39
+ severidade: NivelSeveridade.MEDIO,
40
+ mensagem: "Consulta usa SELECT * (todas as colunas)",
41
+ sugestao: "Especifique apenas as colunas necessárias para melhorar performance"
42
+ });
43
+ }
44
+ // Detecta múltiplos JOINs
45
+ const juncoes = (sql.match(/join/gi) || []).length;
46
+ if (juncoes > 2) {
47
+ dicas.push({
48
+ tipo: TipoDica.JUNCAO_REDUNDANTE,
49
+ severidade: NivelSeveridade.ALTO,
50
+ mensagem: `Consulta tem ${juncoes} junções (acima do recomendado)`,
51
+ sugestao: "Considere usar eager loading ou reorganizar a estrutura de dados",
52
+ metrica: {
53
+ nome: "Junções",
54
+ valor: juncoes,
55
+ unidade: "quantidade"
56
+ }
57
+ });
58
+ }
59
+ // Detecta falta de LIMIT
60
+ if (!sql.toLowerCase().includes("limit") && !sql.toLowerCase().includes("top")) {
61
+ dicas.push({
62
+ tipo: TipoDica.PAGINACAO_FALTANTE,
63
+ severidade: NivelSeveridade.ALTO,
64
+ mensagem: "Consulta sem LIMITE pode retornar muitas linhas",
65
+ sugestao: "Adicione paginação com LIMITE e DESLOCAMENTO para dados em larga escala"
66
+ });
67
+ }
68
+ // Análise de desempenho se métricas disponíveis
69
+ if (metricasExecucao) {
70
+ if (metricasExecucao.tempoMs > 1000 && metricasExecucao.linhas > 10000) {
71
+ dicas.push({
72
+ tipo: TipoDica.CACHE_RECOMENDADA,
73
+ severidade: NivelSeveridade.ALTO,
74
+ mensagem: "Consulta lenta com muitos resultados",
75
+ sugestao: "Use cache para resultados desta consulta ou otimize índices",
76
+ metrica: {
77
+ nome: "Tempo",
78
+ valor: metricasExecucao.tempoMs,
79
+ unidade: "ms"
80
+ }
81
+ });
82
+ }
83
+ if (metricasExecucao.tempoMs > 500 && juncoes > 0) {
84
+ dicas.push({
85
+ tipo: TipoDica.USAR_CARGA_EM_LOTE,
86
+ severidade: NivelSeveridade.MEDIO,
87
+ mensagem: "JOINs resultando em consulta lenta",
88
+ sugestao: "Considere usar carga em lote em vez de junções diretas"
89
+ });
90
+ }
91
+ }
92
+ return dicas;
93
+ }
94
+ /**
95
+ * Formata dicas para exibição
96
+ */
97
+ static formatarDicas(dicas) {
98
+ if (dicas.length === 0) {
99
+ return "✅ Nenhuma dica de otimização necessária";
100
+ }
101
+ const linhas = [
102
+ `📋 Dicas de Otimização (${dicas.length} encontrada${dicas.length > 1 ? 's' : ''}):`,
103
+ ""
104
+ ];
105
+ // Agrupa por severidade
106
+ const porSeveridade = new Map();
107
+ for (const dica of dicas) {
108
+ if (!porSeveridade.has(dica.severidade)) {
109
+ porSeveridade.set(dica.severidade, []);
110
+ }
111
+ porSeveridade.get(dica.severidade).push(dica);
112
+ }
113
+ // Ordem de severidade
114
+ const ordem = [
115
+ NivelSeveridade.CRITICO,
116
+ NivelSeveridade.ALTO,
117
+ NivelSeveridade.MEDIO,
118
+ NivelSeveridade.BAIXO,
119
+ NivelSeveridade.INFO
120
+ ];
121
+ for (const severidade of ordem) {
122
+ const dicasPorSev = porSeveridade.get(severidade);
123
+ if (!dicasPorSev)
124
+ continue;
125
+ const icone = {
126
+ [NivelSeveridade.CRITICO]: "🔴",
127
+ [NivelSeveridade.ALTO]: "🟠",
128
+ [NivelSeveridade.MEDIO]: "🟡",
129
+ [NivelSeveridade.BAIXO]: "🔵",
130
+ [NivelSeveridade.INFO]: "ℹ️"
131
+ };
132
+ linhas.push(`\n${icone[severidade]} ${severidade}:`);
133
+ for (const dica of dicasPorSev) {
134
+ linhas.push(` • ${dica.mensagem}`);
135
+ linhas.push(` → ${dica.sugestao}`);
136
+ if (dica.metrica) {
137
+ linhas.push(` | ${dica.metrica.nome}: ${dica.metrica.valor} ${dica.metrica.unidade}`);
138
+ }
139
+ }
140
+ }
141
+ return linhas.join("\n");
142
+ }
143
+ }
144
+ exports.AnalisadorDicas = AnalisadorDicas;
145
+ //# sourceMappingURL=analisador-dicas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"analisador-dicas.js","sourceRoot":"","sources":["../../fontes/analisador-dicas.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;;AAEH,IAAY,QAUX;AAVD,WAAY,QAAQ;IAChB,6CAAiC,CAAA;IACjC,iCAAqB,CAAA;IACrB,2DAA+C,CAAA;IAC/C,mDAAuC,CAAA;IACvC,qDAAyC,CAAA;IACzC,mDAAuC,CAAA;IACvC,2DAA+C,CAAA;IAC/C,qDAAyC,CAAA;IACzC,mDAAuC,CAAA;AAC3C,CAAC,EAVW,QAAQ,wBAAR,QAAQ,QAUnB;AAED,IAAY,eAMX;AAND,WAAY,eAAe;IACvB,sCAAmB,CAAA;IACnB,gCAAa,CAAA;IACb,kCAAe,CAAA;IACf,kCAAe,CAAA;IACf,gCAAa,CAAA;AACjB,CAAC,EANW,eAAe,+BAAf,eAAe,QAM1B;AAcD,MAAa,eAAe;IACxB;;OAEG;IACH,MAAM,CAAC,QAAQ,CACX,GAAW,EACX,gBAIC;QAED,MAAM,KAAK,GAAW,EAAE,CAAC;QAEzB,mBAAmB;QACnB,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;YACzC,KAA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
@@ -0,0 +1,39 @@
1
+ export declare class CacheConsultas<T = any> {
2
+ private cache;
3
+ private limite;
4
+ private estatisticas;
5
+ constructor(limite?: number);
6
+ /**
7
+ * Gera chave única para a consulta
8
+ */
9
+ private gerarChave;
10
+ /**
11
+ * Obtém consulta do cache se existir
12
+ */
13
+ obter(sql: string, parametros?: any[]): T | null;
14
+ /**
15
+ * Armazena consulta no cache
16
+ */
17
+ armazenar(sql: string, consulta: T, parametros?: any[]): void;
18
+ /**
19
+ * Remove entrada menos usada/mais antiga
20
+ */
21
+ private limparEntrada;
22
+ /**
23
+ * Limpa todo o cache
24
+ */
25
+ limparTudo(): void;
26
+ /**
27
+ * Retorna estatísticas de cache
28
+ */
29
+ obterEstatisticas(): {
30
+ tamanho: number;
31
+ limite: number;
32
+ acertos: number;
33
+ extravios: number;
34
+ limpezas: number;
35
+ taxaAcerto: string;
36
+ utilizacao: string;
37
+ };
38
+ }
39
+ //# sourceMappingURL=cache-consultas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-consultas.d.ts","sourceRoot":"","sources":["../../fontes/cache-consultas.ts"],"names":[],"mappings":"AAYA,qBAAa,cAAc,CAAC,CAAC,GAAG,GAAG;IAC/B,OAAO,CAAC,KAAK,CAA2C;IACxD,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,YAAY,CAIlB;gBAEU,MAAM,GAAE,MAAY;IAIhC;;OAEG;IACH,OAAO,CAAC,UAAU;IAKlB;;OAEG;IACH,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,GAAG,EAAE,GAAG,CAAC,GAAG,IAAI;IAehD;;OAEG;IACH,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,CAAC,EAAE,UAAU,CAAC,EAAE,GAAG,EAAE,GAAG,IAAI;IAe7D;;OAEG;IACH,OAAO,CAAC,aAAa;IAsBrB;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB;;OAEG;IACH,iBAAiB;;;;;;;;;CAgBpB"}
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CacheConsultas = void 0;
4
+ class CacheConsultas {
5
+ constructor(limite = 100) {
6
+ this.cache = new Map();
7
+ this.estatisticas = {
8
+ acertos: 0,
9
+ extravios: 0,
10
+ limpezas: 0
11
+ };
12
+ this.limite = limite;
13
+ }
14
+ /**
15
+ * Gera chave única para a consulta
16
+ */
17
+ gerarChave(sql, parametros = []) {
18
+ const parametrosStr = JSON.stringify(parametros);
19
+ return `${sql}:${parametrosStr}`;
20
+ }
21
+ /**
22
+ * Obtém consulta do cache se existir
23
+ */
24
+ obter(sql, parametros) {
25
+ const chave = this.gerarChave(sql, parametros);
26
+ const entrada = this.cache.get(chave);
27
+ if (entrada) {
28
+ entrada.acessos++;
29
+ entrada.ultimoAcesso = new Date();
30
+ this.estatisticas.acertos++;
31
+ return entrada.consulta;
32
+ }
33
+ this.estatisticas.extravios++;
34
+ return null;
35
+ }
36
+ /**
37
+ * Armazena consulta no cache
38
+ */
39
+ armazenar(sql, consulta, parametros) {
40
+ const chave = this.gerarChave(sql, parametros);
41
+ // Limpar se cache cheio
42
+ if (this.cache.size >= this.limite) {
43
+ this.limparEntrada();
44
+ }
45
+ this.cache.set(chave, {
46
+ consulta,
47
+ acessos: 0,
48
+ ultimoAcesso: new Date()
49
+ });
50
+ }
51
+ /**
52
+ * Remove entrada menos usada/mais antiga
53
+ */
54
+ limparEntrada() {
55
+ let chaveParaRemover = null;
56
+ let menorPontuacao = Infinity;
57
+ for (const [chave, entrada] of this.cache.entries()) {
58
+ // Pontuação = acessos + dias desde último acesso (penalidade)
59
+ const diasDesdeAcesso = (new Date().getTime() - entrada.ultimoAcesso.getTime()) / (1000 * 60 * 60 * 24);
60
+ const pontuacao = entrada.acessos - (diasDesdeAcesso * 0.1);
61
+ if (pontuacao < menorPontuacao) {
62
+ menorPontuacao = pontuacao;
63
+ chaveParaRemover = chave;
64
+ }
65
+ }
66
+ if (chaveParaRemover) {
67
+ this.cache.delete(chaveParaRemover);
68
+ this.estatisticas.limpezas++;
69
+ }
70
+ }
71
+ /**
72
+ * Limpa todo o cache
73
+ */
74
+ limparTudo() {
75
+ this.cache.clear();
76
+ this.estatisticas = { acertos: 0, extravios: 0, limpezas: 0 };
77
+ }
78
+ /**
79
+ * Retorna estatísticas de cache
80
+ */
81
+ obterEstatisticas() {
82
+ const total = this.estatisticas.acertos + this.estatisticas.extravios;
83
+ const taxaAcerto = total > 0
84
+ ? ((this.estatisticas.acertos / total) * 100).toFixed(2)
85
+ : "0.00";
86
+ return {
87
+ tamanho: this.cache.size,
88
+ limite: this.limite,
89
+ acertos: this.estatisticas.acertos,
90
+ extravios: this.estatisticas.extravios,
91
+ limpezas: this.estatisticas.limpezas,
92
+ taxaAcerto: `${taxaAcerto}%`,
93
+ utilizacao: `${((this.cache.size / this.limite) * 100).toFixed(2)}%`
94
+ };
95
+ }
96
+ }
97
+ exports.CacheConsultas = CacheConsultas;
98
+ //# sourceMappingURL=cache-consultas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-consultas.js","sourceRoot":"","sources":["../../fontes/cache-consultas.ts"],"names":[],"mappings":";;;AAYA,MAAa,cAAc;IASvB,YAAY,SAAiB,GAAG;QARxB,UAAK,GAAiC,IAAI,GAAG,EAAE,CAAC;QAEhD,iBAAY,GAAG;YACnB,OAAO,EAAE,CAAC;YACV,SAAS,EAAE,CAAC;YACZ,QAAQ,EAAE,CAAC;SACd,CAAC;QAGE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;IAED;;OAEG;IACK,UAAU,CAAC,GAAW,EAAE,aAAoB,EAAE;QAClD,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC;QACjD,OAAO,GAAG,GAAG,IAAI,aAAa,EAAE,CAAC;IACrC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,GAAW,EA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AAE,GAAG,UAAU,GAAG;YAC5B,UAAU,EAAE,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG;SACvE,CAAC;IACN,CAAC;CACJ;AA7GD,wCA6GC"}
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Carregador em Lote (DataLoader)
3
+ *
4
+ * Agrupa múltiplas requisições de relacionamentos em poucas queries,
5
+ * evitando o problema N+1 através de batching inteligente.
6
+ */
7
+ interface ConfiguracaoLote {
8
+ tamanhoLote: number;
9
+ intervaloMs: number;
10
+ cache: boolean;
11
+ }
12
+ export declare class CarregadorLote<T> {
13
+ private funcaoCarregamento;
14
+ private fila;
15
+ private tempoMaximo;
16
+ private cache;
17
+ private configuracao;
18
+ constructor(funcaoCarregamento: (chaves: any[]) => Promise<Map<any, T>>, config?: Partial<ConfiguracaoLote>);
19
+ /**
20
+ * Solicita carregamento de um item
21
+ */
22
+ carregar(chave: any): Promise<T>;
23
+ /**
24
+ * Processa lote de carregamentos
25
+ */
26
+ private processar;
27
+ /**
28
+ * Limpa cache
29
+ */
30
+ limparCache(): void;
31
+ /**
32
+ * Retorna estatísticas
33
+ */
34
+ obterEstatisticas(): {
35
+ filaAtual: number;
36
+ tamanhoCache: number;
37
+ tamanhoLote: number;
38
+ intervaloMs: number;
39
+ };
40
+ }
41
+ /**
42
+ * Gerenciador de Carregadores em Lote para Relacionamentos
43
+ */
44
+ export declare class GerenciadorCarregadoresLote {
45
+ private carregadores;
46
+ /**
47
+ * Cria carregador para um relacionamento específico
48
+ */
49
+ criarCarregador<T>(chave: string, funcaoCarregamento: (ids: any[]) => Promise<Map<any, T>>, config?: Partial<ConfiguracaoLote>): CarregadorLote<T>;
50
+ /**
51
+ * Obtém carregador existente
52
+ */
53
+ obterCarregador<T>(chave: string): CarregadorLote<T> | null;
54
+ /**
55
+ * Limpa todos os carregadores
56
+ */
57
+ limparTodos(): void;
58
+ /**
59
+ * Obtém status de todos os carregadores
60
+ */
61
+ obterStatus(): Record<string, any>;
62
+ }
63
+ export {};
64
+ //# sourceMappingURL=carregador-lotes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"carregador-lotes.d.ts","sourceRoot":"","sources":["../../fontes/carregador-lotes.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,UAAU,gBAAgB;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,OAAO,CAAC;CAClB;AAQD,qBAAa,cAAc,CAAC,CAAC;IAOrB,OAAO,CAAC,kBAAkB;IAN9B,OAAO,CAAC,IAAI,CAA2B;IACvC,OAAO,CAAC,WAAW,CAA+B;IAClD,OAAO,CAAC,KAAK,CAA6B;IAC1C,OAAO,CAAC,YAAY,CAAmB;gBAG3B,kBAAkB,EAAE,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EACnE,MAAM,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC;IAStC;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAAC,CAAC,CAAC;IAwBhC;;OAEG;YACW,SAAS;IA2CvB;;OAEG;IACH,WAAW,IAAI,IAAI;IAInB;;OAEG;IACH,iBAAiB;;;;;;CAQpB;AAED;;GAEG;AACH,qBAAa,2BAA2B;IACpC,OAAO,CAAC,YAAY,CAA+C;IAEnE;;OAEG;IACH,eAAe,CAAC,CAAC,EACb,KAAK,EAAE,MAAM,EACb,kBAAkB,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EACxD,MAAM,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,GACnC,cAAc,CAAC,CAAC,CAAC;IAMpB;;OAEG;IACH,eAAe,CAAC,CAAC,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc,CAAC,CAAC,CAAC,GAAG,IAAI;IAI3D;;OAEG;IACH,WAAW,IAAI,IAAI;IAOnB;;OAEG;IACH,WAAW;CASd"}
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ /**
3
+ * Carregador em Lote (DataLoader)
4
+ *
5
+ * Agrupa múltiplas requisições de relacionamentos em poucas queries,
6
+ * evitando o problema N+1 através de batching inteligente.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.GerenciadorCarregadoresLote = exports.CarregadorLote = void 0;
10
+ class CarregadorLote {
11
+ constructor(funcaoCarregamento, config) {
12
+ this.funcaoCarregamento = funcaoCarregamento;
13
+ this.fila = [];
14
+ this.tempoMaximo = null;
15
+ this.cache = new Map();
16
+ this.configuracao = {
17
+ tamanhoLote: config?.tamanhoLote ?? 100,
18
+ intervaloMs: config?.intervaloMs ?? 16,
19
+ cache: config?.cache ?? true
20
+ };
21
+ }
22
+ /**
23
+ * Solicita carregamento de um item
24
+ */
25
+ carregar(chave) {
26
+ return new Promise((resolver, rejeitar) => {
27
+ // Verifica cache primeiro
28
+ if (this.configuracao.cache) {
29
+ const chaveStr = String(chave);
30
+ const emCache = this.cache.get(chaveStr);
31
+ if (emCache) {
32
+ return resolver(emCache);
33
+ }
34
+ }
35
+ // Adiciona à fila
36
+ this.fila.push({ chave, resolver, rejeitar });
37
+ // Processa se atingiu tamanho do lote
38
+ if (this.fila.length >= this.configuracao.tamanhoLote) {
39
+ this.processar();
40
+ }
41
+ else if (!this.tempoMaximo) {
42
+ // Agenda processamento após delay
43
+ this.tempoMaximo = setTimeout(() => this.processar(), this.configuracao.intervaloMs);
44
+ }
45
+ });
46
+ }
47
+ /**
48
+ * Processa lote de carregamentos
49
+ */
50
+ async processar() {
51
+ const loteAtual = this.fila.splice(0, this.configuracao.tamanhoLote);
52
+ if (this.tempoMaximo) {
53
+ clearTimeout(this.tempoMaximo);
54
+ this.tempoMaximo = null;
55
+ }
56
+ if (loteAtual.length === 0)
57
+ return;
58
+ try {
59
+ const chaves = loteAtual.map(r => r.chave);
60
+ const resultados = await this.funcaoCarregamento(chaves);
61
+ // Distribui resultados
62
+ for (const requisicao of loteAtual) {
63
+ const resultado = resultados.get(requisicao.chave);
64
+ if (resultado !== undefined) {
65
+ // Armazena em cache
66
+ if (this.configuracao.cache) {
67
+ this.cache.set(String(requisicao.chave), resultado);
68
+ }
69
+ requisicao.resolver(resultado);
70
+ }
71
+ else {
72
+ requisicao.rejeitar(new Error(`Nenhum resultado para chave: ${requisicao.chave}`));
73
+ }
74
+ }
75
+ }
76
+ catch (erro) {
77
+ // Rejeita todos em caso de erro
78
+ for (const requisicao of loteAtual) {
79
+ requisicao.rejeitar(erro);
80
+ }
81
+ }
82
+ // Processa próximo lote se houver
83
+ if (this.fila.length > 0) {
84
+ this.processar();
85
+ }
86
+ }
87
+ /**
88
+ * Limpa cache
89
+ */
90
+ limparCache() {
91
+ this.cache.clear();
92
+ }
93
+ /**
94
+ * Retorna estatísticas
95
+ */
96
+ obterEstatisticas() {
97
+ return {
98
+ filaAtual: this.fila.length,
99
+ tamanhoCache: this.cache.size,
100
+ tamanhoLote: this.configuracao.tamanhoLote,
101
+ intervaloMs: this.configuracao.intervaloMs
102
+ };
103
+ }
104
+ }
105
+ exports.CarregadorLote = CarregadorLote;
106
+ /**
107
+ * Gerenciador de Carregadores em Lote para Relacionamentos
108
+ */
109
+ class GerenciadorCarregadoresLote {
110
+ constructor() {
111
+ this.carregadores = new Map();
112
+ }
113
+ /**
114
+ * Cria carregador para um relacionamento específico
115
+ */
116
+ criarCarregador(chave, funcaoCarregamento, config) {
117
+ const carregador = new CarregadorLote(funcaoCarregamento, config);
118
+ this.carregadores.set(chave, carregador);
119
+ return carregador;
120
+ }
121
+ /**
122
+ * Obtém carregador existente
123
+ */
124
+ obterCarregador(chave) {
125
+ return this.carregadores.get(chave) || null;
126
+ }
127
+ /**
128
+ * Limpa todos os carregadores
129
+ */
130
+ limparTodos() {
131
+ for (const carregador of this.carregadores.values()) {
132
+ carregador.limparCache();
133
+ }
134
+ this.carregadores.clear();
135
+ }
136
+ /**
137
+ * Obtém status de todos os carregadores
138
+ */
139
+ obterStatus() {
140
+ const status = {};
141
+ for (const [chave, carregador] of this.carregadores.entries()) {
142
+ status[chave] = carregador.obterEstatisticas();
143
+ }
144
+ return status;
145
+ }
146
+ }
147
+ exports.GerenciadorCarregadoresLote = GerenciadorCarregadoresLote;
148
+ //# sourceMappingURL=carregador-lotes.js.map