alias.cjs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. "use strict";
  2. var __defProp = Object.defineProperty;
  3. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  4. var __getOwnPropNames = Object.getOwnPropertyNames;
  5. var __hasOwnProp = Object.prototype.hasOwnProperty;
  6. var __export = (target, all) => {
  7. for (var name in all)
  8. __defProp(target, name, { get: all[name], enumerable: true });
  9. };
  10. var __copyProps = (to, from, except, desc) => {
  11. if (from && typeof from === "object" || typeof from === "function") {
  12. for (let key of __getOwnPropNames(from))
  13. if (!__hasOwnProp.call(to, key) && key !== except)
  14. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  15. }
  16. return to;
  17. };
  18. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  19. var alias_exports = {};
  20. __export(alias_exports, {
  21. ColumnAliasProxyHandler: () => ColumnAliasProxyHandler,
  22. RelationTableAliasProxyHandler: () => RelationTableAliasProxyHandler,
  23. TableAliasProxyHandler: () => TableAliasProxyHandler,
  24. aliasedRelation: () => aliasedRelation,
  25. aliasedTable: () => aliasedTable,
  26. aliasedTableColumn: () => aliasedTableColumn,
  27. mapColumnsInAliasedSQLToAlias: () => mapColumnsInAliasedSQLToAlias,
  28. mapColumnsInSQLToAlias: () => mapColumnsInSQLToAlias
  29. });
  30. module.exports = __toCommonJS(alias_exports);
  31. var import_column = require("./column.cjs");
  32. var import_entity = require("./entity.cjs");
  33. var import_sql = require("./sql/sql.cjs");
  34. var import_table = require("./table.cjs");
  35. var import_view_common = require("./view-common.cjs");
  36. class ColumnAliasProxyHandler {
  37. constructor(table) {
  38. this.table = table;
  39. }
  40. static [import_entity.entityKind] = "ColumnAliasProxyHandler";
  41. get(columnObj, prop) {
  42. if (prop === "table") {
  43. return this.table;
  44. }
  45. return columnObj[prop];
  46. }
  47. }
  48. class TableAliasProxyHandler {
  49. constructor(alias, replaceOriginalName) {
  50. this.alias = alias;
  51. this.replaceOriginalName = replaceOriginalName;
  52. }
  53. static [import_entity.entityKind] = "TableAliasProxyHandler";
  54. get(target, prop) {
  55. if (prop === import_table.Table.Symbol.IsAlias) {
  56. return true;
  57. }
  58. if (prop === import_table.Table.Symbol.Name) {
  59. return this.alias;
  60. }
  61. if (this.replaceOriginalName && prop === import_table.Table.Symbol.OriginalName) {
  62. return this.alias;
  63. }
  64. if (prop === import_view_common.ViewBaseConfig) {
  65. return {
  66. ...target[import_view_common.ViewBaseConfig],
  67. name: this.alias,
  68. isAlias: true
  69. };
  70. }
  71. if (prop === import_table.Table.Symbol.Columns) {
  72. const columns = target[import_table.Table.Symbol.Columns];
  73. if (!columns) {
  74. return columns;
  75. }
  76. const proxiedColumns = {};
  77. Object.keys(columns).map((key) => {
  78. proxiedColumns[key] = new Proxy(
  79. columns[key],
  80. new ColumnAliasProxyHandler(new Proxy(target, this))
  81. );
  82. });
  83. return proxiedColumns;
  84. }
  85. const value = target[prop];
  86. if ((0, import_entity.is)(value, import_column.Column)) {
  87. return new Proxy(value, new ColumnAliasProxyHandler(new Proxy(target, this)));
  88. }
  89. return value;
  90. }
  91. }
  92. class RelationTableAliasProxyHandler {
  93. constructor(alias) {
  94. this.alias = alias;
  95. }
  96. static [import_entity.entityKind] = "RelationTableAliasProxyHandler";
  97. get(target, prop) {
  98. if (prop === "sourceTable") {
  99. return aliasedTable(target.sourceTable, this.alias);
  100. }
  101. return target[prop];
  102. }
  103. }
  104. function aliasedTable(table, tableAlias) {
  105. return new Proxy(table, new TableAliasProxyHandler(tableAlias, false));
  106. }
  107. function aliasedRelation(relation, tableAlias) {
  108. return new Proxy(relation, new RelationTableAliasProxyHandler(tableAlias));
  109. }
  110. function aliasedTableColumn(column, tableAlias) {
  111. return new Proxy(
  112. column,
  113. new ColumnAliasProxyHandler(new Proxy(column.table, new TableAliasProxyHandler(tableAlias, false)))
  114. );
  115. }
  116. function mapColumnsInAliasedSQLToAlias(query, alias) {
  117. return new import_sql.SQL.Aliased(mapColumnsInSQLToAlias(query.sql, alias), query.fieldAlias);
  118. }
  119. function mapColumnsInSQLToAlias(query, alias) {
  120. return import_sql.sql.join(query.queryChunks.map((c) => {
  121. if ((0, import_entity.is)(c, import_column.Column)) {
  122. return aliasedTableColumn(c, alias);
  123. }
  124. if ((0, import_entity.is)(c, import_sql.SQL)) {
  125. return mapColumnsInSQLToAlias(c, alias);
  126. }
  127. if ((0, import_entity.is)(c, import_sql.SQL.Aliased)) {
  128. return mapColumnsInAliasedSQLToAlias(c, alias);
  129. }
  130. return c;
  131. }));
  132. }
  133. // Annotate the CommonJS export names for ESM import in node:
  134. 0 && (module.exports = {
  135. ColumnAliasProxyHandler,
  136. RelationTableAliasProxyHandler,
  137. TableAliasProxyHandler,
  138. aliasedRelation,
  139. aliasedTable,
  140. aliasedTableColumn,
  141. mapColumnsInAliasedSQLToAlias,
  142. mapColumnsInSQLToAlias
  143. });
  144. //# sourceMappingURL=alias.cjs.map