| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- import { entityKind, is } from "../entity.js";
- import { SelectionProxyHandler } from "../selection-proxy.js";
- import { getTableColumns } from "../utils.js";
- import { QueryBuilder } from "./query-builders/query-builder.js";
- import { pgTable } from "./table.js";
- import { PgViewBase } from "./view-base.js";
- import { PgViewConfig } from "./view-common.js";
- class DefaultViewBuilderCore {
- constructor(name, schema) {
- this.name = name;
- this.schema = schema;
- }
- static [entityKind] = "PgDefaultViewBuilderCore";
- config = {};
- with(config) {
- this.config.with = config;
- return this;
- }
- }
- class ViewBuilder extends DefaultViewBuilderCore {
- static [entityKind] = "PgViewBuilder";
- as(qb) {
- if (typeof qb === "function") {
- qb = qb(new QueryBuilder());
- }
- const selectionProxy = new SelectionProxyHandler({
- alias: this.name,
- sqlBehavior: "error",
- sqlAliasedBehavior: "alias",
- replaceOriginalName: true
- });
- const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
- return new Proxy(
- new PgView({
- pgConfig: this.config,
- config: {
- name: this.name,
- schema: this.schema,
- selectedFields: aliasedSelection,
- query: qb.getSQL().inlineParams()
- }
- }),
- selectionProxy
- );
- }
- }
- class ManualViewBuilder extends DefaultViewBuilderCore {
- static [entityKind] = "PgManualViewBuilder";
- columns;
- constructor(name, columns, schema) {
- super(name, schema);
- this.columns = getTableColumns(pgTable(name, columns));
- }
- existing() {
- return new Proxy(
- new PgView({
- pgConfig: void 0,
- config: {
- name: this.name,
- schema: this.schema,
- selectedFields: this.columns,
- query: void 0
- }
- }),
- new SelectionProxyHandler({
- alias: this.name,
- sqlBehavior: "error",
- sqlAliasedBehavior: "alias",
- replaceOriginalName: true
- })
- );
- }
- as(query) {
- return new Proxy(
- new PgView({
- pgConfig: this.config,
- config: {
- name: this.name,
- schema: this.schema,
- selectedFields: this.columns,
- query: query.inlineParams()
- }
- }),
- new SelectionProxyHandler({
- alias: this.name,
- sqlBehavior: "error",
- sqlAliasedBehavior: "alias",
- replaceOriginalName: true
- })
- );
- }
- }
- class MaterializedViewBuilderCore {
- constructor(name, schema) {
- this.name = name;
- this.schema = schema;
- }
- static [entityKind] = "PgMaterializedViewBuilderCore";
- config = {};
- using(using) {
- this.config.using = using;
- return this;
- }
- with(config) {
- this.config.with = config;
- return this;
- }
- tablespace(tablespace) {
- this.config.tablespace = tablespace;
- return this;
- }
- withNoData() {
- this.config.withNoData = true;
- return this;
- }
- }
- class MaterializedViewBuilder extends MaterializedViewBuilderCore {
- static [entityKind] = "PgMaterializedViewBuilder";
- as(qb) {
- if (typeof qb === "function") {
- qb = qb(new QueryBuilder());
- }
- const selectionProxy = new SelectionProxyHandler({
- alias: this.name,
- sqlBehavior: "error",
- sqlAliasedBehavior: "alias",
- replaceOriginalName: true
- });
- const aliasedSelection = new Proxy(qb.getSelectedFields(), selectionProxy);
- return new Proxy(
- new PgMaterializedView({
- pgConfig: {
- with: this.config.with,
- using: this.config.using,
- tablespace: this.config.tablespace,
- withNoData: this.config.withNoData
- },
- config: {
- name: this.name,
- schema: this.schema,
- selectedFields: aliasedSelection,
- query: qb.getSQL().inlineParams()
- }
- }),
- selectionProxy
- );
- }
- }
- class ManualMaterializedViewBuilder extends MaterializedViewBuilderCore {
- static [entityKind] = "PgManualMaterializedViewBuilder";
- columns;
- constructor(name, columns, schema) {
- super(name, schema);
- this.columns = getTableColumns(pgTable(name, columns));
- }
- existing() {
- return new Proxy(
- new PgMaterializedView({
- pgConfig: {
- tablespace: this.config.tablespace,
- using: this.config.using,
- with: this.config.with,
- withNoData: this.config.withNoData
- },
- config: {
- name: this.name,
- schema: this.schema,
- selectedFields: this.columns,
- query: void 0
- }
- }),
- new SelectionProxyHandler({
- alias: this.name,
- sqlBehavior: "error",
- sqlAliasedBehavior: "alias",
- replaceOriginalName: true
- })
- );
- }
- as(query) {
- return new Proxy(
- new PgMaterializedView({
- pgConfig: {
- tablespace: this.config.tablespace,
- using: this.config.using,
- with: this.config.with,
- withNoData: this.config.withNoData
- },
- config: {
- name: this.name,
- schema: this.schema,
- selectedFields: this.columns,
- query: query.inlineParams()
- }
- }),
- new SelectionProxyHandler({
- alias: this.name,
- sqlBehavior: "error",
- sqlAliasedBehavior: "alias",
- replaceOriginalName: true
- })
- );
- }
- }
- class PgView extends PgViewBase {
- static [entityKind] = "PgView";
- [PgViewConfig];
- constructor({ pgConfig, config }) {
- super(config);
- if (pgConfig) {
- this[PgViewConfig] = {
- with: pgConfig.with
- };
- }
- }
- }
- const PgMaterializedViewConfig = Symbol.for("drizzle:PgMaterializedViewConfig");
- class PgMaterializedView extends PgViewBase {
- static [entityKind] = "PgMaterializedView";
- [PgMaterializedViewConfig];
- constructor({ pgConfig, config }) {
- super(config);
- this[PgMaterializedViewConfig] = {
- with: pgConfig?.with,
- using: pgConfig?.using,
- tablespace: pgConfig?.tablespace,
- withNoData: pgConfig?.withNoData
- };
- }
- }
- function pgViewWithSchema(name, selection, schema) {
- if (selection) {
- return new ManualViewBuilder(name, selection, schema);
- }
- return new ViewBuilder(name, schema);
- }
- function pgMaterializedViewWithSchema(name, selection, schema) {
- if (selection) {
- return new ManualMaterializedViewBuilder(name, selection, schema);
- }
- return new MaterializedViewBuilder(name, schema);
- }
- function pgView(name, columns) {
- return pgViewWithSchema(name, columns, void 0);
- }
- function pgMaterializedView(name, columns) {
- return pgMaterializedViewWithSchema(name, columns, void 0);
- }
- function isPgView(obj) {
- return is(obj, PgView);
- }
- function isPgMaterializedView(obj) {
- return is(obj, PgMaterializedView);
- }
- export {
- DefaultViewBuilderCore,
- ManualMaterializedViewBuilder,
- ManualViewBuilder,
- MaterializedViewBuilder,
- MaterializedViewBuilderCore,
- PgMaterializedView,
- PgMaterializedViewConfig,
- PgView,
- ViewBuilder,
- isPgMaterializedView,
- isPgView,
- pgMaterializedView,
- pgMaterializedViewWithSchema,
- pgView,
- pgViewWithSchema
- };
- //# sourceMappingURL=view.js.map
|