UNPKG

@rnaga/wp-node

Version:

👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**

209 lines (208 loc) • 8.71 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var PostsQuery_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.PostsQuery = void 0; const component_1 = require("../decorators/component"); const alias_1 = require("./alias"); const meta_query_1 = require("./meta.query"); const query_builder_1 = require("./query-builder"); const query_builders_1 = require("./query-builders"); const terms_query_1 = require("./terms.query"); let PostsQuery = PostsQuery_1 = class PostsQuery extends query_builder_1.QueryBuilder { alias; builders; builder; table = "posts"; constructor(alias, builders, builder) { super(alias, builders, builder, PostsQuery_1); this.alias = alias; this.builders = builders; this.builder = builder; } get from() { this.builder.table(this.alias.as("posts")); return this; } get(id) { const { column } = this.alias; this.builder.where(column("posts", "ID"), id).first(); return this; } withMeta(type = "right") { const meta = this.builders.get(meta_query_1.MetaQuery, this.builder, this.alias); meta.setPrimaryTable("post").from.joinPrimary(type); return this; } withoutMeta(key, value) { const { column } = this.alias; this.andWhere((query) => { query.builder.where(column("posts", "ID"), "not in", (qb) => { const meta = this.builders.get(meta_query_1.MetaQuery, qb); meta .setPrimaryTable("post") .from.joinPrimary("inner") .select(["post_id"]); if (value) meta.where(key, value); else meta.withKeys([key]); }); }); return this; } withChildren(id) { const { as, column: toColumn, get } = this.alias; this.builder .withRecursive(get("cte_c").table, (qb) => { qb.select(toColumn("posts", "ID", "c"), this.builders.raw("0 as depth")) .from(as("posts", "c")) .where(toColumn("posts", "post_parent", "c"), id) .union((qb) => { qb.select(toColumn("posts", "ID", "c2"), this.builders.raw(`${get("cte_c").key}.depth + 1`)) .from(as("posts", "c2")) .join(as("cte_c"), toColumn("posts", "post_parent", "c2"), toColumn("cte_c", "ID")); }); }) .select(toColumn("cte_c", "depth"), toColumn("posts", "*")) .join(as("cte_c"), toColumn("posts", "ID"), toColumn("cte_c", "ID")); return this; } withParents(id) { const { as, column: toColumn, get } = this.alias; this.builder .withRecursive(get("cte_p").table, (qb) => { qb.select(toColumn("posts", "ID", "p"), toColumn("posts", "post_parent", "p"), this.builders.raw("0 as depth")) .from(as("posts", "p")) .where(toColumn("posts", "ID", "p"), id) .where(toColumn("posts", "post_parent", "p"), ">", 0) .union((qb) => { qb.select(toColumn("posts", "ID", "p2"), toColumn("posts", "post_parent", "p2"), this.builders.raw(`${get("cte_p").key}.depth + 1`)) .from(as("posts", "p2")) .join(as("cte_p"), toColumn("cte_p", "ID"), toColumn("posts", "post_parent", "p2")); }); }) .select(toColumn("cte_p", "depth"), toColumn("posts", "*")) .join(as("cte_p"), toColumn("posts", "ID"), toColumn("cte_p", "ID")); return this; } select(columns) { this.builder.select(columns); return this; } whereIn(column, values) { this.where(column, values); return this; } where(column, value, op = "=") { const { column: toColumn } = this.alias; if (Array.isArray(value)) { op = "in"; } this.builder.where(toColumn((column == "meta_key" || column == "meta_value" ? "postmeta" : "posts"), column), op, value); return this; } whereLike(column, value, options) { const { column: toColumn } = this.alias; if (options?.not === true) { this.builder.not; } this.builder.whereILike(toColumn("posts", column), `%${value}%`); return this; } whereNotLike(column, value) { return this.whereLike(column, value, { not: true }); } countPublished(postType = "post") { const { column } = this.alias; this.from.builder .count("* as count") .where(column("posts", "post_status"), "publish") .where(column("posts", "post_type"), postType) .first(); return this; } withTerms(taxonomies, fn) { const termsQuery = this.builders.get(terms_query_1.TermsQuery, this.builder, this.alias); const { column } = this.alias; this.builder .__ref(termsQuery) .joinTermRelationships(column("posts", "ID")) .joinTermTaxonomy() .joinTerms() .andWhere((query) => { for (const taxonomy of taxonomies) { query.or.where("taxonomy", taxonomy); } }); fn && fn(termsQuery); return this; } // Used by term.trx.ts / _update_post_term_count countTerm(termTaxonomyId, postStatuses, postTypes) { const termsQuery = this.builders.get(terms_query_1.TermsQuery, this.builder, this.alias); const { column } = this.alias; this.from.builder .count("* as count") .__ref(termsQuery) .joinTermRelationships(column("posts", "ID")) .builder.__ref(this) .whereIn("post_status", postStatuses) .whereIn("post_type", postTypes) .builder.__ref(termsQuery) .where("terms_relationships.term_taxonomy_id", termTaxonomyId) .builder.first(); return this; } // Used by term.trx.ts / _update_post_term_count countAttachment(termTaxonomyId, postStatues) { const terms = this.builders.get(terms_query_1.TermsQuery, this.builder, this.alias); const { column } = this.alias; this.from.builder .count("* as count") .__ref(terms) .joinTermRelationships(column("posts", "ID")) .builder.__ref(this) .builder.andWhere((subBuilder) => { this.builders .get(PostsQuery_1, subBuilder, this.alias) .whereIn("post_status", postStatues) .builder.orWhere((subBuilder2) => { const subBuilderWhere = this.builders.get(PostsQuery_1); const { column: innerColumn } = subBuilderWhere.alias; subBuilderWhere .select(["post_status"]) .from.builder.whereRaw(`${innerColumn("posts", "ID")} = ${column("posts", "post_parent")}`); this.builders .get(PostsQuery_1, subBuilder2, this.alias) .whereIn("post_status", ["inherit"]) .where("post_parent", 0, ">") .builder.whereRaw(`(${subBuilderWhere.builder}) in (${postStatues .map(() => "?") .join(",")})`, postStatues); }); }) .__ref(this) .where("post_type", "attachment") .builder.__ref(terms) .where("terms_relationships.term_taxonomy_id", termTaxonomyId) .builder.first(); return this; } }; exports.PostsQuery = PostsQuery; exports.PostsQuery = PostsQuery = PostsQuery_1 = __decorate([ (0, component_1.queryBuilder)(), __metadata("design:paramtypes", [alias_1.Alias, query_builders_1.QueryBuilders, Object]) ], PostsQuery);