selection-proxy.cjs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 selection_proxy_exports = {};
  20. __export(selection_proxy_exports, {
  21. SelectionProxyHandler: () => SelectionProxyHandler
  22. });
  23. module.exports = __toCommonJS(selection_proxy_exports);
  24. var import_alias = require("./alias.cjs");
  25. var import_column = require("./column.cjs");
  26. var import_entity = require("./entity.cjs");
  27. var import_sql = require("./sql/sql.cjs");
  28. var import_subquery = require("./subquery.cjs");
  29. var import_view_common = require("./view-common.cjs");
  30. class SelectionProxyHandler {
  31. static [import_entity.entityKind] = "SelectionProxyHandler";
  32. config;
  33. constructor(config) {
  34. this.config = { ...config };
  35. }
  36. get(subquery, prop) {
  37. if (prop === "_") {
  38. return {
  39. ...subquery["_"],
  40. selectedFields: new Proxy(
  41. subquery._.selectedFields,
  42. this
  43. )
  44. };
  45. }
  46. if (prop === import_view_common.ViewBaseConfig) {
  47. return {
  48. ...subquery[import_view_common.ViewBaseConfig],
  49. selectedFields: new Proxy(
  50. subquery[import_view_common.ViewBaseConfig].selectedFields,
  51. this
  52. )
  53. };
  54. }
  55. if (typeof prop === "symbol") {
  56. return subquery[prop];
  57. }
  58. const columns = (0, import_entity.is)(subquery, import_subquery.Subquery) ? subquery._.selectedFields : (0, import_entity.is)(subquery, import_sql.View) ? subquery[import_view_common.ViewBaseConfig].selectedFields : subquery;
  59. const value = columns[prop];
  60. if ((0, import_entity.is)(value, import_sql.SQL.Aliased)) {
  61. if (this.config.sqlAliasedBehavior === "sql" && !value.isSelectionField) {
  62. return value.sql;
  63. }
  64. const newValue = value.clone();
  65. newValue.isSelectionField = true;
  66. return newValue;
  67. }
  68. if ((0, import_entity.is)(value, import_sql.SQL)) {
  69. if (this.config.sqlBehavior === "sql") {
  70. return value;
  71. }
  72. throw new Error(
  73. `You tried to reference "${prop}" field from a subquery, which is a raw SQL field, but it doesn't have an alias declared. Please add an alias to the field using ".as('alias')" method.`
  74. );
  75. }
  76. if ((0, import_entity.is)(value, import_column.Column)) {
  77. if (this.config.alias) {
  78. return new Proxy(
  79. value,
  80. new import_alias.ColumnAliasProxyHandler(
  81. new Proxy(
  82. value.table,
  83. new import_alias.TableAliasProxyHandler(this.config.alias, this.config.replaceOriginalName ?? false)
  84. )
  85. )
  86. );
  87. }
  88. return value;
  89. }
  90. if (typeof value !== "object" || value === null) {
  91. return value;
  92. }
  93. return new Proxy(value, new SelectionProxyHandler(this.config));
  94. }
  95. }
  96. // Annotate the CommonJS export names for ESM import in node:
  97. 0 && (module.exports = {
  98. SelectionProxyHandler
  99. });
  100. //# sourceMappingURL=selection-proxy.cjs.map