relq 1.0.87 → 1.0.88

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.
@@ -118,12 +118,21 @@ function buildLateralSubquery(tableName, alias, builder, tableDef) {
118
118
  const internals = builder[join_internals_1.JOIN_INTERNAL];
119
119
  parts.push('SELECT');
120
120
  const selectedProps = internals.getSelectedColumns();
121
+ const innerJoins = internals.getInnerJoins();
122
+ const hasInnerJoins = innerJoins.length > 0;
123
+ const toSnake = (s) => s.replace(/[A-Z]/g, l => `_${l.toLowerCase()}`);
121
124
  if (selectedProps && selectedProps.length > 0) {
122
125
  const tableColumns = tableDef?.$columns || tableDef;
123
126
  const selectCols = selectedProps.map(prop => {
124
127
  const columnDef = tableColumns?.[prop];
125
- const sqlName = columnDef?.$columnName || prop;
126
- return `"${alias}"."${sqlName}" AS "${prop}"`;
128
+ if (columnDef || !hasInnerJoins) {
129
+ const sqlName = columnDef?.$columnName || toSnake(prop);
130
+ return `"${alias}"."${sqlName}" AS "${prop}"`;
131
+ }
132
+ else {
133
+ const sqlName = toSnake(prop);
134
+ return `"${sqlName}" AS "${prop}"`;
135
+ }
127
136
  }).join(', ');
128
137
  parts.push(selectCols);
129
138
  }
@@ -131,7 +140,6 @@ function buildLateralSubquery(tableName, alias, builder, tableDef) {
131
140
  parts.push(`"${alias}".*`);
132
141
  }
133
142
  parts.push(`FROM "${tableName}" AS "${alias}"`);
134
- const innerJoins = internals.getInnerJoins();
135
143
  for (const join of innerJoins) {
136
144
  parts.push(`${join.type} "${join.table}" AS "${join.alias}" ON ${join.onClause}`);
137
145
  }
@@ -98,12 +98,12 @@ class JoinConditionBuilder {
98
98
  this.whereConditions.push(...collector.getConditions());
99
99
  return this;
100
100
  }
101
- select(columns) {
102
- if (columns === '*') {
103
- this.selectedColumns = undefined;
101
+ select(...args) {
102
+ if (args.length === 1 && Array.isArray(args[0])) {
103
+ this.selectedColumns = args[0];
104
104
  }
105
105
  else {
106
- this.selectedColumns = columns;
106
+ this.selectedColumns = args;
107
107
  }
108
108
  return this;
109
109
  }
@@ -34,28 +34,34 @@ class JoinManyConditionBuilder extends join_condition_builder_1.JoinConditionBui
34
34
  this.rightProxy = rightProxy;
35
35
  };
36
36
  }
37
- innerJoin(table, callback) {
37
+ innerJoin(tableOrAlias, callback) {
38
38
  if (!this.proxyCreator) {
39
39
  throw new Error('innerJoin requires proxy creator - use raw innerJoinRaw() instead');
40
40
  }
41
- const { proxy: innerProxy, tableName } = this.proxyCreator(table, table);
41
+ const [tableKey, alias] = Array.isArray(tableOrAlias)
42
+ ? tableOrAlias
43
+ : [tableOrAlias, tableOrAlias];
44
+ const { proxy: innerProxy, tableName } = this.proxyCreator(tableKey, alias);
42
45
  const conditionBuilder = new join_condition_builder_1.JoinConditionBuilder();
43
46
  callback(conditionBuilder, innerProxy);
44
47
  const internals = conditionBuilder[join_internals_1.JOIN_INTERNAL];
45
48
  const onClause = internals.toSQL();
46
- this.innerJoins.push({ type: 'JOIN', table: tableName, alias: table, onClause });
49
+ this.innerJoins.push({ type: 'JOIN', table: tableName, alias, onClause });
47
50
  return this;
48
51
  }
49
- leftInnerJoin(table, callback) {
52
+ leftInnerJoin(tableOrAlias, callback) {
50
53
  if (!this.proxyCreator) {
51
54
  throw new Error('leftInnerJoin requires proxy creator - use raw leftInnerJoinRaw() instead');
52
55
  }
53
- const { proxy: innerProxy, tableName } = this.proxyCreator(table, table);
56
+ const [tableKey, alias] = Array.isArray(tableOrAlias)
57
+ ? tableOrAlias
58
+ : [tableOrAlias, tableOrAlias];
59
+ const { proxy: innerProxy, tableName } = this.proxyCreator(tableKey, alias);
54
60
  const conditionBuilder = new join_condition_builder_1.JoinConditionBuilder();
55
61
  callback(conditionBuilder, innerProxy);
56
62
  const internals = conditionBuilder[join_internals_1.JOIN_INTERNAL];
57
63
  const onClause = internals.toSQL();
58
- this.innerJoins.push({ type: 'LEFT JOIN', table: tableName, alias: table, onClause });
64
+ this.innerJoins.push({ type: 'LEFT JOIN', table: tableName, alias, onClause });
59
65
  return this;
60
66
  }
61
67
  innerJoinRaw(table, alias, onClause) {
@@ -66,12 +72,12 @@ class JoinManyConditionBuilder extends join_condition_builder_1.JoinConditionBui
66
72
  this.innerJoins.push({ type: 'LEFT JOIN', table, alias, onClause });
67
73
  return this;
68
74
  }
69
- select(columns) {
70
- if (columns === '*') {
71
- this.selectedColumns = undefined;
75
+ select(...args) {
76
+ if (args.length === 1 && Array.isArray(args[0])) {
77
+ this.selectedColumns = args[0];
72
78
  }
73
79
  else {
74
- this.selectedColumns = columns;
80
+ this.selectedColumns = args;
75
81
  }
76
82
  return this;
77
83
  }
@@ -114,12 +114,21 @@ function buildLateralSubquery(tableName, alias, builder, tableDef) {
114
114
  const internals = builder[JOIN_INTERNAL];
115
115
  parts.push('SELECT');
116
116
  const selectedProps = internals.getSelectedColumns();
117
+ const innerJoins = internals.getInnerJoins();
118
+ const hasInnerJoins = innerJoins.length > 0;
119
+ const toSnake = (s) => s.replace(/[A-Z]/g, l => `_${l.toLowerCase()}`);
117
120
  if (selectedProps && selectedProps.length > 0) {
118
121
  const tableColumns = tableDef?.$columns || tableDef;
119
122
  const selectCols = selectedProps.map(prop => {
120
123
  const columnDef = tableColumns?.[prop];
121
- const sqlName = columnDef?.$columnName || prop;
122
- return `"${alias}"."${sqlName}" AS "${prop}"`;
124
+ if (columnDef || !hasInnerJoins) {
125
+ const sqlName = columnDef?.$columnName || toSnake(prop);
126
+ return `"${alias}"."${sqlName}" AS "${prop}"`;
127
+ }
128
+ else {
129
+ const sqlName = toSnake(prop);
130
+ return `"${sqlName}" AS "${prop}"`;
131
+ }
123
132
  }).join(', ');
124
133
  parts.push(selectCols);
125
134
  }
@@ -127,7 +136,6 @@ function buildLateralSubquery(tableName, alias, builder, tableDef) {
127
136
  parts.push(`"${alias}".*`);
128
137
  }
129
138
  parts.push(`FROM "${tableName}" AS "${alias}"`);
130
- const innerJoins = internals.getInnerJoins();
131
139
  for (const join of innerJoins) {
132
140
  parts.push(`${join.type} "${join.table}" AS "${join.alias}" ON ${join.onClause}`);
133
141
  }
@@ -91,12 +91,12 @@ export class JoinConditionBuilder {
91
91
  this.whereConditions.push(...collector.getConditions());
92
92
  return this;
93
93
  }
94
- select(columns) {
95
- if (columns === '*') {
96
- this.selectedColumns = undefined;
94
+ select(...args) {
95
+ if (args.length === 1 && Array.isArray(args[0])) {
96
+ this.selectedColumns = args[0];
97
97
  }
98
98
  else {
99
- this.selectedColumns = columns;
99
+ this.selectedColumns = args;
100
100
  }
101
101
  return this;
102
102
  }
@@ -27,28 +27,34 @@ export class JoinManyConditionBuilder extends JoinConditionBuilder {
27
27
  this.rightProxy = rightProxy;
28
28
  };
29
29
  }
30
- innerJoin(table, callback) {
30
+ innerJoin(tableOrAlias, callback) {
31
31
  if (!this.proxyCreator) {
32
32
  throw new Error('innerJoin requires proxy creator - use raw innerJoinRaw() instead');
33
33
  }
34
- const { proxy: innerProxy, tableName } = this.proxyCreator(table, table);
34
+ const [tableKey, alias] = Array.isArray(tableOrAlias)
35
+ ? tableOrAlias
36
+ : [tableOrAlias, tableOrAlias];
37
+ const { proxy: innerProxy, tableName } = this.proxyCreator(tableKey, alias);
35
38
  const conditionBuilder = new JoinConditionBuilder();
36
39
  callback(conditionBuilder, innerProxy);
37
40
  const internals = conditionBuilder[JOIN_INTERNAL];
38
41
  const onClause = internals.toSQL();
39
- this.innerJoins.push({ type: 'JOIN', table: tableName, alias: table, onClause });
42
+ this.innerJoins.push({ type: 'JOIN', table: tableName, alias, onClause });
40
43
  return this;
41
44
  }
42
- leftInnerJoin(table, callback) {
45
+ leftInnerJoin(tableOrAlias, callback) {
43
46
  if (!this.proxyCreator) {
44
47
  throw new Error('leftInnerJoin requires proxy creator - use raw leftInnerJoinRaw() instead');
45
48
  }
46
- const { proxy: innerProxy, tableName } = this.proxyCreator(table, table);
49
+ const [tableKey, alias] = Array.isArray(tableOrAlias)
50
+ ? tableOrAlias
51
+ : [tableOrAlias, tableOrAlias];
52
+ const { proxy: innerProxy, tableName } = this.proxyCreator(tableKey, alias);
47
53
  const conditionBuilder = new JoinConditionBuilder();
48
54
  callback(conditionBuilder, innerProxy);
49
55
  const internals = conditionBuilder[JOIN_INTERNAL];
50
56
  const onClause = internals.toSQL();
51
- this.innerJoins.push({ type: 'LEFT JOIN', table: tableName, alias: table, onClause });
57
+ this.innerJoins.push({ type: 'LEFT JOIN', table: tableName, alias, onClause });
52
58
  return this;
53
59
  }
54
60
  innerJoinRaw(table, alias, onClause) {
@@ -59,12 +65,12 @@ export class JoinManyConditionBuilder extends JoinConditionBuilder {
59
65
  this.innerJoins.push({ type: 'LEFT JOIN', table, alias, onClause });
60
66
  return this;
61
67
  }
62
- select(columns) {
63
- if (columns === '*') {
64
- this.selectedColumns = undefined;
68
+ select(...args) {
69
+ if (args.length === 1 && Array.isArray(args[0])) {
70
+ this.selectedColumns = args[0];
65
71
  }
66
72
  else {
67
- this.selectedColumns = columns;
73
+ this.selectedColumns = args;
68
74
  }
69
75
  return this;
70
76
  }
package/dist/index.d.ts CHANGED
@@ -7644,17 +7644,17 @@ declare class JoinConditionBuilder<TLeft = any, TRight = any> {
7644
7644
  where(callback: (q: TypedConditionBuilder<TRight>) => TypedConditionBuilder<TRight>): this;
7645
7645
  /**
7646
7646
  * Select specific columns from the joined table.
7647
- * If not called or called with '*', all columns are selected.
7647
+ * If not called, all columns are selected.
7648
7648
  * The return type narrows TRight to only the selected columns.
7649
7649
  *
7650
- * @param columns - Array of column names to select, or '*' for all columns
7651
- *
7652
7650
  * @example
7653
7651
  * on.equal(orders.userId, users.id)
7654
- * .select(['id', 'name', 'email'])
7655
- * on.select('*') // Select all (default)
7652
+ * .select(['id', 'name', 'email']) // array syntax
7653
+ * on.equal(orders.userId, users.id)
7654
+ * .select('id', 'name', 'email') // spread syntax
7656
7655
  */
7657
- select<K extends ColumnKeys<TRight>>(columns: K[] | "*"): JoinConditionBuilder<TLeft, PickColumns<TRight, K>>;
7656
+ select<K extends ColumnKeys<TRight>>(columns: K[]): JoinConditionBuilder<TLeft, PickColumns<TRight, K>>;
7657
+ select<K extends ColumnKeys<TRight>>(...columns: K[]): JoinConditionBuilder<TLeft, PickColumns<TRight, K>>;
7658
7658
  /** @internal */
7659
7659
  get [JOIN_INTERNAL](): JoinConditionInternals;
7660
7660
  /**
@@ -7786,7 +7786,10 @@ declare class JoinManyConditionBuilder<TSchema = any, TLeft = any, TRight = any,
7786
7786
  * // JOIN LATERAL (SELECT ... FROM purchases JOIN products ON ... WHERE ...) ...
7787
7787
  * ```
7788
7788
  */
7789
- innerJoin<TInnerKey extends Exclude<keyof TSchema & string, TRightKey>>(table: TInnerKey, callback: InnerJoinCallback<TRight, TSchema[TInnerKey]>): this;
7789
+ innerJoin<TInnerKey extends Exclude<keyof TSchema & string, TRightKey>>(tableOrAlias: TInnerKey | [
7790
+ TInnerKey,
7791
+ string
7792
+ ], callback: InnerJoinCallback<TRight, TSchema[TInnerKey]>): this;
7790
7793
  /**
7791
7794
  * Add a LEFT JOIN within the LATERAL subquery.
7792
7795
  * Autocomplete excludes parent tables already in scope.
@@ -7794,7 +7797,10 @@ declare class JoinManyConditionBuilder<TSchema = any, TLeft = any, TRight = any,
7794
7797
  * @param table - Table name (schema key) to join
7795
7798
  * @param callback - Join callback receiving (on, innerTable)
7796
7799
  */
7797
- leftInnerJoin<TInnerKey extends Exclude<keyof TSchema & string, TRightKey>>(table: TInnerKey, callback: InnerJoinCallback<TRight, TSchema[TInnerKey]>): this;
7800
+ leftInnerJoin<TInnerKey extends Exclude<keyof TSchema & string, TRightKey>>(tableOrAlias: TInnerKey | [
7801
+ TInnerKey,
7802
+ string
7803
+ ], callback: InnerJoinCallback<TRight, TSchema[TInnerKey]>): this;
7798
7804
  /**
7799
7805
  * Add a raw inner JOIN with SQL string (no type safety).
7800
7806
  * Use when you don't have schema access.
@@ -7812,9 +7818,12 @@ declare class JoinManyConditionBuilder<TSchema = any, TLeft = any, TRight = any,
7812
7818
  * Select specific columns from the joined table.
7813
7819
  * The return type narrows TRight to only the selected columns.
7814
7820
  *
7815
- * @param columns - Array of column names to select, or '*' for all columns
7821
+ * @example
7822
+ * on.select(['id', 'name']) // array syntax
7823
+ * on.select('id', 'name') // spread syntax
7816
7824
  */
7817
- select<K extends ColumnKeys<TRight>>(columns: K[] | "*"): JoinManyConditionBuilder<TSchema, TLeft, PickColumns<TRight, K>, TRightKey>;
7825
+ select<K extends ColumnKeys<TRight>>(columns: K[]): JoinManyConditionBuilder<TSchema, TLeft, PickColumns<TRight, K>, TRightKey>;
7826
+ select<K extends ColumnKeys<TRight>>(...columns: K[]): JoinManyConditionBuilder<TSchema, TLeft, PickColumns<TRight, K>, TRightKey>;
7818
7827
  /** @internal */
7819
7828
  get [JOIN_INTERNAL](): JoinManyInternals;
7820
7829
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "relq",
3
- "version": "1.0.87",
3
+ "version": "1.0.88",
4
4
  "description": "The Fully-Typed PostgreSQL ORM for TypeScript",
5
5
  "author": "Olajide Mathew O. <olajide.mathew@yuniq.solutions>",
6
6
  "license": "MIT",