view.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import { entityKind } from "../entity.js";
  2. import { SelectionProxyHandler } from "../selection-proxy.js";
  3. import { getTableColumns } from "../utils.js";
  4. import { QueryBuilder } from "./query-builders/query-builder.js";
  5. import { mysqlTable } from "./table.js";
  6. import { MySqlViewBase } from "./view-base.js";
  7. import { MySqlViewConfig } from "./view-common.js";
  8. class ViewBuilderCore {
  9. constructor(name, schema) {
  10. this.name = name;
  11. this.schema = schema;
  12. }
  13. static [entityKind] = "MySqlViewBuilder";
  14. config = {};
  15. algorithm(algorithm) {
  16. this.config.algorithm = algorithm;
  17. return this;
  18. }
  19. sqlSecurity(sqlSecurity) {
  20. this.config.sqlSecurity = sqlSecurity;
  21. return this;
  22. }
  23. withCheckOption(withCheckOption) {
  24. this.config.withCheckOption = withCheckOption ?? "cascaded";
  25. return this;
  26. }
  27. }
  28. class ViewBuilder extends ViewBuilderCore {
  29. static [entityKind] = "MySqlViewBuilder";
  30. as(qb) {
  31. if (typeof qb === "function") {
  32. qb = qb(new QueryBuilder());
  33. }
  34. const selectionProxy = new SelectionProxyHandler({
  35. alias: this.name,
  36. sqlBehavior: "error",
  37. sqlAliasedBehavior: "alias",
  38. replaceOriginalName: true
  39. });
  40. const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
  41. return new Proxy(
  42. new MySqlView({
  43. mysqlConfig: this.config,
  44. config: {
  45. name: this.name,
  46. schema: this.schema,
  47. selectedFields: aliasedSelection,
  48. query: qb.getSQL().inlineParams()
  49. }
  50. }),
  51. selectionProxy
  52. );
  53. }
  54. }
  55. class ManualViewBuilder extends ViewBuilderCore {
  56. static [entityKind] = "MySqlManualViewBuilder";
  57. columns;
  58. constructor(name, columns, schema) {
  59. super(name, schema);
  60. this.columns = getTableColumns(mysqlTable(name, columns));
  61. }
  62. existing() {
  63. return new Proxy(
  64. new MySqlView({
  65. mysqlConfig: void 0,
  66. config: {
  67. name: this.name,
  68. schema: this.schema,
  69. selectedFields: this.columns,
  70. query: void 0
  71. }
  72. }),
  73. new SelectionProxyHandler({
  74. alias: this.name,
  75. sqlBehavior: "error",
  76. sqlAliasedBehavior: "alias",
  77. replaceOriginalName: true
  78. })
  79. );
  80. }
  81. as(query) {
  82. return new Proxy(
  83. new MySqlView({
  84. mysqlConfig: this.config,
  85. config: {
  86. name: this.name,
  87. schema: this.schema,
  88. selectedFields: this.columns,
  89. query: query.inlineParams()
  90. }
  91. }),
  92. new SelectionProxyHandler({
  93. alias: this.name,
  94. sqlBehavior: "error",
  95. sqlAliasedBehavior: "alias",
  96. replaceOriginalName: true
  97. })
  98. );
  99. }
  100. }
  101. class MySqlView extends MySqlViewBase {
  102. static [entityKind] = "MySqlView";
  103. [MySqlViewConfig];
  104. constructor({ mysqlConfig, config }) {
  105. super(config);
  106. this[MySqlViewConfig] = mysqlConfig;
  107. }
  108. }
  109. function mysqlViewWithSchema(name, selection, schema) {
  110. if (selection) {
  111. return new ManualViewBuilder(name, selection, schema);
  112. }
  113. return new ViewBuilder(name, schema);
  114. }
  115. function mysqlView(name, selection) {
  116. return mysqlViewWithSchema(name, selection, void 0);
  117. }
  118. export {
  119. ManualViewBuilder,
  120. MySqlView,
  121. ViewBuilder,
  122. ViewBuilderCore,
  123. mysqlView,
  124. mysqlViewWithSchema
  125. };
  126. //# sourceMappingURL=view.js.map