@rnaga/wp-node
Version:
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
219 lines (218 loc) • 9.01 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 CommentsQuery_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommentsQuery = 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");
let CommentsQuery = CommentsQuery_1 = class CommentsQuery extends query_builder_1.QueryBuilder {
alias;
builders;
builder;
table = "comments";
#parent;
#children;
constructor(alias, builders, builder) {
super(alias, builders, builder, CommentsQuery_1);
this.alias = alias;
this.builders = builders;
this.builder = builder;
}
get from() {
this.builder.table(this.alias.as("comments"));
return this;
}
get parent() {
if (!this.#children) {
throw new Error("this.#parent not defined");
}
return this.#parent;
}
get children() {
if (!this.#children) {
throw new Error("this.#children not defined");
}
return this.#children;
}
get(id) {
const { column } = this.alias;
this.builder.where(column("comments", "comment_ID"), id).first();
return this;
}
countApproved(postId) {
const { column } = this.alias;
this.builder
.where(column("comments", "comment_post_ID"), postId)
.where(column("comments", "comment_approved"), "1")
.count("* as count");
this.withoutNote();
return this;
}
// Exclude notes (since WP 6.9)
withoutNote() {
const { column } = this.alias;
this.builder.whereNotIn(column("comments", "comment_type"), ["note"]);
return this;
}
withMeta(type = "right") {
const meta = this.builders.get(meta_query_1.MetaQuery, this.builder, this.alias);
meta.setPrimaryTable("comment").from.joinPrimary(type);
return this;
}
withPosts(ids = [], type = "inner") {
const { as, column } = this.alias;
this.builder[type === "inner" ? "innerJoin" : "leftJoin"](as("posts"), column("comments", "comment_post_ID"), column("posts", "ID"));
if (ids.length > 0)
this.builder.whereIn(column("comments", "comment_post_ID"), ids);
return this;
}
withUsers(userIds = [], type = "inner") {
const { as, column } = this.alias;
this.builder[type === "inner" ? "innerJoin" : "leftJoin"](as("users"), column("comments", "user_id"), column("users", "ID"));
if (userIds.length > 0)
this.builder.whereIn(column("comments", "user_id"), userIds);
return this;
}
withParent() {
this.#parent = this.builders.get(CommentsQuery_1, this.builder);
const { as: parentAs, column: parentColumn } = this.#parent.alias;
const { column } = this.alias;
this.builder.leftJoin(parentAs("comments"), parentColumn("comments", "comment_ID"), column("comments", "comment_parent"));
return this;
}
withChildren(column, obj, limit = 9999) {
const { column: parentColumn } = this.alias;
this.#children = this.builders.get(CommentsQuery_1, this.builder);
const { as, column: toColumn, get } = this.#children.alias;
this.#children.builder
.withRecursive(get("cte").table, (qb) => {
qb.select(toColumn("comments", "comment_ID", "c"), toColumn("comments", "comment_parent", "c"), this.builders.raw("0 as depth"))
.from(as("comments", "c"))
.whereIn(toColumn("comments", column, "c"), obj)
.union((qb) => {
qb.select(toColumn("comments", "comment_ID", "c2"), toColumn("comments", "comment_parent", "c2"), this.builders.raw(`${get("cte").key}.depth + 1`))
.from(as("comments", "c2"))
.join(as("cte"), toColumn("comments", "comment_parent", "c2"), toColumn("cte", "comment_ID"));
});
})
.select(toColumn("cte", "depth"), toColumn("cte", "comment_parent"), parentColumn("comments", "comment_ID"))
.join(as("cte"), parentColumn("comments", "comment_ID"), toColumn("cte", "comment_ID"));
if (limit > 0) {
this.#children.builder.limit(limit);
}
return this;
}
withStatus(status) {
const { column } = this.alias;
const flags = status == "hold" ? [0] : status == "approve" ? [1] : [1, 0];
this.builder.whereIn(column("comments", "comment_approved"), flags);
return this;
}
withType(type) {
const { column } = this.alias;
let values;
switch (type) {
case "comment":
case "comments":
values = ["", "comment"];
break;
case "pings":
values = ["pingback", "trackback"];
break;
default:
values = [type];
}
this.builder.whereIn(column("comments", "comment_type"), values);
return this;
}
select(columns) {
this.builder.clear("select");
if (columns === "*") {
this.builder.distinct().select("*");
}
else {
const { column } = this.alias;
this.builder.select(...columns.map((c) => {
if (c === "*") {
return "*";
}
// This works with withParent
if (c.startsWith("parent_")) {
const actualColumn = c.slice("parent_".length);
if (!this.#parent) {
// fallback
return column("comments", "parent");
}
const { column: columnP } = this.#parent.alias;
return columnP("comments", `comment_${actualColumn}`) + ` as ${c}`;
}
if (c.startsWith("user_") && c !== "user_id") {
const userColumn = c.slice("user_".length);
return column("users", userColumn);
}
if (c == "user_id" || c == "meta_key" || c == "meta_value") {
return c;
}
// This works with withChildren
if (c === "depth") {
if (!this.#children) {
// fallback
return column("comments", "comment_ID");
}
const { column: columnC } = this.#children.alias;
return columnC("cte", "depth");
}
if (c === "parent" && this.#children) {
const { column: columnC } = this.#children.alias;
return columnC("cte", "comment_parent");
}
return column("comments", `comment_${c}`);
}));
}
return this;
}
whereLike(column, search, options) {
const { column: toColumn } = this.alias;
if (options?.not === true) {
this.builder.not;
}
this.builder.whereILike(toColumn("comments", `comment_${column}`), `%${search}%`);
return this;
}
whereNotLike(column, search) {
return this.whereLike(column, search, { not: true });
}
whereIn(column, values) {
this.where(column, values, "in");
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"
? "meta"
: "comments"), column === "user_id" || column == "meta_key" || column == "meta_value"
? column
: `comment_${column}`), op, value);
return this;
}
};
exports.CommentsQuery = CommentsQuery;
exports.CommentsQuery = CommentsQuery = CommentsQuery_1 = __decorate([
(0, component_1.queryBuilder)(),
__metadata("design:paramtypes", [alias_1.Alias,
query_builders_1.QueryBuilders, Object])
], CommentsQuery);