@ug.software/opposer 3.0.10 → 3.0.11

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 (227) hide show
  1. package/lib/bin/commands/build.js +15 -28
  2. package/lib/bin/commands/init.js +27 -30
  3. package/lib/esm/server/security/jwt/index.d.ts +1 -1
  4. package/lib/esm/server/security/jwt/index.js +21 -29
  5. package/lib/esm/system/index.d.ts +1 -1
  6. package/lib/esm/system/index.js +42 -55
  7. package/package.json +3 -3
  8. package/lib/cjs/examples/full-app/index.d.ts +0 -1
  9. package/lib/cjs/examples/full-app/index.js +0 -102
  10. package/lib/cjs/examples/full-app/models/author.d.ts +0 -7
  11. package/lib/cjs/examples/full-app/models/author.js +0 -42
  12. package/lib/cjs/examples/full-app/models/book.d.ts +0 -9
  13. package/lib/cjs/examples/full-app/models/book.js +0 -51
  14. package/lib/cjs/examples/full-app/models/category.d.ts +0 -4
  15. package/lib/cjs/examples/full-app/models/category.js +0 -26
  16. package/lib/cjs/examples/full-app/schedules/catalog-refresh.d.ts +0 -28
  17. package/lib/cjs/examples/full-app/schedules/catalog-refresh.js +0 -149
  18. package/lib/cjs/examples/full-app/schedules/inventory-sync.d.ts +0 -12
  19. package/lib/cjs/examples/full-app/schedules/inventory-sync.js +0 -61
  20. package/lib/cjs/examples/full-app/schedules/library-alerts.d.ts +0 -11
  21. package/lib/cjs/examples/full-app/schedules/library-alerts.js +0 -55
  22. package/lib/cjs/examples/minimal/src/handlers/book.d.ts +0 -11
  23. package/lib/cjs/examples/minimal/src/handlers/book.js +0 -51
  24. package/lib/cjs/examples/minimal/src/index.d.ts +0 -1
  25. package/lib/cjs/examples/minimal/src/index.js +0 -14
  26. package/lib/cjs/examples/minimal/src/models/books.d.ts +0 -4
  27. package/lib/cjs/examples/minimal/src/models/books.js +0 -26
  28. package/lib/cjs/examples/orm/models/permission.d.ts +0 -6
  29. package/lib/cjs/examples/orm/models/permission.js +0 -38
  30. package/lib/cjs/examples/orm/models/profile.d.ts +0 -7
  31. package/lib/cjs/examples/orm/models/profile.js +0 -42
  32. package/lib/cjs/examples/orm/models/user.d.ts +0 -11
  33. package/lib/cjs/examples/orm/models/user.js +0 -70
  34. package/lib/cjs/examples/orm/mysql/index.d.ts +0 -1
  35. package/lib/cjs/examples/orm/mysql/index.js +0 -44
  36. package/lib/cjs/examples/orm/postgres/index.d.ts +0 -1
  37. package/lib/cjs/examples/orm/postgres/index.js +0 -40
  38. package/lib/cjs/examples/orm/sqlite/index.d.ts +0 -1
  39. package/lib/cjs/examples/orm/sqlite/index.js +0 -41
  40. package/lib/cjs/index.d.ts +0 -2
  41. package/lib/cjs/index.js +0 -23
  42. package/lib/cjs/interfaces/controller.d.ts +0 -109
  43. package/lib/cjs/interfaces/controller.js +0 -2
  44. package/lib/cjs/interfaces/field.d.ts +0 -23
  45. package/lib/cjs/interfaces/field.js +0 -2
  46. package/lib/cjs/interfaces/handler.d.ts +0 -18
  47. package/lib/cjs/interfaces/handler.js +0 -2
  48. package/lib/cjs/interfaces/jwt.d.ts +0 -21
  49. package/lib/cjs/interfaces/jwt.js +0 -2
  50. package/lib/cjs/interfaces/model.d.ts +0 -9
  51. package/lib/cjs/interfaces/model.js +0 -2
  52. package/lib/cjs/interfaces/request.d.ts +0 -13
  53. package/lib/cjs/interfaces/request.js +0 -2
  54. package/lib/cjs/interfaces/schema.d.ts +0 -9
  55. package/lib/cjs/interfaces/schema.js +0 -2
  56. package/lib/cjs/interfaces/security.d.ts +0 -32
  57. package/lib/cjs/interfaces/security.js +0 -2
  58. package/lib/cjs/interfaces/server.d.ts +0 -37
  59. package/lib/cjs/interfaces/server.js +0 -2
  60. package/lib/cjs/interfaces/system.d.ts +0 -41
  61. package/lib/cjs/interfaces/system.js +0 -2
  62. package/lib/cjs/orm/decorators/index.d.ts +0 -33
  63. package/lib/cjs/orm/decorators/index.js +0 -131
  64. package/lib/cjs/orm/driver/mysql.d.ts +0 -14
  65. package/lib/cjs/orm/driver/mysql.js +0 -103
  66. package/lib/cjs/orm/driver/postgres.d.ts +0 -14
  67. package/lib/cjs/orm/driver/postgres.js +0 -105
  68. package/lib/cjs/orm/driver/sqlite.d.ts +0 -14
  69. package/lib/cjs/orm/driver/sqlite.js +0 -143
  70. package/lib/cjs/orm/index.d.ts +0 -8
  71. package/lib/cjs/orm/index.js +0 -28
  72. package/lib/cjs/orm/metadata.d.ts +0 -47
  73. package/lib/cjs/orm/metadata.js +0 -47
  74. package/lib/cjs/orm/opposer.d.ts +0 -30
  75. package/lib/cjs/orm/opposer.js +0 -33
  76. package/lib/cjs/orm/query-builder.d.ts +0 -32
  77. package/lib/cjs/orm/query-builder.js +0 -278
  78. package/lib/cjs/orm/repository.d.ts +0 -54
  79. package/lib/cjs/orm/repository.js +0 -325
  80. package/lib/cjs/orm/validation.d.ts +0 -44
  81. package/lib/cjs/orm/validation.js +0 -128
  82. package/lib/cjs/package.json +0 -3
  83. package/lib/cjs/persistent/cache/core-context.d.ts +0 -17
  84. package/lib/cjs/persistent/cache/core-context.js +0 -39
  85. package/lib/cjs/persistent/cache/global.d.ts +0 -18
  86. package/lib/cjs/persistent/cache/global.js +0 -22
  87. package/lib/cjs/persistent/cache/index.d.ts +0 -3
  88. package/lib/cjs/persistent/cache/index.js +0 -12
  89. package/lib/cjs/persistent/cache/session.d.ts +0 -19
  90. package/lib/cjs/persistent/cache/session.js +0 -39
  91. package/lib/cjs/persistent/cache/storage.d.ts +0 -14
  92. package/lib/cjs/persistent/cache/storage.js +0 -88
  93. package/lib/cjs/persistent/cache/store.d.ts +0 -16
  94. package/lib/cjs/persistent/cache/store.js +0 -112
  95. package/lib/cjs/persistent/context/index.d.ts +0 -3
  96. package/lib/cjs/persistent/context/index.js +0 -5
  97. package/lib/cjs/persistent/decorators/global.d.ts +0 -1
  98. package/lib/cjs/persistent/decorators/global.js +0 -25
  99. package/lib/cjs/persistent/decorators/session.d.ts +0 -1
  100. package/lib/cjs/persistent/decorators/session.js +0 -27
  101. package/lib/cjs/persistent/index.d.ts +0 -6
  102. package/lib/cjs/persistent/index.js +0 -18
  103. package/lib/cjs/persistent/interfaces/context.d.ts +0 -5
  104. package/lib/cjs/persistent/interfaces/context.js +0 -2
  105. package/lib/cjs/persistent/interfaces/system.d.ts +0 -47
  106. package/lib/cjs/persistent/interfaces/system.js +0 -29
  107. package/lib/cjs/persistent/system/index.d.ts +0 -7
  108. package/lib/cjs/persistent/system/index.js +0 -44
  109. package/lib/cjs/persistent/utils/memory.d.ts +0 -8
  110. package/lib/cjs/persistent/utils/memory.js +0 -44
  111. package/lib/cjs/persistent/utils/timer.d.ts +0 -14
  112. package/lib/cjs/persistent/utils/timer.js +0 -21
  113. package/lib/cjs/playground/build/client/assets/AddRounded-ByHfnsiW.js +0 -4
  114. package/lib/cjs/playground/build/client/assets/Button-DLrxHRm7.js +0 -1
  115. package/lib/cjs/playground/build/client/assets/Container-CgITmmbk.js +0 -1
  116. package/lib/cjs/playground/build/client/assets/Divider-B_Wx9srO.js +0 -1
  117. package/lib/cjs/playground/build/client/assets/List-juBjUmfP.js +0 -1
  118. package/lib/cjs/playground/build/client/assets/ListItemText-DgWZmgzc.js +0 -1
  119. package/lib/cjs/playground/build/client/assets/MenuItem-D_5SuVKQ.js +0 -1
  120. package/lib/cjs/playground/build/client/assets/Modal-BwXR_5Bh.js +0 -1
  121. package/lib/cjs/playground/build/client/assets/TableRow-B9hAmlnV.js +0 -2
  122. package/lib/cjs/playground/build/client/assets/TextField-UybdTIGB.js +0 -3
  123. package/lib/cjs/playground/build/client/assets/Tooltip-BGcUWUcF.js +0 -1
  124. package/lib/cjs/playground/build/client/assets/auth-CD1rXHzz.js +0 -1
  125. package/lib/cjs/playground/build/client/assets/auth-GyTIVKy5.js +0 -1
  126. package/lib/cjs/playground/build/client/assets/confirm-Dr0pbiV6.js +0 -1
  127. package/lib/cjs/playground/build/client/assets/dividerClasses-CIiqeEPO.js +0 -1
  128. package/lib/cjs/playground/build/client/assets/entry.client-D6FYz1yh.js +0 -13
  129. package/lib/cjs/playground/build/client/assets/index-CJ0wdt6Z.js +0 -1
  130. package/lib/cjs/playground/build/client/assets/index-CQc11yq_.js +0 -1153
  131. package/lib/cjs/playground/build/client/assets/index-Cr4I-4J2.js +0 -1
  132. package/lib/cjs/playground/build/client/assets/index-CtPqstFl.js +0 -26
  133. package/lib/cjs/playground/build/client/assets/index-Ct_NE85o.js +0 -1
  134. package/lib/cjs/playground/build/client/assets/index-D0I6xwmb.js +0 -1
  135. package/lib/cjs/playground/build/client/assets/index-DmDCpKb3.js +0 -1
  136. package/lib/cjs/playground/build/client/assets/index-DsSkAwyn.js +0 -1
  137. package/lib/cjs/playground/build/client/assets/index-_DMgWZ3Y.js +0 -1
  138. package/lib/cjs/playground/build/client/assets/listItemIconClasses-39Itzgzt.js +0 -1
  139. package/lib/cjs/playground/build/client/assets/listItemTextClasses-EQFLPLzt.js +0 -1
  140. package/lib/cjs/playground/build/client/assets/manifest-c06e9a7f.js +0 -1
  141. package/lib/cjs/playground/build/client/assets/mergeSlotProps-DptgQgAT.js +0 -1
  142. package/lib/cjs/playground/build/client/assets/playground-Hl52p9f5.js +0 -108
  143. package/lib/cjs/playground/build/client/assets/root-CQTBmuv8.js +0 -1
  144. package/lib/cjs/playground/build/client/assets/toast-CsAH5FIf.js +0 -1
  145. package/lib/cjs/playground/build/client/assets/use-request-BZNkzlTr.js +0 -1
  146. package/lib/cjs/playground/build/client/favicon.ico +0 -0
  147. package/lib/cjs/playground/build/client/index.html +0 -6
  148. package/lib/cjs/playground/index.d.ts +0 -2
  149. package/lib/cjs/playground/index.js +0 -135
  150. package/lib/cjs/scheduler/controllers/index.d.ts +0 -19
  151. package/lib/cjs/scheduler/controllers/index.js +0 -62
  152. package/lib/cjs/scheduler/decorators/index.d.ts +0 -9
  153. package/lib/cjs/scheduler/decorators/index.js +0 -21
  154. package/lib/cjs/scheduler/handlers/index.d.ts +0 -19
  155. package/lib/cjs/scheduler/handlers/index.js +0 -62
  156. package/lib/cjs/scheduler/index.d.ts +0 -24
  157. package/lib/cjs/scheduler/index.js +0 -110
  158. package/lib/cjs/scheduler/models/history.d.ts +0 -10
  159. package/lib/cjs/scheduler/models/history.js +0 -50
  160. package/lib/cjs/server/constants/index.d.ts +0 -78
  161. package/lib/cjs/server/constants/index.js +0 -372
  162. package/lib/cjs/server/context/index.d.ts +0 -17
  163. package/lib/cjs/server/context/index.js +0 -33
  164. package/lib/cjs/server/controller/index.d.ts +0 -3
  165. package/lib/cjs/server/controller/index.js +0 -217
  166. package/lib/cjs/server/controllers/index.d.ts +0 -5
  167. package/lib/cjs/server/controllers/index.js +0 -72
  168. package/lib/cjs/server/core/index.d.ts +0 -16
  169. package/lib/cjs/server/core/index.js +0 -110
  170. package/lib/cjs/server/core/middleware/body-parser.d.ts +0 -2
  171. package/lib/cjs/server/core/middleware/body-parser.js +0 -27
  172. package/lib/cjs/server/core/middleware/cors.d.ts +0 -4
  173. package/lib/cjs/server/core/middleware/cors.js +0 -32
  174. package/lib/cjs/server/core/middleware/logger.d.ts +0 -2
  175. package/lib/cjs/server/core/middleware/logger.js +0 -15
  176. package/lib/cjs/server/decorators/controller.d.ts +0 -3
  177. package/lib/cjs/server/decorators/controller.js +0 -14
  178. package/lib/cjs/server/decorators/field.d.ts +0 -2
  179. package/lib/cjs/server/decorators/field.js +0 -36
  180. package/lib/cjs/server/decorators/handler.d.ts +0 -3
  181. package/lib/cjs/server/decorators/handler.js +0 -14
  182. package/lib/cjs/server/decorators/index.d.ts +0 -7
  183. package/lib/cjs/server/decorators/index.js +0 -26
  184. package/lib/cjs/server/decorators/is-public-method.d.ts +0 -3
  185. package/lib/cjs/server/decorators/is-public-method.js +0 -16
  186. package/lib/cjs/server/decorators/is-public.d.ts +0 -3
  187. package/lib/cjs/server/decorators/is-public.js +0 -14
  188. package/lib/cjs/server/decorators/method.d.ts +0 -3
  189. package/lib/cjs/server/decorators/method.js +0 -16
  190. package/lib/cjs/server/decorators/payload.d.ts +0 -3
  191. package/lib/cjs/server/decorators/payload.js +0 -15
  192. package/lib/cjs/server/handlers/index.d.ts +0 -5
  193. package/lib/cjs/server/handlers/index.js +0 -72
  194. package/lib/cjs/server/helpers/index.d.ts +0 -17
  195. package/lib/cjs/server/helpers/index.js +0 -39
  196. package/lib/cjs/server/index.d.ts +0 -12
  197. package/lib/cjs/server/index.js +0 -176
  198. package/lib/cjs/server/security/controller/auth.d.ts +0 -76
  199. package/lib/cjs/server/security/controller/auth.js +0 -346
  200. package/lib/cjs/server/security/index.d.ts +0 -2
  201. package/lib/cjs/server/security/index.js +0 -10
  202. package/lib/cjs/server/security/jwt/index.d.ts +0 -23
  203. package/lib/cjs/server/security/jwt/index.js +0 -108
  204. package/lib/cjs/server/security/middleware/autorization.d.ts +0 -3
  205. package/lib/cjs/server/security/middleware/autorization.js +0 -46
  206. package/lib/cjs/server/security/middleware/permission.d.ts +0 -3
  207. package/lib/cjs/server/security/middleware/permission.js +0 -138
  208. package/lib/cjs/server/security/models/crp.d.ts +0 -8
  209. package/lib/cjs/server/security/models/crp.js +0 -42
  210. package/lib/cjs/server/security/models/ke.d.ts +0 -7
  211. package/lib/cjs/server/security/models/ke.js +0 -38
  212. package/lib/cjs/server/security/models/rl.d.ts +0 -9
  213. package/lib/cjs/server/security/models/rl.js +0 -50
  214. package/lib/cjs/server/security/models/se.d.ts +0 -11
  215. package/lib/cjs/server/security/models/se.js +0 -54
  216. package/lib/cjs/server/security/models/usr.d.ts +0 -14
  217. package/lib/cjs/server/security/models/usr.js +0 -82
  218. package/lib/cjs/server/services/delete.d.ts +0 -13
  219. package/lib/cjs/server/services/delete.js +0 -49
  220. package/lib/cjs/server/services/get.d.ts +0 -4
  221. package/lib/cjs/server/services/get.js +0 -145
  222. package/lib/cjs/server/services/insert.d.ts +0 -13
  223. package/lib/cjs/server/services/insert.js +0 -74
  224. package/lib/cjs/server/services/update.d.ts +0 -13
  225. package/lib/cjs/server/services/update.js +0 -60
  226. package/lib/cjs/system/index.d.ts +0 -13
  227. package/lib/cjs/system/index.js +0 -237
@@ -1,47 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MetadataStore = exports.HOOK_METADATA_KEY = exports.FIELD_METADATA_KEY = exports.ENTITY_METADATA_KEY = void 0;
4
- require("reflect-metadata");
5
- exports.ENTITY_METADATA_KEY = Symbol("orm:entity");
6
- exports.FIELD_METADATA_KEY = Symbol("orm:field");
7
- exports.HOOK_METADATA_KEY = Symbol("orm:hook");
8
- class MetadataStore {
9
- static registerEntity(target, metadata) {
10
- this.entities.set(target, metadata);
11
- }
12
- static registerField(target, metadata) {
13
- const fields = this.fields.get(target) || [];
14
- fields.push(metadata);
15
- this.fields.set(target, fields);
16
- }
17
- static registerHook(target, hook) {
18
- const hooks = this.hooks.get(target) || [];
19
- hooks.push(hook);
20
- this.hooks.set(target, hooks);
21
- }
22
- static getEntity(target) {
23
- return this.entities.get(target);
24
- }
25
- static getFields(target) {
26
- return this.fields.get(target) || [];
27
- }
28
- static getPersistableFields(target) {
29
- const fields = this.getFields(target);
30
- return fields.filter((field) => {
31
- if (field.type === "relation") {
32
- return field.relation?.type !== "one-to-many" && field.relation?.type !== "many-to-many";
33
- }
34
- return true;
35
- });
36
- }
37
- static getHooks(target) {
38
- return this.hooks.get(target) || [];
39
- }
40
- static getAllEntities() {
41
- return Array.from(this.entities.values());
42
- }
43
- }
44
- exports.MetadataStore = MetadataStore;
45
- MetadataStore.entities = new Map();
46
- MetadataStore.fields = new Map();
47
- MetadataStore.hooks = new Map();
@@ -1,30 +0,0 @@
1
- import { EntityMetadata, FieldMetadata } from "./metadata.js";
2
- import { Repository } from "./repository.js";
3
- export type QueryResultRow = {
4
- [key: string]: unknown;
5
- };
6
- export interface DatabaseDriver {
7
- connect(): Promise<void>;
8
- disconnect(): Promise<void>;
9
- query<T = QueryResultRow>(sql: string, params?: any[]): Promise<T[]>;
10
- createTable(entity: EntityMetadata, fields: FieldMetadata[]): Promise<void>;
11
- quoteIdentifier(identifier: string): string;
12
- }
13
- export interface ConnectionOptions {
14
- type: "postgres" | "sqlite" | "mysql";
15
- host?: string;
16
- port?: number;
17
- username?: string;
18
- password?: string;
19
- database: string;
20
- logging?: boolean;
21
- }
22
- export declare class OpposerDatabase {
23
- private driver;
24
- private entities;
25
- constructor(driver: DatabaseDriver, entities: Function[]);
26
- connect(): Promise<void>;
27
- getDriver(): DatabaseDriver;
28
- getEntities(): Function[];
29
- getRepository<T>(target: new (...args: any[]) => T): Repository<T>;
30
- }
@@ -1,33 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpposerDatabase = void 0;
4
- const metadata_js_1 = require("./metadata.js");
5
- const repository_js_1 = require("./repository.js");
6
- class OpposerDatabase {
7
- constructor(driver, entities) {
8
- this.driver = driver;
9
- this.entities = entities;
10
- }
11
- async connect() {
12
- await this.driver.connect();
13
- // After connecting, ensure tables exist (simplified sync for now)
14
- for (const entity of this.entities) {
15
- const metadata = metadata_js_1.MetadataStore.getEntity(entity);
16
- const fields = metadata_js_1.MetadataStore.getPersistableFields(entity);
17
- if (metadata) {
18
- console.log(`-> Creating table "${metadata.tableName}" with fields: ${fields.map(f => f.name).join(", ")}`);
19
- await this.driver.createTable(metadata, fields);
20
- }
21
- }
22
- }
23
- getDriver() {
24
- return this.driver;
25
- }
26
- getEntities() {
27
- return this.entities;
28
- }
29
- getRepository(target) {
30
- return new repository_js_1.Repository(this, target);
31
- }
32
- }
33
- exports.OpposerDatabase = OpposerDatabase;
@@ -1,32 +0,0 @@
1
- import { QueryBuilder as OpposerQueryBuilder, RelationBuilder } from '../interfaces/controller.js';
2
- import { EntityMetadata, FieldMetadata } from './metadata.js';
3
- import { DatabaseDriver } from './opposer.js';
4
- export declare class QueryTranslator {
5
- private entity;
6
- private fields;
7
- private driver;
8
- constructor(entity: EntityMetadata, fields: FieldMetadata[], driver: DatabaseDriver);
9
- private validateField;
10
- private validateTargetField;
11
- private renderFilter;
12
- translateFilter(query: OpposerQueryBuilder): {
13
- sql: string;
14
- params: any[];
15
- joins: Set<string>;
16
- };
17
- private quoteField;
18
- private renderOperator;
19
- translateSelect(select: string[]): string;
20
- translateRelations(relations: (string | RelationBuilder)[]): {
21
- select: string[];
22
- joins: string[];
23
- };
24
- translateAggregate(aggregates: {
25
- [key: string]: 'sum' | 'avg' | 'min' | 'max' | 'count';
26
- }): string;
27
- translateGroup(fields: string[]): string;
28
- translatePagination(pagination: {
29
- page: number;
30
- take: number;
31
- } | undefined): string;
32
- }
@@ -1,278 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.QueryTranslator = void 0;
4
- const metadata_js_1 = require("./metadata.js");
5
- class QueryTranslator {
6
- constructor(entity, fields, driver) {
7
- this.entity = entity;
8
- this.fields = fields;
9
- this.driver = driver;
10
- }
11
- validateField(field) {
12
- if (field.includes('.')) {
13
- const [relationName, ...rest] = field.split('.');
14
- const relationField = this.fields.find((f) => f.name === relationName && f.type === 'relation');
15
- if (relationField && relationField.relation) {
16
- const targetEntity = relationField.relation.target();
17
- const targetFields = metadata_js_1.MetadataStore.getFields(targetEntity);
18
- const subField = rest.join('.');
19
- return this.validateTargetField(targetFields, subField);
20
- }
21
- return false;
22
- }
23
- return (this.fields.some((f) => {
24
- return f.name === field;
25
- }) ||
26
- field === 'id' ||
27
- field === '*');
28
- }
29
- validateTargetField(fields, field) {
30
- if (field.includes('.')) {
31
- const [relationName, ...rest] = field.split('.');
32
- const relationField = fields.find((f) => f.name === relationName && f.type === 'relation');
33
- if (relationField && relationField.relation) {
34
- const targetEntity = relationField.relation.target();
35
- const targetFields = metadata_js_1.MetadataStore.getFields(targetEntity);
36
- return this.validateTargetField(targetFields, rest.join('.'));
37
- }
38
- return false;
39
- }
40
- return fields.some((f) => f.name === field) || field === 'id' || field === '*';
41
- }
42
- renderFilter(query, params, joins, prefix = '') {
43
- if (!query || typeof query !== 'object') {
44
- return '';
45
- }
46
- const parts = [];
47
- for (const [key, value] of Object.entries(query)) {
48
- if (key === '$or' && Array.isArray(value)) {
49
- const orParts = value
50
- .map((v) => {
51
- return this.renderFilter(v, params, joins, prefix);
52
- })
53
- .filter((v) => {
54
- return v !== '';
55
- })
56
- .map((v) => {
57
- return `(${v})`;
58
- });
59
- if (orParts.length > 0) {
60
- parts.push(`(${orParts.join(' OR ')})`);
61
- }
62
- continue;
63
- }
64
- const fullPath = prefix ? `${prefix}.${key}` : key;
65
- if (!this.validateField(fullPath)) {
66
- continue;
67
- }
68
- // If it's a nested object that is NOT an operator ($l, $eq, etc) and NOT a Date,
69
- // and it corresponds to a relation, we treat it as a nested filter.
70
- const isOperator = typeof key === 'string' && key.startsWith('$');
71
- const isNestedFilter = typeof value === 'object' &&
72
- value !== null &&
73
- !(value instanceof Date) &&
74
- !Object.keys(value).some(k => k.startsWith('$'));
75
- if (isNestedFilter) {
76
- // It's a nested filter (e.g., { author: { name: "..." } })
77
- joins.add(key); // Ensure the join is added for this level
78
- const nestedFilter = this.renderFilter(value, params, joins, fullPath);
79
- if (nestedFilter) {
80
- parts.push(nestedFilter);
81
- }
82
- continue;
83
- }
84
- if (fullPath.includes('.')) {
85
- const [relationName] = fullPath.split('.');
86
- joins.add(relationName);
87
- }
88
- if (typeof value === 'object' && value !== null && !(value instanceof Date)) {
89
- for (const [op, val] of Object.entries(value)) {
90
- const operatorSql = this.renderOperator(fullPath, op, val, params);
91
- if (operatorSql) {
92
- parts.push(operatorSql);
93
- }
94
- }
95
- }
96
- else {
97
- params.push(value);
98
- parts.push(`${this.quoteField(fullPath)} = $${params.length}`);
99
- }
100
- }
101
- return parts.join(' AND ');
102
- }
103
- translateFilter(query) {
104
- const params = [];
105
- const joins = new Set();
106
- const sql = this.renderFilter(query, params, joins);
107
- return { sql: sql ? `WHERE ${sql}` : '', params, joins };
108
- }
109
- quoteField(field) {
110
- if (field.includes('.')) {
111
- return field
112
- .split('.')
113
- .map((p) => this.driver.quoteIdentifier(p))
114
- .join('.');
115
- }
116
- return `${this.driver.quoteIdentifier(this.entity.tableName)}.${this.driver.quoteIdentifier(field)}`;
117
- }
118
- renderOperator(column, op, val, params) {
119
- const quotedColumn = this.quoteField(column);
120
- switch (op) {
121
- case '$l':
122
- params.push(val);
123
- return `${quotedColumn} LIKE $${params.length}`;
124
- case '$il':
125
- params.push(val);
126
- return `${quotedColumn} ILIKE $${params.length}`;
127
- case '$in':
128
- case '$nin': {
129
- if (!Array.isArray(val) || val.length === 0) {
130
- return '';
131
- }
132
- const placeholders = val
133
- .map((v) => {
134
- params.push(v);
135
- return `$${params.length}`;
136
- })
137
- .join(', ');
138
- const operator = op === '$in' ? 'IN' : 'NOT IN';
139
- return `${quotedColumn} ${operator} (${placeholders})`;
140
- }
141
- case '$btw':
142
- if (!Array.isArray(val) || val.length < 2) {
143
- return '';
144
- }
145
- params.push(val[0]);
146
- params.push(val[1]);
147
- return `${quotedColumn} BETWEEN $${params.length - 1} AND $${params.length}`;
148
- case '$mt':
149
- params.push(val);
150
- return `${quotedColumn} > $${params.length}`;
151
- case '$mte':
152
- params.push(val);
153
- return `${quotedColumn} >= $${params.length}`;
154
- case '$lt':
155
- params.push(val);
156
- return `${quotedColumn} < $${params.length}`;
157
- case '$lte':
158
- params.push(val);
159
- return `${quotedColumn} <= $${params.length}`;
160
- case '$eq':
161
- params.push(val);
162
- return `${quotedColumn} = $${params.length}`;
163
- default:
164
- return '';
165
- }
166
- }
167
- translateSelect(select) {
168
- const table = this.driver.quoteIdentifier(this.entity.tableName);
169
- if (!select || select.length === 0) {
170
- return `${table}.*`;
171
- }
172
- return select
173
- .filter((s) => {
174
- return this.validateField(s);
175
- })
176
- .map((s) => {
177
- return this.quoteField(s);
178
- })
179
- .join(', ');
180
- }
181
- translateRelations(relations) {
182
- const select = [];
183
- const joins = [];
184
- for (const rel of relations) {
185
- const relationName = typeof rel === 'string' ? rel : rel.model;
186
- const relationField = this.fields.find((f) => f.name === relationName && f.type === 'relation');
187
- if (!relationField) {
188
- throw new Error(`Relation '${relationName}' not found on model '${this.entity.name}'`);
189
- }
190
- if (relationField.relation) {
191
- const targetEntity = relationField.relation.target();
192
- const targetMetadata = metadata_js_1.MetadataStore.getEntity(targetEntity);
193
- const targetFields = metadata_js_1.MetadataStore.getFields(targetEntity);
194
- if (targetMetadata) {
195
- const targetTable = this.driver.quoteIdentifier(targetMetadata.tableName);
196
- const relationAlias = this.driver.quoteIdentifier(relationName);
197
- const sourceTable = this.driver.quoteIdentifier(this.entity.tableName);
198
- // Determine join columns
199
- const targetPrimary = targetFields.find(f => f.primary);
200
- const targetPrimaryKeyName = targetPrimary ? targetPrimary.name : 'id';
201
- let joinCondition = '';
202
- if (relationField.relation.type === 'many-to-one' || (relationField.relation.type === 'one-to-one' && relationField.relation.joinColumn)) {
203
- joinCondition = `${sourceTable}.${this.driver.quoteIdentifier(relationName)} = ${relationAlias}.${this.driver.quoteIdentifier(targetPrimaryKeyName)}`;
204
- }
205
- else if (relationField.relation.type === 'one-to-many' || (relationField.relation.type === 'one-to-one' && !relationField.relation.joinColumn)) {
206
- const inverseSide = relationField.relation.inverseSide;
207
- if (inverseSide) {
208
- joinCondition = `${relationAlias}.${this.driver.quoteIdentifier(inverseSide)} = ${sourceTable}.id`;
209
- }
210
- }
211
- if (joinCondition) {
212
- joins.push(`LEFT JOIN ${targetTable} AS ${relationAlias} ON ${joinCondition}`);
213
- const selectedFields = typeof rel === 'object' && rel.select ? rel.select : ['*'];
214
- const targetPrimaryFields = targetFields.filter(f => f.primary);
215
- // Always ensure primary keys are included for hydration
216
- for (const pf of targetPrimaryFields) {
217
- if (!selectedFields.includes(pf.name) && !selectedFields.includes('*')) {
218
- select.push(`${relationAlias}.${this.driver.quoteIdentifier(pf.name)} AS ${this.driver.quoteIdentifier(`${relationName}.${pf.name}`)}`);
219
- }
220
- }
221
- for (const f of selectedFields) {
222
- if (typeof f === 'string') {
223
- if (f === '*') {
224
- for (const tf of targetFields) {
225
- if (tf.type !== 'relation') {
226
- select.push(`${relationAlias}.${this.driver.quoteIdentifier(tf.name)} AS ${this.driver.quoteIdentifier(`${relationName}.${tf.name}`)}`);
227
- }
228
- }
229
- // Also include 'id' if not in targetFields
230
- if (!targetFields.some(tf => tf.name === 'id')) {
231
- select.push(`${relationAlias}.id AS ${this.driver.quoteIdentifier(`${relationName}.id`)}`);
232
- }
233
- }
234
- else {
235
- select.push(`${relationAlias}.${this.driver.quoteIdentifier(f)} AS ${this.driver.quoteIdentifier(`${relationName}.${f}`)}`);
236
- }
237
- }
238
- // TODO: nested relations
239
- }
240
- }
241
- }
242
- }
243
- }
244
- return { select, joins };
245
- }
246
- translateAggregate(aggregates) {
247
- return Object.entries(aggregates)
248
- .map(([field, op]) => {
249
- const quotedField = field === '*' ? '*' : this.quoteField(field);
250
- const alias = this.driver.quoteIdentifier(`${op}_${field.replace('*', 'all').replace('.', '_')}`);
251
- return `${op.toUpperCase()}(${quotedField}) as ${alias}`;
252
- })
253
- .join(', ');
254
- }
255
- translateGroup(fields) {
256
- if (!fields || fields.length === 0) {
257
- return '';
258
- }
259
- const quotedFields = fields
260
- .filter((f) => {
261
- return this.validateField(f);
262
- })
263
- .map((f) => {
264
- return this.quoteField(f);
265
- })
266
- .join(', ');
267
- return quotedFields ? `GROUP BY ${quotedFields}` : '';
268
- }
269
- translatePagination(pagination) {
270
- if (!pagination) {
271
- return '';
272
- }
273
- const limit = Math.max(0, pagination.take || 10);
274
- const offset = Math.max(0, pagination.page || 0) * limit;
275
- return `LIMIT ${limit} OFFSET ${offset}`;
276
- }
277
- }
278
- exports.QueryTranslator = QueryTranslator;
@@ -1,54 +0,0 @@
1
- import { OpposerDatabase } from './opposer.js';
2
- import { EntityMetadata, FieldMetadata } from './metadata.js';
3
- import { QueryBuilder as OpposerQueryBuilder, RelationBuilder } from '../interfaces/controller.js';
4
- export declare class Repository<T> {
5
- private connection;
6
- private target;
7
- private metadata;
8
- private fields;
9
- private translator;
10
- constructor(connection: OpposerDatabase, target: Function);
11
- get Metadata(): EntityMetadata;
12
- get Fields(): FieldMetadata[];
13
- find(options: {
14
- where?: OpposerQueryBuilder;
15
- select?: string[];
16
- pagination?: {
17
- page: number;
18
- take: number;
19
- };
20
- relation?: (string | RelationBuilder)[];
21
- }): Promise<T[]>;
22
- findOne(options: {
23
- where?: OpposerQueryBuilder;
24
- select?: string[];
25
- relation?: (string | RelationBuilder)[];
26
- }): Promise<T | null>;
27
- private reconstruct;
28
- private merge;
29
- private executeHooks;
30
- insert(data: Partial<T>): Promise<T>;
31
- update(where: OpposerQueryBuilder, data: Partial<T>): Promise<void>;
32
- delete(where: OpposerQueryBuilder): Promise<void>;
33
- count(where: OpposerQueryBuilder): Promise<number>;
34
- exists(where: OpposerQueryBuilder): Promise<boolean>;
35
- aggregate(options: {
36
- where?: OpposerQueryBuilder;
37
- aggregate: {
38
- [key: string]: 'sum' | 'avg' | 'min' | 'max' | 'count';
39
- };
40
- }): Promise<any>;
41
- distinct(options: {
42
- where?: OpposerQueryBuilder;
43
- field: string;
44
- }): Promise<any[]>;
45
- group(options: {
46
- where?: OpposerQueryBuilder;
47
- by: string[];
48
- aggregate?: {
49
- [key: string]: 'sum' | 'avg' | 'min' | 'max' | 'count';
50
- };
51
- select?: string[];
52
- }): Promise<any[]>;
53
- validate(data: Partial<T>): Record<string, string[]>;
54
- }