view.js 3.1 KB

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