alias.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { Column } from "./column.js";
  2. import { entityKind, is } from "./entity.js";
  3. import { SQL, sql } from "./sql/sql.js";
  4. import { Table } from "./table.js";
  5. import { ViewBaseConfig } from "./view-common.js";
  6. class ColumnAliasProxyHandler {
  7. constructor(table) {
  8. this.table = table;
  9. }
  10. static [entityKind] = "ColumnAliasProxyHandler";
  11. get(columnObj, prop) {
  12. if (prop === "table") {
  13. return this.table;
  14. }
  15. return columnObj[prop];
  16. }
  17. }
  18. class TableAliasProxyHandler {
  19. constructor(alias, replaceOriginalName) {
  20. this.alias = alias;
  21. this.replaceOriginalName = replaceOriginalName;
  22. }
  23. static [entityKind] = "TableAliasProxyHandler";
  24. get(target, prop) {
  25. if (prop === Table.Symbol.IsAlias) {
  26. return true;
  27. }
  28. if (prop === Table.Symbol.Name) {
  29. return this.alias;
  30. }
  31. if (this.replaceOriginalName && prop === Table.Symbol.OriginalName) {
  32. return this.alias;
  33. }
  34. if (prop === ViewBaseConfig) {
  35. return {
  36. ...target[ViewBaseConfig],
  37. name: this.alias,
  38. isAlias: true
  39. };
  40. }
  41. if (prop === Table.Symbol.Columns) {
  42. const columns = target[Table.Symbol.Columns];
  43. if (!columns) {
  44. return columns;
  45. }
  46. const proxiedColumns = {};
  47. Object.keys(columns).map((key) => {
  48. proxiedColumns[key] = new Proxy(
  49. columns[key],
  50. new ColumnAliasProxyHandler(new Proxy(target, this))
  51. );
  52. });
  53. return proxiedColumns;
  54. }
  55. const value = target[prop];
  56. if (is(value, Column)) {
  57. return new Proxy(value, new ColumnAliasProxyHandler(new Proxy(target, this)));
  58. }
  59. return value;
  60. }
  61. }
  62. class RelationTableAliasProxyHandler {
  63. constructor(alias) {
  64. this.alias = alias;
  65. }
  66. static [entityKind] = "RelationTableAliasProxyHandler";
  67. get(target, prop) {
  68. if (prop === "sourceTable") {
  69. return aliasedTable(target.sourceTable, this.alias);
  70. }
  71. return target[prop];
  72. }
  73. }
  74. function aliasedTable(table, tableAlias) {
  75. return new Proxy(table, new TableAliasProxyHandler(tableAlias, false));
  76. }
  77. function aliasedRelation(relation, tableAlias) {
  78. return new Proxy(relation, new RelationTableAliasProxyHandler(tableAlias));
  79. }
  80. function aliasedTableColumn(column, tableAlias) {
  81. return new Proxy(
  82. column,
  83. new ColumnAliasProxyHandler(new Proxy(column.table, new TableAliasProxyHandler(tableAlias, false)))
  84. );
  85. }
  86. function mapColumnsInAliasedSQLToAlias(query, alias) {
  87. return new SQL.Aliased(mapColumnsInSQLToAlias(query.sql, alias), query.fieldAlias);
  88. }
  89. function mapColumnsInSQLToAlias(query, alias) {
  90. return sql.join(query.queryChunks.map((c) => {
  91. if (is(c, Column)) {
  92. return aliasedTableColumn(c, alias);
  93. }
  94. if (is(c, SQL)) {
  95. return mapColumnsInSQLToAlias(c, alias);
  96. }
  97. if (is(c, SQL.Aliased)) {
  98. return mapColumnsInAliasedSQLToAlias(c, alias);
  99. }
  100. return c;
  101. }));
  102. }
  103. export {
  104. ColumnAliasProxyHandler,
  105. RelationTableAliasProxyHandler,
  106. TableAliasProxyHandler,
  107. aliasedRelation,
  108. aliasedTable,
  109. aliasedTableColumn,
  110. mapColumnsInAliasedSQLToAlias,
  111. mapColumnsInSQLToAlias
  112. };
  113. //# sourceMappingURL=alias.js.map