view.cjs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 view_exports = {};
  20. __export(view_exports, {
  21. ManualViewBuilder: () => ManualViewBuilder,
  22. MySqlView: () => MySqlView,
  23. ViewBuilder: () => ViewBuilder,
  24. ViewBuilderCore: () => ViewBuilderCore,
  25. mysqlView: () => mysqlView,
  26. mysqlViewWithSchema: () => mysqlViewWithSchema
  27. });
  28. module.exports = __toCommonJS(view_exports);
  29. var import_entity = require("../entity.cjs");
  30. var import_selection_proxy = require("../selection-proxy.cjs");
  31. var import_utils = require("../utils.cjs");
  32. var import_query_builder = require("./query-builders/query-builder.cjs");
  33. var import_table = require("./table.cjs");
  34. var import_view_base = require("./view-base.cjs");
  35. var import_view_common = require("./view-common.cjs");
  36. class ViewBuilderCore {
  37. constructor(name, schema) {
  38. this.name = name;
  39. this.schema = schema;
  40. }
  41. static [import_entity.entityKind] = "MySqlViewBuilder";
  42. config = {};
  43. algorithm(algorithm) {
  44. this.config.algorithm = algorithm;
  45. return this;
  46. }
  47. sqlSecurity(sqlSecurity) {
  48. this.config.sqlSecurity = sqlSecurity;
  49. return this;
  50. }
  51. withCheckOption(withCheckOption) {
  52. this.config.withCheckOption = withCheckOption ?? "cascaded";
  53. return this;
  54. }
  55. }
  56. class ViewBuilder extends ViewBuilderCore {
  57. static [import_entity.entityKind] = "MySqlViewBuilder";
  58. as(qb) {
  59. if (typeof qb === "function") {
  60. qb = qb(new import_query_builder.QueryBuilder());
  61. }
  62. const selectionProxy = new import_selection_proxy.SelectionProxyHandler({
  63. alias: this.name,
  64. sqlBehavior: "error",
  65. sqlAliasedBehavior: "alias",
  66. replaceOriginalName: true
  67. });
  68. const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
  69. return new Proxy(
  70. new MySqlView({
  71. mysqlConfig: this.config,
  72. config: {
  73. name: this.name,
  74. schema: this.schema,
  75. selectedFields: aliasedSelection,
  76. query: qb.getSQL().inlineParams()
  77. }
  78. }),
  79. selectionProxy
  80. );
  81. }
  82. }
  83. class ManualViewBuilder extends ViewBuilderCore {
  84. static [import_entity.entityKind] = "MySqlManualViewBuilder";
  85. columns;
  86. constructor(name, columns, schema) {
  87. super(name, schema);
  88. this.columns = (0, import_utils.getTableColumns)((0, import_table.mysqlTable)(name, columns));
  89. }
  90. existing() {
  91. return new Proxy(
  92. new MySqlView({
  93. mysqlConfig: void 0,
  94. config: {
  95. name: this.name,
  96. schema: this.schema,
  97. selectedFields: this.columns,
  98. query: void 0
  99. }
  100. }),
  101. new import_selection_proxy.SelectionProxyHandler({
  102. alias: this.name,
  103. sqlBehavior: "error",
  104. sqlAliasedBehavior: "alias",
  105. replaceOriginalName: true
  106. })
  107. );
  108. }
  109. as(query) {
  110. return new Proxy(
  111. new MySqlView({
  112. mysqlConfig: this.config,
  113. config: {
  114. name: this.name,
  115. schema: this.schema,
  116. selectedFields: this.columns,
  117. query: query.inlineParams()
  118. }
  119. }),
  120. new import_selection_proxy.SelectionProxyHandler({
  121. alias: this.name,
  122. sqlBehavior: "error",
  123. sqlAliasedBehavior: "alias",
  124. replaceOriginalName: true
  125. })
  126. );
  127. }
  128. }
  129. class MySqlView extends import_view_base.MySqlViewBase {
  130. static [import_entity.entityKind] = "MySqlView";
  131. [import_view_common.MySqlViewConfig];
  132. constructor({ mysqlConfig, config }) {
  133. super(config);
  134. this[import_view_common.MySqlViewConfig] = mysqlConfig;
  135. }
  136. }
  137. function mysqlViewWithSchema(name, selection, schema) {
  138. if (selection) {
  139. return new ManualViewBuilder(name, selection, schema);
  140. }
  141. return new ViewBuilder(name, schema);
  142. }
  143. function mysqlView(name, selection) {
  144. return mysqlViewWithSchema(name, selection, void 0);
  145. }
  146. // Annotate the CommonJS export names for ESM import in node:
  147. 0 && (module.exports = {
  148. ManualViewBuilder,
  149. MySqlView,
  150. ViewBuilder,
  151. ViewBuilderCore,
  152. mysqlView,
  153. mysqlViewWithSchema
  154. });
  155. //# sourceMappingURL=view.cjs.map