@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
234 lines (233 loc) • 9.17 kB
JavaScript
"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 TermsQuery_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.TermsQuery = void 0;
const component_1 = require("../decorators/component");
const alias_1 = require("./alias");
const query_builder_1 = require("./query-builder");
const query_builders_1 = require("./query-builders");
const meta_query_1 = require("./meta.query");
let TermsQuery = TermsQuery_1 = class TermsQuery extends query_builder_1.QueryBuilder {
alias;
builders;
builder;
table = "terms";
constructor(alias, builders, builder) {
super(alias, builders, builder, TermsQuery_1);
this.alias = alias;
this.builders = builders;
this.builder = builder;
}
get from() {
const { column, as } = this.alias;
this.builder
.innerJoin(as("term_taxonomy"), column("terms", "term_id"), column("term_taxonomy", "term_id"))
// .leftJoin(
// as("term_relationships"),
// column("term_taxonomy", "term_taxonomy_id"),
// column("term_relationships", "term_taxonomy_id")
// )
.from(as(this.table))
.distinct();
//.select(column("terms", "term_id"));
return this;
}
get selectTerms() {
const { as } = this.alias;
this.builder
.clear("join")
.clear("columns")
.clear("select")
.from(as("terms"));
return this;
}
get selectTermTaxonomy() {
const { as } = this.alias;
this.builder
.clear("join")
.clear("columns")
.clear("select")
.from(as("term_taxonomy"));
return this;
}
get selectTermRelationships() {
const { as } = this.alias;
this.builder
.clear("join")
.clear("columns")
.clear("select")
.from(as("term_relationships"));
return this;
}
exists(column, value, taxonomy) {
this.builder.where((subBuilder) => {
const qb = this.builders.get(TermsQuery_1, subBuilder, this.alias);
qb.from.builder.where(qb.getColumn(column), value).limit(1);
if (taxonomy) {
qb.builder.where(qb.getColumn("taxonomy"), taxonomy);
}
});
return this;
}
maxGroup() {
const { column, as } = this.alias;
return this.builder
.clear("select")
.clear("join")
.clear("where")
.max(column("terms", "term_group"), { as: "max" })
.from(as("terms"));
}
get(id) {
const { column } = this.alias;
this.builder.where(column("terms", "term_id"), id).first();
return this;
}
joinTermRelationships(left) {
const { as, column } = this.alias;
this.builder.join(as("term_relationships"), left, column("term_relationships", "object_id"));
// .innerJoin(
// as("term_taxonomy"),
// column("term_taxonomy", "term_taxonomy_id"),
// column("term_relationships", "term_taxonomy_id")
// )
// .innerJoin(
// as("terms"),
// column("terms", "term_id"),
// column("term_taxonomy", "term_id")
// );
//this.replaceBuilder(builder);
return this;
}
joinTerms() {
const { as, column } = this.alias;
this.builder.innerJoin(as("terms"), column("terms", "term_id"), column("term_taxonomy", "term_id"));
return this;
}
joinTermTaxonomy() {
const { as, column } = this.alias;
this.builder.innerJoin(as("term_taxonomy"), column("term_taxonomy", "term_taxonomy_id"), column("term_relationships", "term_taxonomy_id"));
return this;
}
withMeta(type = "right") {
const { column, as } = this.alias;
this.builder.clear("select").clear("join");
const meta = this.builders.get(meta_query_1.MetaQuery, this.builder, this.alias);
meta.setPrimaryTable("term").from.joinPrimary(type);
this.builder.innerJoin(as("term_taxonomy"), column("terms", "term_id"), column("term_taxonomy", "term_id"));
return this;
}
withoutObjectIds(ids) {
this.withObjectIds(ids, true);
return this;
}
withObjectIds(ids, exclude = false) {
const { as, column } = this.alias;
this.builder.innerJoin(as("term_relationships"), column("term_taxonomy", "term_taxonomy_id"), column("term_relationships", "term_taxonomy_id"));
if (!exclude)
this.builder.whereIn(column("term_relationships", "object_id"), ids);
else
this.builder.whereNotIn(column("term_relationships", "object_id"), ids);
return this;
}
withChildren(column, obj) {
const { as, column: toColumn, get } = this.alias;
this.builder
.withRecursive(get("cte_terms").table, (qb) => {
qb.select(toColumn("term_taxonomy", "term_taxonomy_id", "c"), toColumn("term_taxonomy", "term_id", "c"), toColumn("term_taxonomy", "parent", "c"), this.builders.raw("0 as depth"))
.from(as("term_taxonomy", "c"))
.join(as("terms", "c"), toColumn("term_taxonomy", "term_id", "c"), toColumn("terms", "term_id", "c"))
.whereIn(`${this.getColumn(column, "c")}`, obj)
.union((qb) => {
qb.select(toColumn("term_taxonomy", "term_taxonomy_id", "c2"), toColumn("term_taxonomy", "term_id", "c2"), toColumn("term_taxonomy", "parent", "c2"), this.builders.raw(`${get("cte_terms").key}.depth + 1`))
.from(as("term_taxonomy", "c2"))
.join(as("cte_terms"), toColumn("term_taxonomy", "parent", "c2"), toColumn("cte_terms", "term_taxonomy_id"));
});
})
.join(as("cte_terms"), toColumn("term_taxonomy", "term_id"), toColumn("cte_terms", "term_id"));
return this;
}
getColumn(column, alias) {
const { get } = this.alias;
let tableColumn;
switch (column) {
case "terms_relationships.term_taxonomy_id":
column = "term_taxonomy_id";
// eslint-disable-next-line no-fallthrough
case "object_id":
case "term_order":
tableColumn = get("term_relationships").key;
break;
case "terms.name":
column = "name";
tableColumn = get("terms").key;
break;
case "terms.term_id":
column = "term_id";
tableColumn = get("terms").key;
break;
case "name":
case "slug":
case "term_group":
tableColumn = get("terms").key;
break;
case "depth":
tableColumn = get("cte_terms").key;
break;
default:
tableColumn = get("term_taxonomy").key;
}
return alias
? `${tableColumn}_${alias}.${column}`
: `${tableColumn}.${column}`;
}
select(columns) {
this.builder.clear("select"); //.select(this.getColumn("term_id"));
if (columns === "*") {
this.builder.distinct().select("*");
}
else {
columns.map((c) => c == "depth"
? this.builder.max(this.getColumn(c), { as: "depth" })
: this.builder.select(this.getColumn(c)));
}
return this;
}
groupBy(column) {
this.builder.groupBy(this.getColumn(column));
return this;
}
whereIn(column, obj) {
this.where(column, obj, "in");
return this;
}
where(column, v, op = "=") {
this.builder.where(this.getColumn(column), op, v);
return this;
}
whereLike(column, search, options) {
if (options?.not === true) {
this.builder.not;
}
this.builder.whereILike(this.getColumn(column), `%${search}%`);
return this;
}
whereNotLike(column, search) {
return this.whereLike(column, search);
}
};
exports.TermsQuery = TermsQuery;
exports.TermsQuery = TermsQuery = TermsQuery_1 = __decorate([
(0, component_1.queryBuilder)(),
__metadata("design:paramtypes", [alias_1.Alias,
query_builders_1.QueryBuilders, Object])
], TermsQuery);