view.cjs 9.1 KB

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