AK-21/Graphite-Industrial-Intelligence
0
1import { entityKind, is } from "../entity.js";2import { SelectionProxyHandler } from "../selection-proxy.js";3import { getTableColumns } from "../utils.js";4import { QueryBuilder } from "./query-builders/query-builder.js";5import { gelTable } from "./table.js";6import { GelViewBase } from "./view-base.js";7import { GelViewConfig } from "./view-common.js";8class DefaultViewBuilderCore {9 constructor(name, schema) {10 this.name = name;11 this.schema = schema;12 }13 static [entityKind] = "GelDefaultViewBuilderCore";14 config = {};15 with(config) {16 this.config.with = config;17 return this;18 }19}20class ViewBuilder extends DefaultViewBuilderCore {21 static [entityKind] = "GelViewBuilder";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: true31 });32 const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);33 return new Proxy(34 new GelView({35 GelConfig: this.config,36 config: {37 name: this.name,38 schema: this.schema,39 selectedFields: aliasedSelection,40 query: qb.getSQL().inlineParams()41 }42 }),43 selectionProxy44 );45 }46}47class ManualViewBuilder extends DefaultViewBuilderCore {48 static [entityKind] = "GelManualViewBuilder";49 columns;50 constructor(name, columns, schema) {51 super(name, schema);52 this.columns = getTableColumns(gelTable(name, columns));53 }54 existing() {55 return new Proxy(56 new GelView({57 GelConfig: void 0,58 config: {59 name: this.name,60 schema: this.schema,61 selectedFields: this.columns,62 query: void 063 }64 }),65 new SelectionProxyHandler({66 alias: this.name,67 sqlBehavior: "error",68 sqlAliasedBehavior: "alias",69 replaceOriginalName: true70 })71 );72 }73 as(query) {74 return new Proxy(75 new GelView({76 GelConfig: 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: true89 })90 );91 }92}93class MaterializedViewBuilderCore {94 constructor(name, schema) {95 this.name = name;96 this.schema = schema;97 }98 static [entityKind] = "GelMaterializedViewBuilderCore";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}117class MaterializedViewBuilder extends MaterializedViewBuilderCore {118 static [entityKind] = "GelMaterializedViewBuilder";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: true128 });129 const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);130 return new Proxy(131 new GelMaterializedView({132 GelConfig: {133 with: this.config.with,134 using: this.config.using,135 tablespace: this.config.tablespace,136 withNoData: this.config.withNoData137 },138 config: {139 name: this.name,140 schema: this.schema,141 selectedFields: aliasedSelection,142 query: qb.getSQL().inlineParams()143 }144 }),145 selectionProxy146 );147 }148}149class ManualMaterializedViewBuilder extends MaterializedViewBuilderCore {150 static [entityKind] = "GelManualMaterializedViewBuilder";151 columns;152 constructor(name, columns, schema) {153 super(name, schema);154 this.columns = getTableColumns(gelTable(name, columns));155 }156 existing() {157 return new Proxy(158 new GelMaterializedView({159 GelConfig: {160 tablespace: this.config.tablespace,161 using: this.config.using,162 with: this.config.with,163 withNoData: this.config.withNoData164 },165 config: {166 name: this.name,167 schema: this.schema,168 selectedFields: this.columns,169 query: void 0170 }171 }),172 new SelectionProxyHandler({173 alias: this.name,174 sqlBehavior: "error",175 sqlAliasedBehavior: "alias",176 replaceOriginalName: true177 })178 );179 }180 as(query) {181 return new Proxy(182 new GelMaterializedView({183 GelConfig: {184 tablespace: this.config.tablespace,185 using: this.config.using,186 with: this.config.with,187 withNoData: this.config.withNoData188 },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: true201 })202 );203 }204}205class GelView extends GelViewBase {206 static [entityKind] = "GelView";207 [GelViewConfig];208 constructor({ GelConfig, config }) {209 super(config);210 if (GelConfig) {211 this[GelViewConfig] = {212 with: GelConfig.with213 };214 }215 }216}217const GelMaterializedViewConfig = Symbol.for("drizzle:GelMaterializedViewConfig");218class GelMaterializedView extends GelViewBase {219 static [entityKind] = "GelMaterializedView";220 [GelMaterializedViewConfig];221 constructor({ GelConfig, config }) {222 super(config);223 this[GelMaterializedViewConfig] = {224 with: GelConfig?.with,225 using: GelConfig?.using,226 tablespace: GelConfig?.tablespace,227 withNoData: GelConfig?.withNoData228 };229 }230}231function gelViewWithSchema(name, selection, schema) {232 if (selection) {233 return new ManualViewBuilder(name, selection, schema);234 }235 return new ViewBuilder(name, schema);236}237function gelMaterializedViewWithSchema(name, selection, schema) {238 if (selection) {239 return new ManualMaterializedViewBuilder(name, selection, schema);240 }241 return new MaterializedViewBuilder(name, schema);242}243function gelView(name, columns) {244 return gelViewWithSchema(name, columns, void 0);245}246function gelMaterializedView(name, columns) {247 return gelMaterializedViewWithSchema(name, columns, void 0);248}249function isGelView(obj) {250 return is(obj, GelView);251}252function isGelMaterializedView(obj) {253 return is(obj, GelMaterializedView);254}255export {256 DefaultViewBuilderCore,257 GelMaterializedView,258 GelMaterializedViewConfig,259 GelView,260 ManualMaterializedViewBuilder,261 ManualViewBuilder,262 MaterializedViewBuilder,263 MaterializedViewBuilderCore,264 ViewBuilder,265 gelMaterializedViewWithSchema,266 gelViewWithSchema267};268//# sourceMappingURL=view.js.map