view.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. import { entityKind, is } 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 { pgTable } from "./table.js";
  6. import { PgViewBase } from "./view-base.js";
  7. import { PgViewConfig } from "./view-common.js";
  8. class DefaultViewBuilderCore {
  9. constructor(name, schema) {
  10. this.name = name;
  11. this.schema = schema;
  12. }
  13. static [entityKind] = "PgDefaultViewBuilderCore";
  14. config = {};
  15. with(config) {
  16. this.config.with = config;
  17. return this;
  18. }
  19. }
  20. class ViewBuilder extends DefaultViewBuilderCore {
  21. static [entityKind] = "PgViewBuilder";
  22. as(qb) {
  23. if (typeof qb === "function") {
  24. qb = qb(new QueryBuilder());
  25. }
  26. const selectionProxy = new SelectionProxyHandler({
  27. alias: this.name,
  28. sqlBehavior: "error",
  29. sqlAliasedBehavior: "alias",
  30. replaceOriginalName: true
  31. });
  32. const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
  33. return new Proxy(
  34. new PgView({
  35. pgConfig: this.config,
  36. config: {
  37. name: this.name,
  38. schema: this.schema,
  39. selectedFields: aliasedSelection,
  40. query: qb.getSQL().inlineParams()
  41. }
  42. }),
  43. selectionProxy
  44. );
  45. }
  46. }
  47. class ManualViewBuilder extends DefaultViewBuilderCore {
  48. static [entityKind] = "PgManualViewBuilder";
  49. columns;
  50. constructor(name, columns, schema) {
  51. super(name, schema);
  52. this.columns = getTableColumns(pgTable(name, columns));
  53. }
  54. existing() {
  55. return new Proxy(
  56. new PgView({
  57. pgConfig: void 0,
  58. config: {
  59. name: this.name,
  60. schema: this.schema,
  61. selectedFields: this.columns,
  62. query: void 0
  63. }
  64. }),
  65. new SelectionProxyHandler({
  66. alias: this.name,
  67. sqlBehavior: "error",
  68. sqlAliasedBehavior: "alias",
  69. replaceOriginalName: true
  70. })
  71. );
  72. }
  73. as(query) {
  74. return new Proxy(
  75. new PgView({
  76. pgConfig: this.config,
  77. config: {
  78. name: this.name,
  79. schema: this.schema,
  80. selectedFields: this.columns,
  81. query: query.inlineParams()
  82. }
  83. }),
  84. new SelectionProxyHandler({
  85. alias: this.name,
  86. sqlBehavior: "error",
  87. sqlAliasedBehavior: "alias",
  88. replaceOriginalName: true
  89. })
  90. );
  91. }
  92. }
  93. class MaterializedViewBuilderCore {
  94. constructor(name, schema) {
  95. this.name = name;
  96. this.schema = schema;
  97. }
  98. static [entityKind] = "PgMaterializedViewBuilderCore";
  99. config = {};
  100. using(using) {
  101. this.config.using = using;
  102. return this;
  103. }
  104. with(config) {
  105. this.config.with = config;
  106. return this;
  107. }
  108. tablespace(tablespace) {
  109. this.config.tablespace = tablespace;
  110. return this;
  111. }
  112. withNoData() {
  113. this.config.withNoData = true;
  114. return this;
  115. }
  116. }
  117. class MaterializedViewBuilder extends MaterializedViewBuilderCore {
  118. static [entityKind] = "PgMaterializedViewBuilder";
  119. as(qb) {
  120. if (typeof qb === "function") {
  121. qb = qb(new QueryBuilder());
  122. }
  123. const selectionProxy = new SelectionProxyHandler({
  124. alias: this.name,
  125. sqlBehavior: "error",
  126. sqlAliasedBehavior: "alias",
  127. replaceOriginalName: true
  128. });
  129. const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
  130. return new Proxy(
  131. new PgMaterializedView({
  132. pgConfig: {
  133. with: this.config.with,
  134. using: this.config.using,
  135. tablespace: this.config.tablespace,
  136. withNoData: this.config.withNoData
  137. },
  138. config: {
  139. name: this.name,
  140. schema: this.schema,
  141. selectedFields: aliasedSelection,
  142. query: qb.getSQL().inlineParams()
  143. }
  144. }),
  145. selectionProxy
  146. );
  147. }
  148. }
  149. class ManualMaterializedViewBuilder extends MaterializedViewBuilderCore {
  150. static [entityKind] = "PgManualMaterializedViewBuilder";
  151. columns;
  152. constructor(name, columns, schema) {
  153. super(name, schema);
  154. this.columns = getTableColumns(pgTable(name, columns));
  155. }
  156. existing() {
  157. return new Proxy(
  158. new PgMaterializedView({
  159. pgConfig: {
  160. tablespace: this.config.tablespace,
  161. using: this.config.using,
  162. with: this.config.with,
  163. withNoData: this.config.withNoData
  164. },
  165. config: {
  166. name: this.name,
  167. schema: this.schema,
  168. selectedFields: this.columns,
  169. query: void 0
  170. }
  171. }),
  172. new SelectionProxyHandler({
  173. alias: this.name,
  174. sqlBehavior: "error",
  175. sqlAliasedBehavior: "alias",
  176. replaceOriginalName: true
  177. })
  178. );
  179. }
  180. as(query) {
  181. return new Proxy(
  182. new PgMaterializedView({
  183. pgConfig: {
  184. tablespace: this.config.tablespace,
  185. using: this.config.using,
  186. with: this.config.with,
  187. withNoData: this.config.withNoData
  188. },
  189. config: {
  190. name: this.name,
  191. schema: this.schema,
  192. selectedFields: this.columns,
  193. query: query.inlineParams()
  194. }
  195. }),
  196. new SelectionProxyHandler({
  197. alias: this.name,
  198. sqlBehavior: "error",
  199. sqlAliasedBehavior: "alias",
  200. replaceOriginalName: true
  201. })
  202. );
  203. }
  204. }
  205. class PgView extends PgViewBase {
  206. static [entityKind] = "PgView";
  207. [PgViewConfig];
  208. constructor({ pgConfig, config }) {
  209. super(config);
  210. if (pgConfig) {
  211. this[PgViewConfig] = {
  212. with: pgConfig.with
  213. };
  214. }
  215. }
  216. }
  217. const PgMaterializedViewConfig = Symbol.for("drizzle:PgMaterializedViewConfig");
  218. class PgMaterializedView extends PgViewBase {
  219. static [entityKind] = "PgMaterializedView";
  220. [PgMaterializedViewConfig];
  221. constructor({ pgConfig, config }) {
  222. super(config);
  223. this[PgMaterializedViewConfig] = {
  224. with: pgConfig?.with,
  225. using: pgConfig?.using,
  226. tablespace: pgConfig?.tablespace,
  227. withNoData: pgConfig?.withNoData
  228. };
  229. }
  230. }
  231. function pgViewWithSchema(name, selection, schema) {
  232. if (selection) {
  233. return new ManualViewBuilder(name, selection, schema);
  234. }
  235. return new ViewBuilder(name, schema);
  236. }
  237. function pgMaterializedViewWithSchema(name, selection, schema) {
  238. if (selection) {
  239. return new ManualMaterializedViewBuilder(name, selection, schema);
  240. }
  241. return new MaterializedViewBuilder(name, schema);
  242. }
  243. function pgView(name, columns) {
  244. return pgViewWithSchema(name, columns, void 0);
  245. }
  246. function pgMaterializedView(name, columns) {
  247. return pgMaterializedViewWithSchema(name, columns, void 0);
  248. }
  249. function isPgView(obj) {
  250. return is(obj, PgView);
  251. }
  252. function isPgMaterializedView(obj) {
  253. return is(obj, PgMaterializedView);
  254. }
  255. export {
  256. DefaultViewBuilderCore,
  257. ManualMaterializedViewBuilder,
  258. ManualViewBuilder,
  259. MaterializedViewBuilder,
  260. MaterializedViewBuilderCore,
  261. PgMaterializedView,
  262. PgMaterializedViewConfig,
  263. PgView,
  264. ViewBuilder,
  265. isPgMaterializedView,
  266. isPgView,
  267. pgMaterializedView,
  268. pgMaterializedViewWithSchema,
  269. pgView,
  270. pgViewWithSchema
  271. };
  272. //# sourceMappingURL=view.js.map