@cheetah.js/orm 0.1.96 → 0.1.98

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.
@@ -55,6 +55,12 @@ export declare class SqlBuilder<T> {
55
55
  private objectToStringMap;
56
56
  private mapObjectKey;
57
57
  private isNestedObject;
58
+ private buildColumnPath;
59
+ private splitPath;
60
+ private resolvePathEntity;
61
+ private nextEntity;
62
+ private resolveColumn;
63
+ private joinSegments;
58
64
  private getTableName;
59
65
  private t;
60
66
  private getEntity;
@@ -310,7 +310,8 @@ class SqlBuilder {
310
310
  return this.objectToStringMap(obj[key], fullKey);
311
311
  }
312
312
  if (parentKey) {
313
- return [`${this.columnManager.discoverAlias(fullKey, true)} ${obj[key]}`];
313
+ const columnPath = this.buildColumnPath(fullKey);
314
+ return [`${this.columnManager.discoverAlias(columnPath, true)} ${obj[key]}`];
314
315
  }
315
316
  const columnName = value_processor_1.ValueProcessor.getColumnName(key, this.entity);
316
317
  return [`${this.columnManager.discoverAlias(columnName, true)} ${obj[key]}`];
@@ -318,6 +319,45 @@ class SqlBuilder {
318
319
  isNestedObject(value) {
319
320
  return typeof value === 'object' && value !== null;
320
321
  }
322
+ buildColumnPath(path) {
323
+ const segments = this.splitPath(path);
324
+ const entity = this.resolvePathEntity(segments.parents);
325
+ const column = this.resolveColumn(segments.column, entity);
326
+ return this.joinSegments(segments.parents, column);
327
+ }
328
+ splitPath(path) {
329
+ const parts = path.split('.');
330
+ const column = parts.pop() ?? path;
331
+ return { parents: parts, column };
332
+ }
333
+ resolvePathEntity(parents) {
334
+ let current = this.entity;
335
+ for (const relation of parents) {
336
+ current = this.nextEntity(current, relation);
337
+ }
338
+ return current;
339
+ }
340
+ nextEntity(entity, relation) {
341
+ const relations = entity.relations ?? [];
342
+ const meta = relations.find(rel => rel.propertyKey === relation);
343
+ if (!meta) {
344
+ throw new Error(`Relationship "${relation}" not found for ORDER BY path`);
345
+ }
346
+ const next = this.entityStorage.get(meta.entity());
347
+ if (!next) {
348
+ throw new Error(`Entity metadata not found for relation "${relation}"`);
349
+ }
350
+ return next;
351
+ }
352
+ resolveColumn(column, entity) {
353
+ return value_processor_1.ValueProcessor.getColumnName(column, entity);
354
+ }
355
+ joinSegments(parents, column) {
356
+ if (parents.length === 0) {
357
+ return column;
358
+ }
359
+ return `${parents.join('.')}.${column}`;
360
+ }
321
361
  getTableName() {
322
362
  const tableName = this.entity.tableName || this.model.name.toLowerCase();
323
363
  const schema = this.entity.schema || 'public';
@@ -22,4 +22,7 @@ export declare class SqlColumnManager {
22
22
  private buildJoinMaps;
23
23
  private findNextAlias;
24
24
  private formatColumnWithAlias;
25
+ private getColumnNameFromProperty;
26
+ private getColumnNameFromPropertyForEntity;
27
+ private getEntityFromAlias;
25
28
  }
@@ -65,10 +65,11 @@ class SqlColumnManager {
65
65
  return column.includes('.');
66
66
  }
67
67
  buildSimpleColumnAlias(column, alias, onlyAlias) {
68
+ const columnName = this.getColumnNameFromProperty(column);
68
69
  if (onlyAlias) {
69
- return `${alias}."${column}"`;
70
+ return `${alias}."${columnName}"`;
70
71
  }
71
- return `${alias}."${column}" as ${alias}_${column}`;
72
+ return `${alias}."${columnName}" as ${alias}_${columnName}`;
72
73
  }
73
74
  buildNestedColumnAlias(column, onlyAlias) {
74
75
  this.validateJoinsExist();
@@ -118,11 +119,39 @@ class SqlColumnManager {
118
119
  }
119
120
  return null;
120
121
  }
121
- formatColumnWithAlias(alias, columnName, onlyAlias) {
122
+ formatColumnWithAlias(alias, propertyName, onlyAlias) {
123
+ const entity = this.getEntityFromAlias(alias);
124
+ const columnName = this.getColumnNameFromPropertyForEntity(propertyName, entity);
122
125
  if (onlyAlias) {
123
126
  return `${alias}."${columnName}"`;
124
127
  }
125
128
  return `${alias}."${columnName}" as ${alias}_${columnName}`;
126
129
  }
130
+ getColumnNameFromProperty(propertyName) {
131
+ return this.getColumnNameFromPropertyForEntity(propertyName, this.entity);
132
+ }
133
+ getColumnNameFromPropertyForEntity(propertyName, entity) {
134
+ if (entity.properties[propertyName]) {
135
+ return entity.properties[propertyName].options.columnName;
136
+ }
137
+ const relation = entity.relations?.find(rel => rel.propertyKey === propertyName);
138
+ if (relation) {
139
+ return relation.columnName;
140
+ }
141
+ return propertyName;
142
+ }
143
+ getEntityFromAlias(alias) {
144
+ if (alias === this.statements.alias) {
145
+ return this.entity;
146
+ }
147
+ const join = this.statements.join?.find(j => j.joinAlias === alias);
148
+ if (join?.joinEntity) {
149
+ const entity = this.entityStorage.get(join.joinEntity);
150
+ if (entity) {
151
+ return entity;
152
+ }
153
+ }
154
+ return this.entity;
155
+ }
127
156
  }
128
157
  exports.SqlColumnManager = SqlColumnManager;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.1.96",
3
+ "version": "0.1.98",
4
4
  "description": "A simple ORM for Cheetah.js",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",
@@ -55,5 +55,5 @@
55
55
  "bun",
56
56
  "value-object"
57
57
  ],
58
- "gitHead": "fedfd3f994f58ea231d1d441adab2a147c57482e"
58
+ "gitHead": "e53b18f593bfab449b6684ff1ccbb74639efefad"
59
59
  }